1 //===-- <Support/ilist> - Intrusive Linked List Template ---------*- C++ -*--=//
3 // This file defines classes to implement an intrusive doubly linked list class
4 // (ie each node of the list must contain a next and previous field for the
7 // The ilist_traits trait class is used to gain access to the next and previous
8 // fields of the node type that the list is instantiated with. If it is not
9 // specialized, the list defaults to using the getPrev(), getNext() method calls
10 // to get the next and previous pointers.
12 // The ilist class itself, should be a plug in replacement for list, assuming
13 // that the nodes contain next/prev pointers. This list replacement does not
14 // provides a constant time size() method, so be careful to use empty() when you
15 // really want to know if it's empty.
17 // The ilist class is implemented by allocating a 'tail' node when the list is
18 // created (using ilist_traits<>::createEndMarker()). This tail node is
19 // absolutely required because the user must be able to compute end()-1. Because
20 // of this, users of the direct next/prev links will see an extra link on the
21 // end of the list, which should be ignored.
23 // Requirements for a user of this list:
25 // 1. The user must provide {g|s}et{Next|Prev} methods, or specialize
26 // ilist_traits to provide an alternate way of getting and setting next and
29 //===----------------------------------------------------------------------===//
31 #ifndef INCLUDED_SUPPORT_ILIST
32 #define INCLUDED_SUPPORT_ILIST
36 #include <Support/iterator>
38 template<typename NodeTy, typename Traits> class iplist;
39 template<typename NodeTy> class ilist_iterator;
41 // Template traits for intrusive list. By specializing this template class, you
42 // can change what next/prev fields are used to store the links...
43 template<typename NodeTy>
45 static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); }
46 static NodeTy *getNext(NodeTy *N) { return N->getNext(); }
47 static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); }
48 static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); }
50 static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
51 static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
53 static NodeTy *createNode() { return new NodeTy(); }
54 static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
57 void addNodeToList(NodeTy *NTy) {}
58 void removeNodeFromList(NodeTy *NTy) {}
59 void transferNodesFromList(iplist<NodeTy, ilist_traits> &L2,
60 ilist_iterator<NodeTy> first,
61 ilist_iterator<NodeTy> last) {}
64 // Const traits are the same as nonconst traits...
66 struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
69 //===----------------------------------------------------------------------===//
70 // ilist_iterator<Node> - Iterator for intrusive list.
72 template<typename NodeTy>
74 : public bidirectional_iterator<NodeTy, ptrdiff_t> {
75 typedef ilist_traits<NodeTy> Traits;
76 typedef bidirectional_iterator<NodeTy, ptrdiff_t> super;
78 typedef typename super::pointer pointer;
79 typedef typename super::reference reference;
82 typedef size_t size_type;
84 ilist_iterator(pointer NP) : NodePtr(NP) {}
85 ilist_iterator(reference NR) : NodePtr(&NR) {}
86 ilist_iterator() : NodePtr(0) {}
88 // This is templated so that we can allow constructing a const iterator from
89 // a nonconst iterator...
90 template<class node_ty>
91 ilist_iterator(const ilist_iterator<node_ty> &RHS)
92 : NodePtr(RHS.getNodePtrUnchecked()) {}
94 // This is templated so that we can allow assigning to a const iterator from
95 // a nonconst iterator...
96 template<class node_ty>
97 const ilist_iterator &operator=(const ilist_iterator<node_ty> &RHS) {
98 NodePtr = RHS.getNodePtrUnchecked();
103 operator pointer() const {
104 assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
108 reference operator*() const {
109 assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
112 pointer operator->() { return &operator*(); }
113 const pointer operator->() const { return &operator*(); }
115 // Comparison operators
116 bool operator==(const ilist_iterator &RHS) const {
117 return NodePtr == RHS.NodePtr;
119 bool operator!=(const ilist_iterator &RHS) const {
120 return NodePtr != RHS.NodePtr;
123 // Increment and decrement operators...
124 ilist_iterator &operator--() { // predecrement - Back up
125 NodePtr = Traits::getPrev(NodePtr);
126 assert(NodePtr && "--'d off the beginning of an ilist!");
129 ilist_iterator &operator++() { // preincrement - Advance
130 NodePtr = Traits::getNext(NodePtr);
131 assert(NodePtr && "++'d off the end of an ilist!");
134 ilist_iterator operator--(int) { // postdecrement operators...
135 ilist_iterator tmp = *this;
139 ilist_iterator operator++(int) { // postincrement operators...
140 ilist_iterator tmp = *this;
146 // Dummy operators to make errors apparent...
147 template<class X> void operator+(X Val) {}
148 template<class X> void operator-(X Val) {}
150 // Internal interface, do not use...
151 pointer getNodePtrUnchecked() const { return NodePtr; }
155 //===----------------------------------------------------------------------===//
157 // iplist - The subset of list functionality that can safely be used on nodes of
158 // polymorphic types, ie a heterogeneus list with a common base class that holds
159 // the next/prev pointers...
161 template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
162 class iplist : public Traits {
165 static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
166 static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
168 typedef NodeTy *pointer;
169 typedef const NodeTy *const_pointer;
170 typedef NodeTy &reference;
171 typedef const NodeTy &const_reference;
172 typedef NodeTy value_type;
173 typedef ilist_iterator<NodeTy> iterator;
174 typedef ilist_iterator<const NodeTy> const_iterator;
175 typedef size_t size_type;
176 typedef ptrdiff_t difference_type;
177 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
178 typedef std::reverse_iterator<iterator> reverse_iterator;
180 iplist() : Head(createNode()), Tail(Head) {
184 ~iplist() { clear(); delete Tail; }
186 // Iterator creation methods...
187 iterator begin() { return iterator(Head); }
188 const_iterator begin() const { return const_iterator(Head); }
189 iterator end() { return iterator(Tail); }
190 const_iterator end() const { return const_iterator(Tail); }
192 // reverse iterator creation methods...
193 reverse_iterator rbegin() { return reverse_iterator(end()); }
194 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
195 reverse_iterator rend() { return reverse_iterator(begin()); }
196 const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
198 // Miscellaneous inspection routines...
199 size_type max_size() const { return size_type(-1); }
200 bool empty() const { return Head == Tail; }
202 // Front and back accessor functions...
204 assert(!empty() && "Called front() on empty list!");
207 const_reference front() const {
208 assert(!empty() && "Called front() on empty list!");
212 assert(!empty() && "Called back() on empty list!");
213 return *getPrev(Tail);
215 const_reference back() const {
216 assert(!empty() && "Called back() on empty list!");
217 return *getPrev(Tail);
220 void swap(iplist &RHS) {
221 abort(); // Swap does not use list traits callback correctly yet!
222 std::swap(Head, RHS.Head);
223 std::swap(Tail, RHS.Tail);
226 iterator insert(iterator where, NodeTy *New) {
227 NodeTy *CurNode = where.getNodePtrUnchecked(), *PrevNode = getPrev(CurNode);
228 setNext(New, CurNode);
229 setPrev(New, PrevNode);
232 setNext(PrevNode, New);
235 setPrev(CurNode, New);
237 addNodeToList(New); // Notify traits that we added a node...
241 NodeTy *remove(iterator &IT) {
242 assert(IT != end() && "Cannot remove end of list!");
244 NodeTy *NextNode = getNext(Node);
245 NodeTy *PrevNode = getPrev(Node);
248 setNext(PrevNode, NextNode);
251 setPrev(NextNode, PrevNode);
253 removeNodeFromList(Node); // Notify traits that we added a node...
257 NodeTy *remove(const iterator &IT) {
259 return remove(MutIt);
262 // erase - remove a node from the controlled sequence... and delete it.
263 iterator erase(iterator where) {
264 delete remove(where);
270 // transfer - The heart of the splice function. Move linked list nodes from
271 // [first, last) into position.
273 void transfer(iterator position, iplist &L2, iterator first, iterator last) {
274 assert(first != last && "Should be checked by callers");
275 if (position != last) {
276 // Remove [first, last) from its old position.
277 NodeTy *First = &*first, *Prev = getPrev(First);
278 NodeTy *Next = last.getNodePtrUnchecked(), *Last = getPrev(Next);
285 // Splice [first, last) into its new position.
286 NodeTy *PosNext = position.getNodePtrUnchecked();
287 NodeTy *PosPrev = getPrev(PosNext);
289 // Fix head of list...
291 setNext(PosPrev, First);
294 setPrev(First, PosPrev);
296 // Fix end of list...
297 setNext(Last, PosNext);
298 setPrev(PosNext, Last);
300 transferNodesFromList(L2, First, PosNext);
306 //===----------------------------------------------------------------------===
307 // Functionality derived from other functions defined above...
310 size_type size() const {
312 size_type Result = std::distance(begin(), end());
314 size_type Result = 0;
315 std::distance(begin(), end(), Result);
320 iterator erase(iterator first, iterator last) {
321 while (first != last)
322 first = erase(first);
326 void clear() { erase(begin(), end()); }
328 // Front and back inserters...
329 void push_front(NodeTy *val) { insert(begin(), val); }
330 void push_back(NodeTy *val) { insert(end(), val); }
332 assert(!empty() && "pop_front() on empty list!");
336 assert(!empty() && "pop_back() on empty list!");
337 iterator t = end(); erase(--t);
340 // Special forms of insert...
341 template<class InIt> void insert(iterator where, InIt first, InIt last) {
342 for (; first != last; ++first) insert(where, *first);
345 // Splice members - defined in terms of transfer...
346 void splice(iterator where, iplist &L2) {
348 transfer(where, L2, L2.begin(), L2.end());
350 void splice(iterator where, iplist &L2, iterator first) {
351 iterator last = first; ++last;
352 if (where == first || where == last) return; // No change
353 transfer(where, L2, first, last);
355 void splice(iterator where, iplist &L2, iterator first, iterator last) {
356 if (first != last) transfer(where, L2, first, last);
361 //===----------------------------------------------------------------------===
362 // High-Level Functionality that shouldn't really be here, but is part of list
365 // These two functions are actually called remove/remove_if in list<>, but
366 // they actually do the job of erase, rename them accordingly.
368 void erase(const NodeTy &val) {
369 for (iterator I = begin(), E = end(); I != E; ) {
370 iterator next = I; ++next;
371 if (*I == val) erase(I);
375 template<class Pr1> void erase_if(Pr1 pred) {
376 for (iterator I = begin(), E = end(); I != E; ) {
377 iterator next = I; ++next;
378 if (pred(*I)) erase(I);
383 template<class Pr2> void unique(Pr2 pred) {
385 for (iterator I = begin(), E = end(), Next = begin(); ++Next != E;) {
393 void unique() { unique(op_equal); }
395 template<class Pr3> void merge(iplist &right, Pr3 pred) {
396 iterator first1 = begin(), last1 = end();
397 iterator first2 = right.begin(), last2 = right.end();
398 while (first1 != last1 && first2 != last2)
399 if (pred(*first2, *first1)) {
400 iterator next = first2;
401 transfer(first1, right, first2, ++next);
406 if (first2 != last2) transfer(last1, right, first2, last2);
408 void merge(iplist &right) { return merge(right, op_less); }
410 template<class Pr3> void sort(Pr3 pred);
411 void sort() { sort(op_less); }
416 template<typename NodeTy>
417 struct ilist : public iplist<NodeTy> {
418 typedef typename iplist<NodeTy>::size_type size_type;
419 typedef typename iplist<NodeTy>::iterator iterator;
422 ilist(const ilist &right) {
423 insert(begin(), right.begin(), right.end());
425 explicit ilist(size_type count) {
426 insert(begin(), count, NodeTy());
428 ilist(size_type count, const NodeTy &val) {
429 insert(begin(), count, val);
431 template<class InIt> ilist(InIt first, InIt last) {
432 insert(begin(), first, last);
436 // Forwarding functions: A workaround for GCC 2.95 which does not correctly
437 // support 'using' declarations to bring a hidden member into scope.
439 iterator insert(iterator a, NodeTy *b){ return iplist<NodeTy>::insert(a, b); }
440 void push_front(NodeTy *a) { iplist<NodeTy>::push_front(a); }
441 void push_back(NodeTy *a) { iplist<NodeTy>::push_back(a); }
444 // Main implementation here - Insert for a node passed by value...
445 iterator insert(iterator where, const NodeTy &val) {
446 return insert(where, createNode(val));
450 // Front and back inserters...
451 void push_front(const NodeTy &val) { insert(begin(), val); }
452 void push_back(const NodeTy &val) { insert(end(), val); }
454 // Special forms of insert...
455 template<class InIt> void insert(iterator where, InIt first, InIt last) {
456 for (; first != last; ++first) insert(where, *first);
458 void insert(iterator where, size_type count, const NodeTy &val) {
459 for (; count != 0; --count) insert(where, val);
462 // Assign special forms...
463 void assign(size_type count, const NodeTy &val) {
464 iterator I = begin();
465 for (; I != end() && count != 0; ++I, --count)
468 insert(end(), n, val);
472 template<class InIt> void assign(InIt first, InIt last) {
473 iterator first1 = begin(), last1 = end();
474 for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
477 erase(first1, last1);
479 insert(last1, first2, last2);
484 void resize(size_type newsize, NodeTy val) {
485 iterator i = begin();
487 for ( ; i != end() && len < newsize; ++i, ++len) /* empty*/ ;
492 insert(end(), newsize - len, val);
494 void resize(size_type newsize) { resize(newsize, NodeTy()); }
498 // Ensure that swap uses the fast list swap...
500 void swap(iplist<Ty> &Left, iplist<Ty> &Right) {
503 } // End 'std' extensions...