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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef COMMAND_PARAMS_POOL_H
#define COMMAND_PARAMS_POOL_H
#include <cassert>
#include <algorithm>
#include <vector>
#include "System/creg/creg_cond.h"
template<typename T, size_t N, size_t S> struct TCommandParamsPool {
public:
const T* GetPtr(unsigned int i, unsigned int j ) const { assert(i < pages.size()); return (pages[i].data( ) + j); }
// T* GetPtr(unsigned int i, unsigned int j ) { assert(i < pages.size()); return (pages[i].data( ) + j); }
T Get (unsigned int i, unsigned int j ) const { assert(i < pages.size()); return (pages[i].at (j) ); }
T Set (unsigned int i, unsigned int j, T v) { assert(i < pages.size()); return (pages[i].at (j) = v); }
size_t Push(unsigned int i, T v) {
assert(i < pages.size());
pages[i].push_back(v);
return (pages[i].size());
}
void ReleasePage(unsigned int i) {
assert(i < pages.size());
indcs.push_back(i);
}
unsigned int AcquirePage() {
if (indcs.empty()) {
const size_t numIndices = indcs.size();
pages.resize(std::max(N, pages.size() << 1));
indcs.resize(std::max(N, indcs.size() << 1));
const auto beg = indcs.begin() + numIndices;
const auto end = indcs.end();
// generate new indices
std::for_each(beg, end, [&](const unsigned int& i) { indcs[&i - &indcs[0]] = &i - &indcs[0]; });
}
const unsigned int pageIndex = indcs.back();
pages[pageIndex].clear();
pages[pageIndex].reserve(S);
indcs.pop_back();
return pageIndex;
}
private:
std::vector< std::vector<T> > pages;
std::vector<unsigned int> indcs;
};
typedef TCommandParamsPool<float, 256, 32> CommandParamsPool;
extern CommandParamsPool cmdParamsPool;
#endif
|