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