vector.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #pragma once
  2. #include "common.h"
  3. #include "memory.h"
  4. namespace pkpy{
  5. template<typename T>
  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*2);
  50. _data[_size++] = std::forward<__ValueT>(t);
  51. }
  52. template<typename... Args>
  53. void emplace_back(Args&&... args) {
  54. if (_size == _capacity) reserve(_capacity*2);
  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*2);
  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& data() { return vec; }
  136. };
  137. template <typename T, typename Container=std::vector<T>>
  138. class stack_no_copy: public stack<T, Container>{
  139. public:
  140. stack_no_copy() = default;
  141. stack_no_copy(const stack_no_copy& other) = delete;
  142. stack_no_copy& operator=(const stack_no_copy& other) = delete;
  143. stack_no_copy(stack_no_copy&& other) noexcept = default;
  144. stack_no_copy& operator=(stack_no_copy&& other) noexcept = default;
  145. };
  146. } // namespace pkpy