ilist.h 14 KB

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