vector 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. T* newptr = nullptr;
  85. if (n)
  86. newptr = alloc_traits::allocate(m_alloc, n);
  87. for (size_t i = 0; i < m_size; ++i) {
  88. if (n)
  89. alloc_traits::construct(m_alloc, newptr + i, std::move(m_data[i]));
  90. alloc_traits::destroy(m_alloc, m_data + i);
  91. }
  92. alloc_traits::deallocate(m_alloc, m_data, m_capacity);
  93. m_data = newptr;
  94. m_capacity = n;
  95. }
  96. // make m_capacity >= n >= m_size
  97. constexpr void _pre_resize(size_type n)
  98. {
  99. while (n < m_size)
  100. pop_back();
  101. reserve(n);
  102. }
  103. public:
  104. constexpr vector(void)
  105. noexcept(noexcept(Allocator()))
  106. : m_data(), m_size(), m_capacity(), m_alloc() {}
  107. constexpr explicit vector(const Allocator& alloc) noexcept
  108. : m_data(), m_size(), m_capacity(), m_alloc(alloc) {}
  109. constexpr vector(size_type n, const T& val,
  110. const Allocator& alloc = Allocator())
  111. : vector(alloc) { resize(n, val); }
  112. constexpr explicit vector(size_type n,
  113. const Allocator& alloc = Allocator())
  114. : vector(alloc) { resize(n); }
  115. // TODO: check whether InputIter satisfies LegacyInputIterator
  116. template <typename InputIter>
  117. constexpr vector(InputIter first, InputIter last,
  118. const Allocator& alloc = Allocator())
  119. : vector(alloc) { insert(cbegin(), first, last); }
  120. constexpr vector(const vector& other)
  121. : vector(std::allocator_traits<allocator_type>::
  122. select_on_container_copy_construction(other.m_alloc))
  123. { insert(cbegin(), other.begin(), other.end()); }
  124. constexpr vector(const vector& other, const Allocator& alloc)
  125. : vector(alloc) { insert(cbegin(), other.begin(), other.end()); }
  126. constexpr vector(vector&& other) noexcept
  127. : m_data(std::exchange(other.m_data, nullptr))
  128. , m_size(std::exchange(other.m_size, 0))
  129. , m_capacity(std::exchange(other.m_capacity, 0))
  130. , m_alloc(std::move(other.m_alloc)) {}
  131. constexpr vector(vector&& other, const Allocator& alloc)
  132. : vector(alloc)
  133. {
  134. if (alloc == other.get_allocator()) {
  135. m_data = std::exchange(other.m_data, nullptr);
  136. m_size = std::exchange(other.m_size, 0);
  137. m_capacity = std::exchange(other.m_capacity, 0);
  138. } else {
  139. // TODO: std::move_iterator
  140. // insert(cbegin(), std::make_move_iterator(other.begin()),
  141. // std::make_move_iterator(other.end()));
  142. for (auto& item : other)
  143. emplace_back(std::move(item));
  144. }
  145. }
  146. constexpr vector(std::initializer_list<T> init,
  147. const Allocator& alloc = Allocator())
  148. : vector(alloc) { insert(cbegin(), init.begin(), init.end()); }
  149. constexpr ~vector()
  150. {
  151. resize(0);
  152. shrink_to_fit();
  153. }
  154. constexpr vector& operator=(const vector& other)
  155. {
  156. clear();
  157. if constexpr (alloc_traits::
  158. propagate_on_container_copy_assignment::value) {
  159. if (m_alloc != other.m_alloc)
  160. shrink_to_fit();
  161. m_alloc = other.m_alloc;
  162. }
  163. insert(cbegin(), other.begin(), other.end());
  164. return *this;
  165. }
  166. constexpr vector& operator=(vector&& other)
  167. {
  168. clear();
  169. if constexpr (alloc_traits::
  170. propagate_on_container_move_assignment::value) {
  171. shrink_to_fit();
  172. m_alloc = std::move(other.m_alloc);
  173. }
  174. else {
  175. if (m_alloc != other.m_alloc) {
  176. // TODO: std::move_iterator
  177. for (auto& item : other)
  178. emplace_back(std::move(item));
  179. return *this;
  180. }
  181. shrink_to_fit();
  182. }
  183. m_data = std::exchange(other.m_data, nullptr);
  184. m_size = std::exchange(other.m_size, 0);
  185. m_capacity = std::exchange(other.m_capacity, 0);
  186. return *this;
  187. }
  188. constexpr vector& operator=(std::initializer_list<T> init)
  189. {
  190. assign(init.begin(), init.end());
  191. return *this;
  192. }
  193. constexpr void assign(size_type n, const T& val)
  194. {
  195. clear();
  196. resize(n, val);
  197. }
  198. // TODO: check whether InputIter satisfies LegacyInputIterator
  199. template <typename InputIter>
  200. constexpr void assign(InputIter first, InputIter last)
  201. {
  202. clear();
  203. insert(cbegin(), first, last);
  204. }
  205. constexpr void assign(std::initializer_list<T> init)
  206. {
  207. clear();
  208. insert(cbegin(), init.begin(), init.end());
  209. }
  210. constexpr allocator_type get_allocator(void) const noexcept
  211. { return m_alloc; }
  212. constexpr reference at(size_type pos)
  213. {
  214. // TODO: exceptions
  215. // if (pos >= sz)
  216. // throw std::out_of_range("vector::at");
  217. return m_data[pos];
  218. }
  219. constexpr const_reference at(size_type pos) const
  220. {
  221. // TODO: exceptions
  222. // if (pos >= sz)
  223. // throw std::out_of_range("vector::at");
  224. return m_data[pos];
  225. }
  226. constexpr reference operator[](size_type pos) noexcept
  227. { return m_data[pos]; }
  228. constexpr const_reference operator[](size_type pos) const noexcept
  229. { return m_data[pos]; }
  230. constexpr reference front() noexcept
  231. { return m_data[0]; }
  232. constexpr const_reference front() const noexcept
  233. { return m_data[0]; }
  234. constexpr reference back() noexcept
  235. { return m_data[m_size - 1]; }
  236. constexpr const_reference back() const noexcept
  237. { return m_data[m_size - 1]; }
  238. constexpr T* data(void) noexcept
  239. { return m_data; }
  240. constexpr const T* data(void) const noexcept
  241. { return m_data; }
  242. // TODO: std::reverse_iterator
  243. constexpr iterator begin() noexcept
  244. { return iterator { m_data }; }
  245. constexpr const_iterator begin() const noexcept
  246. { return const_iterator { m_data }; }
  247. constexpr const_iterator cbegin() const noexcept
  248. { return const_iterator { m_data }; }
  249. constexpr iterator end() noexcept
  250. { return iterator { m_data + m_size }; }
  251. constexpr const_iterator end() const noexcept
  252. { return const_iterator { m_data + m_size }; }
  253. constexpr const_iterator cend() const noexcept
  254. { return const_iterator { m_data + m_size }; }
  255. [[nodiscard]] constexpr bool empty() const noexcept
  256. { return m_size == 0; }
  257. constexpr size_type size() const noexcept
  258. { return m_size; }
  259. constexpr size_type capacity() const noexcept
  260. { return m_capacity; }
  261. constexpr void reserve(size_type new_cap)
  262. {
  263. if (new_cap > m_capacity)
  264. _reallocate_safe(new_cap);
  265. }
  266. constexpr void resize(size_type n)
  267. {
  268. _pre_resize(n);
  269. while (n > m_size)
  270. emplace_back();
  271. }
  272. constexpr void resize(size_type n, const value_type& value)
  273. {
  274. _pre_resize(n);
  275. while (n > m_size)
  276. emplace_back(value);
  277. }
  278. constexpr void shrink_to_fit()
  279. {
  280. if (m_size < m_capacity)
  281. _reallocate_safe(m_size);
  282. }
  283. constexpr void clear() noexcept
  284. { resize(0); }
  285. template <typename... Args>
  286. constexpr iterator emplace(const_iterator pos, Args&&... args)
  287. {
  288. size_type idx = pos - m_data;
  289. if (!m_data)
  290. reserve(1);
  291. if (m_size == m_capacity)
  292. reserve(m_capacity * 2);
  293. for (size_type i = m_size; i > idx; --i)
  294. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i-1]));
  295. alloc_traits::construct(m_alloc, m_data + idx,
  296. std::forward<Args>(args)...);
  297. ++m_size;
  298. return iterator { m_data + idx };
  299. }
  300. constexpr iterator insert(const_iterator pos, T&& val)
  301. { return emplace(pos, std::move(val)); }
  302. constexpr iterator insert(const_iterator pos, const T& val)
  303. { return emplace(pos, val); }
  304. constexpr iterator insert(const_iterator pos, size_type n, const T& val)
  305. {
  306. if (!n)
  307. return pos;
  308. size_type idx = pos - m_data;
  309. if (!pos)
  310. reserve(n);
  311. if (m_size + n > m_capacity)
  312. reserve(m_size + n);
  313. for (size_type i = m_size + n - 1; i >= idx + n; --i)
  314. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i-n]));
  315. for (size_type i = idx; i < idx + n; ++i)
  316. alloc_traits::construct(m_alloc, m_data + i, val);
  317. m_size += n;
  318. return iterator { m_data + idx };
  319. }
  320. // TODO: LegacyInputIterator version of this
  321. template <typename ForwardIter>
  322. constexpr iterator insert(const_iterator pos,
  323. ForwardIter first, ForwardIter last)
  324. {
  325. size_type idx = pos - m_data;
  326. size_type n = 0;
  327. ForwardIter tmp = first;
  328. while (tmp != last)
  329. ++n, ++tmp;
  330. if (!n)
  331. return pos;
  332. if (!pos)
  333. reserve(n);
  334. if (m_size + n > m_capacity)
  335. reserve(m_size + n);
  336. for (size_type i = m_size + n - 1; i >= idx + n; --i)
  337. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i-n]));
  338. for (size_type i = idx; i < idx + n; ++i)
  339. alloc_traits::construct(m_alloc, m_data + i, *first++);
  340. m_size += n;
  341. return iterator { m_data + idx };
  342. }
  343. constexpr iterator insert(const_iterator pos, std::initializer_list<T> init)
  344. { return insert(pos, init.begin(), init.end()); }
  345. constexpr iterator erase(const_iterator pos)
  346. {
  347. size_type idx = pos - m_data;
  348. alloc_traits::destroy(m_alloc, m_data + idx);
  349. for (size_type i = idx; i < m_size - 1; ++i)
  350. alloc_traits::construct(m_alloc, m_data + i, std::move(m_data[i+1]));
  351. --m_size;
  352. return iterator { m_data + idx };
  353. }
  354. constexpr iterator erase(const_iterator first, const_iterator last)
  355. {
  356. size_type n = last - first;
  357. if (!n)
  358. return last;
  359. size_type idx = first - m_data;
  360. for (size_type i = idx; i < idx + n; ++i)
  361. alloc_traits::destroy(m_alloc, m_data + i);
  362. for (size_type i = idx; i < m_size - n; ++i)
  363. m_alloc.construct(m_data + i, std::move(m_data[i+n]));
  364. m_size -= n;
  365. return iterator { m_data + idx };
  366. }
  367. constexpr void push_back(const T& val) { insert(cend(), val); }
  368. constexpr void push_back(T&& val) { insert(cend(), std::move(val)); }
  369. template <typename... Args>
  370. constexpr void emplace_back(Args&&... args)
  371. { emplace(cend(), std::forward<Args>(args)...); }
  372. constexpr void pop_back() { erase(--cend()); }
  373. constexpr void swap(vector& other) noexcept(
  374. alloc_traits::propagate_on_container_swap::value
  375. || alloc_traits::is_always_equal::value)
  376. {
  377. if (alloc_traits::propagate_on_container_swap::value)
  378. std::swap(m_alloc, other.m_alloc);
  379. std::swap(m_data, other.m_data);
  380. std::swap(m_size, other.m_size);
  381. std::swap(m_capacity, other.m_capacity);
  382. }
  383. };
  384. template <typename T, typename Allocator>
  385. constexpr void swap(
  386. std::vector<T, Allocator>& lhs,
  387. std::vector<T, Allocator>& rhs) noexcept(noexcept(lhs.swap(rhs)))
  388. { lhs.swap(rhs); }
  389. template <typename T, typename Allocator, typename U>
  390. constexpr typename std::vector<T, Allocator>::size_type
  391. erase(std::vector<T, Allocator>& vec, const U& value)
  392. {
  393. typename std::vector<T, Allocator>::size_type n = 0;
  394. for (auto iter = vec.begin(); iter != vec.end(); ) {
  395. if (*iter == value) {
  396. iter = vec.erase(iter);
  397. ++n;
  398. } else {
  399. ++iter;
  400. }
  401. }
  402. return n;
  403. }
  404. template <typename T, typename Allocator, typename Pred>
  405. constexpr typename std::vector<T, Allocator>::size_type
  406. erase_if(std::vector<T, Allocator>& vec, Pred pred)
  407. {
  408. typename std::vector<T, Allocator>::size_type n = 0;
  409. for (auto iter = vec.begin(); iter != vec.end(); ) {
  410. if (pred(*iter)) {
  411. iter = vec.erase(iter);
  412. ++n;
  413. } else {
  414. ++iter;
  415. }
  416. }
  417. return n;
  418. }
  419. } // namespace std
  420. #endif