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 I'm 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
37 template<typename NodeTy, typename Traits> class iplist;
38 template<typename NodeTy> class ilist_iterator;
40 // Template traits for intrusive list. By specializing this template class, you
41 // can change what next/prev fields are used to store the links...
42 template<typename NodeTy>
44 static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); }
45 static NodeTy *getNext(NodeTy *N) { return N->getNext(); }
46 static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); }
47 static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); }
49 static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
50 static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
52 static NodeTy *createNode() { return new NodeTy(); }
53 static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
56 void addNodeToList(NodeTy *NTy) {}
57 void removeNodeFromList(NodeTy *NTy) {}
58 void transferNodesFromList(iplist<NodeTy, ilist_traits> &L2,
59 ilist_iterator<NodeTy> first,
60 ilist_iterator<NodeTy> last) {}
63 // Const traits are the same as nonconst traits...
65 struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
68 //===----------------------------------------------------------------------===//
69 // ilist_iterator<Node> - Iterator for intrusive list.
71 template<typename NodeTy>
72 class ilist_iterator : public std::bidirectional_iterator<NodeTy, ptrdiff_t> {
73 typedef ilist_traits<NodeTy> Traits;
76 typedef size_t size_type;
78 ilist_iterator(pointer NP) : NodePtr(NP) {}
79 ilist_iterator() : NodePtr(0) {}
81 // This is templated so that we can allow constructing a const iterator from
82 // a nonconst iterator...
83 template<class node_ty>
84 ilist_iterator(const ilist_iterator<node_ty> &RHS)
85 : NodePtr(RHS.getNodePtrUnchecked()) {}
87 // This is templated so that we can allow assigning to a const iterator from
88 // a nonconst iterator...
89 template<class node_ty>
90 const ilist_iterator &operator=(const ilist_iterator<node_ty> &RHS) {
91 NodePtr = RHS.getNodePtrUnchecked();
96 operator pointer() const {
97 assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
101 reference operator*() const {
102 assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
105 pointer operator->() { return &operator*(); }
106 const pointer operator->() const { return &operator*(); }
108 // Comparison operators
109 bool operator==(const ilist_iterator &RHS) const {
110 return NodePtr == RHS.NodePtr;
112 bool operator!=(const ilist_iterator &RHS) const {
113 return NodePtr != RHS.NodePtr;
116 // Increment and decrement operators...
117 ilist_iterator &operator--() { // predecrement - Back up
118 NodePtr = Traits::getPrev(NodePtr);
119 assert(NodePtr && "--'d off the beginning of an ilist!");
122 ilist_iterator &operator++() { // preincrement - Advance
123 NodePtr = Traits::getNext(NodePtr);
124 assert(NodePtr && "++'d off the end of an ilist!");
127 ilist_iterator operator--(int) { // postdecrement operators...
128 ilist_iterator tmp = *this;
132 ilist_iterator operator++(int) { // postincrement operators...
133 ilist_iterator tmp = *this;
139 // Dummy operators to make errors apparent...
140 template<class X> void operator+(X Val) {}
141 template<class X> void operator-(X Val) {}
143 // Internal interface, do not use...
144 pointer getNodePtrUnchecked() const { return NodePtr; }
148 //===----------------------------------------------------------------------===//
150 // iplist - The subset of list functionality that can safely be used on nodes of
151 // polymorphic types, ie a heterogeneus list with a common base class that holds
152 // the next/prev pointers...
154 template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
155 class iplist : public Traits {
158 static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
159 static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
161 typedef NodeTy *pointer;
162 typedef const NodeTy *const_pointer;
163 typedef NodeTy &reference;
164 typedef const NodeTy &const_reference;
165 typedef NodeTy value_type;
166 typedef ilist_iterator<NodeTy> iterator;
167 typedef ilist_iterator<const NodeTy> const_iterator;
168 typedef size_t size_type;
169 typedef ptrdiff_t difference_type;
170 typedef reverse_iterator<const_iterator> const_reverse_iterator;
171 typedef reverse_iterator<iterator> reverse_iterator;
173 iplist() : Head(createNode()), Tail(Head) {
177 ~iplist() { clear(); delete Tail; }
179 // Iterator creation methods...
180 iterator begin() { return iterator(Head); }
181 const_iterator begin() const { return const_iterator(Head); }
182 iterator end() { return iterator(Tail); }
183 const_iterator end() const { return const_iterator(Tail); }
185 // reverse iterator creation methods...
186 reverse_iterator rbegin() { return reverse_iterator(end()); }
187 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
188 reverse_iterator rend() { return reverse_iterator(begin()); }
189 const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
191 // Miscellaneous inspection routines...
192 size_type max_size() const { return size_type(-1); }
193 bool empty() const { return Head == Tail; }
195 // Front and back accessor functions...
197 assert(!empty() && "Called front() on empty list!");
200 const_reference front() const {
201 assert(!empty() && "Called front() on empty list!");
205 assert(!empty() && "Called back() on empty list!");
206 return *getPrev(Tail);
208 const_reference back() const {
209 assert(!empty() && "Called back() on empty list!");
210 return *getPrev(Tail);
213 void swap(iplist &RHS) {
214 abort(); // Swap does not use list traits callback correctly yet!
215 std::swap(Head, RHS.Head);
216 std::swap(Tail, RHS.Tail);
219 iterator insert(iterator where, NodeTy *New) {
220 NodeTy *CurNode = where.getNodePtrUnchecked(), *PrevNode = getPrev(CurNode);
221 setNext(New, CurNode);
222 setPrev(New, PrevNode);
225 setNext(PrevNode, New);
228 setPrev(CurNode, New);
230 addNodeToList(New); // Notify traits that we added a node...
234 NodeTy *remove(iterator &IT) {
235 assert(IT != end() && "Cannot remove end of list!");
237 NodeTy *NextNode = getNext(Node);
238 NodeTy *PrevNode = getPrev(Node);
241 setNext(PrevNode, NextNode);
244 setPrev(NextNode, PrevNode);
246 removeNodeFromList(Node); // Notify traits that we added a node...
250 NodeTy *remove(const iterator &IT) {
252 return remove(MutIt);
255 // erase - remove a node from the controlled sequence... and delete it.
256 iterator erase(iterator where) {
257 delete remove(where);
263 // transfer - The heart of the splice function. Move linked list nodes from
264 // [first, last) into position.
266 void transfer(iterator position, iplist &L2, iterator first, iterator last) {
267 assert(first != last && "Should be checked by callers");
268 if (position != last) {
269 // Remove [first, last) from its old position.
270 NodeTy *First = &*first, *Prev = getPrev(First);
271 NodeTy *Next = last.getNodePtrUnchecked(), *Last = getPrev(Next);
278 // Splice [first, last) into its new position.
279 NodeTy *PosNext = position.getNodePtrUnchecked();
280 NodeTy *PosPrev = getPrev(PosNext);
282 // Fix head of list...
284 setNext(PosPrev, First);
287 setPrev(First, PosPrev);
289 // Fix end of list...
290 setNext(Last, PosNext);
291 setPrev(PosNext, Last);
293 transferNodesFromList(L2, First, PosNext);
299 //===----------------------------------------------------------------------===
300 // Functionality derived from other functions defined above...
303 size_type size() const {
304 size_type Result = 0;
305 std::distance(begin(), end(), Result);
309 iterator erase(iterator first, iterator last) {
310 while (first != last)
311 first = erase(first);
315 void clear() { erase(begin(), end()); }
317 // Front and back inserters...
318 void push_front(NodeTy *val) { insert(begin(), val); }
319 void push_back(NodeTy *val) { insert(end(), val); }
321 assert(!empty() && "pop_front() on empty list!");
325 assert(!empty() && "pop_back() on empty list!");
326 iterator t = end(); erase(--t);
329 // Special forms of insert...
330 template<class InIt> void insert(iterator where, InIt first, InIt last) {
331 for (; first != last; ++first) insert(where, *first);
334 // Splice members - defined in terms of transfer...
335 void splice(iterator where, iplist &L2) {
337 transfer(where, L2, L2.begin(), L2.end());
339 void splice(iterator where, iplist &L2, iterator first) {
340 iterator last = first; ++last;
341 if (where == first || where == last) return; // No change
342 transfer(where, L2, first, last);
344 void splice(iterator where, iplist &L2, iterator first, iterator last) {
345 if (first != last) transfer(where, L2, first, last);
350 //===----------------------------------------------------------------------===
351 // High-Level Functionality that shouldn't really be here, but is part of list
354 // These two functions are actually called remove/remove_if in list<>, but
355 // they actually do the job of erase, rename them accordingly.
357 void erase(const NodeTy &val) {
358 for (iterator I = begin(), E = end(); I != E; ) {
359 iterator next = I; ++next;
360 if (*I == val) erase(I);
364 template<class Pr1> void erase_if(Pr1 pred) {
365 for (iterator I = begin(), E = end(); I != E; ) {
366 iterator next = I; ++next;
367 if (pred(*I)) erase(I);
372 template<class Pr2> void unique(Pr2 pred) {
374 for (iterator I = begin(), E = end(), Next = begin(); ++Next != E;) {
382 void unique() { unique(op_equal); }
384 template<class Pr3> void merge(iplist &right, Pr3 pred) {
385 iterator first1 = begin(), last1 = end();
386 iterator first2 = right.begin(), last2 = right.end();
387 while (first1 != last1 && first2 != last2)
388 if (pred(*first2, *first1)) {
389 iterator next = first2;
390 transfer(first1, right, first2, ++next);
395 if (first2 != last2) transfer(last1, right, first2, last2);
397 void merge(iplist &right) { return merge(right, op_less); }
399 template<class Pr3> void sort(Pr3 pred);
400 void sort() { sort(op_less); }
405 template<typename NodeTy>
406 struct ilist : public iplist<NodeTy> {
408 ilist(const ilist &right) {
409 insert(begin(), right.begin(), right.end());
411 explicit ilist(size_type count) {
412 insert(begin(), count, NodeTy());
414 ilist(size_type count, const NodeTy &val) {
415 insert(begin(), count, val);
417 template<class InIt> ilist(InIt first, InIt last) {
418 insert(begin(), first, last);
422 // Forwarding functions: A workaround for GCC 2.95 which does not correctly
423 // support 'using' declarations to bring a hidden member into scope.
425 iterator insert(iterator a, NodeTy *b){ return iplist<NodeTy>::insert(a, b); }
426 void push_front(NodeTy *a) { iplist<NodeTy>::push_front(a); }
427 void push_back(NodeTy *a) { iplist<NodeTy>::push_back(a); }
430 // Main implementation here - Insert for a node passed by value...
431 iterator insert(iterator where, const NodeTy &val) {
432 return insert(where, createNode(val));
436 // Front and back inserters...
437 void push_front(const NodeTy &val) { insert(begin(), val); }
438 void push_back(const NodeTy &val) { insert(end(), val); }
440 // Special forms of insert...
441 template<class InIt> void insert(iterator where, InIt first, InIt last) {
442 for (; first != last; ++first) insert(where, *first);
444 void insert(iterator where, size_type count, const NodeTy &val) {
445 for (; count != 0; --count) insert(where, val);
448 // Assign special forms...
449 void assign(size_type count, const NodeTy &val) {
450 iterator I = begin();
451 for (; I != end() && count != 0; ++I, --count)
454 insert(end(), n, val);
458 template<class InIt> void assign(InIt first, InIt last) {
459 iterator first1 = begin(), last1 = end();
460 for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
463 erase(first1, last1);
465 insert(last1, first2, last2);
470 void resize(size_type newsize, NodeTy val) {
471 iterator i = begin();
473 for ( ; i != end() && len < newsize; ++i, ++len) /* empty*/ ;
478 insert(end(), newsize - len, val);
480 void resize(size_type newsize) { resize(newsize, NodeTy()); }
485 // Ensure that swap uses the fast list swap...
487 void swap(iplist<Ty> &Left, iplist<Ty> &Right) {
490 } // End 'std' extensions...