|
| 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 | +} |
0 commit comments