You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prevector.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // Copyright (c) 2015 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef _BITCOIN_PREVECTOR_H_
  5. #define _BITCOIN_PREVECTOR_H_
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <iterator>
  10. #pragma pack(push, 1)
  11. /** Implements a drop-in replacement for std::vector<T> which stores up to N
  12. * elements directly (without heap allocation). The types Size and Diff are
  13. * used to store element counts, and can be any unsigned + signed type.
  14. *
  15. * Storage layout is either:
  16. * - Direct allocation:
  17. * - Size _size: the number of used elements (between 0 and N)
  18. * - T direct[N]: an array of N elements of type T
  19. * (only the first _size are initialized).
  20. * - Indirect allocation:
  21. * - Size _size: the number of used elements plus N + 1
  22. * - Size capacity: the number of allocated elements
  23. * - T* indirect: a pointer to an array of capacity elements of type T
  24. * (only the first _size are initialized).
  25. *
  26. * The data type T must be movable by memmove/realloc(). Once we switch to C++,
  27. * move constructors can be used instead.
  28. */
  29. template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
  30. class prevector {
  31. public:
  32. typedef Size size_type;
  33. typedef Diff difference_type;
  34. typedef T value_type;
  35. typedef value_type& reference;
  36. typedef const value_type& const_reference;
  37. typedef value_type* pointer;
  38. typedef const value_type* const_pointer;
  39. class iterator {
  40. T* ptr;
  41. public:
  42. typedef Diff difference_type;
  43. typedef T value_type;
  44. typedef T* pointer;
  45. typedef T& reference;
  46. typedef std::random_access_iterator_tag iterator_category;
  47. iterator(T* ptr_) : ptr(ptr_) {}
  48. T& operator*() const { return *ptr; }
  49. T* operator->() const { return ptr; }
  50. T& operator[](size_type pos) { return ptr[pos]; }
  51. const T& operator[](size_type pos) const { return ptr[pos]; }
  52. iterator& operator++() { ptr++; return *this; }
  53. iterator& operator--() { ptr--; return *this; }
  54. iterator operator++(int) { iterator copy(*this); ++(*this); return copy; }
  55. iterator operator--(int) { iterator copy(*this); --(*this); return copy; }
  56. difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
  57. iterator operator+(size_type n) { return iterator(ptr + n); }
  58. iterator& operator+=(size_type n) { ptr += n; return *this; }
  59. iterator operator-(size_type n) { return iterator(ptr - n); }
  60. iterator& operator-=(size_type n) { ptr -= n; return *this; }
  61. bool operator==(iterator x) const { return ptr == x.ptr; }
  62. bool operator!=(iterator x) const { return ptr != x.ptr; }
  63. bool operator>=(iterator x) const { return ptr >= x.ptr; }
  64. bool operator<=(iterator x) const { return ptr <= x.ptr; }
  65. bool operator>(iterator x) const { return ptr > x.ptr; }
  66. bool operator<(iterator x) const { return ptr < x.ptr; }
  67. };
  68. class reverse_iterator {
  69. T* ptr;
  70. public:
  71. typedef Diff difference_type;
  72. typedef T value_type;
  73. typedef T* pointer;
  74. typedef T& reference;
  75. typedef std::bidirectional_iterator_tag iterator_category;
  76. reverse_iterator(T* ptr_) : ptr(ptr_) {}
  77. T& operator*() { return *ptr; }
  78. const T& operator*() const { return *ptr; }
  79. T* operator->() { return ptr; }
  80. const T* operator->() const { return ptr; }
  81. reverse_iterator& operator--() { ptr++; return *this; }
  82. reverse_iterator& operator++() { ptr--; return *this; }
  83. reverse_iterator operator++(int) { reverse_iterator copy(*this); ++(*this); return copy; }
  84. reverse_iterator operator--(int) { reverse_iterator copy(*this); --(*this); return copy; }
  85. bool operator==(reverse_iterator x) const { return ptr == x.ptr; }
  86. bool operator!=(reverse_iterator x) const { return ptr != x.ptr; }
  87. };
  88. class const_iterator {
  89. const T* ptr;
  90. public:
  91. typedef Diff difference_type;
  92. typedef const T value_type;
  93. typedef const T* pointer;
  94. typedef const T& reference;
  95. typedef std::random_access_iterator_tag iterator_category;
  96. const_iterator(const T* ptr_) : ptr(ptr_) {}
  97. const_iterator(iterator x) : ptr(&(*x)) {}
  98. const T& operator*() const { return *ptr; }
  99. const T* operator->() const { return ptr; }
  100. const T& operator[](size_type pos) const { return ptr[pos]; }
  101. const_iterator& operator++() { ptr++; return *this; }
  102. const_iterator& operator--() { ptr--; return *this; }
  103. const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
  104. const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
  105. difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
  106. const_iterator operator+(size_type n) { return const_iterator(ptr + n); }
  107. const_iterator& operator+=(size_type n) { ptr += n; return *this; }
  108. const_iterator operator-(size_type n) { return const_iterator(ptr - n); }
  109. const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
  110. bool operator==(const_iterator x) const { return ptr == x.ptr; }
  111. bool operator!=(const_iterator x) const { return ptr != x.ptr; }
  112. bool operator>=(const_iterator x) const { return ptr >= x.ptr; }
  113. bool operator<=(const_iterator x) const { return ptr <= x.ptr; }
  114. bool operator>(const_iterator x) const { return ptr > x.ptr; }
  115. bool operator<(const_iterator x) const { return ptr < x.ptr; }
  116. };
  117. class const_reverse_iterator {
  118. const T* ptr;
  119. public:
  120. typedef Diff difference_type;
  121. typedef const T value_type;
  122. typedef const T* pointer;
  123. typedef const T& reference;
  124. typedef std::bidirectional_iterator_tag iterator_category;
  125. const_reverse_iterator(T* ptr_) : ptr(ptr_) {}
  126. const_reverse_iterator(reverse_iterator x) : ptr(&(*x)) {}
  127. const T& operator*() const { return *ptr; }
  128. const T* operator->() const { return ptr; }
  129. const_reverse_iterator& operator--() { ptr++; return *this; }
  130. const_reverse_iterator& operator++() { ptr--; return *this; }
  131. const_reverse_iterator operator++(int) { const_reverse_iterator copy(*this); ++(*this); return copy; }
  132. const_reverse_iterator operator--(int) { const_reverse_iterator copy(*this); --(*this); return copy; }
  133. bool operator==(const_reverse_iterator x) const { return ptr == x.ptr; }
  134. bool operator!=(const_reverse_iterator x) const { return ptr != x.ptr; }
  135. };
  136. private:
  137. size_type _size;
  138. union direct_or_indirect {
  139. char direct[sizeof(T) * N];
  140. struct {
  141. size_type capacity;
  142. char* indirect;
  143. };
  144. } _union;
  145. T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
  146. const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
  147. T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect) + pos; }
  148. const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect) + pos; }
  149. bool is_direct() const { return _size <= N; }
  150. void change_capacity(size_type new_capacity) {
  151. if (new_capacity <= N) {
  152. if (!is_direct()) {
  153. T* indirect = indirect_ptr(0);
  154. T* src = indirect;
  155. T* dst = direct_ptr(0);
  156. memcpy(dst, src, size() * sizeof(T));
  157. free(indirect);
  158. _size -= N + 1;
  159. }
  160. } else {
  161. if (!is_direct()) {
  162. _union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
  163. _union.capacity = new_capacity;
  164. } else {
  165. char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
  166. T* src = direct_ptr(0);
  167. T* dst = reinterpret_cast<T*>(new_indirect);
  168. memcpy(dst, src, size() * sizeof(T));
  169. _union.indirect = new_indirect;
  170. _union.capacity = new_capacity;
  171. _size += N + 1;
  172. }
  173. }
  174. }
  175. T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
  176. const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
  177. public:
  178. void assign(size_type n, const T& val) {
  179. clear();
  180. if (capacity() < n) {
  181. change_capacity(n);
  182. }
  183. while (size() < n) {
  184. _size++;
  185. new(static_cast<void*>(item_ptr(size() - 1))) T(val);
  186. }
  187. }
  188. template<typename InputIterator>
  189. void assign(InputIterator first, InputIterator last) {
  190. size_type n = last - first;
  191. clear();
  192. if (capacity() < n) {
  193. change_capacity(n);
  194. }
  195. while (first != last) {
  196. _size++;
  197. new(static_cast<void*>(item_ptr(size() - 1))) T(*first);
  198. ++first;
  199. }
  200. }
  201. prevector() : _size(0) {}
  202. explicit prevector(size_type n) : _size(0) {
  203. resize(n);
  204. }
  205. explicit prevector(size_type n, const T& val = T()) : _size(0) {
  206. change_capacity(n);
  207. while (size() < n) {
  208. _size++;
  209. new(static_cast<void*>(item_ptr(size() - 1))) T(val);
  210. }
  211. }
  212. template<typename InputIterator>
  213. prevector(InputIterator first, InputIterator last) : _size(0) {
  214. size_type n = last - first;
  215. change_capacity(n);
  216. while (first != last) {
  217. _size++;
  218. new(static_cast<void*>(item_ptr(size() - 1))) T(*first);
  219. ++first;
  220. }
  221. }
  222. prevector(const prevector<N, T, Size, Diff>& other) : _size(0) {
  223. change_capacity(other.size());
  224. const_iterator it = other.begin();
  225. while (it != other.end()) {
  226. _size++;
  227. new(static_cast<void*>(item_ptr(size() - 1))) T(*it);
  228. ++it;
  229. }
  230. }
  231. prevector& operator=(const prevector<N, T, Size, Diff>& other) {
  232. if (&other == this) {
  233. return *this;
  234. }
  235. resize(0);
  236. change_capacity(other.size());
  237. const_iterator it = other.begin();
  238. while (it != other.end()) {
  239. _size++;
  240. new(static_cast<void*>(item_ptr(size() - 1))) T(*it);
  241. ++it;
  242. }
  243. return *this;
  244. }
  245. size_type size() const {
  246. return is_direct() ? _size : _size - N - 1;
  247. }
  248. bool empty() const {
  249. return size() == 0;
  250. }
  251. iterator begin() { return iterator(item_ptr(0)); }
  252. const_iterator begin() const { return const_iterator(item_ptr(0)); }
  253. iterator end() { return iterator(item_ptr(size())); }
  254. const_iterator end() const { return const_iterator(item_ptr(size())); }
  255. reverse_iterator rbegin() { return reverse_iterator(item_ptr(size() - 1)); }
  256. const_reverse_iterator rbegin() const { return const_reverse_iterator(item_ptr(size() - 1)); }
  257. reverse_iterator rend() { return reverse_iterator(item_ptr(-1)); }
  258. const_reverse_iterator rend() const { return const_reverse_iterator(item_ptr(-1)); }
  259. size_t capacity() const {
  260. if (is_direct()) {
  261. return N;
  262. } else {
  263. return _union.capacity;
  264. }
  265. }
  266. T& operator[](size_type pos) {
  267. return *item_ptr(pos);
  268. }
  269. const T& operator[](size_type pos) const {
  270. return *item_ptr(pos);
  271. }
  272. void resize(size_type new_size) {
  273. if (size() > new_size) {
  274. erase(item_ptr(new_size), end());
  275. }
  276. if (new_size > capacity()) {
  277. change_capacity(new_size);
  278. }
  279. while (size() < new_size) {
  280. _size++;
  281. new(static_cast<void*>(item_ptr(size() - 1))) T();
  282. }
  283. }
  284. void reserve(size_type new_capacity) {
  285. if (new_capacity > capacity()) {
  286. change_capacity(new_capacity);
  287. }
  288. }
  289. void shrink_to_fit() {
  290. change_capacity(size());
  291. }
  292. void clear() {
  293. resize(0);
  294. }
  295. iterator insert(iterator pos, const T& value) {
  296. size_type p = pos - begin();
  297. size_type new_size = size() + 1;
  298. if (capacity() < new_size) {
  299. change_capacity(new_size + (new_size >> 1));
  300. }
  301. memmove(item_ptr(p + 1), item_ptr(p), (size() - p) * sizeof(T));
  302. _size++;
  303. new(static_cast<void*>(item_ptr(p))) T(value);
  304. return iterator(item_ptr(p));
  305. }
  306. void insert(iterator pos, size_type count, const T& value) {
  307. size_type p = pos - begin();
  308. size_type new_size = size() + count;
  309. if (capacity() < new_size) {
  310. change_capacity(new_size + (new_size >> 1));
  311. }
  312. memmove(item_ptr(p + count), item_ptr(p), (size() - p) * sizeof(T));
  313. _size += count;
  314. for (size_type i = 0; i < count; i++) {
  315. new(static_cast<void*>(item_ptr(p + i))) T(value);
  316. }
  317. }
  318. template<typename InputIterator>
  319. void insert(iterator pos, InputIterator first, InputIterator last) {
  320. size_type p = pos - begin();
  321. difference_type count = last - first;
  322. size_type new_size = size() + count;
  323. if (capacity() < new_size) {
  324. change_capacity(new_size + (new_size >> 1));
  325. }
  326. memmove(item_ptr(p + count), item_ptr(p), (size() - p) * sizeof(T));
  327. _size += count;
  328. while (first != last) {
  329. new(static_cast<void*>(item_ptr(p))) T(*first);
  330. ++p;
  331. ++first;
  332. }
  333. }
  334. iterator erase(iterator pos) {
  335. return erase(pos, pos + 1);
  336. }
  337. iterator erase(iterator first, iterator last) {
  338. iterator p = first;
  339. char* endp = (char*)&(*end());
  340. while (p != last) {
  341. (*p).~T();
  342. _size--;
  343. ++p;
  344. }
  345. memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
  346. return first;
  347. }
  348. void push_back(const T& value) {
  349. size_type new_size = size() + 1;
  350. if (capacity() < new_size) {
  351. change_capacity(new_size + (new_size >> 1));
  352. }
  353. new(item_ptr(size())) T(value);
  354. _size++;
  355. }
  356. void pop_back() {
  357. erase(end() - 1, end());
  358. }
  359. T& front() {
  360. return *item_ptr(0);
  361. }
  362. const T& front() const {
  363. return *item_ptr(0);
  364. }
  365. T& back() {
  366. return *item_ptr(size() - 1);
  367. }
  368. const T& back() const {
  369. return *item_ptr(size() - 1);
  370. }
  371. void swap(prevector<N, T, Size, Diff>& other) {
  372. std::swap(_union, other._union);
  373. std::swap(_size, other._size);
  374. }
  375. ~prevector() {
  376. clear();
  377. if (!is_direct()) {
  378. free(_union.indirect);
  379. _union.indirect = NULL;
  380. }
  381. }
  382. bool operator==(const prevector<N, T, Size, Diff>& other) const {
  383. if (other.size() != size()) {
  384. return false;
  385. }
  386. const_iterator b1 = begin();
  387. const_iterator b2 = other.begin();
  388. const_iterator e1 = end();
  389. while (b1 != e1) {
  390. if ((*b1) != (*b2)) {
  391. return false;
  392. }
  393. ++b1;
  394. ++b2;
  395. }
  396. return true;
  397. }
  398. bool operator!=(const prevector<N, T, Size, Diff>& other) const {
  399. return !(*this == other);
  400. }
  401. bool operator<(const prevector<N, T, Size, Diff>& other) const {
  402. if (size() < other.size()) {
  403. return true;
  404. }
  405. if (size() > other.size()) {
  406. return false;
  407. }
  408. const_iterator b1 = begin();
  409. const_iterator b2 = other.begin();
  410. const_iterator e1 = end();
  411. while (b1 != e1) {
  412. if ((*b1) < (*b2)) {
  413. return true;
  414. }
  415. if ((*b2) < (*b1)) {
  416. return false;
  417. }
  418. ++b1;
  419. ++b2;
  420. }
  421. return false;
  422. }
  423. size_t allocated_memory() const {
  424. if (is_direct()) {
  425. return 0;
  426. } else {
  427. return ((size_t)(sizeof(T))) * _union.capacity;
  428. }
  429. }
  430. value_type* data() noexcept {
  431. return item_ptr(0);
  432. }
  433. const value_type* data() const {
  434. return item_ptr(0);
  435. }
  436. };
  437. #pragma pack(pop)
  438. #endif