DynamicArray.inl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (C) 2009-present, 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/DynamicArray.h>
  6. namespace anki {
  7. template<typename T, typename TMemoryPool, typename TSize>
  8. void DynamicArray<T, TMemoryPool, TSize>::resizeStorage(Size newSize)
  9. {
  10. if(newSize > m_capacity)
  11. {
  12. // Need to grow
  13. m_capacity = (newSize > Size(F64(m_capacity) * kGrowScale)) ? newSize : Size(F64(m_capacity) * kGrowScale);
  14. Value* newStorage = static_cast<Value*>(m_pool.allocate(m_capacity * sizeof(Value), alignof(Value)));
  15. // Move old elements to the new storage
  16. if(m_data)
  17. {
  18. for(Size i = 0; i < m_size; ++i)
  19. {
  20. callConstructor(newStorage[i], std::move(m_data[i]));
  21. m_data[i].~T();
  22. }
  23. m_pool.free(m_data);
  24. }
  25. m_data = newStorage;
  26. }
  27. else if(newSize < m_size)
  28. {
  29. ANKI_ASSERT(m_capacity > 0);
  30. ANKI_ASSERT(m_size > 0);
  31. ANKI_ASSERT(m_data);
  32. // Delete remaining stuff
  33. for(Size i = newSize; i < m_size; ++i)
  34. {
  35. m_data[i].~T();
  36. }
  37. m_size = newSize;
  38. if(newSize < Size(F64(m_capacity) / kShrinkScale) || newSize == 0)
  39. {
  40. // Need to shrink
  41. m_capacity = newSize;
  42. if(newSize)
  43. {
  44. Value* newStorage = static_cast<Value*>(m_pool.allocate(m_capacity * sizeof(Value), alignof(Value)));
  45. for(Size i = 0; i < m_size; ++i)
  46. {
  47. callConstructor(newStorage[i], std::move(m_data[i]));
  48. m_data[i].~T();
  49. }
  50. m_pool.free(m_data);
  51. m_data = newStorage;
  52. }
  53. else
  54. {
  55. m_pool.free(m_data);
  56. m_data = nullptr;
  57. }
  58. }
  59. }
  60. }
  61. template<typename T, typename TMemoryPool, typename TSize>
  62. template<typename... TArgs>
  63. void DynamicArray<T, TMemoryPool, TSize>::resize(Size newSize, TArgs... args)
  64. {
  65. if constexpr(std::is_copy_constructible<T>::value)
  66. {
  67. const Bool willGrow = newSize > m_size;
  68. resizeStorage(newSize);
  69. if(willGrow)
  70. {
  71. // Fill with new values
  72. for(TSize i = m_size; i < newSize; ++i)
  73. {
  74. ::new(&m_data[i]) T(args...);
  75. }
  76. m_size = newSize;
  77. }
  78. }
  79. else
  80. {
  81. ANKI_ASSERT(m_size == 0 && "Cannot resize storage for non-copyable");
  82. m_data = static_cast<T*>(m_pool.allocate(sizeof(T) * newSize, alignof(T)));
  83. for(TSize i = 0; i < newSize; ++i)
  84. {
  85. ::new(&m_data[i]) T(args...);
  86. }
  87. m_size = newSize;
  88. m_capacity = newSize;
  89. }
  90. ANKI_ASSERT(m_size <= m_capacity);
  91. ANKI_ASSERT(m_size == newSize);
  92. }
  93. template<typename T, typename TMemoryPool, typename TSize>
  94. template<typename... TArgs>
  95. typename DynamicArray<T, TMemoryPool, TSize>::Iterator DynamicArray<T, TMemoryPool, TSize>::emplaceAt(ConstIterator where, TArgs&&... args)
  96. {
  97. const Value* wherePtr = where;
  98. Size outIdx = getMaxNumericLimit<Size>();
  99. if(wherePtr != nullptr)
  100. {
  101. // The "where" arg points to an element inside the array or the end.
  102. // Preconditions
  103. ANKI_ASSERT(wherePtr >= m_data);
  104. ANKI_ASSERT(wherePtr <= m_data + m_size);
  105. ANKI_ASSERT(!isEmpty());
  106. const Size oldSize = m_size;
  107. const Size whereIdx = Size(wherePtr - m_data); // Get that before grow the storage
  108. ANKI_ASSERT(whereIdx >= 0u && whereIdx <= oldSize);
  109. // Resize storage
  110. resizeStorage(oldSize + 1u);
  111. Size elementsToMoveRight = oldSize - whereIdx;
  112. if(elementsToMoveRight == 0)
  113. {
  114. // "where" arg points to the end of the array
  115. outIdx = oldSize;
  116. }
  117. else
  118. {
  119. // Construct the last element because we will move to it
  120. callConstructor(m_data[oldSize]);
  121. // Move the elements one place to the right
  122. while(elementsToMoveRight--)
  123. {
  124. const Size idx = whereIdx + elementsToMoveRight;
  125. m_data[idx + 1] = std::move(m_data[idx]);
  126. }
  127. // Even if it's moved, call the destructor
  128. m_data[whereIdx].~Value();
  129. // Construct our object
  130. outIdx = whereIdx;
  131. }
  132. }
  133. else
  134. {
  135. // The "where" arg points to an empty array. Easy to handle
  136. ANKI_ASSERT(isEmpty());
  137. resizeStorage(1);
  138. outIdx = 0;
  139. }
  140. // Construct the new object
  141. ANKI_ASSERT(outIdx != getMaxNumericLimit<Size>());
  142. callConstructor(m_data[outIdx], std::forward<TArgs>(args)...);
  143. // Increase the size because resizeStorage will not
  144. ++m_size;
  145. return &m_data[outIdx];
  146. }
  147. template<typename T, typename TMemoryPool, typename TSize>
  148. void DynamicArray<T, TMemoryPool, TSize>::erase(ConstIterator first, ConstIterator last)
  149. {
  150. ANKI_ASSERT(first != last);
  151. ANKI_ASSERT(m_data);
  152. ANKI_ASSERT(first >= m_data && first < m_data + m_size);
  153. ANKI_ASSERT(last > m_data && last <= m_data + m_size);
  154. // Move from the back to close the gap
  155. const Size firsti = Size(first - m_data);
  156. const Size lasti = Size(last - m_data);
  157. const Size toMove = m_size - lasti;
  158. for(Size i = 0; i < toMove; ++i)
  159. {
  160. m_data[firsti + i] = std::move(m_data[lasti + i]);
  161. }
  162. // Resize storage
  163. const Size newSize = m_size - Size(last - first);
  164. resizeStorage(newSize);
  165. }
  166. } // end namespace anki