SparseArray.inl.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/SparseArray.h>
  6. namespace anki {
  7. template<typename T, typename TIndex>
  8. template<typename TAlloc>
  9. void SparseArray<T, TIndex>::destroy(TAlloc& alloc)
  10. {
  11. if(m_elements)
  12. {
  13. for(Index i = 0; i < m_capacity; ++i)
  14. {
  15. if(m_metadata[i].m_alive)
  16. {
  17. destroyElement(m_elements[i]);
  18. }
  19. }
  20. alloc.deallocate(m_elements, m_capacity);
  21. ANKI_ASSERT(m_metadata);
  22. alloc.deallocate(m_metadata, m_capacity);
  23. }
  24. resetMembers();
  25. }
  26. template<typename T, typename TIndex>
  27. template<typename TAlloc, typename... TArgs>
  28. void SparseArray<T, TIndex>::emplaceInternal(TAlloc& alloc, Index idx, TArgs&&... args)
  29. {
  30. if(m_capacity == 0 || calcLoadFactor() > m_maxLoadFactor)
  31. {
  32. grow(alloc);
  33. }
  34. Value tmp(std::forward<TArgs>(args)...);
  35. m_elementCount += insert(alloc, idx, tmp);
  36. invalidateIterators();
  37. }
  38. template<typename T, typename TIndex>
  39. template<typename TAlloc, typename... TArgs>
  40. typename SparseArray<T, TIndex>::Iterator SparseArray<T, TIndex>::emplace(TAlloc& alloc, Index idx, TArgs&&... args)
  41. {
  42. emplaceInternal(alloc, idx, std::forward<TArgs>(args)...);
  43. return Iterator(this, findInternal(idx)
  44. #if ANKI_EXTRA_CHECKS
  45. ,
  46. m_iteratorVer
  47. #endif
  48. );
  49. }
  50. template<typename T, typename TIndex>
  51. template<typename TAlloc>
  52. TIndex SparseArray<T, TIndex>::insert(TAlloc& alloc, Index idx, Value& val)
  53. {
  54. while(true)
  55. {
  56. const Index desiredPos = mod(idx);
  57. const Index endPos = mod(desiredPos + m_probeCount);
  58. Index pos = desiredPos;
  59. while(pos != endPos)
  60. {
  61. Metadata& meta = m_metadata[pos];
  62. Value& crntVal = m_elements[pos];
  63. if(!meta.m_alive)
  64. {
  65. // Empty slot was found, construct in-place
  66. meta.m_alive = true;
  67. meta.m_idx = idx;
  68. alloc.construct(&crntVal, std::move(val));
  69. return 1;
  70. }
  71. else if(meta.m_idx == idx)
  72. {
  73. // Same index was found, replace
  74. meta.m_idx = idx;
  75. destroyElement(crntVal);
  76. alloc.construct(&crntVal, std::move(val));
  77. return 0;
  78. }
  79. // Do the robin-hood
  80. const Index otherDesiredPos = mod(meta.m_idx);
  81. if(distanceFromDesired(pos, otherDesiredPos) < distanceFromDesired(pos, desiredPos))
  82. {
  83. // Swap
  84. std::swap(val, crntVal);
  85. std::swap(idx, meta.m_idx);
  86. break;
  87. }
  88. pos = mod(pos + 1u);
  89. }
  90. if(pos == endPos)
  91. {
  92. // Didn't found an empty place, need to grow and try again
  93. grow(alloc);
  94. }
  95. }
  96. return 0;
  97. }
  98. template<typename T, typename TIndex>
  99. template<typename TAlloc>
  100. void SparseArray<T, TIndex>::grow(TAlloc& alloc)
  101. {
  102. if(m_capacity == 0)
  103. {
  104. ANKI_ASSERT(m_elementCount == 0);
  105. m_capacity = m_initialStorageSize;
  106. m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
  107. m_metadata =
  108. static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
  109. memset(m_metadata, 0, m_capacity * sizeof(Metadata));
  110. return;
  111. }
  112. // Allocate new storage
  113. Value* const oldElements = m_elements;
  114. Metadata* const oldMetadata = m_metadata;
  115. const Index oldCapacity = m_capacity;
  116. [[maybe_unused]] const Index oldElementCount = m_elementCount;
  117. m_capacity *= 2;
  118. m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
  119. m_metadata =
  120. static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
  121. memset(m_metadata, 0, m_capacity * sizeof(Metadata));
  122. m_elementCount = 0;
  123. // Find from where we start
  124. Index startPos = ~Index(0);
  125. for(Index i = 0; i < oldCapacity; ++i)
  126. {
  127. if(oldMetadata[i].m_alive)
  128. {
  129. const Index desiredPos = mod(oldMetadata[i].m_idx, oldCapacity);
  130. if(desiredPos <= i)
  131. {
  132. startPos = i;
  133. break;
  134. }
  135. }
  136. }
  137. ANKI_ASSERT(startPos != ~Index(0));
  138. // Start re-inserting
  139. Index count = oldCapacity;
  140. Index pos = startPos;
  141. while(count--)
  142. {
  143. if(oldMetadata[pos].m_alive)
  144. {
  145. Index c = insert(alloc, oldMetadata[pos].m_idx, oldElements[pos]);
  146. ANKI_ASSERT(c > 0);
  147. m_elementCount += c;
  148. // The element was moved to a new storage so we need to destroy the original
  149. destroyElement(oldElements[pos]);
  150. }
  151. pos = mod(pos + 1, oldCapacity);
  152. }
  153. ANKI_ASSERT(oldElementCount == m_elementCount);
  154. // Finalize
  155. alloc.getMemoryPool().free(oldElements);
  156. alloc.getMemoryPool().free(oldMetadata);
  157. }
  158. template<typename T, typename TIndex>
  159. template<typename TAlloc>
  160. void SparseArray<T, TIndex>::erase(TAlloc& alloc, Iterator it)
  161. {
  162. ANKI_ASSERT(it.m_array == this);
  163. ANKI_ASSERT(it.m_elementIdx != getMaxNumericLimit<Index>());
  164. ANKI_ASSERT(it.m_iteratorVer == m_iteratorVer);
  165. ANKI_ASSERT(m_elementCount > 0);
  166. const Index pos = it.m_elementIdx;
  167. ANKI_ASSERT(pos < m_capacity);
  168. ANKI_ASSERT(m_metadata[pos].m_alive);
  169. // Shift elements
  170. Index crntPos; // Also the one that will get deleted
  171. Index nextPos = pos;
  172. while(true)
  173. {
  174. crntPos = nextPos;
  175. nextPos = mod(nextPos + 1);
  176. Metadata& crntMeta = m_metadata[crntPos];
  177. Metadata& nextMeta = m_metadata[nextPos];
  178. Value& crntEl = m_elements[crntPos];
  179. Value& nextEl = m_elements[nextPos];
  180. if(!nextMeta.m_alive)
  181. {
  182. // On gaps, stop
  183. break;
  184. }
  185. const Index nextDesiredPos = mod(nextMeta.m_idx);
  186. if(nextDesiredPos == nextPos)
  187. {
  188. // The element is where it want's to be, stop
  189. break;
  190. }
  191. // Shift left
  192. std::swap(crntEl, nextEl);
  193. crntMeta.m_idx = nextMeta.m_idx;
  194. }
  195. // Delete the element in the given pos
  196. destroyElement(m_elements[crntPos]);
  197. m_metadata[crntPos].m_alive = false;
  198. --m_elementCount;
  199. // If you erased everything destroy the storage
  200. if(m_elementCount == 0)
  201. {
  202. destroy(alloc);
  203. }
  204. invalidateIterators();
  205. }
  206. template<typename T, typename TIndex>
  207. void SparseArray<T, TIndex>::validate() const
  208. {
  209. if(m_capacity == 0)
  210. {
  211. ANKI_ASSERT(m_elementCount == 0 && m_elements == nullptr && m_metadata == nullptr);
  212. return;
  213. }
  214. ANKI_ASSERT(m_elementCount < m_capacity);
  215. // Find from where we start
  216. Index startPos = ~Index(0);
  217. for(Index i = 0; i < m_capacity; ++i)
  218. {
  219. if(m_metadata[i].m_alive)
  220. {
  221. const Index desiredPos = mod(m_metadata[i].m_idx);
  222. if(desiredPos <= i)
  223. {
  224. startPos = i;
  225. break;
  226. }
  227. }
  228. }
  229. // Start iterating
  230. U elementCount = 0;
  231. Index count = m_capacity;
  232. Index pos = startPos;
  233. Index prevPos = ~Index(0);
  234. while(count--)
  235. {
  236. if(m_metadata[pos].m_alive)
  237. {
  238. [[maybe_unused]] const Index myDesiredPos = mod(m_metadata[pos].m_idx);
  239. ANKI_ASSERT(distanceFromDesired(pos, myDesiredPos) < m_probeCount);
  240. if(prevPos != ~Index(0))
  241. {
  242. [[maybe_unused]] Index prevDesiredPos = mod(m_metadata[prevPos].m_idx);
  243. ANKI_ASSERT(myDesiredPos >= prevDesiredPos);
  244. }
  245. ++elementCount;
  246. prevPos = pos;
  247. }
  248. else
  249. {
  250. prevPos = ~Index(0);
  251. }
  252. pos = mod(pos + 1);
  253. }
  254. ANKI_ASSERT(m_elementCount == elementCount);
  255. }
  256. template<typename T, typename TIndex>
  257. TIndex SparseArray<T, TIndex>::findInternal(Index idx) const
  258. {
  259. if(ANKI_UNLIKELY(m_elementCount == 0))
  260. {
  261. return getMaxNumericLimit<Index>();
  262. }
  263. const Index desiredPos = mod(idx);
  264. const Index endPos = mod(desiredPos + m_probeCount);
  265. Index pos = desiredPos;
  266. while(pos != endPos)
  267. {
  268. if(m_metadata[pos].m_alive && m_metadata[pos].m_idx == idx)
  269. {
  270. return pos;
  271. }
  272. pos = mod(pos + 1);
  273. }
  274. return getMaxNumericLimit<Index>();
  275. }
  276. template<typename T, typename TIndex>
  277. template<typename TAlloc>
  278. void SparseArray<T, TIndex>::clone(TAlloc& alloc, SparseArray& b) const
  279. {
  280. ANKI_ASSERT(b.m_elements == nullptr && b.m_metadata == nullptr);
  281. if(m_capacity == 0)
  282. {
  283. return;
  284. }
  285. // Allocate memory
  286. b.m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
  287. b.m_metadata =
  288. static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
  289. memcpy(b.m_metadata, m_metadata, m_capacity * sizeof(Metadata));
  290. for(U i = 0; i < m_capacity; ++i)
  291. {
  292. if(m_metadata[i].m_alive)
  293. {
  294. ::new(&b.m_elements[i]) Value(m_elements[i]);
  295. }
  296. }
  297. // Set the rest
  298. b.m_elementCount = m_elementCount;
  299. b.m_capacity = m_capacity;
  300. b.m_initialStorageSize = m_initialStorageSize;
  301. b.m_probeCount = m_probeCount;
  302. b.m_maxLoadFactor = m_maxLoadFactor;
  303. b.invalidateIterators();
  304. }
  305. } // end namespace anki