/usr/include/dawgdic/object-pool.h is in libdawgdic-dev 0.4.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef DAWGDIC_OBJECT_POOL_H
#define DAWGDIC_OBJECT_POOL_H
#include <vector>
#include "base-types.h"
namespace dawgdic {
// This class works like an array of objects with compact memory management.
template <typename OBJECT_TYPE, SizeType BLOCK_SIZE = 1 << 10>
class ObjectPool {
public:
typedef OBJECT_TYPE ObjectType;
ObjectPool() : blocks_(), size_(0) {}
~ObjectPool() {
Clear();
}
// Accessors.
ObjectType &operator[](SizeType index) {
return blocks_[index / BLOCK_SIZE][index % BLOCK_SIZE];
}
const ObjectType &operator[](SizeType index) const {
return blocks_[index / BLOCK_SIZE][index % BLOCK_SIZE];
}
// Number of allocated objects.
SizeType size() const {
return size_;
}
// Deletes all objects and frees memory.
void Clear() {
for (SizeType i = 0; i < blocks_.size(); ++i) {
delete [] blocks_[i];
}
std::vector<ObjectType *>(0).swap(blocks_);
size_ = 0;
}
// Swaps object pools.
void Swap(ObjectPool *pool) {
blocks_.swap(pool->blocks_);
std::swap(size_, pool->size_);
}
// Allocates memory for a new object and returns its ID.
SizeType Allocate() {
if (size_ == BLOCK_SIZE * blocks_.size()) {
blocks_.push_back(new ObjectType[BLOCK_SIZE]);
}
return size_++;
}
private:
std::vector<ObjectType *> blocks_;
SizeType size_;
// Disallows copies.
ObjectPool(const ObjectPool &);
ObjectPool &operator=(const ObjectPool &);
};
} // namespace dawgdic
#endif // DAWGDIC_OBJECT_POOL_H
|