vector.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #include <new>
  3. #include <nall/array-span.hpp>
  4. #include <nall/array-view.hpp>
  5. #include <nall/bit.hpp>
  6. #include <nall/function.hpp>
  7. #include <nall/iterator.hpp>
  8. #include <nall/maybe.hpp>
  9. #include <nall/memory.hpp>
  10. #include <nall/merge-sort.hpp>
  11. #include <nall/range.hpp>
  12. #include <nall/traits.hpp>
  13. #include <nall/view.hpp>
  14. namespace nall {
  15. template<typename T>
  16. struct vector_base {
  17. using type = vector_base;
  18. //core.hpp
  19. vector_base() = default;
  20. vector_base(const initializer_list<T>& values);
  21. vector_base(const type& source);
  22. vector_base(type&& source);
  23. ~vector_base();
  24. explicit operator bool() const;
  25. operator array_span<T>();
  26. operator array_view<T>() const;
  27. template<typename Cast = T> auto capacity() const -> uint64_t;
  28. template<typename Cast = T> auto size() const -> uint64_t;
  29. template<typename Cast = T> auto data() -> Cast*;
  30. template<typename Cast = T> auto data() const -> const Cast*;
  31. //assign.hpp
  32. auto operator=(const type& source) -> type&;
  33. auto operator=(type&& source) -> type&;
  34. //compare.hpp
  35. auto operator==(const type& source) const -> bool;
  36. auto operator!=(const type& source) const -> bool;
  37. //memory.hpp
  38. auto reset() -> void;
  39. auto acquire(const T* data, uint64_t size, uint64_t capacity = 0) -> void;
  40. auto release() -> T*;
  41. auto reserveLeft(uint64_t capacity) -> bool;
  42. auto reserveRight(uint64_t capacity) -> bool;
  43. auto reserve(uint64_t capacity) -> bool { return reserveRight(capacity); }
  44. auto reallocateLeft(uint64_t size) -> bool;
  45. auto reallocateRight(uint64_t size) -> bool;
  46. auto reallocate(uint64_t size) -> bool { return reallocateRight(size); }
  47. auto resizeLeft(uint64_t size, const T& value = T()) -> bool;
  48. auto resizeRight(uint64_t size, const T& value = T()) -> bool;
  49. auto resize(uint64_t size, const T& value = T()) -> bool { return resizeRight(size, value); }
  50. //access.hpp
  51. alwaysinline auto operator[](uint64_t offset) -> T&;
  52. alwaysinline auto operator[](uint64_t offset) const -> const T&;
  53. alwaysinline auto operator()(uint64_t offset) -> T&;
  54. alwaysinline auto operator()(uint64_t offset, const T& value) const -> const T&;
  55. alwaysinline auto left() -> T&;
  56. alwaysinline auto first() -> T& { return left(); }
  57. alwaysinline auto left() const -> const T&;
  58. alwaysinline auto first() const -> const T& { return left(); }
  59. alwaysinline auto right() -> T&;
  60. alwaysinline auto last() -> T& { return right(); }
  61. alwaysinline auto right() const -> const T&;
  62. alwaysinline auto last() const -> const T& { return right(); }
  63. //modify.hpp
  64. auto prepend(const T& value) -> void;
  65. auto prepend(T&& value) -> void;
  66. auto prepend(const type& values) -> void;
  67. auto prepend(type&& values) -> void;
  68. auto append(const T& value) -> void;
  69. auto append(T&& value) -> void;
  70. auto append(const type& values) -> void;
  71. auto append(type&& values) -> void;
  72. auto insert(uint64_t offset, const T& value) -> void;
  73. auto removeLeft(uint64_t length = 1) -> void;
  74. auto removeFirst(uint64_t length = 1) -> void { return removeLeft(length); }
  75. auto removeRight(uint64_t length = 1) -> void;
  76. auto removeLast(uint64_t length = 1) -> void { return removeRight(length); }
  77. auto remove(uint64_t offset, uint64_t length = 1) -> void;
  78. auto removeByIndex(uint64_t offset) -> bool;
  79. auto removeByValue(const T& value) -> bool;
  80. auto takeLeft() -> T;
  81. auto takeFirst() -> T { return move(takeLeft()); }
  82. auto takeRight() -> T;
  83. auto takeLast() -> T { return move(takeRight()); }
  84. auto take(uint64_t offset) -> T;
  85. //iterator.hpp
  86. auto begin() -> iterator<T> { return {data(), 0}; }
  87. auto end() -> iterator<T> { return {data(), size()}; }
  88. auto begin() const -> iterator_const<T> { return {data(), 0}; }
  89. auto end() const -> iterator_const<T> { return {data(), size()}; }
  90. auto rbegin() -> reverse_iterator<T> { return {data(), size() - 1}; }
  91. auto rend() -> reverse_iterator<T> { return {data(), (uint64_t)-1}; }
  92. auto rbegin() const -> reverse_iterator_const<T> { return {data(), size() - 1}; }
  93. auto rend() const -> reverse_iterator_const<T> { return {data(), (uint64_t)-1}; }
  94. //utility.hpp
  95. auto fill(const T& value = {}) -> void;
  96. auto sort(const function<bool (const T& lhs, const T& rhs)>& comparator = [](auto& lhs, auto& rhs) { return lhs < rhs; }) -> void;
  97. auto reverse() -> void;
  98. auto find(const function<bool (const T& lhs)>& comparator) -> maybe<uint64_t>;
  99. auto find(const T& value) const -> maybe<uint64_t>;
  100. auto findSorted(const T& value) const -> maybe<uint64_t>;
  101. auto foreach(const function<void (const T&)>& callback) -> void;
  102. auto foreach(const function<void (uint, const T&)>& callback) -> void;
  103. protected:
  104. T* _pool = nullptr; //pointer to first initialized element in pool
  105. uint64_t _size = 0; //number of initialized elements in pool
  106. uint64_t _left = 0; //number of allocated elements free on the left of pool
  107. uint64_t _right = 0; //number of allocated elements free on the right of pool
  108. };
  109. }
  110. #define vector vector_base
  111. #include <nall/vector/core.hpp>
  112. #include <nall/vector/assign.hpp>
  113. #include <nall/vector/compare.hpp>
  114. #include <nall/vector/memory.hpp>
  115. #include <nall/vector/access.hpp>
  116. #include <nall/vector/modify.hpp>
  117. #include <nall/vector/iterator.hpp>
  118. #include <nall/vector/utility.hpp>
  119. #undef vector
  120. namespace nall {
  121. template<typename T> struct vector : vector_base<T> {
  122. using vector_base<T>::vector_base;
  123. };
  124. }
  125. #include <nall/vector/specialization/uint8_t.hpp>