Array.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file Array.h
  13. @brief The Array<T> template class. Provides a std::vector-like array, but with instance-based memory allocation. */
  14. #include "SharedPtr.h"
  15. #include "Allocator.h"
  16. #include "StdCMallocHeap.h"
  17. namespace kNet
  18. {
  19. template<typename T, typename AllocT = StdCAlloc>
  20. class Array;
  21. /// Sequential iterator for Array.
  22. template<typename T, typename AllocT>
  23. class ArrayIterator
  24. {
  25. public:
  26. ArrayIterator(const ArrayIterator &rhs):ptr(rhs.ptr), container(rhs.container) {}
  27. ~ArrayIterator() {}
  28. inline void operator++()
  29. {
  30. assert(ptr <= container->endptr());
  31. assert(ptr >= container->beginptr());
  32. ++ptr;
  33. }
  34. inline void operator--()
  35. {
  36. assert(ptr <= container->endptr());
  37. assert(ptr >= container->beginptr());
  38. --ptr;
  39. }
  40. inline operator T*()
  41. {
  42. assert(ptr >= container->beginptr());
  43. assert(ptr <= container->endptr());
  44. return ptr;
  45. }
  46. inline operator const T*() const
  47. {
  48. assert(ptr >= container->beginptr());
  49. assert(ptr <= container->endptr());
  50. return ptr;
  51. }
  52. inline const T &operator*() const
  53. {
  54. assert(ptr >= container->beginptr());
  55. assert(ptr <= container->endptr());
  56. return *ptr;
  57. }
  58. inline T &operator*()
  59. {
  60. assert(ptr >= container->beginptr());
  61. assert(ptr <= container->endptr());
  62. return *ptr;
  63. }
  64. inline bool operator<(const ArrayIterator &rhs) const { return ptr < rhs.ptr; }
  65. inline bool operator<=(const ArrayIterator &rhs) const { return ptr <= rhs.ptr; }
  66. inline bool operator==(const ArrayIterator &rhs) const { return ptr == rhs.ptr; }
  67. inline bool operator!=(const ArrayIterator &rhs) const { return ptr != rhs.ptr; }
  68. inline bool operator>(const ArrayIterator &rhs) const { return ptr > rhs.ptr; }
  69. inline bool operator>=(const ArrayIterator &rhs) const { return ptr >= rhs.ptr; }
  70. private:
  71. T *ptr;
  72. Array<T, AllocT> *container;
  73. ArrayIterator(Array<T, AllocT> &container_, T *ptr_)
  74. :container(&container_), ptr(ptr_)
  75. {
  76. assert(ptr >= container->beginptr());
  77. assert(ptr <= container->endptr());
  78. }
  79. friend class Array<T, AllocT>;
  80. };
  81. /// std::vector -equivalent.
  82. template<typename T, typename AllocT>
  83. class Array
  84. {
  85. public:
  86. typedef ArrayIterator<T, AllocT> iterator;
  87. Array(AllocT *allocator_ = 0)
  88. :data(0), allocator(allocator_), cap(0), used(0)
  89. { }
  90. Array(const Array &rhs)
  91. :data(0), allocator(rhs.allocator), cap(0), used(0)
  92. {
  93. reserve(rhs.cap);
  94. for(size_t i = 0; i < rhs.size(); ++i)
  95. data[i] = rhs.data[i];
  96. used = rhs.used;
  97. }
  98. ~Array()
  99. {
  100. for(size_t i = 0; i < used; ++i)
  101. data[i] = T();
  102. DeleteArray(data, allocator);
  103. }
  104. const T &front() const { assert(used > 0); return data[0]; }
  105. const T &back() const { assert(used > 0); return data[used-1]; }
  106. T &front() { assert(used > 0); return data[0]; }
  107. T &back() { assert(used > 0); return data[used-1]; }
  108. /// @return The number of elements in the container.
  109. size_t size() const { return used; }
  110. /// @return The number of elements that the container can hold without needing to reserve more.
  111. size_t capacity() const { return cap; }
  112. /// Sets the allocator used by this Array.
  113. void set_allocator(AllocT *allocator_)
  114. {
  115. assert(cap == 0);
  116. assert(data == 0);
  117. allocator = allocator_;
  118. }
  119. /// @return The allocator used by this Array, or 0 if none was set.
  120. AllocT *get_allocator() const { return allocator; }
  121. const T *beginptr() const { return data; } ///< Pointer to the first element.
  122. const T *endptr() const { return data + used; } ///< Pointer to one past the last used element.
  123. const T *capptr() const { return data + cap; } ///< Pointer to the first element beyond allocated memory.
  124. /// @return An iterator to start of the data.
  125. iterator begin() { return iterator(*this, data); }
  126. /// @return A const pointer to start of the data.
  127. // const *begin() const { return data; }
  128. /// @return An iterator to past the last element in the array.
  129. iterator end() { return iterator(*this, data + used); }
  130. /// @return A const pointer to past the last element in the array.
  131. const T *end() const { return data + used ; }
  132. void insert(const T &val) { insert(used, val); }
  133. /// Inserts an element before the ith element of the Array. Running time is O(n).
  134. void insert(int i, const T &val)
  135. {
  136. if (used >= cap)
  137. reserve(cap*2);
  138. // Push all data up one index to make room for the new element.
  139. for(int j = used+1; j > i; --j)
  140. data[j] = data[j-1];
  141. // Add the new element.
  142. data[i] = val;
  143. ++used;
  144. }
  145. void InsertMultiple(int i, const T &val, int numTimes)
  146. {
  147. if (used + numTimes > cap)
  148. reserve((cap+numTimes)*2);
  149. // Push all data up numTimes slots to make room for the new element.
  150. for(int j = used+numTimes; j > i+numTimes; --j)
  151. data[j] = data[j-numTimes];
  152. // Add the new element.
  153. for(int j = i; j < i + numTimes; ++j)
  154. data[j] = val;
  155. used += numTimes;
  156. }
  157. /// Returns true if the members of this array all have distinct values. Tests each pair and runs in O(n^2).
  158. bool MembersUnique() const
  159. {
  160. for(size_t i = 0; i < used; ++i)
  161. for(size_t j = i+1; j < used; ++j)
  162. if (data[i] == data[j])
  163. return false;
  164. return true;
  165. }
  166. /// Inserts an element to the front of the Array. Running time is O(n).
  167. void push_front(const T &val) { insert(0, val); }
  168. /// Inserts the given element to back of the Array. Running time is O(1).
  169. void push_back(const T &val)
  170. {
  171. if (used >= cap)
  172. reserve(cap*2);
  173. data[used] = val;
  174. ++used;
  175. }
  176. /// Inserts the given element to back of the Array, but without doing bounds checking.
  177. /// Call this function only if you know there is enough capacity in the Array.
  178. void push_back_unsafe(const T &val)
  179. {
  180. assert(used < cap);
  181. data[used++] = val;
  182. }
  183. /// Inserts an uninitialized value to the back of the Array, and returns a reference to it.
  184. /// This function was implemented after profiling a code generation issue with VS2008 that
  185. /// resulted in suboptimal performance.
  186. T &push_back_unsafe_pod()
  187. {
  188. assert(used < cap);
  189. return data[used++];
  190. }
  191. /// Inserts a default-constructed element to back of the Array. Running time is O(1).
  192. T &push_back()
  193. {
  194. if (used >= cap)
  195. reserve(cap*2);
  196. data[used] = T();
  197. return data[used++];
  198. }
  199. /// Erases a range of elements, excluding the element at index end. That is, the interval [start, end[.
  200. /// Running time is O(n).
  201. void erase(int start, int end)
  202. {
  203. assert(start >= 0);
  204. assert(end >= 0);
  205. assert(start <= end);
  206. assert(end <= used);
  207. if (start == end)
  208. return;
  209. const int items = end-start;
  210. for(int i = start; i < used-items; ++i)
  211. data[i] = data[i+items];
  212. for(int i = used-items; i < used; ++i)
  213. data[i] = T(); ///< \todo Placement new/delete instead of this crap.
  214. used -= items;
  215. }
  216. /// Removes the ith element from the array. Running time is O(n).
  217. void erase(int i)
  218. {
  219. assert(i >= 0);
  220. erase((size_t)i);
  221. }
  222. /// Removes the ith element from the array. Running time is O(n).
  223. void erase(size_t i)
  224. {
  225. assert(i < used);
  226. for(size_t j = i; j+1 < used; ++j)
  227. data[j] = data[j+1];
  228. data[used-1] = T(); ///< \todo Placement new/delete instead of this crap.
  229. --used;
  230. }
  231. /// Clears the last element of the array. Running time is O(1).
  232. void pop_back()
  233. {
  234. assert(used > 0);
  235. data[used-1] = T(); ///< \todo Placement new/delete instead of this crap.
  236. --used; /// \note Doesn't call destructor for the popped object.
  237. }
  238. /// Clears the last element of the array. Running time is O(1). Use for pod data that don't need
  239. /// to be cleared when uninitializing.
  240. void pop_back_pod()
  241. {
  242. assert(used > 0);
  243. --used; /// \note Doesn't call destructor for the popped object.
  244. }
  245. /// Removes all elements in the array. Running time is O(n).
  246. void clear()
  247. {
  248. for(size_t i = 0; i < used; ++i)
  249. data[i] = T(); ///< \todo Placement new/delete instead of this crap.
  250. used = 0;
  251. }
  252. /// Clears the whole Array by simply marking the size to 0. Running time is O(1).
  253. /// Only call this function if the element type is a POD that does not need its dtor
  254. /// to be called for cleanup.
  255. void clear_pod()
  256. {
  257. used = 0;
  258. }
  259. /// Enlarges the allocated memory area if it's not big enough to hold newSize elements.
  260. void reserve(size_t newSize)
  261. {
  262. newSize = newSize < initialSize ? initialSize : newSize;
  263. // only to enlarge.
  264. if (newSize < cap)
  265. return;
  266. const size_t alignment = 4; ///\ todo
  267. T *newData = NewArray<T, AllocT>(newSize, allocator, alignment);
  268. // For POD types, this is more efficient, but need some kind of trait mechanism to detect whether T is POD or not.
  269. // memcpy(newData, data, sizeof(T)*used);
  270. for(size_t i = 0; i < used; ++i)
  271. newData[i] = data[i];
  272. DeleteArray(data, allocator);
  273. data = newData;
  274. cap = newSize;
  275. }
  276. const T &operator[](int index) const
  277. {
  278. assert(index >= 0);
  279. assert((size_t)index < used);
  280. return data[index];
  281. }
  282. T &operator[](int index)
  283. {
  284. assert(index >= 0);
  285. assert((size_t)index < used);
  286. return data[index];
  287. }
  288. Array<T, AllocT> &operator =(const Array<T, AllocT> &rhs)
  289. {
  290. if (this == &rhs)
  291. return *this;
  292. reserve(rhs.size());
  293. for(size_t i = 0; i < rhs.size(); ++i)
  294. data[i] = rhs.data[i];
  295. used = rhs.size();
  296. return *this;
  297. }
  298. private:
  299. T *data;
  300. AllocT *allocator;
  301. size_t cap; ///< The number of elements allocated in the data.
  302. size_t used; ///< The number of elements actually in use.
  303. static const size_t initialSize = 32;
  304. };
  305. } // ~kNet