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