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