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