vector.h 13 KB

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