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