-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.h
More file actions
160 lines (125 loc) · 3.16 KB
/
pool.h
File metadata and controls
160 lines (125 loc) · 3.16 KB
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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef GAMOS_POOL_H
#define GAMOS_POOL_H
#include "common/array.h"
namespace Gamos {
template<class T, int shift = 6>
class Pool {
public:
typedef uint size_type; /*!< Size type of the array. */
const size_type _blockSize = (1 << shift);
const size_type _blockMask = _blockSize - 1;
public:
constexpr Pool() : _size(0) {}
explicit Pool(size_type count) : _size(count) {
alloc(count);
size_type n = _size;
for (T * blk : _blocks) {
for (size_type i = 0; n > 0 && i < _blockSize; i++) {
n--;
new (blk + i) T();
}
}
}
~Pool() {
free();
}
template<class... TArgs>
void emplace_back(TArgs &&...args) {
alloc(_size + 1);
const size_type blid = _size >> shift;
const size_type elid = _size & _blockMask;
new (_blocks[blid] + elid)T(Common::forward<TArgs>(args)...);
_size++;
}
void push_back(const T &element) {
emplace_back(element);
}
void push_back(T &&element) {
emplace_back(Common::move(element));
}
T &operator[](size_type idx) {
assert(idx < _size);
const size_type blid = idx >> shift;
const size_type elid = idx & _blockMask;
return _blocks[blid][elid];
}
const T &operator[](size_type idx) const {
assert(idx < _size);
const size_type blid = idx >> shift;
const size_type elid = idx & _blockMask;
return _blocks[blid][elid];
}
size_type size() const {
return _size;
}
bool empty() const {
return (_size == 0);
}
void clear() {
free();
}
T &front() {
assert(_size > 0);
return _blocks[0][0];
}
const T &front() const {
assert(_size > 0);
return _blocks[0][0];
}
T &back() {
assert(_size > 0);
return operator[](_size - 1);
}
const T &back() const {
assert(_size > 0);
return operator[](_size - 1);
}
protected:
void alloc(size_type count) {
size_type blockCount = (count + _blockSize - 1) >> shift;
if (_blocks.size() < blockCount) {
size_type oldsz = _blocks.size();
_blocks.resize(blockCount);
for (size_type i = oldsz; i < blockCount; i++) {
_blocks[i] = (T*)malloc(sizeof(T) * _blockSize);
}
}
}
void free() {
size_type n = _size;
for (T * blk : _blocks) {
for (size_type i = 0; n > 0 && i < _blockSize; i++) {
n--;
blk[i].~T();
}
::free(blk);
}
_blocks.clear();
_size = 0;
}
protected:
Common::Array<T *> _blocks;
size_type _size = 0;
};
}
#endif