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