list 14 KB

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