simple_ilist.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/simple_ilist.h - Simple Intrusive List ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_SIMPLE_ILIST_H
  14. #define LLVM_ADT_SIMPLE_ILIST_H
  15. #include "llvm/ADT/ilist_base.h"
  16. #include "llvm/ADT/ilist_iterator.h"
  17. #include "llvm/ADT/ilist_node.h"
  18. #include "llvm/ADT/ilist_node_options.h"
  19. #include "llvm/Support/Compiler.h"
  20. #include <algorithm>
  21. #include <cassert>
  22. #include <cstddef>
  23. #include <functional>
  24. #include <iterator>
  25. #include <utility>
  26. namespace llvm {
  27. /// A simple intrusive list implementation.
  28. ///
  29. /// This is a simple intrusive list for a \c T that inherits from \c
  30. /// ilist_node<T>. The list never takes ownership of anything inserted in it.
  31. ///
  32. /// Unlike \a iplist<T> and \a ilist<T>, \a simple_ilist<T> never deletes
  33. /// values, and has no callback traits.
  34. ///
  35. /// The API for adding nodes include \a push_front(), \a push_back(), and \a
  36. /// insert(). These all take values by reference (not by pointer), except for
  37. /// the range version of \a insert().
  38. ///
  39. /// There are three sets of API for discarding nodes from the list: \a
  40. /// remove(), which takes a reference to the node to remove, \a erase(), which
  41. /// takes an iterator or iterator range and returns the next one, and \a
  42. /// clear(), which empties out the container. All three are constant time
  43. /// operations. None of these deletes any nodes; in particular, if there is a
  44. /// single node in the list, then these have identical semantics:
  45. /// \li \c L.remove(L.front());
  46. /// \li \c L.erase(L.begin());
  47. /// \li \c L.clear();
  48. ///
  49. /// As a convenience for callers, there are parallel APIs that take a \c
  50. /// Disposer (such as \c std::default_delete<T>): \a removeAndDispose(), \a
  51. /// eraseAndDispose(), and \a clearAndDispose(). These have different names
  52. /// because the extra semantic is otherwise non-obvious. They are equivalent
  53. /// to calling \a std::for_each() on the range to be discarded.
  54. ///
  55. /// The currently available \p Options customize the nodes in the list. The
  56. /// same options must be specified in the \a ilist_node instantiation for
  57. /// compatibility (although the order is irrelevant).
  58. /// \li Use \a ilist_tag to designate which ilist_node for a given \p T this
  59. /// list should use. This is useful if a type \p T is part of multiple,
  60. /// independent lists simultaneously.
  61. /// \li Use \a ilist_sentinel_tracking to always (or never) track whether a
  62. /// node is a sentinel. Specifying \c true enables the \a
  63. /// ilist_node::isSentinel() API. Unlike \a ilist_node::isKnownSentinel(),
  64. /// which is only appropriate for assertions, \a ilist_node::isSentinel() is
  65. /// appropriate for real logic.
  66. ///
  67. /// Here are examples of \p Options usage:
  68. /// \li \c simple_ilist<T> gives the defaults. \li \c
  69. /// simple_ilist<T,ilist_sentinel_tracking<true>> enables the \a
  70. /// ilist_node::isSentinel() API.
  71. /// \li \c simple_ilist<T,ilist_tag<A>,ilist_sentinel_tracking<false>>
  72. /// specifies a tag of A and that tracking should be off (even when
  73. /// LLVM_ENABLE_ABI_BREAKING_CHECKS are enabled).
  74. /// \li \c simple_ilist<T,ilist_sentinel_tracking<false>,ilist_tag<A>> is
  75. /// equivalent to the last.
  76. ///
  77. /// See \a is_valid_option for steps on adding a new option.
  78. template <typename T, class... Options>
  79. class simple_ilist
  80. : ilist_detail::compute_node_options<T, Options...>::type::list_base_type,
  81. ilist_detail::SpecificNodeAccess<
  82. typename ilist_detail::compute_node_options<T, Options...>::type> {
  83. static_assert(ilist_detail::check_options<Options...>::value,
  84. "Unrecognized node option!");
  85. using OptionsT =
  86. typename ilist_detail::compute_node_options<T, Options...>::type;
  87. using list_base_type = typename OptionsT::list_base_type;
  88. ilist_sentinel<OptionsT> Sentinel;
  89. public:
  90. using value_type = typename OptionsT::value_type;
  91. using pointer = typename OptionsT::pointer;
  92. using reference = typename OptionsT::reference;
  93. using const_pointer = typename OptionsT::const_pointer;
  94. using const_reference = typename OptionsT::const_reference;
  95. using iterator = ilist_iterator<OptionsT, false, false>;
  96. using const_iterator = ilist_iterator<OptionsT, false, true>;
  97. using reverse_iterator = ilist_iterator<OptionsT, true, false>;
  98. using const_reverse_iterator = ilist_iterator<OptionsT, true, true>;
  99. using size_type = size_t;
  100. using difference_type = ptrdiff_t;
  101. simple_ilist() = default;
  102. ~simple_ilist() = default;
  103. // No copy constructors.
  104. simple_ilist(const simple_ilist &) = delete;
  105. simple_ilist &operator=(const simple_ilist &) = delete;
  106. // Move constructors.
  107. simple_ilist(simple_ilist &&X) { splice(end(), X); }
  108. simple_ilist &operator=(simple_ilist &&X) {
  109. clear();
  110. splice(end(), X);
  111. return *this;
  112. }
  113. iterator begin() { return ++iterator(Sentinel); }
  114. const_iterator begin() const { return ++const_iterator(Sentinel); }
  115. iterator end() { return iterator(Sentinel); }
  116. const_iterator end() const { return const_iterator(Sentinel); }
  117. reverse_iterator rbegin() { return ++reverse_iterator(Sentinel); }
  118. const_reverse_iterator rbegin() const {
  119. return ++const_reverse_iterator(Sentinel);
  120. }
  121. reverse_iterator rend() { return reverse_iterator(Sentinel); }
  122. const_reverse_iterator rend() const {
  123. return const_reverse_iterator(Sentinel);
  124. }
  125. /// Check if the list is empty in constant time.
  126. [[nodiscard]] bool empty() const { return Sentinel.empty(); }
  127. /// Calculate the size of the list in linear time.
  128. [[nodiscard]] size_type size() const { return std::distance(begin(), end()); }
  129. reference front() { return *begin(); }
  130. const_reference front() const { return *begin(); }
  131. reference back() { return *rbegin(); }
  132. const_reference back() const { return *rbegin(); }
  133. /// Insert a node at the front; never copies.
  134. void push_front(reference Node) { insert(begin(), Node); }
  135. /// Insert a node at the back; never copies.
  136. void push_back(reference Node) { insert(end(), Node); }
  137. /// Remove the node at the front; never deletes.
  138. void pop_front() { erase(begin()); }
  139. /// Remove the node at the back; never deletes.
  140. void pop_back() { erase(--end()); }
  141. /// Swap with another list in place using std::swap.
  142. void swap(simple_ilist &X) { std::swap(*this, X); }
  143. /// Insert a node by reference; never copies.
  144. iterator insert(iterator I, reference Node) {
  145. list_base_type::insertBefore(*I.getNodePtr(), *this->getNodePtr(&Node));
  146. return iterator(&Node);
  147. }
  148. /// Insert a range of nodes; never copies.
  149. template <class Iterator>
  150. void insert(iterator I, Iterator First, Iterator Last) {
  151. for (; First != Last; ++First)
  152. insert(I, *First);
  153. }
  154. /// Clone another list.
  155. template <class Cloner, class Disposer>
  156. void cloneFrom(const simple_ilist &L2, Cloner clone, Disposer dispose) {
  157. clearAndDispose(dispose);
  158. for (const_reference V : L2)
  159. push_back(*clone(V));
  160. }
  161. /// Remove a node by reference; never deletes.
  162. ///
  163. /// \see \a erase() for removing by iterator.
  164. /// \see \a removeAndDispose() if the node should be deleted.
  165. void remove(reference N) { list_base_type::remove(*this->getNodePtr(&N)); }
  166. /// Remove a node by reference and dispose of it.
  167. template <class Disposer>
  168. void removeAndDispose(reference N, Disposer dispose) {
  169. remove(N);
  170. dispose(&N);
  171. }
  172. /// Remove a node by iterator; never deletes.
  173. ///
  174. /// \see \a remove() for removing by reference.
  175. /// \see \a eraseAndDispose() it the node should be deleted.
  176. iterator erase(iterator I) {
  177. assert(I != end() && "Cannot remove end of list!");
  178. remove(*I++);
  179. return I;
  180. }
  181. /// Remove a range of nodes; never deletes.
  182. ///
  183. /// \see \a eraseAndDispose() if the nodes should be deleted.
  184. iterator erase(iterator First, iterator Last) {
  185. list_base_type::removeRange(*First.getNodePtr(), *Last.getNodePtr());
  186. return Last;
  187. }
  188. /// Remove a node by iterator and dispose of it.
  189. template <class Disposer>
  190. iterator eraseAndDispose(iterator I, Disposer dispose) {
  191. auto Next = std::next(I);
  192. erase(I);
  193. dispose(&*I);
  194. return Next;
  195. }
  196. /// Remove a range of nodes and dispose of them.
  197. template <class Disposer>
  198. iterator eraseAndDispose(iterator First, iterator Last, Disposer dispose) {
  199. while (First != Last)
  200. First = eraseAndDispose(First, dispose);
  201. return Last;
  202. }
  203. /// Clear the list; never deletes.
  204. ///
  205. /// \see \a clearAndDispose() if the nodes should be deleted.
  206. void clear() { Sentinel.reset(); }
  207. /// Clear the list and dispose of the nodes.
  208. template <class Disposer> void clearAndDispose(Disposer dispose) {
  209. eraseAndDispose(begin(), end(), dispose);
  210. }
  211. /// Splice in another list.
  212. void splice(iterator I, simple_ilist &L2) {
  213. splice(I, L2, L2.begin(), L2.end());
  214. }
  215. /// Splice in a node from another list.
  216. void splice(iterator I, simple_ilist &L2, iterator Node) {
  217. splice(I, L2, Node, std::next(Node));
  218. }
  219. /// Splice in a range of nodes from another list.
  220. void splice(iterator I, simple_ilist &, iterator First, iterator Last) {
  221. list_base_type::transferBefore(*I.getNodePtr(), *First.getNodePtr(),
  222. *Last.getNodePtr());
  223. }
  224. /// Merge in another list.
  225. ///
  226. /// \pre \c this and \p RHS are sorted.
  227. ///@{
  228. void merge(simple_ilist &RHS) { merge(RHS, std::less<T>()); }
  229. template <class Compare> void merge(simple_ilist &RHS, Compare comp);
  230. ///@}
  231. /// Sort the list.
  232. ///@{
  233. void sort() { sort(std::less<T>()); }
  234. template <class Compare> void sort(Compare comp);
  235. ///@}
  236. };
  237. template <class T, class... Options>
  238. template <class Compare>
  239. void simple_ilist<T, Options...>::merge(simple_ilist &RHS, Compare comp) {
  240. if (this == &RHS || RHS.empty())
  241. return;
  242. iterator LI = begin(), LE = end();
  243. iterator RI = RHS.begin(), RE = RHS.end();
  244. while (LI != LE) {
  245. if (comp(*RI, *LI)) {
  246. // Transfer a run of at least size 1 from RHS to LHS.
  247. iterator RunStart = RI++;
  248. RI = std::find_if(RI, RE, [&](reference RV) { return !comp(RV, *LI); });
  249. splice(LI, RHS, RunStart, RI);
  250. if (RI == RE)
  251. return;
  252. }
  253. ++LI;
  254. }
  255. // Transfer the remaining RHS nodes once LHS is finished.
  256. splice(LE, RHS, RI, RE);
  257. }
  258. template <class T, class... Options>
  259. template <class Compare>
  260. void simple_ilist<T, Options...>::sort(Compare comp) {
  261. // Vacuously sorted.
  262. if (empty() || std::next(begin()) == end())
  263. return;
  264. // Split the list in the middle.
  265. iterator Center = begin(), End = begin();
  266. while (End != end() && ++End != end()) {
  267. ++Center;
  268. ++End;
  269. }
  270. simple_ilist RHS;
  271. RHS.splice(RHS.end(), *this, Center, end());
  272. // Sort the sublists and merge back together.
  273. sort(comp);
  274. RHS.sort(comp);
  275. merge(RHS, comp);
  276. }
  277. } // end namespace llvm
  278. #endif // LLVM_ADT_SIMPLE_ILIST_H
  279. #ifdef __GNUC__
  280. #pragma GCC diagnostic pop
  281. #endif