12 #ifndef hifi_render_IndexedContainer_h
13 #define hifi_render_IndexedContainer_h
20 namespace indexed_container {
22 using Index = int32_t;
23 const Index MAXIMUM_INDEX { 1 << 30 };
24 const Index INVALID_INDEX { -1 };
25 using Indices = std::vector< Index >;
27 template <Index MaxNumElements = MAXIMUM_INDEX>
32 Index _nextNewIndex { 0 };
34 bool checkIndex(Index index)
const {
return ((index >= 0) && (index < _nextNewIndex)); }
35 Index getNumIndices()
const {
return _nextNewIndex - (Index) _freeIndices.size(); }
36 Index getNumFreeIndices()
const {
return (Index) _freeIndices.size(); }
37 Index getNumAllocatedIndices()
const {
return _nextNewIndex; }
39 Index allocateIndex() {
40 if (_freeIndices.empty()) {
41 Index index = _nextNewIndex;
42 if (index >= MaxNumElements) {
52 Index index = _freeIndices.back();
53 _freeIndices.pop_back();
58 void freeIndex(Index index) {
59 if (checkIndex(index)) {
60 _freeIndices.push_back(index);
70 template <
class T, Index MaxNumElements = MAXIMUM_INDEX>
72 Allocator<MaxNumElements> _allocator;
75 using Elements = std::vector<T>;
79 bool checkIndex(Index index)
const {
return _allocator.checkIndex(index); };
80 Index getNumElements()
const {
return _allocator.getNumIndices(); }
81 Index getNumFreeIndices()
const {
return _allocator.getNumFreeIndices(); }
82 Index getNumAllocatedIndices()
const {
return _allocator.getNumAllocatedIndices(); }
84 Index newElement(
const Element& e) {
85 Index index = _allocator.allocateIndex();
86 if (index != INVALID_INDEX) {
87 if (index < (Index) _elements.size()) {
90 assert(index == (Index)_elements.size());
91 _elements.emplace_back(e);
97 const Element& freeElement(Index index) {
98 _allocator.freeIndex(index);
99 return _elements[index];
102 bool isElementFreed(Index index)
const {
103 return std::find(_allocator._freeIndices.begin(), _allocator._freeIndices.end(), index) != _allocator._freeIndices.end();
106 const Element& get(Index index)
const {
107 return _elements[index];
109 Element& edit(Index index) {
110 return _elements[index];
114 template <
class T, Index MaxNumElements = MAXIMUM_INDEX>
115 class IndexedPointerVector {
116 Allocator<MaxNumElements> _allocator;
119 using ElementPtr = std::shared_ptr<Data>;
120 using Elements = std::vector<ElementPtr>;
124 bool checkIndex(Index index)
const {
return _allocator.checkIndex(index); };
125 Index getNumElements()
const {
return _allocator.getNumIndices(); }
126 Index getNumFreeIndices()
const {
return _allocator.getNumFreeIndices(); }
127 Index getNumAllocatedIndices()
const {
return _allocator.getNumAllocatedIndices(); }
129 Index newElement(
const ElementPtr& e) {
130 Index index = _allocator.allocateIndex();
131 if (index != INVALID_INDEX) {
132 if (index < (Index) _elements.size()) {
133 _elements[index] = e;
135 assert(index == (Index) _elements.size());
136 _elements.emplace_back(e);
142 ElementPtr freeElement(Index index) {
144 if (checkIndex(index)) {
145 _allocator.freeIndex(index);
146 freed = _elements[index];
147 _elements[index].reset();
152 ElementPtr get(Index index)
const {
153 if (checkIndex(index)) {
154 return _elements[index];