1 //===-- llvm/ADT/ilist - Intrusive Linked List Template ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines classes to implement an intrusive doubly linked list class
11 // (i.e. each node of the list must contain a next and previous field for the
14 // The ilist_traits trait class is used to gain access to the next and previous
15 // fields of the node type that the list is instantiated with. If it is not
16 // specialized, the list defaults to using the getPrev(), getNext() method calls
17 // to get the next and previous pointers.
19 // The ilist class itself, should be a plug in replacement for list, assuming
20 // that the nodes contain next/prev pointers. This list replacement does not
21 // provides a constant time size() method, so be careful to use empty() when you
22 // really want to know if it's empty.
24 // The ilist class is implemented by allocating a 'tail' node when the list is
25 // created (using ilist_traits<>::createSentinel()). This tail node is
26 // absolutely required because the user must be able to compute end()-1. Because
27 // of this, users of the direct next/prev links will see an extra link on the
28 // end of the list, which should be ignored.
30 // Requirements for a user of this list:
32 // 1. The user must provide {g|s}et{Next|Prev} methods, or specialize
33 // ilist_traits to provide an alternate way of getting and setting next and
36 //===----------------------------------------------------------------------===//
38 #ifndef LLVM_ADT_ILIST
39 #define LLVM_ADT_ILIST
41 #include "llvm/ADT/iterator"
46 template<typename NodeTy, typename Traits> class iplist;
47 template<typename NodeTy> class ilist_iterator;
49 // Template traits for intrusive list. By specializing this template class, you
50 // can change what next/prev fields are used to store the links...
51 template<typename NodeTy>
53 static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); }
54 static NodeTy *getNext(NodeTy *N) { return N->getNext(); }
55 static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); }
56 static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); }
58 static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
59 static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
61 static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
63 static NodeTy *createSentinel() { return new NodeTy(); }
64 static void destroySentinel(NodeTy *N) { delete N; }
66 void addNodeToList(NodeTy *NTy) {}
67 void removeNodeFromList(NodeTy *NTy) {}
68 void transferNodesFromList(iplist<NodeTy, ilist_traits> &L2,
69 ilist_iterator<NodeTy> first,
70 ilist_iterator<NodeTy> last) {}
73 // Const traits are the same as nonconst traits...
75 struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
78 //===----------------------------------------------------------------------===//
79 // ilist_iterator<Node> - Iterator for intrusive list.
81 template<typename NodeTy>
83 : public bidirectional_iterator<NodeTy, ptrdiff_t> {
84 typedef ilist_traits<NodeTy> Traits;
85 typedef bidirectional_iterator<NodeTy, ptrdiff_t> super;
88 typedef size_t size_type;
89 typedef typename super::pointer pointer;
90 typedef typename super::reference reference;
95 ilist_iterator(pointer NP) : NodePtr(NP) {}
96 ilist_iterator(reference NR) : NodePtr(&NR) {}
97 ilist_iterator() : NodePtr(0) {}
99 // This is templated so that we can allow constructing a const iterator from
100 // a nonconst iterator...
101 template<class node_ty>
102 ilist_iterator(const ilist_iterator<node_ty> &RHS)
103 : NodePtr(RHS.getNodePtrUnchecked()) {}
105 // This is templated so that we can allow assigning to a const iterator from
106 // a nonconst iterator...
107 template<class node_ty>
108 const ilist_iterator &operator=(const ilist_iterator<node_ty> &RHS) {
109 NodePtr = RHS.getNodePtrUnchecked();
114 operator pointer() const {
115 assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
119 reference operator*() const {
120 assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
123 pointer operator->() { return &operator*(); }
124 const pointer operator->() const { return &operator*(); }
126 // Comparison operators
127 bool operator==(const ilist_iterator &RHS) const {
128 return NodePtr == RHS.NodePtr;
130 bool operator!=(const ilist_iterator &RHS) const {
131 return NodePtr != RHS.NodePtr;
134 // Increment and decrement operators...
135 ilist_iterator &operator--() { // predecrement - Back up
136 NodePtr = Traits::getPrev(NodePtr);
137 assert(Traits::getNext(NodePtr) && "--'d off the beginning of an ilist!");
140 ilist_iterator &operator++() { // preincrement - Advance
141 NodePtr = Traits::getNext(NodePtr);
142 assert(NodePtr && "++'d off the end of an ilist!");
145 ilist_iterator operator--(int) { // postdecrement operators...
146 ilist_iterator tmp = *this;
150 ilist_iterator operator++(int) { // postincrement operators...
151 ilist_iterator tmp = *this;
156 // Internal interface, do not use...
157 pointer getNodePtrUnchecked() const { return NodePtr; }
160 // do not implement. this is to catch errors when people try to use
161 // them as random access iterators
163 void operator-(int, ilist_iterator<T>);
165 void operator-(ilist_iterator<T>,int);
168 void operator+(int, ilist_iterator<T>);
170 void operator+(ilist_iterator<T>,int);
172 // operator!=/operator== - Allow mixed comparisons without dereferencing
173 // the iterator, which could very likely be pointing to end().
175 bool operator!=(const T* LHS, const ilist_iterator<const T> &RHS) {
176 return LHS != RHS.getNodePtrUnchecked();
179 bool operator==(const T* LHS, const ilist_iterator<const T> &RHS) {
180 return LHS == RHS.getNodePtrUnchecked();
183 bool operator!=(T* LHS, const ilist_iterator<T> &RHS) {
184 return LHS != RHS.getNodePtrUnchecked();
187 bool operator==(T* LHS, const ilist_iterator<T> &RHS) {
188 return LHS == RHS.getNodePtrUnchecked();
192 // Allow ilist_iterators to convert into pointers to a node automatically when
193 // used by the dyn_cast, cast, isa mechanisms...
195 template<typename From> struct simplify_type;
197 template<typename NodeTy> struct simplify_type<ilist_iterator<NodeTy> > {
198 typedef NodeTy* SimpleType;
200 static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
204 template<typename NodeTy> struct simplify_type<const ilist_iterator<NodeTy> > {
205 typedef NodeTy* SimpleType;
207 static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
213 //===----------------------------------------------------------------------===//
215 // iplist - The subset of list functionality that can safely be used on nodes of
216 // polymorphic types, i.e. a heterogenous list with a common base class that
217 // holds the next/prev pointers...
219 template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
220 class iplist : public Traits {
223 // Use the prev node pointer of 'head' as the tail pointer. This is really a
224 // circularly linked list where we snip the 'next' link from the sentinel node
225 // back to the first node in the list (to preserve assertions about going off
226 // the end of the list).
227 NodeTy *getTail() { return getPrev(Head); }
228 const NodeTy *getTail() const { return getPrev(Head); }
229 void setTail(NodeTy *N) { setPrev(Head, N); }
231 static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
232 static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
234 typedef NodeTy *pointer;
235 typedef const NodeTy *const_pointer;
236 typedef NodeTy &reference;
237 typedef const NodeTy &const_reference;
238 typedef NodeTy value_type;
239 typedef ilist_iterator<NodeTy> iterator;
240 typedef ilist_iterator<const NodeTy> const_iterator;
241 typedef size_t size_type;
242 typedef ptrdiff_t difference_type;
243 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
244 typedef std::reverse_iterator<iterator> reverse_iterator;
246 iplist() : Head(Traits::createSentinel()) {
250 ~iplist() { clear(); Traits::destroySentinel(getTail()); }
252 // Iterator creation methods.
253 iterator begin() { return iterator(Head); }
254 const_iterator begin() const { return const_iterator(Head); }
255 iterator end() { return iterator(getTail()); }
256 const_iterator end() const { return const_iterator(getTail()); }
258 // reverse iterator creation methods.
259 reverse_iterator rbegin() { return reverse_iterator(end()); }
260 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
261 reverse_iterator rend() { return reverse_iterator(begin()); }
262 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
265 // Miscellaneous inspection routines.
266 size_type max_size() const { return size_type(-1); }
267 bool empty() const { return Head == getTail(); }
269 // Front and back accessor functions...
271 assert(!empty() && "Called front() on empty list!");
274 const_reference front() const {
275 assert(!empty() && "Called front() on empty list!");
279 assert(!empty() && "Called back() on empty list!");
280 return *getPrev(getTail());
282 const_reference back() const {
283 assert(!empty() && "Called back() on empty list!");
284 return *getPrev(getTail());
287 void swap(iplist &RHS) {
288 abort(); // Swap does not use list traits callback correctly yet!
289 std::swap(Head, RHS.Head);
292 iterator insert(iterator where, NodeTy *New) {
293 NodeTy *CurNode = where.getNodePtrUnchecked(), *PrevNode = getPrev(CurNode);
294 setNext(New, CurNode);
295 setPrev(New, PrevNode);
297 if (CurNode != Head) // Is PrevNode off the beginning of the list?
298 setNext(PrevNode, New);
301 setPrev(CurNode, New);
303 addNodeToList(New); // Notify traits that we added a node...
307 NodeTy *remove(iterator &IT) {
308 assert(IT != end() && "Cannot remove end of list!");
310 NodeTy *NextNode = getNext(Node);
311 NodeTy *PrevNode = getPrev(Node);
313 if (Node != Head) // Is PrevNode off the beginning of the list?
314 setNext(PrevNode, NextNode);
317 setPrev(NextNode, PrevNode);
319 removeNodeFromList(Node); // Notify traits that we removed a node...
323 NodeTy *remove(const iterator &IT) {
325 return remove(MutIt);
328 // erase - remove a node from the controlled sequence... and delete it.
329 iterator erase(iterator where) {
330 delete remove(where);
336 // transfer - The heart of the splice function. Move linked list nodes from
337 // [first, last) into position.
339 void transfer(iterator position, iplist &L2, iterator first, iterator last) {
340 assert(first != last && "Should be checked by callers");
342 if (position != last) {
343 // Note: we have to be careful about the case when we move the first node
344 // in the list. This node is the list sentinel node and we can't move it.
345 NodeTy *ThisSentinel = getTail();
347 NodeTy *L2Sentinel = L2.getTail();
350 // Remove [first, last) from its old position.
351 NodeTy *First = &*first, *Prev = getPrev(First);
352 NodeTy *Next = last.getNodePtrUnchecked(), *Last = getPrev(Next);
359 // Splice [first, last) into its new position.
360 NodeTy *PosNext = position.getNodePtrUnchecked();
361 NodeTy *PosPrev = getPrev(PosNext);
363 // Fix head of list...
365 setNext(PosPrev, First);
368 setPrev(First, PosPrev);
370 // Fix end of list...
371 setNext(Last, PosNext);
372 setPrev(PosNext, Last);
374 transferNodesFromList(L2, First, PosNext);
376 // Now that everything is set, restore the pointers to the list sentinals.
377 L2.setTail(L2Sentinel);
378 setTail(ThisSentinel);
384 //===----------------------------------------------------------------------===
385 // Functionality derived from other functions defined above...
388 size_type size() const {
390 // GCC 2.95 has a broken std::distance
391 size_type Result = 0;
392 std::distance(begin(), end(), Result);
395 return std::distance(begin(), end());
399 iterator erase(iterator first, iterator last) {
400 while (first != last)
401 first = erase(first);
405 void clear() { erase(begin(), end()); }
407 // Front and back inserters...
408 void push_front(NodeTy *val) { insert(begin(), val); }
409 void push_back(NodeTy *val) { insert(end(), val); }
411 assert(!empty() && "pop_front() on empty list!");
415 assert(!empty() && "pop_back() on empty list!");
416 iterator t = end(); erase(--t);
419 // Special forms of insert...
420 template<class InIt> void insert(iterator where, InIt first, InIt last) {
421 for (; first != last; ++first) insert(where, *first);
424 // Splice members - defined in terms of transfer...
425 void splice(iterator where, iplist &L2) {
427 transfer(where, L2, L2.begin(), L2.end());
429 void splice(iterator where, iplist &L2, iterator first) {
430 iterator last = first; ++last;
431 if (where == first || where == last) return; // No change
432 transfer(where, L2, first, last);
434 void splice(iterator where, iplist &L2, iterator first, iterator last) {
435 if (first != last) transfer(where, L2, first, last);
440 //===----------------------------------------------------------------------===
441 // High-Level Functionality that shouldn't really be here, but is part of list
444 // These two functions are actually called remove/remove_if in list<>, but
445 // they actually do the job of erase, rename them accordingly.
447 void erase(const NodeTy &val) {
448 for (iterator I = begin(), E = end(); I != E; ) {
449 iterator next = I; ++next;
450 if (*I == val) erase(I);
454 template<class Pr1> void erase_if(Pr1 pred) {
455 for (iterator I = begin(), E = end(); I != E; ) {
456 iterator next = I; ++next;
457 if (pred(*I)) erase(I);
462 template<class Pr2> void unique(Pr2 pred) {
464 for (iterator I = begin(), E = end(), Next = begin(); ++Next != E;) {
472 void unique() { unique(op_equal); }
474 template<class Pr3> void merge(iplist &right, Pr3 pred) {
475 iterator first1 = begin(), last1 = end();
476 iterator first2 = right.begin(), last2 = right.end();
477 while (first1 != last1 && first2 != last2)
478 if (pred(*first2, *first1)) {
479 iterator next = first2;
480 transfer(first1, right, first2, ++next);
485 if (first2 != last2) transfer(last1, right, first2, last2);
487 void merge(iplist &right) { return merge(right, op_less); }
489 template<class Pr3> void sort(Pr3 pred);
490 void sort() { sort(op_less); }
495 template<typename NodeTy>
496 struct ilist : public iplist<NodeTy> {
497 typedef typename iplist<NodeTy>::size_type size_type;
498 typedef typename iplist<NodeTy>::iterator iterator;
501 ilist(const ilist &right) {
502 insert(this->begin(), right.begin(), right.end());
504 explicit ilist(size_type count) {
505 insert(this->begin(), count, NodeTy());
507 ilist(size_type count, const NodeTy &val) {
508 insert(this->begin(), count, val);
510 template<class InIt> ilist(InIt first, InIt last) {
511 insert(this->begin(), first, last);
515 // Forwarding functions: A workaround for GCC 2.95 which does not correctly
516 // support 'using' declarations to bring a hidden member into scope.
518 iterator insert(iterator a, NodeTy *b){ return iplist<NodeTy>::insert(a, b); }
519 void push_front(NodeTy *a) { iplist<NodeTy>::push_front(a); }
520 void push_back(NodeTy *a) { iplist<NodeTy>::push_back(a); }
523 // Main implementation here - Insert for a node passed by value...
524 iterator insert(iterator where, const NodeTy &val) {
525 return insert(where, createNode(val));
529 // Front and back inserters...
530 void push_front(const NodeTy &val) { insert(this->begin(), val); }
531 void push_back(const NodeTy &val) { insert(this->end(), val); }
533 // Special forms of insert...
534 template<class InIt> void insert(iterator where, InIt first, InIt last) {
535 for (; first != last; ++first) insert(where, *first);
537 void insert(iterator where, size_type count, const NodeTy &val) {
538 for (; count != 0; --count) insert(where, val);
541 // Assign special forms...
542 void assign(size_type count, const NodeTy &val) {
543 iterator I = this->begin();
544 for (; I != this->end() && count != 0; ++I, --count)
547 insert(this->end(), val, val);
549 erase(I, this->end());
551 template<class InIt> void assign(InIt first1, InIt last1) {
552 iterator first2 = this->begin(), last2 = this->end();
553 for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
556 erase(first1, last1);
558 insert(last1, first2, last2);
563 void resize(size_type newsize, NodeTy val) {
564 iterator i = this->begin();
566 for ( ; i != this->end() && len < newsize; ++i, ++len) /* empty*/ ;
569 erase(i, this->end());
571 insert(this->end(), newsize - len, val);
573 void resize(size_type newsize) { resize(newsize, NodeTy()); }
576 } // End llvm namespace
579 // Ensure that swap uses the fast list swap...
581 void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) {
584 } // End 'std' extensions...