vector 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. #ifndef __GBLIBCPP_VECTOR__
  2. #define __GBLIBCPP_VECTOR__
  3. #include <bits/iter_ops>
  4. #include <functional>
  5. #include <memory>
  6. #include <initializer_list>
  7. #include <cstddef>
  8. namespace std {
  9. template <typename T, typename Allocator = std::allocator<T>>
  10. class vector {
  11. public:
  12. using value_type = T;
  13. using allocator_type = Allocator;
  14. using size_type = std::size_t;
  15. using difference_type = std::ptrdiff_t;
  16. using reference = T&;
  17. using const_reference = const T&;
  18. template <bool Const>
  19. class _iterator {
  20. public:
  21. // TODO:
  22. // using iterator_category = std::random_access_iterator_tag;
  23. using value_type = std::conditional_t<Const, const T, T>;
  24. using difference_type = std::ptrdiff_t;
  25. using pointer = std::add_pointer_t<value_type>;
  26. using reference = std::add_lvalue_reference_t<value_type>;
  27. private:
  28. T* m_ptr;
  29. public:
  30. constexpr _iterator(void) noexcept : m_ptr() {}
  31. constexpr explicit _iterator(const T* ptr) noexcept
  32. : m_ptr(const_cast<T*>(ptr)) {}
  33. constexpr _iterator(const _iterator& other) noexcept = default;
  34. constexpr _iterator(_iterator&& other) noexcept = default;
  35. constexpr _iterator& operator=(const _iterator& other) noexcept = default;
  36. constexpr _iterator& operator=(_iterator&& other) noexcept = default;
  37. constexpr bool operator==(const _iterator& other) const noexcept = default;
  38. constexpr reference operator*() const noexcept { return *m_ptr; }
  39. constexpr pointer operator&() const noexcept
  40. { return std::addressof(this->operator*()); }
  41. constexpr pointer operator->() const noexcept
  42. { return this->operator&(); }
  43. constexpr _iterator& operator++() noexcept
  44. { ++m_ptr; return *this; }
  45. constexpr _iterator operator++(int) noexcept
  46. { _iterator ret(m_ptr); (void)this->operator++(); return ret; }
  47. constexpr _iterator& operator--(void) noexcept
  48. { --m_ptr; return *this; }
  49. constexpr _iterator operator--(int) noexcept
  50. { _iterator ret(m_ptr); (void)this->operator--(); return ret; }
  51. constexpr _iterator& operator+=(difference_type n) noexcept
  52. { m_ptr += n; return *this; }
  53. constexpr _iterator& operator-=(difference_type n) noexcept
  54. { m_ptr -= n; return *this; }
  55. constexpr _iterator operator+(difference_type n) const noexcept
  56. { return _iterator { m_ptr + n }; }
  57. constexpr _iterator operator-(difference_type n) const noexcept
  58. { return _iterator { m_ptr - n }; }
  59. constexpr difference_type operator-(const _iterator& other) const noexcept
  60. { return m_ptr - other.m_ptr; }
  61. constexpr reference operator[](difference_type n) const noexcept
  62. { return m_ptr[n]; }
  63. constexpr operator bool() { return m_ptr; }
  64. constexpr operator _iterator<true>() { return _iterator<true> { m_ptr }; }
  65. constexpr operator _iterator<false>() { return _iterator<false> { m_ptr }; }
  66. constexpr operator const T*() { return m_ptr; }
  67. };
  68. private:
  69. using alloc_traits = std::allocator_traits<Allocator>;
  70. public:
  71. using pointer = typename alloc_traits::pointer;
  72. using const_pointer = typename alloc_traits::const_pointer;
  73. using iterator = _iterator<false>;
  74. using const_iterator = _iterator<true>;
  75. private:
  76. T* m_data;
  77. size_type m_size;
  78. size_type m_capacity;
  79. allocator_type m_alloc;
  80. private:
  81. // assert(n >= m_size)
  82. constexpr void _reallocate_safe(size_type n)
  83. {
  84. auto* newptr = alloc_traits::allocate(m_alloc, n);
  85. for (size_t i = 0; i < m_size; ++i) {
  86. alloc_traits::construct(m_alloc, newptr + i, std::move(m_data[i]));
  87. alloc_traits::destroy(m_alloc, m_data + i);
  88. }
  89. alloc_traits::deallocate(m_alloc, m_data, m_capacity);
  90. m_data = newptr;
  91. m_capacity = n;
  92. }
  93. // make m_capacity >= n >= m_size
  94. constexpr void _pre_resize(size_type n)
  95. {
  96. if (n < m_size) {
  97. while (n < m_size)
  98. pop_back();
  99. }
  100. else if (n > m_size) {
  101. reserve(n);
  102. }
  103. }
  104. public:
  105. constexpr vector(void)
  106. noexcept(noexcept(Allocator()))
  107. : m_data(), m_size(), m_capacity(), m_alloc() {}
  108. constexpr explicit vector(const Allocator& alloc) noexcept
  109. : m_data(), m_size(), m_capacity(), m_alloc(alloc) {}
  110. constexpr vector(size_type n, const T& val,
  111. const Allocator& alloc = Allocator())
  112. : vector(alloc) { resize(n, val); }
  113. constexpr explicit vector(size_type n,
  114. const Allocator& alloc = Allocator())
  115. : vector(alloc) { resize(n); }
  116. // TODO: check whether InputIter satisfies LegacyInputIterator
  117. template <typename InputIter>
  118. constexpr vector(InputIter first, InputIter last,
  119. const Allocator& alloc = Allocator())
  120. : vector(alloc) { insert(cbegin(), first, last); }
  121. constexpr vector(const vector& other)
  122. : vector(std::allocator_traits<allocator_type>::
  123. select_on_container_copy_construction(other.m_alloc))
  124. { insert(cbegin(), other.begin(), other.end()); }
  125. constexpr vector(const vector& other, const Allocator& alloc)
  126. : vector(alloc) { insert(cbegin(), other.begin(), other.end()); }
  127. constexpr vector(vector&& other) noexcept
  128. : m_data(std::exchange(other.m_data, nullptr))
  129. , m_size(std::exchange(other.m_size, 0))
  130. , m_capacity(std::exchange(other.m_capacity, 0))
  131. , m_alloc(std::move(other.m_alloc)) {}
  132. constexpr vector(vector&& other, const Allocator& alloc)
  133. : vector(alloc)
  134. {
  135. if (alloc == other.get_allocator()) {
  136. m_data = std::exchange(other.m_data, nullptr);
  137. m_size = std::exchange(other.m_size, 0);
  138. m_capacity = std::exchange(other.m_capacity, 0);
  139. } else {
  140. // TODO: std::move_iterator
  141. // insert(cbegin(), std::make_move_iterator(other.begin()),
  142. // std::make_move_iterator(other.end()));
  143. for (auto& item : other)
  144. emplace_back(std::move(item));
  145. }
  146. }
  147. constexpr vector(std::initializer_list<T> init,
  148. const Allocator& alloc = Allocator())
  149. : vector(alloc) { insert(cbegin(), init.begin(), init.end()); }
  150. constexpr ~vector()
  151. {
  152. resize(0);
  153. shrink_to_fit();
  154. }
  155. constexpr vector& operator=(const vector& other)
  156. {
  157. clear();
  158. if constexpr (alloc_traits::
  159. propagate_on_container_copy_assignment::value) {
  160. if (m_alloc != other.m_alloc)
  161. shrink_to_fit();
  162. m_alloc = other.m_alloc;
  163. }
  164. insert(cbegin(), other.begin(), other.end());
  165. return *this;
  166. }
  167. constexpr vector& operator=(vector&& other)
  168. {
  169. clear();
  170. if constexpr (alloc_traits::
  171. propagate_on_container_move_assignment::value) {
  172. shrink_to_fit();
  173. m_alloc = std::move(other.m_alloc);
  174. }
  175. else {
  176. if (m_alloc != other.m_alloc) {
  177. // TODO: std::move_iterator
  178. for (auto& item : other)
  179. emplace_back(std::move(item));
  180. return *this;
  181. }
  182. shrink_to_fit();
  183. }
  184. m_data = std::exchange(other.m_data, nullptr);
  185. m_size = std::exchange(other.m_size, 0);
  186. m_capacity = std::exchange(other.m_capacity, 0);
  187. return *this;
  188. }
  189. constexpr vector& operator=(std::initializer_list<T> init)
  190. {
  191. assign(init.begin(), init.end());
  192. return *this;
  193. }
  194. constexpr void assign(size_type n, const T& val)
  195. {
  196. clear();
  197. resize(n, val);
  198. }
  199. // TODO: check whether InputIter satisfies LegacyInputIterator
  200. template <typename InputIter>
  201. constexpr void assign(InputIter first, InputIter last)
  202. {
  203. clear();
  204. insert(cbegin(), first, last);
  205. }
  206. constexpr void assign(std::initializer_list<T> init)
  207. {
  208. clear();
  209. insert(cbegin(), init.begin(), init.end());
  210. }
  211. constexpr allocator_type get_allocator(void) const noexcept
  212. { return m_alloc; }
  213. constexpr reference at(size_type pos)
  214. {
  215. // TODO: exceptions
  216. // if (pos >= sz)
  217. // throw std::out_of_range("vector::at");
  218. return m_data[pos];
  219. }
  220. constexpr const_reference at(size_type pos) const
  221. {
  222. // TODO: exceptions
  223. // if (pos >= sz)
  224. // throw std::out_of_range("vector::at");
  225. return m_data[pos];
  226. }
  227. constexpr reference operator[](size_type pos) noexcept
  228. { return m_data[pos]; }
  229. constexpr const_reference operator[](size_type pos) const noexcept
  230. { return m_data[pos]; }
  231. constexpr reference front() noexcept
  232. { return m_data[0]; }
  233. constexpr const_reference front() const noexcept
  234. { return m_data[0]; }
  235. constexpr reference back() noexcept
  236. { return m_data[m_size - 1]; }
  237. constexpr const_reference back() const noexcept
  238. { return m_data[m_size - 1]; }
  239. constexpr T* data(void) noexcept
  240. { return m_data; }
  241. constexpr const T* data(void) const noexcept
  242. { return m_data; }
  243. // TODO: std::reverse_iterator
  244. constexpr iterator begin() noexcept
  245. { return iterator { m_data }; }
  246. constexpr const_iterator begin() const noexcept
  247. { return const_iterator { m_data }; }
  248. constexpr const_iterator cbegin() const noexcept
  249. { return const_iterator { m_data }; }
  250. constexpr iterator end() noexcept
  251. { return iterator { m_data + m_size }; }
  252. constexpr const_iterator end() const noexcept
  253. { return const_iterator { m_data + m_size }; }
  254. constexpr const_iterator cend() const noexcept
  255. { return const_iterator { m_data + m_size }; }
  256. [[nodiscard]] constexpr bool empty() const noexcept
  257. { return m_size == 0; }
  258. constexpr size_type size() const noexcept
  259. { return m_size; }
  260. constexpr size_type capacity() const noexcept
  261. { return m_capacity; }
  262. constexpr void reserve(size_type new_cap)
  263. {
  264. if (new_cap > m_capacity)
  265. _reallocate_safe(new_cap);
  266. }
  267. constexpr void resize(size_type n)
  268. {
  269. _pre_resize(n);
  270. while (n > m_size)
  271. emplace_back();
  272. }
  273. constexpr void resize(size_type n, const value_type& value)
  274. {
  275. _pre_resize(n);
  276. while (n > m_size)
  277. emplace_back(value);
  278. }
  279. constexpr void shrink_to_fit()
  280. {
  281. if (m_size != m_capacity)
  282. _reallocate_safe(m_size);
  283. }
  284. constexpr void clear() noexcept
  285. { resize(0); }
  286. template <typename... Args>
  287. constexpr iterator emplace(const_iterator pos, Args&&... args)
  288. {
  289. size_type idx = pos - m_data;
  290. if (!pos)
  291. reserve(1);
  292. if (m_size == m_capacity)
  293. reserve(m_capacity * 2);
  294. for (size_type i = m_size; i > idx; --i)
  295. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i-1]));
  296. alloc_traits::construct(m_alloc, m_data + idx,
  297. std::forward<Args>(args)...);
  298. ++m_size;
  299. return iterator { m_data + idx };
  300. }
  301. constexpr iterator insert(const_iterator pos, T&& val)
  302. { return emplace(pos, std::move(val)); }
  303. constexpr iterator insert(const_iterator pos, const T& val)
  304. { return emplace(pos, val); }
  305. constexpr iterator insert(const_iterator pos, size_type n, const T& val)
  306. {
  307. if (!n)
  308. return pos;
  309. size_type idx = pos - m_data;
  310. if (!pos)
  311. reserve(n);
  312. if (m_size + n > m_capacity)
  313. reserve(m_capacity * 2);
  314. for (size_type i = m_size + n - 1; i >= idx + n; --i)
  315. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i-n]));
  316. for (size_type i = idx; i < idx + n; ++i)
  317. alloc_traits::construct(m_alloc, m_data + i, val);
  318. m_size += n;
  319. return iterator { m_data + idx };
  320. }
  321. // TODO: LegacyInputIterator version of this
  322. template <typename ForwardIter>
  323. constexpr iterator insert(const_iterator pos,
  324. ForwardIter first, ForwardIter last)
  325. {
  326. size_type idx = pos - m_data;
  327. size_type n = 0;
  328. ForwardIter tmp = first;
  329. while (tmp != last)
  330. ++n, ++tmp;
  331. if (!n)
  332. return pos;
  333. if (!pos)
  334. reserve(n);
  335. if (m_size + n > m_capacity)
  336. reserve(m_capacity * 2);
  337. for (size_type i = m_size + n - 1; i >= idx + n; --i)
  338. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i-n]));
  339. for (size_type i = idx; i < idx + n; ++i)
  340. alloc_traits::construct(m_alloc, m_data + i, *first++);
  341. m_size += n;
  342. return iterator { m_data + idx };
  343. }
  344. constexpr iterator insert(const_iterator pos, std::initializer_list<T> init)
  345. { return insert(pos, init.begin(), init.end()); }
  346. constexpr iterator erase(const_iterator pos)
  347. {
  348. size_type idx = pos - m_data;
  349. alloc_traits::destroy(m_alloc, m_data + idx);
  350. for (size_type i = idx; i < m_size - 1; ++i)
  351. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i+1]));
  352. --m_size;
  353. return iterator { m_data + idx };
  354. }
  355. constexpr iterator erase(const_iterator first, const_iterator last)
  356. {
  357. size_type n = last - first;
  358. if (!n)
  359. return last;
  360. size_type idx = first - m_data;
  361. for (size_type i = idx; i < idx + n; ++i)
  362. alloc_traits::destroy(m_alloc, m_data + i);
  363. for (size_type i = idx; i < m_size - n; ++i)
  364. m_alloc.construct(m_data + i, std::move(m_data[i+n]));
  365. m_size -= n;
  366. return iterator { m_data + idx };
  367. }
  368. constexpr void push_back(const T& val) { insert(cend(), val); }
  369. constexpr void push_back(T&& val) { insert(cend(), std::move(val)); }
  370. template <typename... Args>
  371. constexpr void emplace_back(Args&&... args)
  372. { emplace(cend(), std::forward<Args>(args)...); }
  373. constexpr void pop_back() { erase(--cend()); }
  374. constexpr void swap(vector& other) noexcept(
  375. alloc_traits::propagate_on_container_swap::value
  376. || alloc_traits::is_always_equal::value)
  377. {
  378. if (alloc_traits::propagate_on_container_swap::value)
  379. std::swap(m_alloc, other.m_alloc);
  380. std::swap(m_data, other.m_data);
  381. std::swap(m_size, other.m_size);
  382. std::swap(m_capacity, other.m_capacity);
  383. }
  384. };
  385. template <typename T, typename Allocator>
  386. constexpr void swap(
  387. std::vector<T, Allocator>& lhs,
  388. std::vector<T, Allocator>& rhs) noexcept(noexcept(lhs.swap(rhs)))
  389. { lhs.swap(rhs); }
  390. template <typename T, typename Allocator, typename U>
  391. constexpr typename std::vector<T, Allocator>::size_type
  392. erase(std::vector<T, Allocator>& vec, const U& value)
  393. {
  394. typename std::vector<T, Allocator>::size_type n = 0;
  395. for (auto iter = vec.begin(); iter != vec.end(); ) {
  396. if (*iter == value) {
  397. iter = vec.erase(iter);
  398. ++n;
  399. } else {
  400. ++iter;
  401. }
  402. }
  403. return n;
  404. }
  405. template <typename T, typename Allocator, typename Pred>
  406. constexpr typename std::vector<T, Allocator>::size_type
  407. erase_if(std::vector<T, Allocator>& vec, Pred pred)
  408. {
  409. typename std::vector<T, Allocator>::size_type n = 0;
  410. for (auto iter = vec.begin(); iter != vec.end(); ) {
  411. if (pred(*iter)) {
  412. iter = vec.erase(iter);
  413. ++n;
  414. } else {
  415. ++iter;
  416. }
  417. }
  418. return n;
  419. }
  420. } // namespace std
  421. #endif