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