simple_ilist.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. LLVM_NODISCARD bool empty() const { return Sentinel.empty(); }
  127. /// Calculate the size of the list in linear time.
  128. LLVM_NODISCARD size_type size() const {
  129. return std::distance(begin(), end());
  130. }
  131. reference front() { return *begin(); }
  132. const_reference front() const { return *begin(); }
  133. reference back() { return *rbegin(); }
  134. const_reference back() const { return *rbegin(); }
  135. /// Insert a node at the front; never copies.
  136. void push_front(reference Node) { insert(begin(), Node); }
  137. /// Insert a node at the back; never copies.
  138. void push_back(reference Node) { insert(end(), Node); }
  139. /// Remove the node at the front; never deletes.
  140. void pop_front() { erase(begin()); }
  141. /// Remove the node at the back; never deletes.
  142. void pop_back() { erase(--end()); }
  143. /// Swap with another list in place using std::swap.
  144. void swap(simple_ilist &X) { std::swap(*this, X); }
  145. /// Insert a node by reference; never copies.
  146. iterator insert(iterator I, reference Node) {
  147. list_base_type::insertBefore(*I.getNodePtr(), *this->getNodePtr(&Node));
  148. return iterator(&Node);
  149. }
  150. /// Insert a range of nodes; never copies.
  151. template <class Iterator>
  152. void insert(iterator I, Iterator First, Iterator Last) {
  153. for (; First != Last; ++First)
  154. insert(I, *First);
  155. }
  156. /// Clone another list.
  157. template <class Cloner, class Disposer>
  158. void cloneFrom(const simple_ilist &L2, Cloner clone, Disposer dispose) {
  159. clearAndDispose(dispose);
  160. for (const_reference V : L2)
  161. push_back(*clone(V));
  162. }
  163. /// Remove a node by reference; never deletes.
  164. ///
  165. /// \see \a erase() for removing by iterator.
  166. /// \see \a removeAndDispose() if the node should be deleted.
  167. void remove(reference N) { list_base_type::remove(*this->getNodePtr(&N)); }
  168. /// Remove a node by reference and dispose of it.
  169. template <class Disposer>
  170. void removeAndDispose(reference N, Disposer dispose) {
  171. remove(N);
  172. dispose(&N);
  173. }
  174. /// Remove a node by iterator; never deletes.
  175. ///
  176. /// \see \a remove() for removing by reference.
  177. /// \see \a eraseAndDispose() it the node should be deleted.
  178. iterator erase(iterator I) {
  179. assert(I != end() && "Cannot remove end of list!");
  180. remove(*I++);
  181. return I;
  182. }
  183. /// Remove a range of nodes; never deletes.
  184. ///
  185. /// \see \a eraseAndDispose() if the nodes should be deleted.
  186. iterator erase(iterator First, iterator Last) {
  187. list_base_type::removeRange(*First.getNodePtr(), *Last.getNodePtr());
  188. return Last;
  189. }
  190. /// Remove a node by iterator and dispose of it.
  191. template <class Disposer>
  192. iterator eraseAndDispose(iterator I, Disposer dispose) {
  193. auto Next = std::next(I);
  194. erase(I);
  195. dispose(&*I);
  196. return Next;
  197. }
  198. /// Remove a range of nodes and dispose of them.
  199. template <class Disposer>
  200. iterator eraseAndDispose(iterator First, iterator Last, Disposer dispose) {
  201. while (First != Last)
  202. First = eraseAndDispose(First, dispose);
  203. return Last;
  204. }
  205. /// Clear the list; never deletes.
  206. ///
  207. /// \see \a clearAndDispose() if the nodes should be deleted.
  208. void clear() { Sentinel.reset(); }
  209. /// Clear the list and dispose of the nodes.
  210. template <class Disposer> void clearAndDispose(Disposer dispose) {
  211. eraseAndDispose(begin(), end(), dispose);
  212. }
  213. /// Splice in another list.
  214. void splice(iterator I, simple_ilist &L2) {
  215. splice(I, L2, L2.begin(), L2.end());
  216. }
  217. /// Splice in a node from another list.
  218. void splice(iterator I, simple_ilist &L2, iterator Node) {
  219. splice(I, L2, Node, std::next(Node));
  220. }
  221. /// Splice in a range of nodes from another list.
  222. void splice(iterator I, simple_ilist &, iterator First, iterator Last) {
  223. list_base_type::transferBefore(*I.getNodePtr(), *First.getNodePtr(),
  224. *Last.getNodePtr());
  225. }
  226. /// Merge in another list.
  227. ///
  228. /// \pre \c this and \p RHS are sorted.
  229. ///@{
  230. void merge(simple_ilist &RHS) { merge(RHS, std::less<T>()); }
  231. template <class Compare> void merge(simple_ilist &RHS, Compare comp);
  232. ///@}
  233. /// Sort the list.
  234. ///@{
  235. void sort() { sort(std::less<T>()); }
  236. template <class Compare> void sort(Compare comp);
  237. ///@}
  238. };
  239. template <class T, class... Options>
  240. template <class Compare>
  241. void simple_ilist<T, Options...>::merge(simple_ilist &RHS, Compare comp) {
  242. if (this == &RHS || RHS.empty())
  243. return;
  244. iterator LI = begin(), LE = end();
  245. iterator RI = RHS.begin(), RE = RHS.end();
  246. while (LI != LE) {
  247. if (comp(*RI, *LI)) {
  248. // Transfer a run of at least size 1 from RHS to LHS.
  249. iterator RunStart = RI++;
  250. RI = std::find_if(RI, RE, [&](reference RV) { return !comp(RV, *LI); });
  251. splice(LI, RHS, RunStart, RI);
  252. if (RI == RE)
  253. return;
  254. }
  255. ++LI;
  256. }
  257. // Transfer the remaining RHS nodes once LHS is finished.
  258. splice(LE, RHS, RI, RE);
  259. }
  260. template <class T, class... Options>
  261. template <class Compare>
  262. void simple_ilist<T, Options...>::sort(Compare comp) {
  263. // Vacuously sorted.
  264. if (empty() || std::next(begin()) == end())
  265. return;
  266. // Split the list in the middle.
  267. iterator Center = begin(), End = begin();
  268. while (End != end() && ++End != end()) {
  269. ++Center;
  270. ++End;
  271. }
  272. simple_ilist RHS;
  273. RHS.splice(RHS.end(), *this, Center, end());
  274. // Sort the sublists and merge back together.
  275. sort(comp);
  276. RHS.sort(comp);
  277. merge(RHS, comp);
  278. }
  279. } // end namespace llvm
  280. #endif // LLVM_ADT_SIMPLE_ILIST_H
  281. #ifdef __GNUC__
  282. #pragma GCC diagnostic pop
  283. #endif