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