b2GrowableBuffer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2014 Google, Inc.
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_GROWABLE_BUFFER_H
  19. #define B2_GROWABLE_BUFFER_H
  20. #include <Box2D/Common/b2BlockAllocator.h>
  21. #include <string.h>
  22. #include <memory.h>
  23. #include <algorithm>
  24. /// A simple array-like container, similar to std::vector.
  25. /// If we ever start using stl, we should replace this with std::vector.
  26. template <typename T>
  27. class b2GrowableBuffer
  28. {
  29. public:
  30. b2GrowableBuffer(b2BlockAllocator& allocator) :
  31. data(NULL),
  32. count(0),
  33. capacity(0),
  34. allocator(&allocator)
  35. {
  36. #if defined(LIQUIDFUN_SIMD_NEON)
  37. // b2ParticleAssemply.neon.s assumes these values are at fixed offsets.
  38. // If this assert fails, be sure to update the assembly offsets!
  39. // ldr r3, [r9, #0] @ r3 = out = contacts.data
  40. // ldr r6, [r9, #8] @ r6 = contacts.capacity
  41. b2Assert((intptr_t)&data - (intptr_t)(this) == 0
  42. && (intptr_t)&capacity - (intptr_t)(this) == 8);
  43. #endif // defined(LIQUIDFUN_SIMD_NEON)
  44. }
  45. b2GrowableBuffer(const b2GrowableBuffer<T>& rhs) :
  46. data(NULL),
  47. count(rhs.count),
  48. capacity(rhs.capacity),
  49. allocator(rhs.allocator)
  50. {
  51. if (rhs.data != NULL)
  52. {
  53. data = (T*) allocator->Allocate(sizeof(T) * capacity);
  54. memcpy(data, rhs.data, sizeof(T) * count);
  55. }
  56. }
  57. ~b2GrowableBuffer()
  58. {
  59. Free();
  60. }
  61. T& Append()
  62. {
  63. if (count >= capacity)
  64. {
  65. Grow();
  66. }
  67. return data[count++];
  68. }
  69. void Reserve(int32 newCapacity)
  70. {
  71. if (capacity >= newCapacity)
  72. return;
  73. // Reallocate and copy.
  74. T* newData = (T*) allocator->Allocate(sizeof(T) * newCapacity);
  75. if (data)
  76. {
  77. memcpy(newData, data, sizeof(T) * count);
  78. allocator->Free(data, sizeof(T) * capacity);
  79. }
  80. // Update pointer and capacity.
  81. capacity = newCapacity;
  82. data = newData;
  83. }
  84. void Grow()
  85. {
  86. // Double the capacity.
  87. int32 newCapacity = capacity ? 2 * capacity
  88. : b2_minParticleSystemBufferCapacity;
  89. b2Assert(newCapacity > capacity);
  90. Reserve(newCapacity);
  91. }
  92. void Free()
  93. {
  94. if (data == NULL)
  95. return;
  96. allocator->Free(data, sizeof(data[0]) * capacity);
  97. data = NULL;
  98. capacity = 0;
  99. count = 0;
  100. }
  101. void Shorten(const T* newEnd)
  102. {
  103. b2Assert(newEnd >= data);
  104. count = (int32) (newEnd - data);
  105. }
  106. T& operator[](int i)
  107. {
  108. return data[i];
  109. }
  110. const T& operator[](int i) const
  111. {
  112. return data[i];
  113. }
  114. T* Data()
  115. {
  116. return data;
  117. }
  118. const T* Data() const
  119. {
  120. return data;
  121. }
  122. T* Begin()
  123. {
  124. return data;
  125. }
  126. const T* Begin() const
  127. {
  128. return data;
  129. }
  130. T* End()
  131. {
  132. return &data[count];
  133. }
  134. const T* End() const
  135. {
  136. return &data[count];
  137. }
  138. int32 GetCount() const
  139. {
  140. return count;
  141. }
  142. void SetCount(int32 newCount)
  143. {
  144. b2Assert(0 <= newCount && newCount <= capacity);
  145. count = newCount;
  146. }
  147. int32 GetCapacity() const
  148. {
  149. return capacity;
  150. }
  151. template<class UnaryPredicate>
  152. T* RemoveIf(UnaryPredicate pred)
  153. {
  154. T* newEnd = std::remove_if(data, data + count, pred);
  155. Shorten(newEnd);
  156. return newEnd;
  157. }
  158. template<class BinaryPredicate>
  159. T* Unique(BinaryPredicate pred)
  160. {
  161. T* newEnd = std::unique(data, data + count, pred);
  162. Shorten(newEnd);
  163. return newEnd;
  164. }
  165. private:
  166. T* data;
  167. int32 count;
  168. int32 capacity;
  169. b2BlockAllocator* allocator;
  170. };
  171. #endif // B2_GROWABLE_BUFFER_H