Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / include / Support / ilist
1 //===-- Support/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 SUPPORT_ILIST
39 #define SUPPORT_ILIST
40
41 #include <Support/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() { return new NodeTy(); }
62   static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
63
64
65   void addNodeToList(NodeTy *NTy) {}
66   void removeNodeFromList(NodeTy *NTy) {}
67   void transferNodesFromList(iplist<NodeTy, ilist_traits> &L2,
68                              ilist_iterator<NodeTy> first,
69                              ilist_iterator<NodeTy> last) {}
70 };
71
72 // Const traits are the same as nonconst traits...
73 template<typename Ty>
74 struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
75
76
77 //===----------------------------------------------------------------------===//
78 // ilist_iterator<Node> - Iterator for intrusive list.
79 //
80 template<typename NodeTy>
81 class ilist_iterator
82   : public bidirectional_iterator<NodeTy, ptrdiff_t> {
83   typedef ilist_traits<NodeTy> Traits;
84   typedef bidirectional_iterator<NodeTy, ptrdiff_t> super;
85
86 public:
87   typedef size_t size_type;
88   typedef typename super::pointer pointer;
89   typedef typename super::reference reference;
90 private:
91   pointer NodePtr;
92 public:
93
94   ilist_iterator(pointer NP) : NodePtr(NP) {}
95   ilist_iterator(reference NR) : NodePtr(&NR) {}
96   ilist_iterator() : NodePtr(0) {}
97
98   // This is templated so that we can allow constructing a const iterator from
99   // a nonconst iterator...
100   template<class node_ty>
101   ilist_iterator(const ilist_iterator<node_ty> &RHS)
102     : NodePtr(RHS.getNodePtrUnchecked()) {}
103
104   // This is templated so that we can allow assigning to a const iterator from
105   // a nonconst iterator...
106   template<class node_ty>
107   const ilist_iterator &operator=(const ilist_iterator<node_ty> &RHS) {
108     NodePtr = RHS.getNodePtrUnchecked();
109     return *this;
110   }
111
112   // Accessors...
113   operator pointer() const {
114     assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
115     return NodePtr;
116   }
117
118   reference operator*() const {
119     assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
120     return *NodePtr;
121   }
122   pointer operator->() { return &operator*(); }
123   const pointer operator->() const { return &operator*(); }
124
125   // Comparison operators
126   bool operator==(const ilist_iterator &RHS) const {
127     return NodePtr == RHS.NodePtr;
128   }
129   bool operator!=(const ilist_iterator &RHS) const {
130     return NodePtr != RHS.NodePtr;
131   }
132
133   // Increment and decrement operators...
134   ilist_iterator &operator--() {      // predecrement - Back up
135     NodePtr = Traits::getPrev(NodePtr);
136     assert(NodePtr && "--'d off the beginning of an ilist!");
137     return *this;
138   }
139   ilist_iterator &operator++() {      // preincrement - Advance
140     NodePtr = Traits::getNext(NodePtr);
141     assert(NodePtr && "++'d off the end of an ilist!");
142     return *this;
143   }
144   ilist_iterator operator--(int) {    // postdecrement operators...
145     ilist_iterator tmp = *this;
146     --*this;
147     return tmp;
148   }
149   ilist_iterator operator++(int) {    // postincrement operators...
150     ilist_iterator tmp = *this;
151     ++*this;
152     return tmp;
153   }
154
155   // Internal interface, do not use...
156   pointer getNodePtrUnchecked() const { return NodePtr; }
157 };
158
159 // do not implement. this is to catch errors when people try to use
160 // them as random access iterators
161 template<typename T>
162 void operator-(int, ilist_iterator<T>);
163 template<typename T>
164 void operator-(ilist_iterator<T>,int);
165
166 template<typename T>
167 void operator+(int, ilist_iterator<T>);
168 template<typename T>
169 void operator+(ilist_iterator<T>,int);
170
171 //===----------------------------------------------------------------------===//
172 // ilist_compat_iterator<Node> - Compatibility iterator for intrusive list.
173 // This makes an ilist<X> act like an std::list<X*>, where you have to
174 // dereference stuff multiple times.  This should only be used for temporary
175 // migration purposes.  Because we don't support "changing the pointer", we only
176 // expose constant pointers.
177 //
178 template<typename NodeTy>
179 class ilist_compat_iterator
180   : public bidirectional_iterator<NodeTy* const, ptrdiff_t> {
181   typedef ilist_traits<NodeTy> Traits;
182   typedef bidirectional_iterator<NodeTy* const, ptrdiff_t> super;
183
184 public:
185   typedef size_t size_type;
186   typedef typename super::pointer pointer;
187   typedef typename super::reference reference;
188 private:
189   NodeTy *NodePtr;
190 public:
191
192   ilist_compat_iterator(NodeTy *NP) : NodePtr(NP) {}
193   ilist_compat_iterator() : NodePtr(0) {}
194
195   // This is templated so that we can allow constructing a const iterator from
196   // a nonconst iterator...
197   template<class node_ty>
198   ilist_compat_iterator(const ilist_compat_iterator<node_ty> &RHS)
199     : NodePtr(RHS.getNodePtrUnchecked()) {}
200
201   // This is templated so that we can allow assigning to a const iterator from
202   // a nonconst iterator...
203   template<class node_ty>
204   const ilist_compat_iterator &operator=(const 
205                                          ilist_compat_iterator<node_ty> &RHS) {
206     NodePtr = RHS.getNodePtrUnchecked();
207     return *this;
208   }
209
210   // Accessors...
211   operator pointer() const {
212     assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
213     return &NodePtr;
214   }
215
216   reference operator*() const {
217     assert(Traits::getNext(NodePtr) != 0 && "Dereferencing end()!");
218     return NodePtr;
219   }
220   pointer operator->() { return &operator*(); }
221   const pointer operator->() const { return &operator*(); }
222
223   // Comparison operators
224   bool operator==(const ilist_compat_iterator &RHS) const {
225     return NodePtr == RHS.NodePtr;
226   }
227   bool operator!=(const ilist_compat_iterator &RHS) const {
228     return NodePtr != RHS.NodePtr;
229   }
230
231   // Increment and decrement operators...
232   ilist_compat_iterator &operator--() {      // predecrement - Back up
233     NodePtr = Traits::getPrev(NodePtr);
234     assert(NodePtr && "--'d off the beginning of an ilist!");
235     return *this;
236   }
237   ilist_compat_iterator &operator++() {      // preincrement - Advance
238     NodePtr = Traits::getNext(NodePtr);
239     assert(NodePtr && "++'d off the end of an ilist!");
240     return *this;
241   }
242   ilist_compat_iterator operator--(int) {    // postdecrement operators...
243     ilist_compat_iterator tmp = *this;
244     --*this;
245     return tmp;
246   }
247   ilist_compat_iterator operator++(int) {    // postincrement operators...
248     ilist_compat_iterator tmp = *this;
249     ++*this;
250     return tmp;
251   }
252
253   // Internal interface, do not use...
254   pointer getNodePtrUnchecked() const { return NodePtr; }
255 };
256
257
258 // Allow ilist_iterators to convert into pointers to a node automatically when
259 // used by the dyn_cast, cast, isa mechanisms...
260
261 template<typename From> struct simplify_type;
262
263 template<typename NodeTy> struct simplify_type<ilist_iterator<NodeTy> > {
264   typedef NodeTy* SimpleType;
265   
266   static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
267     return &*Node;
268   }
269 };
270 template<typename NodeTy> struct simplify_type<const ilist_iterator<NodeTy> > {
271   typedef NodeTy* SimpleType;
272   
273   static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
274     return &*Node;
275   }
276 };
277
278
279 //===----------------------------------------------------------------------===//
280 //
281 // iplist - The subset of list functionality that can safely be used on nodes of
282 // polymorphic types, ie a heterogeneus list with a common base class that holds
283 // the next/prev pointers...
284 //
285 template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
286 class iplist : public Traits {
287   NodeTy *Head, *Tail;
288
289   static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
290   static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
291 public:
292   typedef NodeTy *pointer;
293   typedef const NodeTy *const_pointer;
294   typedef NodeTy &reference;
295   typedef const NodeTy &const_reference;
296   typedef NodeTy value_type;
297   typedef ilist_iterator<NodeTy> iterator;
298   typedef ilist_iterator<const NodeTy> const_iterator;
299   typedef size_t size_type;
300   typedef ptrdiff_t difference_type;
301   typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
302   typedef std::reverse_iterator<iterator>  reverse_iterator;
303
304   iplist() : Head(Traits::createNode()), Tail(Head) {
305     setNext(Head, 0);
306     setPrev(Head, 0);
307   }
308   ~iplist() { clear(); delete Tail; }
309
310   // Iterator creation methods.
311   iterator begin()             { return iterator(Head); }
312   const_iterator begin() const { return const_iterator(Head); }
313   iterator end()               { return iterator(Tail); }
314   const_iterator end() const   { return const_iterator(Tail); }
315
316   // reverse iterator creation methods.
317   reverse_iterator rbegin()            { return reverse_iterator(end()); }
318   const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
319   reverse_iterator rend()              { return reverse_iterator(begin()); }
320   const_reverse_iterator rend() const  {return const_reverse_iterator(begin());}
321
322
323   // "compatibility" iterator creation methods.
324   typedef ilist_compat_iterator<NodeTy> compat_iterator;
325   compat_iterator compat_begin() const { return compat_iterator(Head); }
326   compat_iterator compat_end()   const { return compat_iterator(Tail); }
327
328   // Miscellaneous inspection routines.
329   size_type max_size() const { return size_type(-1); }
330   bool empty() const { return Head == Tail; }
331
332   // Front and back accessor functions...
333   reference front() {
334     assert(!empty() && "Called front() on empty list!");
335     return *Head;
336   }
337   const_reference front() const {
338     assert(!empty() && "Called front() on empty list!");
339     return *Head;
340   }
341   reference back() {
342     assert(!empty() && "Called back() on empty list!");
343     return *getPrev(Tail);
344   }
345   const_reference back() const {
346     assert(!empty() && "Called back() on empty list!");
347     return *getPrev(Tail);
348   }
349
350   void swap(iplist &RHS) {
351     abort();     // Swap does not use list traits callback correctly yet!
352     std::swap(Head, RHS.Head);
353     std::swap(Tail, RHS.Tail);
354   }
355
356   iterator insert(iterator where, NodeTy *New) {
357     NodeTy *CurNode = where.getNodePtrUnchecked(), *PrevNode = getPrev(CurNode);
358     setNext(New, CurNode);
359     setPrev(New, PrevNode);
360
361     if (PrevNode)
362       setNext(PrevNode, New);
363     else
364       Head = New;
365     setPrev(CurNode, New);
366
367     addNodeToList(New);  // Notify traits that we added a node...
368     return New;
369   }
370
371   NodeTy *remove(iterator &IT) {
372     assert(IT != end() && "Cannot remove end of list!");
373     NodeTy *Node = &*IT;
374     NodeTy *NextNode = getNext(Node);
375     NodeTy *PrevNode = getPrev(Node);
376
377     if (PrevNode)
378       setNext(PrevNode, NextNode);
379     else
380       Head = NextNode;
381     setPrev(NextNode, PrevNode);
382     IT = NextNode;
383     removeNodeFromList(Node);  // Notify traits that we removed a node...
384     return Node;
385   }
386
387   NodeTy *remove(const iterator &IT) {
388     iterator MutIt = IT;
389     return remove(MutIt);
390   }
391
392   // erase - remove a node from the controlled sequence... and delete it.
393   iterator erase(iterator where) {
394     delete remove(where);
395     return where;
396   }
397
398
399 private:
400   // transfer - The heart of the splice function.  Move linked list nodes from
401   // [first, last) into position.
402   //
403   void transfer(iterator position, iplist &L2, iterator first, iterator last) {
404     assert(first != last && "Should be checked by callers");
405     if (position != last) {
406       // Remove [first, last) from its old position.
407       NodeTy *First = &*first, *Prev = getPrev(First);
408       NodeTy *Next = last.getNodePtrUnchecked(), *Last = getPrev(Next);
409       if (Prev)
410         setNext(Prev, Next);
411       else
412         L2.Head = Next;
413       setPrev(Next, Prev);
414
415       // Splice [first, last) into its new position.
416       NodeTy *PosNext = position.getNodePtrUnchecked();
417       NodeTy *PosPrev = getPrev(PosNext);
418
419       // Fix head of list...
420       if (PosPrev)
421         setNext(PosPrev, First);
422       else
423         Head = First;
424       setPrev(First, PosPrev);
425
426       // Fix end of list...
427       setNext(Last, PosNext);
428       setPrev(PosNext, Last);
429
430       transferNodesFromList(L2, First, PosNext);
431     }
432   }
433
434 public:
435
436   //===----------------------------------------------------------------------===
437   // Functionality derived from other functions defined above...
438   //
439
440   size_type size() const {
441 #if __GNUC__ == 2
442     // GCC 2.95 has a broken std::distance
443     size_type Result = 0;
444     std::distance(begin(), end(), Result);
445     return Result;
446 #else
447     return std::distance(begin(), end());
448 #endif
449   }
450
451   iterator erase(iterator first, iterator last) {
452     while (first != last)
453       first = erase(first);
454     return last;
455   }
456
457   void clear() { erase(begin(), end()); }
458
459   // Front and back inserters...
460   void push_front(NodeTy *val) { insert(begin(), val); }
461   void push_back(NodeTy *val) { insert(end(), val); }
462   void pop_front() {
463     assert(!empty() && "pop_front() on empty list!");
464     erase(begin());
465   }
466   void pop_back() {
467     assert(!empty() && "pop_back() on empty list!");
468     iterator t = end(); erase(--t);
469   }
470
471   // Special forms of insert...
472   template<class InIt> void insert(iterator where, InIt first, InIt last) {
473     for (; first != last; ++first) insert(where, *first);
474   }
475
476   // Splice members - defined in terms of transfer...
477   void splice(iterator where, iplist &L2) {
478     if (!L2.empty())
479       transfer(where, L2, L2.begin(), L2.end());
480   }
481   void splice(iterator where, iplist &L2, iterator first) {
482     iterator last = first; ++last;
483     if (where == first || where == last) return; // No change
484     transfer(where, L2, first, last);
485   }
486   void splice(iterator where, iplist &L2, iterator first, iterator last) {
487     if (first != last) transfer(where, L2, first, last);
488   }
489
490
491
492   //===----------------------------------------------------------------------===
493   // High-Level Functionality that shouldn't really be here, but is part of list
494   //
495
496   // These two functions are actually called remove/remove_if in list<>, but
497   // they actually do the job of erase, rename them accordingly.
498   //
499   void erase(const NodeTy &val) {
500     for (iterator I = begin(), E = end(); I != E; ) {
501       iterator next = I; ++next;
502       if (*I == val) erase(I);
503       I = next;
504     }
505   }
506   template<class Pr1> void erase_if(Pr1 pred) {
507     for (iterator I = begin(), E = end(); I != E; ) {
508       iterator next = I; ++next;
509       if (pred(*I)) erase(I);
510       I = next;
511     }
512   }
513
514   template<class Pr2> void unique(Pr2 pred) {
515     if (empty()) return;
516     for (iterator I = begin(), E = end(), Next = begin(); ++Next != E;) {
517       if (pred(*I))
518         erase(Next);
519       else
520         I = Next;
521       Next = I;
522     }
523   }
524   void unique() { unique(op_equal); }
525
526   template<class Pr3> void merge(iplist &right, Pr3 pred) {
527     iterator first1 = begin(), last1 = end();
528     iterator first2 = right.begin(), last2 = right.end();
529     while (first1 != last1 && first2 != last2)
530       if (pred(*first2, *first1)) {
531         iterator next = first2;
532         transfer(first1, right, first2, ++next);
533         first2 = next;
534       } else {
535         ++first1;
536       }
537     if (first2 != last2) transfer(last1, right, first2, last2);
538   }
539   void merge(iplist &right) { return merge(right, op_less); }
540
541   template<class Pr3> void sort(Pr3 pred);
542   void sort() { sort(op_less); }
543   void reverse();
544 };
545
546
547 template<typename NodeTy>
548 struct ilist : public iplist<NodeTy> {
549   typedef typename iplist<NodeTy>::size_type size_type;
550   typedef typename iplist<NodeTy>::iterator iterator;
551
552   ilist() {}
553   ilist(const ilist &right) {
554     insert(this->begin(), right.begin(), right.end());
555   }
556   explicit ilist(size_type count) {
557     insert(this->begin(), count, NodeTy());
558   } 
559   ilist(size_type count, const NodeTy &val) {
560     insert(this->begin(), count, val);
561   }
562   template<class InIt> ilist(InIt first, InIt last) {
563     insert(this->begin(), first, last);
564   }
565
566
567   // Forwarding functions: A workaround for GCC 2.95 which does not correctly
568   // support 'using' declarations to bring a hidden member into scope.
569   //
570   iterator insert(iterator a, NodeTy *b){ return iplist<NodeTy>::insert(a, b); }
571   void push_front(NodeTy *a) { iplist<NodeTy>::push_front(a); }
572   void push_back(NodeTy *a)  { iplist<NodeTy>::push_back(a); }
573   
574
575   // Main implementation here - Insert for a node passed by value...
576   iterator insert(iterator where, const NodeTy &val) {
577     return insert(where, createNode(val));
578   }
579
580
581   // Front and back inserters...
582   void push_front(const NodeTy &val) { insert(this->begin(), val); }
583   void push_back(const NodeTy &val) { insert(this->end(), val); }
584
585   // Special forms of insert...
586   template<class InIt> void insert(iterator where, InIt first, InIt last) {
587     for (; first != last; ++first) insert(where, *first);
588   }
589   void insert(iterator where, size_type count, const NodeTy &val) {
590     for (; count != 0; --count) insert(where, val);
591   }
592
593   // Assign special forms...
594   void assign(size_type count, const NodeTy &val) {
595     iterator I = this->begin();
596     for (; I != this->end() && count != 0; ++I, --count)
597       *I = val;
598     if (count != 0)
599       insert(this->end(), val, val);
600     else
601       erase(I, this->end());
602   }
603   template<class InIt> void assign(InIt first1, InIt last1) {
604     iterator first2 = this->begin(), last2 = this->end();
605     for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
606       *first1 = *first2;
607     if (first2 == last2)
608       erase(first1, last1);
609     else
610       insert(last1, first2, last2);
611   }
612
613
614   // Resize members...
615   void resize(size_type newsize, NodeTy val) {
616     iterator i = this->begin();
617     size_type len = 0;
618     for ( ; i != this->end() && len < newsize; ++i, ++len) /* empty*/ ;
619
620     if (len == newsize)
621       erase(i, this->end());
622     else                                          // i == end()
623       insert(this->end(), newsize - len, val);
624   }
625   void resize(size_type newsize) { resize(newsize, NodeTy()); }
626 };
627
628 } // End llvm namespace
629
630 namespace std {
631   // Ensure that swap uses the fast list swap...
632   template<class Ty>
633   void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) {
634     Left.swap(Right);
635   }
636 }  // End 'std' extensions...
637
638 #endif