vector.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #pragma once
  2. #include "common.h"
  3. #include "memory.h"
  4. namespace pkpy{
  5. template<typename T, int Growth=2>
  6. struct pod_vector{
  7. static constexpr int SizeT = sizeof(T);
  8. static constexpr int N = 64 / SizeT;
  9. // static_assert(64 % SizeT == 0);
  10. static_assert(is_pod<T>::value);
  11. static_assert(N >= 4);
  12. int _size;
  13. int _capacity;
  14. T* _data;
  15. using size_type = int;
  16. pod_vector(): _size(0), _capacity(N) {
  17. _data = (T*)pool64_alloc(_capacity * SizeT);
  18. }
  19. // support initializer list
  20. pod_vector(std::initializer_list<T> il): _size(il.size()), _capacity(std::max(N, _size)) {
  21. _data = (T*)pool64_alloc(_capacity * SizeT);
  22. for(int i=0; i<_size; i++) _data[i] = *(il.begin() + i);
  23. }
  24. pod_vector(int size): _size(size), _capacity(std::max(N, size)) {
  25. _data = (T*)pool64_alloc(_capacity * SizeT);
  26. }
  27. pod_vector(const pod_vector& other): _size(other._size), _capacity(other._capacity) {
  28. _data = (T*)pool64_alloc(_capacity * SizeT);
  29. memcpy(_data, other._data, SizeT * _size);
  30. }
  31. pod_vector(pod_vector&& other) noexcept {
  32. _size = other._size;
  33. _capacity = other._capacity;
  34. _data = other._data;
  35. other._data = nullptr;
  36. }
  37. pod_vector& operator=(pod_vector&& other) noexcept {
  38. if(_data!=nullptr) pool64_dealloc(_data);
  39. _size = other._size;
  40. _capacity = other._capacity;
  41. _data = other._data;
  42. other._data = nullptr;
  43. return *this;
  44. }
  45. // remove copy assignment
  46. pod_vector& operator=(const pod_vector& other) = delete;
  47. template<typename __ValueT>
  48. void push_back(__ValueT&& t) {
  49. if (_size == _capacity) reserve(_capacity*Growth);
  50. _data[_size++] = std::forward<__ValueT>(t);
  51. }
  52. template<typename... Args>
  53. void emplace_back(Args&&... args) {
  54. if (_size == _capacity) reserve(_capacity*Growth);
  55. new (&_data[_size++]) T(std::forward<Args>(args)...);
  56. }
  57. void reserve(int cap){
  58. if(cap <= _capacity) return;
  59. _capacity = cap;
  60. T* old_data = _data;
  61. _data = (T*)pool64_alloc(_capacity * SizeT);
  62. if(old_data != nullptr){
  63. memcpy(_data, old_data, SizeT * _size);
  64. pool64_dealloc(old_data);
  65. }
  66. }
  67. void pop_back() { _size--; }
  68. T popx_back() { T t = std::move(_data[_size-1]); _size--; return t; }
  69. void extend(const pod_vector& other){
  70. for(int i=0; i<other.size(); i++) push_back(other[i]);
  71. }
  72. void extend(const T* begin, const T* end){
  73. for(auto it=begin; it!=end; it++) push_back(*it);
  74. }
  75. T& operator[](int index) { return _data[index]; }
  76. const T& operator[](int index) const { return _data[index]; }
  77. T* begin() { return _data; }
  78. T* end() { return _data + _size; }
  79. const T* begin() const { return _data; }
  80. const T* end() const { return _data + _size; }
  81. T& back() { return _data[_size - 1]; }
  82. const T& back() const { return _data[_size - 1]; }
  83. bool empty() const { return _size == 0; }
  84. int size() const { return _size; }
  85. T* data() { return _data; }
  86. const T* data() const { return _data; }
  87. void clear() { _size=0; }
  88. template<typename __ValueT>
  89. void insert(int i, __ValueT&& val){
  90. if (_size == _capacity) reserve(_capacity*Growth);
  91. for(int j=_size; j>i; j--) _data[j] = _data[j-1];
  92. _data[i] = std::forward<__ValueT>(val);
  93. _size++;
  94. }
  95. void erase(int i){
  96. for(int j=i; j<_size-1; j++) _data[j] = _data[j+1];
  97. _size--;
  98. }
  99. void reverse(){
  100. std::reverse(_data, _data+_size);
  101. }
  102. void resize(int size){
  103. if(size > _capacity) reserve(size);
  104. _size = size;
  105. }
  106. std::pair<T*, int> detach() noexcept {
  107. T* p = _data;
  108. int size = _size;
  109. _data = nullptr;
  110. _size = 0;
  111. return {p, size};
  112. }
  113. ~pod_vector() {
  114. if(_data != nullptr) pool64_dealloc(_data);
  115. }
  116. };
  117. template <typename T, typename Container=std::vector<T>>
  118. class stack{
  119. Container vec;
  120. public:
  121. void push(const T& t){ vec.push_back(t); }
  122. void push(T&& t){ vec.push_back(std::move(t)); }
  123. template<typename... Args>
  124. void emplace(Args&&... args){
  125. vec.emplace_back(std::forward<Args>(args)...);
  126. }
  127. void pop(){ vec.pop_back(); }
  128. void clear(){ vec.clear(); }
  129. bool empty() const { return vec.empty(); }
  130. typename Container::size_type size() const { return vec.size(); }
  131. T& top(){ return vec.back(); }
  132. const T& top() const { return vec.back(); }
  133. T popx(){ T t = std::move(vec.back()); vec.pop_back(); return t; }
  134. void reserve(int n){ vec.reserve(n); }
  135. Container& container() { return vec; }
  136. const Container& container() const { return vec; }
  137. };
  138. template <typename T, typename Container=std::vector<T>>
  139. class stack_no_copy: public stack<T, Container>{
  140. public:
  141. stack_no_copy() = default;
  142. stack_no_copy(const stack_no_copy& other) = delete;
  143. stack_no_copy& operator=(const stack_no_copy& other) = delete;
  144. stack_no_copy(stack_no_copy&& other) noexcept = default;
  145. stack_no_copy& operator=(stack_no_copy&& other) noexcept = default;
  146. };
  147. } // namespace pkpy
  148. namespace pkpy
  149. {
  150. // explicitly mark a type as trivially relocatable for better performance
  151. template<typename T>
  152. struct TriviallyRelocatable
  153. {
  154. constexpr static bool value =
  155. std::is_trivially_copyable_v<T> && std::is_trivially_destructible_v<T>;
  156. };
  157. template<typename T>
  158. constexpr inline bool is_trivially_relocatable_v =
  159. TriviallyRelocatable<T>::value;
  160. template<typename T>
  161. struct TriviallyRelocatable<std::shared_ptr<T>>
  162. {
  163. constexpr static bool value = true;
  164. };
  165. // the implementation of small_vector
  166. template<typename T, std::size_t N>
  167. class small_vector
  168. {
  169. alignas(T) char m_buffer[sizeof(T) * N];
  170. T* m_begin;
  171. T* m_end;
  172. T* m_max;
  173. public:
  174. using value_type = T;
  175. using size_type = int;
  176. using difference_type = int;
  177. using reference = T&;
  178. using const_reference = const T&;
  179. using pointer = T*;
  180. using const_pointer = const T*;
  181. using iterator = T*;
  182. using const_iterator = const T*;
  183. using reverse_iterator = std::reverse_iterator<iterator>;
  184. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  185. [[nodiscard]] bool is_small() const { return m_begin == reinterpret_cast<const T*>(m_buffer); }
  186. [[nodiscard]] size_type size() const { return m_end - m_begin; }
  187. [[nodiscard]] size_type capacity() const { return m_max - m_begin; }
  188. [[nodiscard]] bool empty() const { return m_begin == m_end; }
  189. pointer data() { return m_begin; }
  190. const_pointer data() const { return m_begin; }
  191. reference operator[](size_type index) { return m_begin[index]; }
  192. const_reference operator[](size_type index) const { return m_begin[index]; }
  193. iterator begin() { return m_begin; }
  194. const_iterator begin() const { return m_begin; }
  195. iterator end() { return m_end; }
  196. const_iterator end() const { return m_end; }
  197. reference front() { return *begin(); }
  198. const_reference front() const { return *begin(); }
  199. reference back() { return *(end() - 1); }
  200. const_reference back() const { return *(end() - 1); }
  201. reverse_iterator rbegin() { return reverse_iterator(end()); }
  202. const_reverse_iterator rbegin() const
  203. {
  204. return const_reverse_iterator(end());
  205. }
  206. reverse_iterator rend() { return reverse_iterator(begin()); }
  207. const_reverse_iterator rend() const
  208. {
  209. return const_reverse_iterator(begin());
  210. }
  211. private:
  212. static void uninitialized_copy_n(const void* src, size_type n, void* dest)
  213. {
  214. if constexpr (std::is_trivially_copyable_v<T>)
  215. {
  216. std::memcpy(dest, src, sizeof(T) * n);
  217. }
  218. else
  219. {
  220. for (size_type i = 0; i < n; i++)
  221. {
  222. ::new((T*) dest + i) T(*((const T*) src + i));
  223. }
  224. }
  225. }
  226. static void uninitialized_relocate_n(void* src, size_type n, void* dest)
  227. {
  228. if constexpr (is_trivially_relocatable_v<T>)
  229. {
  230. std::memcpy(dest, src, sizeof(T) * n);
  231. }
  232. else
  233. {
  234. for (size_type i = 0; i < n; i++)
  235. {
  236. ::new((T*) dest + i) T(std::move(*((T*) src + i)));
  237. ((T*) src + i)->~T();
  238. }
  239. }
  240. }
  241. public:
  242. small_vector() : m_begin(reinterpret_cast<T*>(m_buffer)), m_end(m_begin), m_max(m_begin + N) {}
  243. small_vector(const small_vector& other) noexcept
  244. {
  245. const auto size = other.size();
  246. const auto capacity = other.capacity();
  247. m_begin = reinterpret_cast<T*>(other.is_small() ? m_buffer : std::malloc(sizeof(T) * capacity));
  248. uninitialized_copy_n(other.begin, size, this->m_begin);
  249. m_end = m_begin + size;
  250. m_max = m_begin + capacity;
  251. }
  252. small_vector(small_vector&& other) noexcept
  253. {
  254. if(other.is_small())
  255. {
  256. m_begin = reinterpret_cast<T*>(m_buffer);
  257. uninitialized_relocate_n(other.m_buffer, other.size(), m_buffer);
  258. m_end = m_begin + other.size();
  259. m_max = m_begin + N;
  260. }
  261. else
  262. {
  263. m_begin = other.m_begin;
  264. m_end = other.m_end;
  265. m_max = other.m_max;
  266. }
  267. other.m_begin = reinterpret_cast<T*>(other.m_buffer);
  268. other.m_end = other.m_begin;
  269. other.m_max = other.m_begin + N;
  270. }
  271. small_vector& operator=(const small_vector& other) noexcept
  272. {
  273. if (this != &other)
  274. {
  275. ~small_vector();
  276. ::new (this) small_vector(other);
  277. }
  278. return *this;
  279. }
  280. small_vector& operator=(small_vector&& other) noexcept
  281. {
  282. if (this != &other)
  283. {
  284. ~small_vector();
  285. :: new (this) small_vector(std::move(other));
  286. }
  287. return *this;
  288. }
  289. ~small_vector()
  290. {
  291. std::destroy(m_begin, m_end);
  292. if (!is_small()) std::free(m_begin);
  293. }
  294. template<typename... Args>
  295. void emplace_back(Args&& ...args) noexcept
  296. {
  297. if (m_end == m_max)
  298. {
  299. const auto new_capacity = capacity() * 2;
  300. const auto size = this->size();
  301. if (!is_small())
  302. {
  303. if constexpr (is_trivially_relocatable_v<T>)
  304. {
  305. m_begin = (pointer)std::realloc(m_begin, sizeof(T) * new_capacity);
  306. }
  307. else
  308. {
  309. auto new_data = (pointer) std::malloc(sizeof(T) * new_capacity);
  310. uninitialized_relocate_n(m_begin, size, new_data);
  311. std::free(m_begin);
  312. m_begin = new_data;
  313. }
  314. }
  315. else
  316. {
  317. auto new_data = (pointer) std::malloc(sizeof(T) * new_capacity);
  318. uninitialized_relocate_n(m_buffer, size, new_data);
  319. m_begin = new_data;
  320. }
  321. m_end = m_begin + size;
  322. m_max = m_begin + new_capacity;
  323. }
  324. ::new(m_end) T(std::forward<Args>(args)...);
  325. m_end++;
  326. }
  327. void push_back(const T& value) { emplace_back(value); }
  328. void push_back(T&& value) { emplace_back(std::move(value)); }
  329. void pop_back()
  330. {
  331. m_end--;
  332. if constexpr (!std::is_trivially_destructible_v<T>)
  333. {
  334. m_end->~T();
  335. }
  336. }
  337. void clear()
  338. {
  339. std::destroy(m_begin, m_end);
  340. m_end = m_begin;
  341. }
  342. };
  343. // small_vector_no_copy_and_move
  344. template<typename T, std::size_t N>
  345. class small_vector_no_copy_and_move: public small_vector<T, N>
  346. {
  347. public:
  348. small_vector_no_copy_and_move() = default;
  349. small_vector_no_copy_and_move(const small_vector_no_copy_and_move& other) = delete;
  350. small_vector_no_copy_and_move& operator=(const small_vector_no_copy_and_move& other) = delete;
  351. small_vector_no_copy_and_move(small_vector_no_copy_and_move&& other) = delete;
  352. small_vector_no_copy_and_move& operator=(small_vector_no_copy_and_move&& other) = delete;
  353. };
  354. } // namespace pkpy