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