Skip to content

Commit 3f4ecae

Browse files
committed
DUMP: ObjectPool
1 parent abc0caf commit 3f4ecae

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

scripts/_dump/ObjectPool.asc

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
2+
int[] Array_ExpandIf(int arr[], int valid_count, int need_capacity, int elem_sz)
3+
{
4+
if (valid_count >= need_capacity) { return arr; }
5+
int want_space = need_capacity * elem_sz;
6+
int new_arr[] = new int[want_space];
7+
int took_space = valid_count * elem_sz;
8+
for (int i = 0; i < took_space; i++) {
9+
new_arr[i] = arr[i];
10+
}
11+
return new_arr;
12+
}
13+
14+
15+
void ObjectPool::AddObjects(int from, int to) {
16+
if (this._capacity <= to) {
17+
int new_capacity = to + 1;
18+
this._usingObj = Array_ExpandIf(this._usingObj, this._capacity, new_capacity);
19+
this._isFree = Array_ExpandIf(this._isFree, this._capacity, new_capacity);
20+
this._freeObj = Array_ExpandIf(this._freeObj, this._capacity, new_capacity);
21+
for (int i = this._capacity; i <= from; i++) {
22+
this._usingObj[i] = false;
23+
this._isFree[i] = false;
24+
}
25+
this._capacity = new_capacity;
26+
}
27+
for (int i = from; i <= to; i++) {
28+
if (!this._usingObj[i]) {
29+
this._freeObj[this._numFreeObj] = i;
30+
this._numFreeObj++;
31+
this._usingObj[i] = true;
32+
this._isFree[i] = true;
33+
this._numUsed++;
34+
}
35+
}
36+
}
37+
38+
void ObjectPool::RemoveAll() {
39+
this._numFreeObj = 0;
40+
for (int i = 0; i < this._capacity; i++) {
41+
this._usingObj[i] = false;
42+
this._isFree[i] = false;
43+
}
44+
this._usingObj = null;
45+
this._isFree = null;
46+
this._freeObj = null;
47+
this._capacity = 0;
48+
this._numUsed = 0;
49+
}
50+
51+
void ObjectPool::RemoveObjects(int from, int to) {
52+
for (int i = from; i <= to; i++) {
53+
if (this._usingObj[i]) {
54+
for (int j = 0; j < this._numFreeObj; j++) {
55+
if (this._freeObj[j] == i) {
56+
for (; j < this._numFreeObj - 1; j++) {
57+
this._freeObj[j] = this._freeObj[j + 1];
58+
}
59+
this._numFreeObj--;
60+
}
61+
}
62+
this._isFree[i] = false;
63+
this._usingObj[i] = false;
64+
this._numUsed--;
65+
}
66+
}
67+
}
68+
69+
int ObjectPool::Acquire() {
70+
if (this._numFreeObj == 0) { return -1; }
71+
this._numFreeObj--;
72+
int id = this._freeObj[this._numFreeObj];
73+
this._isFree[id] = false;
74+
return id;
75+
}
76+
77+
void ObjectPool::Release(int id) {
78+
if (!this._usingObj[id] || this._isFree[id]) { return; }
79+
this._freeObj[this._numFreeObj] = id;
80+
this._numFreeObj++;
81+
this._isFree[id] = true;
82+
}
83+
84+
void ObjectPool::ReleaseAll() {
85+
this._numFreeObj = 0;
86+
for (int i = 0; i < this._capacity; i++) {
87+
if (this._usingObj[i]) {
88+
this._freeObj[this._numFreeObj] = i;
89+
this._numFreeObj++;
90+
this._isFree[i] = true;
91+
}
92+
}
93+
}
94+
95+
int ObjectPool::GetAcquiredNum() {
96+
return this._numUsed - this._numFreeObj;
97+
}
98+
99+
int ObjectPool::GetFreeNum() {
100+
return this._numFreeObj;
101+
}
102+
103+
int ObjectPool::GetTotalNum() {
104+
return this._numUsed;
105+
}
106+
107+
108+
int ObjectPool::GetPoolSize() {
109+
return this._capacity;
110+
}

scripts/_dump/ObjectPool.ash

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
//
3+
// ObjectPool keeps record of available game objects, which may be currrently in use or free.
4+
//
5+
struct ObjectPool {
6+
import void AddObjects(int from, int to);
7+
import void RemoveAll();
8+
import void RemoveObjects(int from, int to);
9+
10+
import int Acquire();
11+
import void Release(int id);
12+
import void ReleaseAll();
13+
14+
// Gets number of acquired objects
15+
import int GetAcquiredNum();
16+
// Gets number of available free objects
17+
import int GetFreeNum();
18+
// Gets total number of registered objects
19+
import int GetTotalNum();
20+
// Gets pool capacity (may include empty slots!)
21+
import int GetPoolSize();
22+
23+
protected int _capacity;
24+
protected int _numUsed;
25+
protected bool _usingObj[];
26+
protected bool _isFree[];
27+
protected int _freeObj[];
28+
protected int _numFreeObj;
29+
};
30+
31+
32+
//
33+
// Helper functions which perform operations on arrays of ints
34+
//
35+
import int[] Array_ExpandIf(int arr[], int valid_count, int need_capacity, int elem_sz = 1);

0 commit comments

Comments
 (0)