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