2f35a7ad25c7f1d1762f772e252b92e2a11ca037
[oota-llvm.git] / include / llvm / ADT / ilist.h
1 //==-- llvm/ADT/ilist.h - Intrusive Linked List Template ---------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines classes to implement an intrusive doubly linked list class
11 // (i.e. 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 // provide 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<>::createSentinel()).  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 LLVM_ADT_ILIST_H
39 #define LLVM_ADT_ILIST_H
40
41 #include "llvm/ADT/iterator.h"
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 /// ilist_nextprev_traits - A fragment for template traits for intrusive list
50 /// that provides default next/prev implementations for common operations.
51 ///
52 template<typename NodeTy>
53 struct ilist_nextprev_traits {
54   static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); }
55   static NodeTy *getNext(NodeTy *N) { return N->getNext(); }
56   static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); }
57   static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); }
58
59   static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); }
60   static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); }
61 };
62
63 template<typename NodeTy>
64 struct ilist_traits;
65
66 /// ilist_sentinel_traits - A fragment for template traits for intrusive list
67 /// that provides default sentinel implementations for common operations.
68 ///
69 /// ilist_sentinel_traits implements a lazy dynamic sentinel allocation
70 /// strategy. The sentinel is stored in the prev field of ilist's Head.
71 ///
72 template<typename NodeTy>
73 struct ilist_sentinel_traits {
74   /// createSentinel - create the dynamic sentinel
75   static NodeTy *createSentinel() { return new NodeTy(); }
76
77   /// destroySentinel - deallocate the dynamic sentinel
78   static void destroySentinel(NodeTy *N) { delete N; }
79
80   /// provideInitialHead - when constructing an ilist, provide a starting
81   /// value for its Head
82   /// @return null node to indicate that it needs to be allocated later
83   static NodeTy *provideInitialHead() { return 0; }
84
85   /// ensureHead - make sure that Head is either already
86   /// initialized or assigned a fresh sentinel
87   /// @return the sentinel
88   static NodeTy *ensureHead(NodeTy *&Head) {
89     if (!Head) {
90       Head = ilist_traits<NodeTy>::createSentinel();
91       ilist_traits<NodeTy>::noteHead(Head, Head);
92       ilist_traits<NodeTy>::setNext(Head, 0);
93       return Head;
94     }
95     return ilist_traits<NodeTy>::getPrev(Head);
96   }
97
98   /// noteHead - stash the sentinel into its default location
99   static void noteHead(NodeTy *NewHead, NodeTy *Sentinel) {
100     ilist_traits<NodeTy>::setPrev(NewHead, Sentinel);
101   }
102 };
103
104 /// ilist_node_traits - A fragment for template traits for intrusive list
105 /// that provides default node related operations.
106 ///
107 template<typename NodeTy>
108 struct ilist_node_traits {
109   static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); }
110   static void deleteNode(NodeTy *V) { delete V; }
111
112   void addNodeToList(NodeTy *) {}
113   void removeNodeFromList(NodeTy *) {}
114   void transferNodesFromList(ilist_node_traits &    /*SrcTraits*/,
115                              ilist_iterator<NodeTy> /*first*/,
116                              ilist_iterator<NodeTy> /*last*/) {}
117 };
118
119 /// ilist_default_traits - Default template traits for intrusive list.
120 /// By inheriting from this, you can easily use default implementations
121 /// for all common operations.
122 ///
123 template<typename NodeTy>
124 struct ilist_default_traits : ilist_nextprev_traits<NodeTy>,
125                               ilist_sentinel_traits<NodeTy>,
126                               ilist_node_traits<NodeTy> {
127 };
128
129 // Template traits for intrusive list.  By specializing this template class, you
130 // can change what next/prev fields are used to store the links...
131 template<typename NodeTy>
132 struct ilist_traits : ilist_default_traits<NodeTy> {};
133
134 // Const traits are the same as nonconst traits...
135 template<typename Ty>
136 struct ilist_traits<const Ty> : public ilist_traits<Ty> {};
137
138 //===----------------------------------------------------------------------===//
139 // ilist_iterator<Node> - Iterator for intrusive list.
140 //
141 template<typename NodeTy>
142 class ilist_iterator
143   : public bidirectional_iterator<NodeTy, ptrdiff_t> {
144
145 public:
146   typedef ilist_traits<NodeTy> Traits;
147   typedef bidirectional_iterator<NodeTy, ptrdiff_t> super;
148
149   typedef typename super::value_type value_type;
150   typedef typename super::difference_type difference_type;
151   typedef typename super::pointer pointer;
152   typedef typename super::reference reference;
153 private:
154   pointer NodePtr;
155
156   // ilist_iterator is not a random-access iterator, but it has an
157   // implicit conversion to pointer-type, which is. Declare (but
158   // don't define) these functions as private to help catch
159   // accidental misuse.
160   void operator[](difference_type) const;
161   void operator+(difference_type) const;
162   void operator-(difference_type) const;
163   void operator+=(difference_type) const;
164   void operator-=(difference_type) const;
165   template<class T> void operator<(T) const;
166   template<class T> void operator<=(T) const;
167   template<class T> void operator>(T) const;
168   template<class T> void operator>=(T) const;
169   template<class T> void operator-(T) const;
170 public:
171
172   ilist_iterator(pointer NP) : NodePtr(NP) {}
173   ilist_iterator(reference NR) : NodePtr(&NR) {}
174   ilist_iterator() : NodePtr(0) {}
175
176   // This is templated so that we can allow constructing a const iterator from
177   // a nonconst iterator...
178   template<class node_ty>
179   ilist_iterator(const ilist_iterator<node_ty> &RHS)
180     : NodePtr(RHS.getNodePtrUnchecked()) {}
181
182   // This is templated so that we can allow assigning to a const iterator from
183   // a nonconst iterator...
184   template<class node_ty>
185   const ilist_iterator &operator=(const ilist_iterator<node_ty> &RHS) {
186     NodePtr = RHS.getNodePtrUnchecked();
187     return *this;
188   }
189
190   // Accessors...
191   operator pointer() const {
192     return NodePtr;
193   }
194
195   reference operator*() const {
196     return *NodePtr;
197   }
198   pointer operator->() const { return &operator*(); }
199
200   // Comparison operators
201   bool operator==(const ilist_iterator &RHS) const {
202     return NodePtr == RHS.NodePtr;
203   }
204   bool operator!=(const ilist_iterator &RHS) const {
205     return NodePtr != RHS.NodePtr;
206   }
207
208   // Increment and decrement operators...
209   ilist_iterator &operator--() {      // predecrement - Back up
210     NodePtr = Traits::getPrev(NodePtr);
211     assert(NodePtr && "--'d off the beginning of an ilist!");
212     return *this;
213   }
214   ilist_iterator &operator++() {      // preincrement - Advance
215     NodePtr = Traits::getNext(NodePtr);
216     return *this;
217   }
218   ilist_iterator operator--(int) {    // postdecrement operators...
219     ilist_iterator tmp = *this;
220     --*this;
221     return tmp;
222   }
223   ilist_iterator operator++(int) {    // postincrement operators...
224     ilist_iterator tmp = *this;
225     ++*this;
226     return tmp;
227   }
228
229   // Internal interface, do not use...
230   pointer getNodePtrUnchecked() const { return NodePtr; }
231 };
232
233 // do not implement. this is to catch errors when people try to use
234 // them as random access iterators
235 template<typename T>
236 void operator-(int, ilist_iterator<T>);
237 template<typename T>
238 void operator-(ilist_iterator<T>,int);
239
240 template<typename T>
241 void operator+(int, ilist_iterator<T>);
242 template<typename T>
243 void operator+(ilist_iterator<T>,int);
244
245 // operator!=/operator== - Allow mixed comparisons without dereferencing
246 // the iterator, which could very likely be pointing to end().
247 template<typename T>
248 bool operator!=(const T* LHS, const ilist_iterator<const T> &RHS) {
249   return LHS != RHS.getNodePtrUnchecked();
250 }
251 template<typename T>
252 bool operator==(const T* LHS, const ilist_iterator<const T> &RHS) {
253   return LHS == RHS.getNodePtrUnchecked();
254 }
255 template<typename T>
256 bool operator!=(T* LHS, const ilist_iterator<T> &RHS) {
257   return LHS != RHS.getNodePtrUnchecked();
258 }
259 template<typename T>
260 bool operator==(T* LHS, const ilist_iterator<T> &RHS) {
261   return LHS == RHS.getNodePtrUnchecked();
262 }
263
264
265 // Allow ilist_iterators to convert into pointers to a node automatically when
266 // used by the dyn_cast, cast, isa mechanisms...
267
268 template<typename From> struct simplify_type;
269
270 template<typename NodeTy> struct simplify_type<ilist_iterator<NodeTy> > {
271   typedef NodeTy* SimpleType;
272
273   static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
274     return &*Node;
275   }
276 };
277 template<typename NodeTy> struct simplify_type<const ilist_iterator<NodeTy> > {
278   typedef NodeTy* SimpleType;
279
280   static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) {
281     return &*Node;
282   }
283 };
284
285
286 //===----------------------------------------------------------------------===//
287 //
288 /// iplist - The subset of list functionality that can safely be used on nodes
289 /// of polymorphic types, i.e. a heterogenous list with a common base class that
290 /// holds the next/prev pointers.  The only state of the list itself is a single
291 /// pointer to the head of the list.
292 ///
293 /// This list can be in one of three interesting states:
294 /// 1. The list may be completely unconstructed.  In this case, the head
295 ///    pointer is null.  When in this form, any query for an iterator (e.g.
296 ///    begin() or end()) causes the list to transparently change to state #2.
297 /// 2. The list may be empty, but contain a sentinel for the end iterator. This
298 ///    sentinel is created by the Traits::createSentinel method and is a link
299 ///    in the list.  When the list is empty, the pointer in the iplist points
300 ///    to the sentinel.  Once the sentinel is constructed, it
301 ///    is not destroyed until the list is.
302 /// 3. The list may contain actual objects in it, which are stored as a doubly
303 ///    linked list of nodes.  One invariant of the list is that the predecessor
304 ///    of the first node in the list always points to the last node in the list,
305 ///    and the successor pointer for the sentinel (which always stays at the
306 ///    end of the list) is always null.
307 ///
308 template<typename NodeTy, typename Traits=ilist_traits<NodeTy> >
309 class iplist : public Traits {
310   mutable NodeTy *Head;
311
312   // Use the prev node pointer of 'head' as the tail pointer.  This is really a
313   // circularly linked list where we snip the 'next' link from the sentinel node
314   // back to the first node in the list (to preserve assertions about going off
315   // the end of the list).
316   NodeTy *getTail() { return this->ensureHead(Head); }
317   const NodeTy *getTail() const { return this->ensureHead(Head); }
318   void setTail(NodeTy *N) const { this->noteHead(Head, N); }
319
320   /// CreateLazySentinel - This method verifies whether the sentinel for the
321   /// list has been created and lazily makes it if not.
322   void CreateLazySentinel() const {
323     this->ensureHead(Head);
324   }
325
326   static bool op_less(NodeTy &L, NodeTy &R) { return L < R; }
327   static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; }
328
329   // No fundamental reason why iplist can't be copyable, but the default
330   // copy/copy-assign won't do.
331   iplist(const iplist &);         // do not implement
332   void operator=(const iplist &); // do not implement
333
334 public:
335   typedef NodeTy *pointer;
336   typedef const NodeTy *const_pointer;
337   typedef NodeTy &reference;
338   typedef const NodeTy &const_reference;
339   typedef NodeTy value_type;
340   typedef ilist_iterator<NodeTy> iterator;
341   typedef ilist_iterator<const NodeTy> const_iterator;
342   typedef size_t size_type;
343   typedef ptrdiff_t difference_type;
344   typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
345   typedef std::reverse_iterator<iterator>  reverse_iterator;
346
347   iplist() : Head(this->provideInitialHead()) {}
348   ~iplist() {
349     if (!Head) return;
350     clear();
351     Traits::destroySentinel(getTail());
352   }
353
354   // Iterator creation methods.
355   iterator begin() {
356     CreateLazySentinel();
357     return iterator(Head);
358   }
359   const_iterator begin() const {
360     CreateLazySentinel();
361     return const_iterator(Head);
362   }
363   iterator end() {
364     CreateLazySentinel();
365     return iterator(getTail());
366   }
367   const_iterator end() const {
368     CreateLazySentinel();
369     return const_iterator(getTail());
370   }
371
372   // reverse iterator creation methods.
373   reverse_iterator rbegin()            { return reverse_iterator(end()); }
374   const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); }
375   reverse_iterator rend()              { return reverse_iterator(begin()); }
376   const_reverse_iterator rend() const { return const_reverse_iterator(begin());}
377
378
379   // Miscellaneous inspection routines.
380   size_type max_size() const { return size_type(-1); }
381   bool empty() const { return Head == 0 || Head == getTail(); }
382
383   // Front and back accessor functions...
384   reference front() {
385     assert(!empty() && "Called front() on empty list!");
386     return *Head;
387   }
388   const_reference front() const {
389     assert(!empty() && "Called front() on empty list!");
390     return *Head;
391   }
392   reference back() {
393     assert(!empty() && "Called back() on empty list!");
394     return *this->getPrev(getTail());
395   }
396   const_reference back() const {
397     assert(!empty() && "Called back() on empty list!");
398     return *this->getPrev(getTail());
399   }
400
401   void swap(iplist &RHS) {
402     assert(0 && "Swap does not use list traits callback correctly yet!");
403     std::swap(Head, RHS.Head);
404   }
405
406   iterator insert(iterator where, NodeTy *New) {
407     NodeTy *CurNode = where.getNodePtrUnchecked();
408     NodeTy *PrevNode = this->getPrev(CurNode);
409     this->setNext(New, CurNode);
410     this->setPrev(New, PrevNode);
411
412     if (CurNode != Head)  // Is PrevNode off the beginning of the list?
413       this->setNext(PrevNode, New);
414     else
415       Head = New;
416     this->setPrev(CurNode, New);
417
418     this->addNodeToList(New);  // Notify traits that we added a node...
419     return New;
420   }
421
422   iterator insertAfter(iterator where, NodeTy *New) {
423     if (empty())
424       return insert(begin(), New);
425     else
426       return insert(++where, New);
427   }
428
429   NodeTy *remove(iterator &IT) {
430     assert(IT != end() && "Cannot remove end of list!");
431     NodeTy *Node = &*IT;
432     NodeTy *NextNode = this->getNext(Node);
433     NodeTy *PrevNode = this->getPrev(Node);
434
435     if (Node != Head)  // Is PrevNode off the beginning of the list?
436       this->setNext(PrevNode, NextNode);
437     else
438       Head = NextNode;
439     this->setPrev(NextNode, PrevNode);
440     IT = NextNode;
441     this->removeNodeFromList(Node);  // Notify traits that we removed a node...
442
443     // Set the next/prev pointers of the current node to null.  This isn't
444     // strictly required, but this catches errors where a node is removed from
445     // an ilist (and potentially deleted) with iterators still pointing at it.
446     // When those iterators are incremented or decremented, they will assert on
447     // the null next/prev pointer instead of "usually working".
448     this->setNext(Node, 0);
449     this->setPrev(Node, 0);
450     return Node;
451   }
452
453   NodeTy *remove(const iterator &IT) {
454     iterator MutIt = IT;
455     return remove(MutIt);
456   }
457
458   // erase - remove a node from the controlled sequence... and delete it.
459   iterator erase(iterator where) {
460     this->deleteNode(remove(where));
461     return where;
462   }
463
464
465 private:
466   // transfer - The heart of the splice function.  Move linked list nodes from
467   // [first, last) into position.
468   //
469   void transfer(iterator position, iplist &L2, iterator first, iterator last) {
470     assert(first != last && "Should be checked by callers");
471
472     if (position != last) {
473       // Note: we have to be careful about the case when we move the first node
474       // in the list.  This node is the list sentinel node and we can't move it.
475       NodeTy *ThisSentinel = getTail();
476       setTail(0);
477       NodeTy *L2Sentinel = L2.getTail();
478       L2.setTail(0);
479
480       // Remove [first, last) from its old position.
481       NodeTy *First = &*first, *Prev = this->getPrev(First);
482       NodeTy *Next = last.getNodePtrUnchecked(), *Last = this->getPrev(Next);
483       if (Prev)
484         this->setNext(Prev, Next);
485       else
486         L2.Head = Next;
487       this->setPrev(Next, Prev);
488
489       // Splice [first, last) into its new position.
490       NodeTy *PosNext = position.getNodePtrUnchecked();
491       NodeTy *PosPrev = this->getPrev(PosNext);
492
493       // Fix head of list...
494       if (PosPrev)
495         this->setNext(PosPrev, First);
496       else
497         Head = First;
498       this->setPrev(First, PosPrev);
499
500       // Fix end of list...
501       this->setNext(Last, PosNext);
502       this->setPrev(PosNext, Last);
503
504       this->transferNodesFromList(L2, First, PosNext);
505
506       // Now that everything is set, restore the pointers to the list sentinels.
507       L2.setTail(L2Sentinel);
508       setTail(ThisSentinel);
509     }
510   }
511
512 public:
513
514   //===----------------------------------------------------------------------===
515   // Functionality derived from other functions defined above...
516   //
517
518   size_type size() const {
519     if (Head == 0) return 0; // Don't require construction of sentinel if empty.
520     return std::distance(begin(), end());
521   }
522
523   iterator erase(iterator first, iterator last) {
524     while (first != last)
525       first = erase(first);
526     return last;
527   }
528
529   void clear() { if (Head) erase(begin(), end()); }
530
531   // Front and back inserters...
532   void push_front(NodeTy *val) { insert(begin(), val); }
533   void push_back(NodeTy *val) { insert(end(), val); }
534   void pop_front() {
535     assert(!empty() && "pop_front() on empty list!");
536     erase(begin());
537   }
538   void pop_back() {
539     assert(!empty() && "pop_back() on empty list!");
540     iterator t = end(); erase(--t);
541   }
542
543   // Special forms of insert...
544   template<class InIt> void insert(iterator where, InIt first, InIt last) {
545     for (; first != last; ++first) insert(where, *first);
546   }
547
548   // Splice members - defined in terms of transfer...
549   void splice(iterator where, iplist &L2) {
550     if (!L2.empty())
551       transfer(where, L2, L2.begin(), L2.end());
552   }
553   void splice(iterator where, iplist &L2, iterator first) {
554     iterator last = first; ++last;
555     if (where == first || where == last) return; // No change
556     transfer(where, L2, first, last);
557   }
558   void splice(iterator where, iplist &L2, iterator first, iterator last) {
559     if (first != last) transfer(where, L2, first, last);
560   }
561
562
563
564   //===----------------------------------------------------------------------===
565   // High-Level Functionality that shouldn't really be here, but is part of list
566   //
567
568   // These two functions are actually called remove/remove_if in list<>, but
569   // they actually do the job of erase, rename them accordingly.
570   //
571   void erase(const NodeTy &val) {
572     for (iterator I = begin(), E = end(); I != E; ) {
573       iterator next = I; ++next;
574       if (*I == val) erase(I);
575       I = next;
576     }
577   }
578   template<class Pr1> void erase_if(Pr1 pred) {
579     for (iterator I = begin(), E = end(); I != E; ) {
580       iterator next = I; ++next;
581       if (pred(*I)) erase(I);
582       I = next;
583     }
584   }
585
586   template<class Pr2> void unique(Pr2 pred) {
587     if (empty()) return;
588     for (iterator I = begin(), E = end(), Next = begin(); ++Next != E;) {
589       if (pred(*I))
590         erase(Next);
591       else
592         I = Next;
593       Next = I;
594     }
595   }
596   void unique() { unique(op_equal); }
597
598   template<class Pr3> void merge(iplist &right, Pr3 pred) {
599     iterator first1 = begin(), last1 = end();
600     iterator first2 = right.begin(), last2 = right.end();
601     while (first1 != last1 && first2 != last2)
602       if (pred(*first2, *first1)) {
603         iterator next = first2;
604         transfer(first1, right, first2, ++next);
605         first2 = next;
606       } else {
607         ++first1;
608       }
609     if (first2 != last2) transfer(last1, right, first2, last2);
610   }
611   void merge(iplist &right) { return merge(right, op_less); }
612
613   template<class Pr3> void sort(Pr3 pred);
614   void sort() { sort(op_less); }
615   void reverse();
616 };
617
618
619 template<typename NodeTy>
620 struct ilist : public iplist<NodeTy> {
621   typedef typename iplist<NodeTy>::size_type size_type;
622   typedef typename iplist<NodeTy>::iterator iterator;
623
624   ilist() {}
625   ilist(const ilist &right) {
626     insert(this->begin(), right.begin(), right.end());
627   }
628   explicit ilist(size_type count) {
629     insert(this->begin(), count, NodeTy());
630   }
631   ilist(size_type count, const NodeTy &val) {
632     insert(this->begin(), count, val);
633   }
634   template<class InIt> ilist(InIt first, InIt last) {
635     insert(this->begin(), first, last);
636   }
637
638   // bring hidden functions into scope
639   using iplist<NodeTy>::insert;
640   using iplist<NodeTy>::push_front;
641   using iplist<NodeTy>::push_back;
642
643   // Main implementation here - Insert for a node passed by value...
644   iterator insert(iterator where, const NodeTy &val) {
645     return insert(where, createNode(val));
646   }
647
648
649   // Front and back inserters...
650   void push_front(const NodeTy &val) { insert(this->begin(), val); }
651   void push_back(const NodeTy &val) { insert(this->end(), val); }
652
653   // Special forms of insert...
654   template<class InIt> void insert(iterator where, InIt first, InIt last) {
655     for (; first != last; ++first) insert(where, *first);
656   }
657   void insert(iterator where, size_type count, const NodeTy &val) {
658     for (; count != 0; --count) insert(where, val);
659   }
660
661   // Assign special forms...
662   void assign(size_type count, const NodeTy &val) {
663     iterator I = this->begin();
664     for (; I != this->end() && count != 0; ++I, --count)
665       *I = val;
666     if (count != 0)
667       insert(this->end(), val, val);
668     else
669       erase(I, this->end());
670   }
671   template<class InIt> void assign(InIt first1, InIt last1) {
672     iterator first2 = this->begin(), last2 = this->end();
673     for ( ; first1 != last1 && first2 != last2; ++first1, ++first2)
674       *first1 = *first2;
675     if (first2 == last2)
676       erase(first1, last1);
677     else
678       insert(last1, first2, last2);
679   }
680
681
682   // Resize members...
683   void resize(size_type newsize, NodeTy val) {
684     iterator i = this->begin();
685     size_type len = 0;
686     for ( ; i != this->end() && len < newsize; ++i, ++len) /* empty*/ ;
687
688     if (len == newsize)
689       erase(i, this->end());
690     else                                          // i == end()
691       insert(this->end(), newsize - len, val);
692   }
693   void resize(size_type newsize) { resize(newsize, NodeTy()); }
694 };
695
696 } // End llvm namespace
697
698 namespace std {
699   // Ensure that swap uses the fast list swap...
700   template<class Ty>
701   void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) {
702     Left.swap(Right);
703   }
704 }  // End 'std' extensions...
705
706 #endif // LLVM_ADT_ILIST_H