Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
qobjectpool.h
1 
2 /************************************************************************************
3  * MIT License
4  *
5  * Copyright (c) 2023 Eray Zesen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * https://github.com/erayzesen/QuarkPhysics
25  *
26 **************************************************************************************/
27 
28 // qobjectpool.h
29 #ifndef QOBJECTPOOL_H
30 #define QOBJECTPOOL_H
31 
32 #include <stdlib.h>
33 #include <vector>
34 #include <iostream>
35 
36 using namespace std;
37 
38 template <typename T>
39 class QObjectPool {
40 public:
41  struct Node
42  {
43  public:
44 
45  Node() {
46 
47  }
48 
49 
50 
51  T* data;
52  int next;
53  int id;
54 
55  };
56 private:
57 
58  /* Node* nodes;
59  size_t size = 2;
60  size_t freeSize = 2;
61  size_t lastFreeNode = -1; */
62 
63  void Resize(size_t newSize){
64  QObjectPool<T>::Node* newNodes = new QObjectPool<T>::Node[newSize];
65 
66  if(size>0){
67  for (size_t i = 0; i < std::min(size, newSize); ++i) {
68  newNodes[i] = nodes[i];
69  }
70  delete[] nodes;
71  }
72 
73  for (size_t i = size; i < newSize; ++i) {
74  newNodes[i].next = i + 1;
75  newNodes[i].id = i;
76  newNodes[i].data = new T();
77  }
78 
79 
80  nodes = newNodes;
81  size = newSize;
82  };
83 
84 public:
85  QObjectPool(){
86  nodes = new QObjectPool<T>::Node[size];
87 
88  for (size_t i = 0; i < size; ++i) {
89  nodes[i].next = i + 1;
90  nodes[i].id = i;
91  nodes[i].data=new T();
92  }
93 
94  nodes[size - 1].next = -1;
95  lastFreeNode = 0;
96  };
97  QObjectPool(size_t initialSize,size_t expansionSize=0){
98  size=initialSize;
99  sizeStep=expansionSize;
100  nodes = new QObjectPool<T>::Node[size];
101 
102  for (size_t i = 0; i < size; ++i) {
103  nodes[i].next = i + 1;
104  nodes[i].id = i;
105  nodes[i].data=new T();
106  }
107 
108  lastFreeNode = 0;
109  };
110  ~QObjectPool(){
111  ClearAll();
112  };
113 
114  Node* nodes;
115  size_t size = 2;
116  size_t sizeStep = 0;
117  size_t freeSize = 2;
118  size_t lastFreeNode = 0;
119 
120 
121  Node & Create(){
122  //cout<<"pool size: "<<size<<" free node index: "<< lastFreeNode<<endl;
123  if (lastFreeNode==size ) {
124  if (sizeStep==0){
125  if(size==0){
126  Resize(2);
127  }else{
128  Resize(size * 2);
129  }
130 
131  }else{
132  Resize(size+sizeStep);
133  }
134 
135  }
136 
137  size_t newNodeIndex=lastFreeNode;
138  lastFreeNode = nodes[lastFreeNode].next;
139 
140  return nodes[newNodeIndex];
141  };
142  void Free(int nodeID){
143  nodes[nodeID].next = lastFreeNode;
144  lastFreeNode = nodeID;
145  };
146  void FreeAll(){
147  for (size_t i = 0; i < size; i++) {
148  nodes[i].next = i + 1;
149  }
150 
151  lastFreeNode = 0;
152  };
153  void ClearAll(){
154  if(size>0){
155  for (int i=0;i<size;++i){
156  //cout<<"pool object deleted index:"<<i<<endl;
157  delete nodes[i].data;
158  nodes[i].data=nullptr;
159  }
160  delete[] nodes;
161  size=0;
162  lastFreeNode=0;
163  }
164 
165  };
166 
167 
168 };
169 
170 
171 
172 #endif // QOBJECTPOOL_H
173 
174 
Definition: qobjectpool.h:39
Definition: qobjectpool.h:42