37 #if NV_FLOW_ARRAY_CACHE_ENABLED
38 static const NvFlowUint64 s_staticCapacity = staticCapacity;
40 static const NvFlowUint64 s_staticCapacity = 0u;
44 NvFlowUint64 capacity = 0u;
45 NvFlowUint64 size = 0u;
46 unsigned char cache[s_staticCapacity *
sizeof(T) + 8u];
50 for (NvFlowUint64 i = 0; i < capacity; i++)
54 if (data !=
nullptr && (T*)cache != data)
56 operator delete[](data);
66 capacity = rhs.capacity;
68 if (rhs.data == (T*)rhs.cache)
71 for (NvFlowUint64 idx = 0u; idx < capacity; idx++)
73 new(data + idx) T(std::move(rhs.data[idx]));
82 void reserve(NvFlowUint64 requestedCapacity)
84 if (requestedCapacity <= capacity)
89 NvFlowUint64 newSize = size;
90 NvFlowUint64 newCapacity = capacity;
91 if (newCapacity < s_staticCapacity)
93 newCapacity = s_staticCapacity;
95 if (newCapacity == 0u)
99 while (newCapacity < requestedCapacity)
104 T* newData = (T*)(newCapacity <= s_staticCapacity ? (
void*)cache :
operator new[](newCapacity *
sizeof(T)));
106 for (NvFlowUint64 i = 0; i < newSize; i++)
108 new(newData + i) T(std::move(data[i]));
110 for (NvFlowUint64 i = newSize; i < newCapacity; i++)
112 new(newData + i) T();
116 prerelease(data + size, capacity - size);
122 capacity = newCapacity;
128 reserve(s_staticCapacity);
140 prerelease(data, capacity);
145 T& operator[](NvFlowUint64 idx)
150 const T& operator[](NvFlowUint64 idx)
const
155 NvFlowUint64 allocateBack()
162 void pushBack(
const T& v)
164 operator[](allocateBack()) = v;
169 return operator[](size - 1);
192NV_FLOW_INLINE
void NvFlowArrayPointer_prerelease(
void* dataIn, NvFlowUint64 size)
223 T allocateBackPointer()
225 NvFlowUint64 allocIdx = this->allocateBack();
226 if (!(*
this)[allocIdx])
228 NvFlowArrayPointer_allocate((*
this)[allocIdx]);
230 return (*
this)[allocIdx];
232 void pushBackPointer(
const T& v)
234 NvFlowUint64 allocIdx = this->allocateBack();
235 deletePointerAtIndex(allocIdx);
236 (*this)[allocIdx] = v;
238 void swapPointers(NvFlowUint64 idxA, NvFlowUint64 idxB)
240 T temp = (*this)[idxA];
241 (*this)[idxA] = (*this)[idxB];
242 (*this)[idxB] = temp;
244 void removeSwapPointerAtIndex(NvFlowUint64 idx)
246 swapPointers(idx, this->size - 1u);
249 void removeSwapPointer(T ptr)
251 for (NvFlowUint64 idx = 0u; idx < this->size; idx++)
253 if ((*
this)[idx] == ptr)
255 removeSwapPointerAtIndex(idx);
260 void deletePointerAtIndex(NvFlowUint64 idx)
265 (*this)[idx] =
nullptr;
268 void deletePointers()
270 this->size = this->capacity;
271 for (NvFlowUint64 idx = 0u; idx < this->size; idx++)
273 deletePointerAtIndex(idx);
283 NvFlowUint64 freeIdx = 0u;
284 NvFlowUint64 frontIdx = 0u;
285 NvFlowUint64 backIdx = 0u;
291 arr(std::move(rhs.arr)),
292 freeIdx(rhs.freeIdx),
293 frontIdx(rhs.frontIdx),
303 return arr[frontIdx];
308 return arr[(backIdx - 1u) & (arr.size - 1)];
311 NvFlowUint64 activeCount()
313 return (backIdx - frontIdx) & (arr.size - 1);
316 NvFlowUint64 freeCount()
318 return (frontIdx - freeIdx) & (arr.size - 1);
323 frontIdx = (frontIdx + 1u) & (arr.size - 1);
328 freeIdx = (freeIdx + 1u) & (arr.size - 1);
331 T& operator[](NvFlowUint64 idx)
333 return arr[(frontIdx + idx) & (arr.size - 1)];
336 const T& operator[](NvFlowUint64 idx)
const
338 return arr[(frontIdx + idx) & (arr.size - 1)];
341 NvFlowUint64 allocateBack()
347 if (freeCount() > 0u)
349 auto tmp = arr[freeIdx];
350 arr[freeIdx] = arr[backIdx];
354 else if ((activeCount() + 1u) > (arr.size - 1))
356 NvFlowUint64 oldSize = arr.size;
357 arr.reserve(2u * oldSize);
358 arr.size = 2u * oldSize;
359 if (backIdx < frontIdx)
361 for (NvFlowUint64 idx = 0u; idx < backIdx; idx++)
363 auto tmp = arr[idx + oldSize];
364 arr[idx + oldSize] = arr[idx];
370 NvFlowUint64 allocIdx = backIdx;
371 backIdx = (backIdx + 1u) & (arr.size - 1);
375 void pushBack(
const T& v)
377 NvFlowUint64 allocIdx = allocateBack();
378 arr.deletePointerAtIndex(allocIdx);
382 T allocateBackPointer()
384 NvFlowUint64 allocIdx = allocateBack();
387 NvFlowArrayPointer_allocate(arr[allocIdx]);
389 return arr[allocIdx];
392 void deletePointers()
394 arr.deletePointers();