-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObjectPool.asc
178 lines (165 loc) · 5.54 KB
/
ObjectPool.asc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//===========================================================================
//
// Array_ExpandIf()
// Takes array "arr" which supposedly is large enough to store "valid_count"
// number of elements. If "need_capacity" is greater than "valid_count", then
// creates a new array, copies old contents there, and returns it.
// Otherwise simply returns existing array.
//
//===========================================================================
import int[] Array_ExpandIf(int arr[], int valid_count, int need_capacity, int elem_sz = 1);
int[] Array_ExpandIf(int arr[], int valid_count, int need_capacity, int elem_sz)
{
if (valid_count >= need_capacity) { return arr; }
int want_space = need_capacity * elem_sz;
int new_arr[] = new int[want_space];
int took_space = valid_count * elem_sz;
for (int i = 0; i < took_space; i++) {
new_arr[i] = arr[i];
}
return new_arr;
}
//===========================================================================
//
// ObjectPool::AddObjects()
// Registers range of IDs.
//
//===========================================================================
void ObjectPool::AddObjects(int from, int to) {
if (this._capacity <= to) {
int new_capacity = to + 1;
this._usingObj = Array_ExpandIf(this._usingObj, this._capacity, new_capacity);
this._isFree = Array_ExpandIf(this._isFree, this._capacity, new_capacity);
this._freeObj = Array_ExpandIf(this._freeObj, this._capacity, new_capacity);
for (int i = this._capacity; i <= from; i++) {
this._usingObj[i] = false;
this._isFree[i] = false;
}
this._capacity = new_capacity;
}
for (int i = from; i <= to; i++) {
if (!this._usingObj[i]) {
this._freeObj[this._numFreeObj] = i;
this._numFreeObj++;
this._usingObj[i] = true;
this._isFree[i] = true;
this._numUsed++;
}
}
}
//===========================================================================
//
// ObjectPool::RemoveObjects()
// Unregisters range of IDs.
//
//===========================================================================
void ObjectPool::RemoveObjects(int from, int to) {
for (int i = from; i <= to; i++) {
if (this._usingObj[i]) {
for (int j = 0; j < this._numFreeObj; j++) {
if (this._freeObj[j] == i) {
for (; j < this._numFreeObj - 1; j++) {
this._freeObj[j] = this._freeObj[j + 1];
}
this._numFreeObj--;
}
}
this._isFree[i] = false;
this._usingObj[i] = false;
this._numUsed--;
}
}
}
//===========================================================================
//
// ObjectPool::RemoveAll()
// Unregisters all IDs.
//
//===========================================================================
void ObjectPool::RemoveAll() {
this._numFreeObj = 0;
for (int i = 0; i < this._capacity; i++) {
this._usingObj[i] = false;
this._isFree[i] = false;
}
this._usingObj = null;
this._isFree = null;
this._freeObj = null;
this._capacity = 0;
this._numUsed = 0;
}
//===========================================================================
//
// ObjectPool::Acquire()
// Returns first found free ID and marks it as used.
//
//===========================================================================
int ObjectPool::Acquire() {
if (this._numFreeObj == 0) { return -1; }
this._numFreeObj--;
int id = this._freeObj[this._numFreeObj];
this._isFree[id] = false;
return id;
}
//===========================================================================
//
// ObjectPool::Release()
// Marks given ID as free.
//
//===========================================================================
void ObjectPool::Release(int id) {
if (!this._usingObj[id] || this._isFree[id]) { return; }
this._freeObj[this._numFreeObj] = id;
this._numFreeObj++;
this._isFree[id] = true;
}
//===========================================================================
//
// ObjectPool::ReleaseAll()
// Marks all known IDs as free.
//
//===========================================================================
void ObjectPool::ReleaseAll() {
this._numFreeObj = 0;
for (int i = 0; i < this._capacity; i++) {
if (this._usingObj[i]) {
this._freeObj[this._numFreeObj] = i;
this._numFreeObj++;
this._isFree[i] = true;
}
}
}
//===========================================================================
//
// ObjectPool::GetAcquiredNum()
//
//===========================================================================
int ObjectPool::GetAcquiredNum() {
return this._numUsed - this._numFreeObj;
}
//===========================================================================
//
// ObjectPool::GetFreeNum()
//
//===========================================================================
int ObjectPool::GetFreeNum() {
return this._numFreeObj;
}
//===========================================================================
//
// ObjectPool::GetTotalNum()
//
//===========================================================================
int ObjectPool::GetTotalNum() {
return this._numUsed;
}
#ifdef DEBUG
//===========================================================================
//
// ObjectPool::GetPoolSize()
//
//===========================================================================
int ObjectPool::GetPoolSize() {
return this._capacity;
}
#endif // DEBUG