remove compat_iterator, which is dead in the tree.
[oota-llvm.git] / include / llvm / ADT / ilist
1 //===-- llvm/ADT/ilist - Intrusive Linked List Template ---------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines classes to implement an intrusive doubly linked list class
11 // (ie each node of the list must contain a next and previous field for the
12 // list.
13 //
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.
18 //
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.
23 //
24 // The ilist class is implemented by allocating a 'tail' node when the list is
25 // created (using ilist_traits<>::createEndMarker()).  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.
29 //
30 // Requirements for a user of this list:
31 //
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
34 //      prev links.
35 //
36 //===----------------------------------------------------------------------===//
37
38 #ifndef LLVM_ADT_ILIST
39 #define LLVM_ADT_ILIST
40
41 #include "llvm/ADT/iterator"
42 #include <cassert>
43
44 namespace llvm {
45
46 template<typename NodeTy, typename Traits> class iplist;
47 template<typename NodeTy> class ilist_iterator;
48
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>
52 struct ilist_traits {
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(); }
57
58   static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
59   static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
60
61   static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
62
63   static NodeTy *createSentinel() { return new NodeTy(); }
64   static void destroySentinel(NodeTy *N) { delete N; }
65
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) {}
71 };
72
73 // Const traits are the same as nonconst traits...
74 template<typename Ty>
75 struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
76
77
78 //===----------------------------------------------------------------------===//
79 // ilist_iterator<Node> - Iterator for intrusive list.
80 //
81 template<typename NodeTy>
82 class ilist_iterator
83   : public bidirectional_iterator<NodeTy, ptrdiff_t> {
84   typedef ilist_traits<NodeTy> Traits;
85   typedef bidirectional_iterator<NodeTy, ptrdiff_t> super;
86
87 public:
88   typedef size_t size_type;
89   typedef typename super::pointer pointer;
90   typedef typename super::reference reference;
91 private:
92   pointer NodePtr;
93 public:
94
95   ilist_iterator(pointer NP) : NodePtr(NP) {}
96   ilist_iterator(reference NR) : NodePtr(&NR) {}
97   ilist_iterator() : NodePtr(0) {}
98
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()) {}
104
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();
110     return *this;
111   }
112
113   // Accessors...
114   operator pointer() const {
115     assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
116     return NodePtr;
117   }
118
119   reference operator*() const {
120     assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
121     return *NodePtr;
122   }
123   pointer operator->() { return &operator*(); }
124   const pointer operator->() const { return &operator*(); }
125
126   // Comparison operators
127   bool operator==(const ilist_iterator &RHS) const {
128     return NodePtr == RHS.NodePtr;
129   }
130   bool operator!=(const ilist_iterator &RHS) const {
131     return NodePtr != RHS.NodePtr;
132   }
133
134   // Increment and decrement operators...
135   ilist_iterator &operator--() {      // predecrement - Back up
136     NodePtr = Traits::getPrev(NodePtr);
137     assert(NodePtr && "--'d off the beginning of an ilist!");
138     return *this;
139   }
140   ilist_iterator &operator++() {      // preincrement - Advance
141     NodePtr = Traits::getNext(NodePtr);
142     assert(NodePtr && "++'d off the end of an ilist!");
143     return *this;
144   }
145   ilist_iterator operator--(int) {    // postdecrement operators...
146     ilist_iterator tmp = *this;
147     --*this;
148     return tmp;
149   }
150   ilist_iterator operator++(int) {    // postincrement operators...
151     ilist_iterator tmp = *this;
152     ++*this;
153     return tmp;
154   }
155
156   // Internal interface, do not use...
157   pointer getNodePtrUnchecked() const { return NodePtr; }
158 };
159
160 // do not implement. this is to catch errors when people try to use
161 // them as random access iterators
162 template<typename T>
163 void operator-(int, ilist_iterator<T>);
164 template<typename T>
165 void operator-(ilist_iterator<T>,int);
166
167 template<typename T>
168 void operator+(int, ilist_iterator<T>);
169 template<typename T>
170 void operator+(ilist_iterator<T>,int);
171
172 // Allow ilist_iterators to convert into pointers to a node automatically when
173 // used by the dyn_cast, cast, isa mechanisms...
174
175 template<typename From> struct simplify_type;
176
177 template<typename NodeTy> struct simplify_type<ilist_iterator<NodeTy> > {
178   typedef NodeTy* SimpleType;
179   
180   static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
181     return &*Node;
182   }
183 };
184 template<typename NodeTy> struct simplify_type<const ilist_iterator<NodeTy> > {
185   typedef NodeTy* SimpleType;
186   
187   static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
188     return &*Node;
189   }
190 };
191
192
193 //===----------------------------------------------------------------------===//
194 //
195 // iplist - The subset of list functionality that can safely be used on nodes of
196 // polymorphic types, ie a heterogeneus list with a common base class that holds
197 // the next/prev pointers...
198 //
199 template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
200 class iplist : public Traits {
201   NodeTy *Head, *Tail;
202
203   static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
204   static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
205 public:
206   typedef NodeTy *pointer;
207   typedef const NodeTy *const_pointer;
208   typedef NodeTy &reference;
209   typedef const NodeTy &const_reference;
210   typedef NodeTy value_type;
211   typedef ilist_iterator<NodeTy> iterator;
212   typedef ilist_iterator<const NodeTy> const_iterator;
213   typedef size_t size_type;
214   typedef ptrdiff_t difference_type;
215   typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
216   typedef std::reverse_iterator<iterator>  reverse_iterator;
217
218   iplist() : Head(Traits::createSentinel()), Tail(Head) {
219     setNext(Head, 0);
220     setPrev(Head, 0);
221   }
222   ~iplist() { clear(); Traits::destroySentinel(Tail); }
223
224   // Iterator creation methods.
225   iterator begin()             { return iterator(Head); }
226   const_iterator begin() const { return const_iterator(Head); }
227   iterator end()               { return iterator(Tail); }
228   const_iterator end() const   { return const_iterator(Tail); }
229
230   // reverse iterator creation methods.
231   reverse_iterator rbegin()            { return reverse_iterator(end()); }
232   const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
233   reverse_iterator rend()              { return reverse_iterator(begin()); }
234   const_reverse_iterator rend() const  {return const_reverse_iterator(begin());}
235
236
237   // Miscellaneous inspection routines.
238   size_type max_size() const { return size_type(-1); }
239   bool empty() const { return Head == Tail; }
240
241   // Front and back accessor functions...
242   reference front() {
243     assert(!empty() && "Called front() on empty list!");
244     return *Head;
245   }
246   const_reference front() const {
247     assert(!empty() && "Called front() on empty list!");
248     return *Head;
249   }
250   reference back() {
251     assert(!empty() && "Called back() on empty list!");
252     return *getPrev(Tail);
253   }
254   const_reference back() const {
255     assert(!empty() && "Called back() on empty list!");
256     return *getPrev(Tail);
257   }
258
259   void swap(iplist &RHS) {
260     abort();     // Swap does not use list traits callback correctly yet!
261     std::swap(Head, RHS.Head);
262     std::swap(Tail, RHS.Tail);
263   }
264
265   iterator insert(iterator where, NodeTy *New) {
266     NodeTy *CurNode = where.getNodePtrUnchecked(), *PrevNode = getPrev(CurNode);
267     setNext(New, CurNode);
268     setPrev(New, PrevNode);
269
270     if (PrevNode)
271       setNext(PrevNode, New);
272     else
273       Head = New;
274     setPrev(CurNode, New);
275
276     addNodeToList(New);  // Notify traits that we added a node...
277     return New;
278   }
279
280   NodeTy *remove(iterator &IT) {
281     assert(IT != end() && "Cannot remove end of list!");
282     NodeTy *Node = &*IT;
283     NodeTy *NextNode = getNext(Node);
284     NodeTy *PrevNode = getPrev(Node);
285
286     if (PrevNode)
287       setNext(PrevNode, NextNode);
288     else
289       Head = NextNode;
290     setPrev(NextNode, PrevNode);
291     IT = NextNode;
292     removeNodeFromList(Node);  // Notify traits that we removed a node...
293     return Node;
294   }
295
296   NodeTy *remove(const iterator &IT) {
297     iterator MutIt = IT;
298     return remove(MutIt);
299   }
300
301   // erase - remove a node from the controlled sequence... and delete it.
302   iterator erase(iterator where) {
303     delete remove(where);
304     return where;
305   }
306
307
308 private:
309   // transfer - The heart of the splice function.  Move linked list nodes from
310   // [first, last) into position.
311   //
312   void transfer(iterator position, iplist &L2, iterator first, iterator last) {
313     assert(first != last && "Should be checked by callers");
314     if (position != last) {
315       // Remove [first, last) from its old position.
316       NodeTy *First = &*first, *Prev = getPrev(First);
317       NodeTy *Next = last.getNodePtrUnchecked(), *Last = getPrev(Next);
318       if (Prev)
319         setNext(Prev, Next);
320       else
321         L2.Head = Next;
322       setPrev(Next, Prev);
323
324       // Splice [first, last) into its new position.
325       NodeTy *PosNext = position.getNodePtrUnchecked();
326       NodeTy *PosPrev = getPrev(PosNext);
327
328       // Fix head of list...
329       if (PosPrev)
330         setNext(PosPrev, First);
331       else
332         Head = First;
333       setPrev(First, PosPrev);
334
335       // Fix end of list...
336       setNext(Last, PosNext);
337       setPrev(PosNext, Last);
338
339       transferNodesFromList(L2, First, PosNext);
340     }
341   }
342
343 public:
344
345   //===----------------------------------------------------------------------===
346   // Functionality derived from other functions defined above...
347   //
348
349   size_type size() const {
350 #if __GNUC__ == 2
351     // GCC 2.95 has a broken std::distance
352     size_type Result = 0;
353     std::distance(begin(), end(), Result);
354     return Result;
355 #else
356     return std::distance(begin(), end());
357 #endif
358   }
359
360   iterator erase(iterator first, iterator last) {
361     while (first != last)
362       first = erase(first);
363     return last;
364   }
365
366   void clear() { erase(begin(), end()); }
367
368   // Front and back inserters...
369   void push_front(NodeTy *val) { insert(begin(), val); }
370   void push_back(NodeTy *val) { insert(end(), val); }
371   void pop_front() {
372     assert(!empty() && "pop_front() on empty list!");
373     erase(begin());
374   }
375   void pop_back() {
376     assert(!empty() && "pop_back() on empty list!");
377     iterator t = end(); erase(--t);
378   }
379
380   // Special forms of insert...
381   template<class InIt> void insert(iterator where, InIt first, InIt last) {
382     for (; first != last; ++first) insert(where, *first);
383   }
384
385   // Splice members - defined in terms of transfer...
386   void splice(iterator where, iplist &L2) {
387     if (!L2.empty())
388       transfer(where, L2, L2.begin(), L2.end());
389   }
390   void splice(iterator where, iplist &L2, iterator first) {
391     iterator last = first; ++last;
392     if (where == first || where == last) return; // No change
393     transfer(where, L2, first, last);
394   }
395   void splice(iterator where, iplist &L2, iterator first, iterator last) {
396     if (first != last) transfer(where, L2, first, last);
397   }
398
399
400
401   //===----------------------------------------------------------------------===
402   // High-Level Functionality that shouldn't really be here, but is part of list
403   //
404
405   // These two functions are actually called remove/remove_if in list<>, but
406   // they actually do the job of erase, rename them accordingly.
407   //
408   void erase(const NodeTy &val) {
409     for (iterator I = begin(), E = end(); I != E; ) {
410       iterator next = I; ++next;
411       if (*I == val) erase(I);
412       I = next;
413     }
414   }
415   template<class Pr1> void erase_if(Pr1 pred) {
416     for (iterator I = begin(), E = end(); I != E; ) {
417       iterator next = I; ++next;
418       if (pred(*I)) erase(I);
419       I = next;
420     }
421   }
422
423   template<class Pr2> void unique(Pr2 pred) {
424     if (empty()) return;
425     for (iterator I = begin(), E = end(), Next = begin(); ++Next != E;) {
426       if (pred(*I))
427         erase(Next);
428       else
429         I = Next;
430       Next = I;
431     }
432   }
433   void unique() { unique(op_equal); }
434
435   template<class Pr3> void merge(iplist &right, Pr3 pred) {
436     iterator first1 = begin(), last1 = end();
437     iterator first2 = right.begin(), last2 = right.end();
438     while (first1 != last1 && first2 != last2)
439       if (pred(*first2, *first1)) {
440         iterator next = first2;
441         transfer(first1, right, first2, ++next);
442         first2 = next;
443       } else {
444         ++first1;
445       }
446     if (first2 != last2) transfer(last1, right, first2, last2);
447   }
448   void merge(iplist &right) { return merge(right, op_less); }
449
450   template<class Pr3> void sort(Pr3 pred);
451   void sort() { sort(op_less); }
452   void reverse();
453 };
454
455
456 template<typename NodeTy>
457 struct ilist : public iplist<NodeTy> {
458   typedef typename iplist<NodeTy>::size_type size_type;
459   typedef typename iplist<NodeTy>::iterator iterator;
460
461   ilist() {}
462   ilist(const ilist &right) {
463     insert(this->begin(), right.begin(), right.end());
464   }
465   explicit ilist(size_type count) {
466     insert(this->begin(), count, NodeTy());
467   } 
468   ilist(size_type count, const NodeTy &val) {
469     insert(this->begin(), count, val);
470   }
471   template<class InIt> ilist(InIt first, InIt last) {
472     insert(this->begin(), first, last);
473   }
474
475
476   // Forwarding functions: A workaround for GCC 2.95 which does not correctly
477   // support 'using' declarations to bring a hidden member into scope.
478   //
479   iterator insert(iterator a, NodeTy *b){ return iplist<NodeTy>::insert(a, b); }
480   void push_front(NodeTy *a) { iplist<NodeTy>::push_front(a); }
481   void push_back(NodeTy *a)  { iplist<NodeTy>::push_back(a); }
482   
483
484   // Main implementation here - Insert for a node passed by value...
485   iterator insert(iterator where, const NodeTy &val) {
486     return insert(where, createNode(val));
487   }
488
489
490   // Front and back inserters...
491   void push_front(const NodeTy &val) { insert(this->begin(), val); }
492   void push_back(const NodeTy &val) { insert(this->end(), val); }
493
494   // Special forms of insert...
495   template<class InIt> void insert(iterator where, InIt first, InIt last) {
496     for (; first != last; ++first) insert(where, *first);
497   }
498   void insert(iterator where, size_type count, const NodeTy &val) {
499     for (; count != 0; --count) insert(where, val);
500   }
501
502   // Assign special forms...
503   void assign(size_type count, const NodeTy &val) {
504     iterator I = this->begin();
505     for (; I != this->end() && count != 0; ++I, --count)
506       *I = val;
507     if (count != 0)
508       insert(this->end(), val, val);
509     else
510       erase(I, this->end());
511   }
512   template<class InIt> void assign(InIt first1, InIt last1) {
513     iterator first2 = this->begin(), last2 = this->end();
514     for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
515       *first1 = *first2;
516     if (first2 == last2)
517       erase(first1, last1);
518     else
519       insert(last1, first2, last2);
520   }
521
522
523   // Resize members...
524   void resize(size_type newsize, NodeTy val) {
525     iterator i = this->begin();
526     size_type len = 0;
527     for ( ; i != this->end() && len < newsize; ++i, ++len) /* empty*/ ;
528
529     if (len == newsize)
530       erase(i, this->end());
531     else                                          // i == end()
532       insert(this->end(), newsize - len, val);
533   }
534   void resize(size_type newsize) { resize(newsize, NodeTy()); }
535 };
536
537 } // End llvm namespace
538
539 namespace std {
540   // Ensure that swap uses the fast list swap...
541   template<class Ty>
542   void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) {
543     Left.swap(Right);
544   }
545 }  // End 'std' extensions...
546
547 #endif