ilist.h 14 KB

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