25 #include "common/array.h" 29 template<
class T,
int shift = 6>
34 const size_type _blockSize = (1 << shift);
35 const size_type _blockMask = _blockSize - 1;
39 constexpr
Pool() : _size(0) {}
41 explicit Pool(size_type count) : _size(count) {
45 for (T * blk : _blocks) {
46 for (size_type i = 0; n > 0 && i < _blockSize; i++) {
57 template<
class... TArgs>
58 void emplace_back(TArgs &&...args) {
61 const size_type blid = _size >> shift;
62 const size_type elid = _size & _blockMask;
64 new (_blocks[blid] + elid)T(Common::forward<TArgs>(args)...);
69 void push_back(
const T &element) {
70 emplace_back(element);
73 void push_back(T &&element) {
77 T &operator[](size_type idx) {
80 const size_type blid = idx >> shift;
81 const size_type elid = idx & _blockMask;
83 return _blocks[blid][elid];
86 const T &operator[](size_type idx)
const {
89 const size_type blid = idx >> shift;
90 const size_type elid = idx & _blockMask;
92 return _blocks[blid][elid];
95 size_type size()
const {
109 return _blocks[0][0];
112 const T &front()
const {
114 return _blocks[0][0];
119 return operator[](_size - 1);
122 const T &back()
const {
124 return operator[](_size - 1);
128 void alloc(size_type count) {
129 size_type blockCount = (count + _blockSize - 1) >> shift;
130 if (_blocks.
size() < blockCount) {
131 size_type oldsz = _blocks.
size();
132 _blocks.
resize(blockCount);
133 for (size_type i = oldsz; i < blockCount; i++) {
134 _blocks[i] = (T*)malloc(
sizeof(T) * _blockSize);
141 for (T * blk : _blocks) {
142 for (size_type i = 0; n > 0 && i < _blockSize; i++) {
uint size_type
Definition: pool.h:32
size_type size() const
Definition: array.h:316
Out move(In first, In last, Out dst)
Definition: algorithm.h:109
void resize(size_type newSize)
Definition: array.h:412