SparseArray.inl.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. 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. start:
  55. const Index desiredPos = mod(idx);
  56. const Index endPos = mod(desiredPos + m_probeCount);
  57. Index pos = desiredPos;
  58. while(pos != endPos)
  59. {
  60. Metadata& meta = m_metadata[pos];
  61. Value& crntVal = m_elements[pos];
  62. if(!meta.m_alive)
  63. {
  64. // Empty slot was found, construct in-place
  65. meta.m_alive = true;
  66. meta.m_idx = idx;
  67. alloc.construct(&crntVal, std::move(val));
  68. return 1;
  69. }
  70. else if(meta.m_idx == idx)
  71. {
  72. // Same index was found, replace
  73. meta.m_idx = idx;
  74. destroyElement(crntVal);
  75. alloc.construct(&crntVal, std::move(val));
  76. return 0;
  77. }
  78. // Do the robin-hood
  79. const Index otherDesiredPos = mod(meta.m_idx);
  80. if(distanceFromDesired(pos, otherDesiredPos) < distanceFromDesired(pos, desiredPos))
  81. {
  82. // Swap
  83. std::swap(val, crntVal);
  84. std::swap(idx, meta.m_idx);
  85. goto start;
  86. }
  87. pos = mod(pos + 1u);
  88. }
  89. // Didn't found an empty place, need to grow and try again
  90. grow(alloc);
  91. goto start;
  92. ANKI_ASSERT(0);
  93. return 0;
  94. }
  95. template<typename T, typename TIndex>
  96. template<typename TAlloc>
  97. void SparseArray<T, TIndex>::grow(TAlloc& alloc)
  98. {
  99. if(m_capacity == 0)
  100. {
  101. ANKI_ASSERT(m_elementCount == 0);
  102. m_capacity = m_initialStorageSize;
  103. m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
  104. m_metadata =
  105. static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
  106. memset(m_metadata, 0, m_capacity * sizeof(Metadata));
  107. return;
  108. }
  109. // Allocate new storage
  110. Value* const oldElements = m_elements;
  111. Metadata* const oldMetadata = m_metadata;
  112. const Index oldCapacity = m_capacity;
  113. const Index oldElementCount = m_elementCount;
  114. (void)oldElementCount;
  115. m_capacity *= 2;
  116. m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
  117. m_metadata =
  118. static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
  119. memset(m_metadata, 0, m_capacity * sizeof(Metadata));
  120. m_elementCount = 0;
  121. // Find from where we start
  122. Index startPos = ~Index(0);
  123. for(Index i = 0; i < oldCapacity; ++i)
  124. {
  125. if(oldMetadata[i].m_alive)
  126. {
  127. const Index desiredPos = mod(oldMetadata[i].m_idx, oldCapacity);
  128. if(desiredPos <= i)
  129. {
  130. startPos = i;
  131. break;
  132. }
  133. }
  134. }
  135. ANKI_ASSERT(startPos != ~Index(0));
  136. // Start re-inserting
  137. Index count = oldCapacity;
  138. Index pos = startPos;
  139. while(count--)
  140. {
  141. if(oldMetadata[pos].m_alive)
  142. {
  143. Index c = insert(alloc, oldMetadata[pos].m_idx, oldElements[pos]);
  144. ANKI_ASSERT(c > 0);
  145. m_elementCount += c;
  146. // The element was moved to a new storage so we need to destroy the original
  147. destroyElement(oldElements[pos]);
  148. }
  149. pos = mod(pos + 1, oldCapacity);
  150. }
  151. ANKI_ASSERT(oldElementCount == m_elementCount);
  152. // Finalize
  153. alloc.getMemoryPool().free(oldElements);
  154. alloc.getMemoryPool().free(oldMetadata);
  155. }
  156. template<typename T, typename TIndex>
  157. template<typename TAlloc>
  158. void SparseArray<T, TIndex>::erase(TAlloc& alloc, Iterator it)
  159. {
  160. ANKI_ASSERT(it.m_array == this);
  161. ANKI_ASSERT(it.m_elementIdx != getMaxNumericLimit<Index>());
  162. ANKI_ASSERT(it.m_iteratorVer == m_iteratorVer);
  163. ANKI_ASSERT(m_elementCount > 0);
  164. const Index pos = it.m_elementIdx;
  165. ANKI_ASSERT(pos < m_capacity);
  166. ANKI_ASSERT(m_metadata[pos].m_alive);
  167. // Shift elements
  168. Index crntPos; // Also the one that will get deleted
  169. Index nextPos = pos;
  170. while(true)
  171. {
  172. crntPos = nextPos;
  173. nextPos = mod(nextPos + 1);
  174. Metadata& crntMeta = m_metadata[crntPos];
  175. Metadata& nextMeta = m_metadata[nextPos];
  176. Value& crntEl = m_elements[crntPos];
  177. Value& nextEl = m_elements[nextPos];
  178. if(!nextMeta.m_alive)
  179. {
  180. // On gaps, stop
  181. break;
  182. }
  183. const Index nextDesiredPos = mod(nextMeta.m_idx);
  184. if(nextDesiredPos == nextPos)
  185. {
  186. // The element is where it want's to be, stop
  187. break;
  188. }
  189. // Shift left
  190. std::swap(crntEl, nextEl);
  191. crntMeta.m_idx = nextMeta.m_idx;
  192. }
  193. // Delete the element in the given pos
  194. destroyElement(m_elements[crntPos]);
  195. m_metadata[crntPos].m_alive = false;
  196. --m_elementCount;
  197. // If you erased everything destroy the storage
  198. if(m_elementCount == 0)
  199. {
  200. destroy(alloc);
  201. }
  202. invalidateIterators();
  203. }
  204. template<typename T, typename TIndex>
  205. void SparseArray<T, TIndex>::validate() const
  206. {
  207. if(m_capacity == 0)
  208. {
  209. ANKI_ASSERT(m_elementCount == 0 && m_elements == nullptr && m_metadata == nullptr);
  210. return;
  211. }
  212. ANKI_ASSERT(m_elementCount < m_capacity);
  213. // Find from where we start
  214. Index startPos = ~Index(0);
  215. for(Index i = 0; i < m_capacity; ++i)
  216. {
  217. if(m_metadata[i].m_alive)
  218. {
  219. const Index desiredPos = mod(m_metadata[i].m_idx);
  220. if(desiredPos <= i)
  221. {
  222. startPos = i;
  223. break;
  224. }
  225. }
  226. }
  227. // Start iterating
  228. U elementCount = 0;
  229. Index count = m_capacity;
  230. Index pos = startPos;
  231. Index prevPos = ~Index(0);
  232. while(count--)
  233. {
  234. if(m_metadata[pos].m_alive)
  235. {
  236. const Index myDesiredPos = mod(m_metadata[pos].m_idx);
  237. (void)myDesiredPos;
  238. ANKI_ASSERT(distanceFromDesired(pos, myDesiredPos) < m_probeCount);
  239. if(prevPos != ~Index(0))
  240. {
  241. Index prevDesiredPos = mod(m_metadata[prevPos].m_idx);
  242. (void)prevDesiredPos;
  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