ilist.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //==-- llvm/ADT/ilist.h - Intrusive Linked List Template ---------*- C++ -*-==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines classes to implement an intrusive doubly linked list class
  10. // (i.e. each node of the list must contain a next and previous field for the
  11. // list.
  12. //
  13. // The ilist class itself should be a plug in replacement for list. This list
  14. // replacement does not provide a constant time size() method, so be careful to
  15. // use empty() when you really want to know if it's empty.
  16. //
  17. // The ilist class is implemented as a circular list. The list itself contains
  18. // a sentinel node, whose Next points at begin() and whose Prev points at
  19. // rbegin(). The sentinel node itself serves as end() and rend().
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_ADT_ILIST_H
  23. #define LLVM_ADT_ILIST_H
  24. #include "llvm/ADT/simple_ilist.h"
  25. #include <cassert>
  26. #include <cstddef>
  27. #include <iterator>
  28. namespace llvm {
  29. /// Use delete by default for iplist and ilist.
  30. ///
  31. /// Specialize this to get different behaviour for ownership-related API. (If
  32. /// you really want ownership semantics, consider using std::list or building
  33. /// something like \a BumpPtrList.)
  34. ///
  35. /// \see ilist_noalloc_traits
  36. template <typename NodeTy> struct ilist_alloc_traits {
  37. static void deleteNode(NodeTy *V) { delete V; }
  38. };
  39. /// Custom traits to do nothing on deletion.
  40. ///
  41. /// Specialize ilist_alloc_traits to inherit from this to disable the
  42. /// non-intrusive deletion in iplist (which implies ownership).
  43. ///
  44. /// If you want purely intrusive semantics with no callbacks, consider using \a
  45. /// simple_ilist instead.
  46. ///
  47. /// \code
  48. /// template <>
  49. /// struct ilist_alloc_traits<MyType> : ilist_noalloc_traits<MyType> {};
  50. /// \endcode
  51. template <typename NodeTy> struct ilist_noalloc_traits {
  52. static void deleteNode(NodeTy *V) {}
  53. };
  54. /// Callbacks do nothing by default in iplist and ilist.
  55. ///
  56. /// Specialize this for to use callbacks for when nodes change their list
  57. /// membership.
  58. template <typename NodeTy> struct ilist_callback_traits {
  59. void addNodeToList(NodeTy *) {}
  60. void removeNodeFromList(NodeTy *) {}
  61. /// Callback before transferring nodes to this list. The nodes may already be
  62. /// in this same list.
  63. template <class Iterator>
  64. void transferNodesFromList(ilist_callback_traits &OldList, Iterator /*first*/,
  65. Iterator /*last*/) {
  66. (void)OldList;
  67. }
  68. };
  69. /// A fragment for template traits for intrusive list that provides default
  70. /// node related operations.
  71. ///
  72. /// TODO: Remove this layer of indirection. It's not necessary.
  73. template <typename NodeTy>
  74. struct ilist_node_traits : ilist_alloc_traits<NodeTy>,
  75. ilist_callback_traits<NodeTy> {};
  76. /// Template traits for intrusive list.
  77. ///
  78. /// Customize callbacks and allocation semantics.
  79. template <typename NodeTy>
  80. struct ilist_traits : public ilist_node_traits<NodeTy> {};
  81. /// Const traits should never be instantiated.
  82. template <typename Ty> struct ilist_traits<const Ty> {};
  83. namespace ilist_detail {
  84. template <class T> T &make();
  85. /// Type trait to check for a traits class that has a getNext member (as a
  86. /// canary for any of the ilist_nextprev_traits API).
  87. template <class TraitsT, class NodeT> struct HasGetNext {
  88. typedef char Yes[1];
  89. typedef char No[2];
  90. template <size_t N> struct SFINAE {};
  91. template <class U>
  92. static Yes &test(U *I, decltype(I->getNext(&make<NodeT>())) * = 0);
  93. template <class> static No &test(...);
  94. public:
  95. static const bool value = sizeof(test<TraitsT>(nullptr)) == sizeof(Yes);
  96. };
  97. /// Type trait to check for a traits class that has a createSentinel member (as
  98. /// a canary for any of the ilist_sentinel_traits API).
  99. template <class TraitsT> struct HasCreateSentinel {
  100. typedef char Yes[1];
  101. typedef char No[2];
  102. template <class U>
  103. static Yes &test(U *I, decltype(I->createSentinel()) * = 0);
  104. template <class> static No &test(...);
  105. public:
  106. static const bool value = sizeof(test<TraitsT>(nullptr)) == sizeof(Yes);
  107. };
  108. /// Type trait to check for a traits class that has a createNode member.
  109. /// Allocation should be managed in a wrapper class, instead of in
  110. /// ilist_traits.
  111. template <class TraitsT, class NodeT> struct HasCreateNode {
  112. typedef char Yes[1];
  113. typedef char No[2];
  114. template <size_t N> struct SFINAE {};
  115. template <class U>
  116. static Yes &test(U *I, decltype(I->createNode(make<NodeT>())) * = 0);
  117. template <class> static No &test(...);
  118. public:
  119. static const bool value = sizeof(test<TraitsT>(nullptr)) == sizeof(Yes);
  120. };
  121. template <class TraitsT, class NodeT> struct HasObsoleteCustomization {
  122. static const bool value = HasGetNext<TraitsT, NodeT>::value ||
  123. HasCreateSentinel<TraitsT>::value ||
  124. HasCreateNode<TraitsT, NodeT>::value;
  125. };
  126. } // end namespace ilist_detail
  127. //===----------------------------------------------------------------------===//
  128. //
  129. /// A wrapper around an intrusive list with callbacks and non-intrusive
  130. /// ownership.
  131. ///
  132. /// This wraps a purely intrusive list (like simple_ilist) with a configurable
  133. /// traits class. The traits can implement callbacks and customize the
  134. /// ownership semantics.
  135. ///
  136. /// This is a subset of ilist functionality that can safely be used on nodes of
  137. /// polymorphic types, i.e. a heterogeneous list with a common base class that
  138. /// holds the next/prev pointers. The only state of the list itself is an
  139. /// ilist_sentinel, which holds pointers to the first and last nodes in the
  140. /// list.
  141. template <class IntrusiveListT, class TraitsT>
  142. class iplist_impl : public TraitsT, IntrusiveListT {
  143. typedef IntrusiveListT base_list_type;
  144. public:
  145. typedef typename base_list_type::pointer pointer;
  146. typedef typename base_list_type::const_pointer const_pointer;
  147. typedef typename base_list_type::reference reference;
  148. typedef typename base_list_type::const_reference const_reference;
  149. typedef typename base_list_type::value_type value_type;
  150. typedef typename base_list_type::size_type size_type;
  151. typedef typename base_list_type::difference_type difference_type;
  152. typedef typename base_list_type::iterator iterator;
  153. typedef typename base_list_type::const_iterator const_iterator;
  154. typedef typename base_list_type::reverse_iterator reverse_iterator;
  155. typedef
  156. typename base_list_type::const_reverse_iterator const_reverse_iterator;
  157. private:
  158. // TODO: Drop this assertion and the transitive type traits anytime after
  159. // v4.0 is branched (i.e,. keep them for one release to help out-of-tree code
  160. // update).
  161. static_assert(
  162. !ilist_detail::HasObsoleteCustomization<TraitsT, value_type>::value,
  163. "ilist customization points have changed!");
  164. static bool op_less(const_reference L, const_reference R) { return L < R; }
  165. static bool op_equal(const_reference L, const_reference R) { return L == R; }
  166. public:
  167. iplist_impl() = default;
  168. iplist_impl(const iplist_impl &) = delete;
  169. iplist_impl &operator=(const iplist_impl &) = delete;
  170. iplist_impl(iplist_impl &&X)
  171. : TraitsT(std::move(static_cast<TraitsT &>(X))),
  172. IntrusiveListT(std::move(static_cast<IntrusiveListT &>(X))) {}
  173. iplist_impl &operator=(iplist_impl &&X) {
  174. *static_cast<TraitsT *>(this) = std::move(static_cast<TraitsT &>(X));
  175. *static_cast<IntrusiveListT *>(this) =
  176. std::move(static_cast<IntrusiveListT &>(X));
  177. return *this;
  178. }
  179. ~iplist_impl() { clear(); }
  180. // Miscellaneous inspection routines.
  181. size_type max_size() const { return size_type(-1); }
  182. using base_list_type::begin;
  183. using base_list_type::end;
  184. using base_list_type::rbegin;
  185. using base_list_type::rend;
  186. using base_list_type::empty;
  187. using base_list_type::front;
  188. using base_list_type::back;
  189. void swap(iplist_impl &RHS) {
  190. assert(0 && "Swap does not use list traits callback correctly yet!");
  191. base_list_type::swap(RHS);
  192. }
  193. iterator insert(iterator where, pointer New) {
  194. this->addNodeToList(New); // Notify traits that we added a node...
  195. return base_list_type::insert(where, *New);
  196. }
  197. iterator insert(iterator where, const_reference New) {
  198. return this->insert(where, new value_type(New));
  199. }
  200. iterator insertAfter(iterator where, pointer New) {
  201. if (empty())
  202. return insert(begin(), New);
  203. else
  204. return insert(++where, New);
  205. }
  206. /// Clone another list.
  207. template <class Cloner> void cloneFrom(const iplist_impl &L2, Cloner clone) {
  208. clear();
  209. for (const_reference V : L2)
  210. push_back(clone(V));
  211. }
  212. pointer remove(iterator &IT) {
  213. pointer Node = &*IT++;
  214. this->removeNodeFromList(Node); // Notify traits that we removed a node...
  215. base_list_type::remove(*Node);
  216. return Node;
  217. }
  218. pointer remove(const iterator &IT) {
  219. iterator MutIt = IT;
  220. return remove(MutIt);
  221. }
  222. pointer remove(pointer IT) { return remove(iterator(IT)); }
  223. pointer remove(reference IT) { return remove(iterator(IT)); }
  224. // erase - remove a node from the controlled sequence... and delete it.
  225. iterator erase(iterator where) {
  226. this->deleteNode(remove(where));
  227. return where;
  228. }
  229. iterator erase(pointer IT) { return erase(iterator(IT)); }
  230. iterator erase(reference IT) { return erase(iterator(IT)); }
  231. /// Remove all nodes from the list like clear(), but do not call
  232. /// removeNodeFromList() or deleteNode().
  233. ///
  234. /// This should only be used immediately before freeing nodes in bulk to
  235. /// avoid traversing the list and bringing all the nodes into cache.
  236. void clearAndLeakNodesUnsafely() { base_list_type::clear(); }
  237. private:
  238. // transfer - The heart of the splice function. Move linked list nodes from
  239. // [first, last) into position.
  240. //
  241. void transfer(iterator position, iplist_impl &L2, iterator first, iterator last) {
  242. if (position == last)
  243. return;
  244. // Notify traits we moved the nodes...
  245. this->transferNodesFromList(L2, first, last);
  246. base_list_type::splice(position, L2, first, last);
  247. }
  248. public:
  249. //===----------------------------------------------------------------------===
  250. // Functionality derived from other functions defined above...
  251. //
  252. using base_list_type::size;
  253. iterator erase(iterator first, iterator last) {
  254. while (first != last)
  255. first = erase(first);
  256. return last;
  257. }
  258. void clear() { erase(begin(), end()); }
  259. // Front and back inserters...
  260. void push_front(pointer val) { insert(begin(), val); }
  261. void push_back(pointer val) { insert(end(), val); }
  262. void pop_front() {
  263. assert(!empty() && "pop_front() on empty list!");
  264. erase(begin());
  265. }
  266. void pop_back() {
  267. assert(!empty() && "pop_back() on empty list!");
  268. iterator t = end(); erase(--t);
  269. }
  270. // Special forms of insert...
  271. template<class InIt> void insert(iterator where, InIt first, InIt last) {
  272. for (; first != last; ++first) insert(where, *first);
  273. }
  274. // Splice members - defined in terms of transfer...
  275. void splice(iterator where, iplist_impl &L2) {
  276. if (!L2.empty())
  277. transfer(where, L2, L2.begin(), L2.end());
  278. }
  279. void splice(iterator where, iplist_impl &L2, iterator first) {
  280. iterator last = first; ++last;
  281. if (where == first || where == last) return; // No change
  282. transfer(where, L2, first, last);
  283. }
  284. void splice(iterator where, iplist_impl &L2, iterator first, iterator last) {
  285. if (first != last) transfer(where, L2, first, last);
  286. }
  287. void splice(iterator where, iplist_impl &L2, reference N) {
  288. splice(where, L2, iterator(N));
  289. }
  290. void splice(iterator where, iplist_impl &L2, pointer N) {
  291. splice(where, L2, iterator(N));
  292. }
  293. template <class Compare>
  294. void merge(iplist_impl &Right, Compare comp) {
  295. if (this == &Right)
  296. return;
  297. this->transferNodesFromList(Right, Right.begin(), Right.end());
  298. base_list_type::merge(Right, comp);
  299. }
  300. void merge(iplist_impl &Right) { return merge(Right, op_less); }
  301. using base_list_type::sort;
  302. /// Get the previous node, or \c nullptr for the list head.
  303. pointer getPrevNode(reference N) const {
  304. auto I = N.getIterator();
  305. if (I == begin())
  306. return nullptr;
  307. return &*std::prev(I);
  308. }
  309. /// Get the previous node, or \c nullptr for the list head.
  310. const_pointer getPrevNode(const_reference N) const {
  311. return getPrevNode(const_cast<reference >(N));
  312. }
  313. /// Get the next node, or \c nullptr for the list tail.
  314. pointer getNextNode(reference N) const {
  315. auto Next = std::next(N.getIterator());
  316. if (Next == end())
  317. return nullptr;
  318. return &*Next;
  319. }
  320. /// Get the next node, or \c nullptr for the list tail.
  321. const_pointer getNextNode(const_reference N) const {
  322. return getNextNode(const_cast<reference >(N));
  323. }
  324. };
  325. /// An intrusive list with ownership and callbacks specified/controlled by
  326. /// ilist_traits, only with API safe for polymorphic types.
  327. ///
  328. /// The \p Options parameters are the same as those for \a simple_ilist. See
  329. /// there for a description of what's available.
  330. template <class T, class... Options>
  331. class iplist
  332. : public iplist_impl<simple_ilist<T, Options...>, ilist_traits<T>> {
  333. using iplist_impl_type = typename iplist::iplist_impl;
  334. public:
  335. iplist() = default;
  336. iplist(const iplist &X) = delete;
  337. iplist &operator=(const iplist &X) = delete;
  338. iplist(iplist &&X) : iplist_impl_type(std::move(X)) {}
  339. iplist &operator=(iplist &&X) {
  340. *static_cast<iplist_impl_type *>(this) = std::move(X);
  341. return *this;
  342. }
  343. };
  344. template <class T, class... Options> using ilist = iplist<T, Options...>;
  345. } // end namespace llvm
  346. namespace std {
  347. // Ensure that swap uses the fast list swap...
  348. template<class Ty>
  349. void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) {
  350. Left.swap(Right);
  351. }
  352. } // end namespace std
  353. #endif // LLVM_ADT_ILIST_H