SparseArray.inl.h 8.1 KB

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