ilist.h revision 208954
1258945Sroberto//==-- llvm/ADT/ilist.h - Intrusive Linked List Template ---------*- C++ -*-==// 2280849Scy// 3258945Sroberto// The LLVM Compiler Infrastructure 4258945Sroberto// 5258945Sroberto// This file is distributed under the University of Illinois Open Source 6258945Sroberto// License. See LICENSE.TXT for details. 7258945Sroberto// 8258945Sroberto//===----------------------------------------------------------------------===// 9258945Sroberto// 10258945Sroberto// This file defines classes to implement an intrusive doubly linked list class 11258945Sroberto// (i.e. each node of the list must contain a next and previous field for the 12258945Sroberto// list. 13258945Sroberto// 14258945Sroberto// The ilist_traits trait class is used to gain access to the next and previous 15258945Sroberto// fields of the node type that the list is instantiated with. If it is not 16258945Sroberto// specialized, the list defaults to using the getPrev(), getNext() method calls 17258945Sroberto// to get the next and previous pointers. 18280849Scy// 19258945Sroberto// The ilist class itself, should be a plug in replacement for list, assuming 20258945Sroberto// that the nodes contain next/prev pointers. This list replacement does not 21258945Sroberto// provide a constant time size() method, so be careful to use empty() when you 22258945Sroberto// really want to know if it's empty. 23258945Sroberto// 24258945Sroberto// The ilist class is implemented by allocating a 'tail' node when the list is 25258945Sroberto// created (using ilist_traits<>::createSentinel()). This tail node is 26258945Sroberto// absolutely required because the user must be able to compute end()-1. Because 27258945Sroberto// of this, users of the direct next/prev links will see an extra link on the 28258945Sroberto// end of the list, which should be ignored. 29258945Sroberto// 30258945Sroberto// Requirements for a user of this list: 31258945Sroberto// 32258945Sroberto// 1. The user must provide {g|s}et{Next|Prev} methods, or specialize 33258945Sroberto// ilist_traits to provide an alternate way of getting and setting next and 34258945Sroberto// prev links. 35258945Sroberto// 36258945Sroberto//===----------------------------------------------------------------------===// 37258945Sroberto 38258945Sroberto#ifndef LLVM_ADT_ILIST_H 39258945Sroberto#define LLVM_ADT_ILIST_H 40258945Sroberto 41258945Sroberto#include <cassert> 42258945Sroberto#include <iterator> 43258945Sroberto 44258945Srobertonamespace llvm { 45258945Sroberto 46258945Srobertotemplate<typename NodeTy, typename Traits> class iplist; 47258945Srobertotemplate<typename NodeTy> class ilist_iterator; 48258945Sroberto 49258945Sroberto/// ilist_nextprev_traits - A fragment for template traits for intrusive list 50258945Sroberto/// that provides default next/prev implementations for common operations. 51258945Sroberto/// 52258945Srobertotemplate<typename NodeTy> 53258945Srobertostruct ilist_nextprev_traits { 54258945Sroberto static NodeTy *getPrev(NodeTy *N) { return N->getPrev(); } 55258945Sroberto static NodeTy *getNext(NodeTy *N) { return N->getNext(); } 56258945Sroberto static const NodeTy *getPrev(const NodeTy *N) { return N->getPrev(); } 57258945Sroberto static const NodeTy *getNext(const NodeTy *N) { return N->getNext(); } 58258945Sroberto 59258945Sroberto static void setPrev(NodeTy *N, NodeTy *Prev) { N->setPrev(Prev); } 60258945Sroberto static void setNext(NodeTy *N, NodeTy *Next) { N->setNext(Next); } 61258945Sroberto}; 62258945Sroberto 63258945Srobertotemplate<typename NodeTy> 64258945Srobertostruct ilist_traits; 65258945Sroberto 66258945Sroberto/// ilist_sentinel_traits - A fragment for template traits for intrusive list 67258945Sroberto/// that provides default sentinel implementations for common operations. 68258945Sroberto/// 69258945Sroberto/// ilist_sentinel_traits implements a lazy dynamic sentinel allocation 70258945Sroberto/// strategy. The sentinel is stored in the prev field of ilist's Head. 71258945Sroberto/// 72258945Srobertotemplate<typename NodeTy> 73258945Srobertostruct ilist_sentinel_traits { 74258945Sroberto /// createSentinel - create the dynamic sentinel 75258945Sroberto static NodeTy *createSentinel() { return new NodeTy(); } 76258945Sroberto 77258945Sroberto /// destroySentinel - deallocate the dynamic sentinel 78258945Sroberto static void destroySentinel(NodeTy *N) { delete N; } 79258945Sroberto 80258945Sroberto /// provideInitialHead - when constructing an ilist, provide a starting 81258945Sroberto /// value for its Head 82258945Sroberto /// @return null node to indicate that it needs to be allocated later 83258945Sroberto static NodeTy *provideInitialHead() { return 0; } 84258945Sroberto 85258945Sroberto /// ensureHead - make sure that Head is either already 86258945Sroberto /// initialized or assigned a fresh sentinel 87258945Sroberto /// @return the sentinel 88258945Sroberto static NodeTy *ensureHead(NodeTy *&Head) { 89258945Sroberto if (!Head) { 90258945Sroberto Head = ilist_traits<NodeTy>::createSentinel(); 91258945Sroberto ilist_traits<NodeTy>::noteHead(Head, Head); 92258945Sroberto ilist_traits<NodeTy>::setNext(Head, 0); 93258945Sroberto return Head; 94258945Sroberto } 95258945Sroberto return ilist_traits<NodeTy>::getPrev(Head); 96258945Sroberto } 97258945Sroberto 98258945Sroberto /// noteHead - stash the sentinel into its default location 99258945Sroberto static void noteHead(NodeTy *NewHead, NodeTy *Sentinel) { 100258945Sroberto ilist_traits<NodeTy>::setPrev(NewHead, Sentinel); 101258945Sroberto } 102258945Sroberto}; 103258945Sroberto 104258945Sroberto/// ilist_node_traits - A fragment for template traits for intrusive list 105258945Sroberto/// that provides default node related operations. 106258945Sroberto/// 107258945Srobertotemplate<typename NodeTy> 108258945Srobertostruct ilist_node_traits { 109258945Sroberto static NodeTy *createNode(const NodeTy &V) { return new NodeTy(V); } 110258945Sroberto static void deleteNode(NodeTy *V) { delete V; } 111258945Sroberto 112258945Sroberto void addNodeToList(NodeTy *) {} 113258945Sroberto void removeNodeFromList(NodeTy *) {} 114258945Sroberto void transferNodesFromList(ilist_node_traits & /*SrcTraits*/, 115258945Sroberto ilist_iterator<NodeTy> /*first*/, 116258945Sroberto ilist_iterator<NodeTy> /*last*/) {} 117258945Sroberto}; 118258945Sroberto 119258945Sroberto/// ilist_default_traits - Default template traits for intrusive list. 120258945Sroberto/// By inheriting from this, you can easily use default implementations 121258945Sroberto/// for all common operations. 122258945Sroberto/// 123258945Srobertotemplate<typename NodeTy> 124258945Srobertostruct ilist_default_traits : public ilist_nextprev_traits<NodeTy>, 125258945Sroberto public ilist_sentinel_traits<NodeTy>, 126258945Sroberto public ilist_node_traits<NodeTy> { 127258945Sroberto}; 128258945Sroberto 129258945Sroberto// Template traits for intrusive list. By specializing this template class, you 130258945Sroberto// can change what next/prev fields are used to store the links... 131258945Srobertotemplate<typename NodeTy> 132258945Srobertostruct ilist_traits : public ilist_default_traits<NodeTy> {}; 133258945Sroberto 134258945Sroberto// Const traits are the same as nonconst traits... 135258945Srobertotemplate<typename Ty> 136258945Srobertostruct ilist_traits<const Ty> : public ilist_traits<Ty> {}; 137258945Sroberto 138258945Sroberto//===----------------------------------------------------------------------===// 139258945Sroberto// ilist_iterator<Node> - Iterator for intrusive list. 140258945Sroberto// 141258945Srobertotemplate<typename NodeTy> 142258945Srobertoclass ilist_iterator 143258945Sroberto : public std::iterator<std::bidirectional_iterator_tag, NodeTy, ptrdiff_t> { 144258945Sroberto 145258945Srobertopublic: 146258945Sroberto typedef ilist_traits<NodeTy> Traits; 147258945Sroberto typedef std::iterator<std::bidirectional_iterator_tag, 148258945Sroberto NodeTy, ptrdiff_t> super; 149280849Scy 150280849Scy typedef typename super::value_type value_type; 151280849Scy typedef typename super::difference_type difference_type; 152280849Scy typedef typename super::pointer pointer; 153280849Scy typedef typename super::reference reference; 154258945Srobertoprivate: 155258945Sroberto pointer NodePtr; 156258945Sroberto 157258945Sroberto // ilist_iterator is not a random-access iterator, but it has an 158258945Sroberto // implicit conversion to pointer-type, which is. Declare (but 159258945Sroberto // don't define) these functions as private to help catch 160258945Sroberto // accidental misuse. 161258945Sroberto void operator[](difference_type) const; 162258945Sroberto void operator+(difference_type) const; 163258945Sroberto void operator-(difference_type) const; 164258945Sroberto void operator+=(difference_type) const; 165258945Sroberto void operator-=(difference_type) const; 166258945Sroberto template<class T> void operator<(T) const; 167258945Sroberto template<class T> void operator<=(T) const; 168258945Sroberto template<class T> void operator>(T) const; 169258945Sroberto template<class T> void operator>=(T) const; 170258945Sroberto template<class T> void operator-(T) const; 171258945Srobertopublic: 172258945Sroberto 173258945Sroberto ilist_iterator(pointer NP) : NodePtr(NP) {} 174258945Sroberto ilist_iterator(reference NR) : NodePtr(&NR) {} 175258945Sroberto ilist_iterator() : NodePtr(0) {} 176258945Sroberto 177258945Sroberto // This is templated so that we can allow constructing a const iterator from 178258945Sroberto // a nonconst iterator... 179258945Sroberto template<class node_ty> 180258945Sroberto ilist_iterator(const ilist_iterator<node_ty> &RHS) 181258945Sroberto : NodePtr(RHS.getNodePtrUnchecked()) {} 182258945Sroberto 183258945Sroberto // This is templated so that we can allow assigning to a const iterator from 184258945Sroberto // a nonconst iterator... 185258945Sroberto template<class node_ty> 186258945Sroberto const ilist_iterator &operator=(const ilist_iterator<node_ty> &RHS) { 187258945Sroberto NodePtr = RHS.getNodePtrUnchecked(); 188258945Sroberto return *this; 189258945Sroberto } 190258945Sroberto 191258945Sroberto // Accessors... 192258945Sroberto operator pointer() const { 193258945Sroberto return NodePtr; 194258945Sroberto } 195258945Sroberto 196258945Sroberto reference operator*() const { 197258945Sroberto return *NodePtr; 198258945Sroberto } 199258945Sroberto pointer operator->() const { return &operator*(); } 200258945Sroberto 201258945Sroberto // Comparison operators 202258945Sroberto bool operator==(const ilist_iterator &RHS) const { 203258945Sroberto return NodePtr == RHS.NodePtr; 204258945Sroberto } 205258945Sroberto bool operator!=(const ilist_iterator &RHS) const { 206258945Sroberto return NodePtr != RHS.NodePtr; 207258945Sroberto } 208258945Sroberto 209258945Sroberto // Increment and decrement operators... 210258945Sroberto ilist_iterator &operator--() { // predecrement - Back up 211258945Sroberto NodePtr = Traits::getPrev(NodePtr); 212258945Sroberto assert(NodePtr && "--'d off the beginning of an ilist!"); 213258945Sroberto return *this; 214258945Sroberto } 215258945Sroberto ilist_iterator &operator++() { // preincrement - Advance 216258945Sroberto NodePtr = Traits::getNext(NodePtr); 217258945Sroberto return *this; 218258945Sroberto } 219258945Sroberto ilist_iterator operator--(int) { // postdecrement operators... 220258945Sroberto ilist_iterator tmp = *this; 221258945Sroberto --*this; 222258945Sroberto return tmp; 223280849Scy } 224280849Scy ilist_iterator operator++(int) { // postincrement operators... 225280849Scy ilist_iterator tmp = *this; 226280849Scy ++*this; 227280849Scy return tmp; 228280849Scy } 229280849Scy 230280849Scy // Internal interface, do not use... 231280849Scy pointer getNodePtrUnchecked() const { return NodePtr; } 232280849Scy}; 233280849Scy 234280849Scy// do not implement. this is to catch errors when people try to use 235258945Sroberto// them as random access iterators 236258945Srobertotemplate<typename T> 237258945Srobertovoid operator-(int, ilist_iterator<T>); 238258945Srobertotemplate<typename T> 239258945Srobertovoid operator-(ilist_iterator<T>,int); 240258945Sroberto 241258945Srobertotemplate<typename T> 242258945Srobertovoid operator+(int, ilist_iterator<T>); 243258945Srobertotemplate<typename T> 244258945Srobertovoid operator+(ilist_iterator<T>,int); 245258945Sroberto 246258945Sroberto// operator!=/operator== - Allow mixed comparisons without dereferencing 247258945Sroberto// the iterator, which could very likely be pointing to end(). 248258945Srobertotemplate<typename T> 249258945Srobertobool operator!=(const T* LHS, const ilist_iterator<const T> &RHS) { 250258945Sroberto return LHS != RHS.getNodePtrUnchecked(); 251258945Sroberto} 252258945Srobertotemplate<typename T> 253258945Srobertobool operator==(const T* LHS, const ilist_iterator<const T> &RHS) { 254258945Sroberto return LHS == RHS.getNodePtrUnchecked(); 255258945Sroberto} 256258945Srobertotemplate<typename T> 257258945Srobertobool operator!=(T* LHS, const ilist_iterator<T> &RHS) { 258258945Sroberto return LHS != RHS.getNodePtrUnchecked(); 259258945Sroberto} 260258945Srobertotemplate<typename T> 261258945Srobertobool operator==(T* LHS, const ilist_iterator<T> &RHS) { 262258945Sroberto return LHS == RHS.getNodePtrUnchecked(); 263258945Sroberto} 264258945Sroberto 265258945Sroberto 266258945Sroberto// Allow ilist_iterators to convert into pointers to a node automatically when 267258945Sroberto// used by the dyn_cast, cast, isa mechanisms... 268258945Sroberto 269258945Srobertotemplate<typename From> struct simplify_type; 270258945Sroberto 271258945Srobertotemplate<typename NodeTy> struct simplify_type<ilist_iterator<NodeTy> > { 272258945Sroberto typedef NodeTy* SimpleType; 273258945Sroberto 274258945Sroberto static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) { 275258945Sroberto return &*Node; 276258945Sroberto } 277258945Sroberto}; 278258945Srobertotemplate<typename NodeTy> struct simplify_type<const ilist_iterator<NodeTy> > { 279258945Sroberto typedef NodeTy* SimpleType; 280258945Sroberto 281258945Sroberto static SimpleType getSimplifiedValue(const ilist_iterator<NodeTy> &Node) { 282258945Sroberto return &*Node; 283258945Sroberto } 284258945Sroberto}; 285258945Sroberto 286258945Sroberto 287258945Sroberto//===----------------------------------------------------------------------===// 288258945Sroberto// 289258945Sroberto/// iplist - The subset of list functionality that can safely be used on nodes 290258945Sroberto/// of polymorphic types, i.e. a heterogenous list with a common base class that 291258945Sroberto/// holds the next/prev pointers. The only state of the list itself is a single 292258945Sroberto/// pointer to the head of the list. 293258945Sroberto/// 294258945Sroberto/// This list can be in one of three interesting states: 295258945Sroberto/// 1. The list may be completely unconstructed. In this case, the head 296258945Sroberto/// pointer is null. When in this form, any query for an iterator (e.g. 297258945Sroberto/// begin() or end()) causes the list to transparently change to state #2. 298258945Sroberto/// 2. The list may be empty, but contain a sentinel for the end iterator. This 299258945Sroberto/// sentinel is created by the Traits::createSentinel method and is a link 300258945Sroberto/// in the list. When the list is empty, the pointer in the iplist points 301258945Sroberto/// to the sentinel. Once the sentinel is constructed, it 302258945Sroberto/// is not destroyed until the list is. 303258945Sroberto/// 3. The list may contain actual objects in it, which are stored as a doubly 304280849Scy/// linked list of nodes. One invariant of the list is that the predecessor 305280849Scy/// of the first node in the list always points to the last node in the list, 306280849Scy/// and the successor pointer for the sentinel (which always stays at the 307280849Scy/// end of the list) is always null. 308280849Scy/// 309280849Scytemplate<typename NodeTy, typename Traits=ilist_traits<NodeTy> > 310280849Scyclass iplist : public Traits { 311280849Scy mutable NodeTy *Head; 312280849Scy 313280849Scy // Use the prev node pointer of 'head' as the tail pointer. This is really a 314280849Scy // circularly linked list where we snip the 'next' link from the sentinel node 315258945Sroberto // back to the first node in the list (to preserve assertions about going off 316258945Sroberto // the end of the list). 317258945Sroberto NodeTy *getTail() { return this->ensureHead(Head); } 318258945Sroberto const NodeTy *getTail() const { return this->ensureHead(Head); } 319258945Sroberto void setTail(NodeTy *N) const { this->noteHead(Head, N); } 320258945Sroberto 321258945Sroberto /// CreateLazySentinel - This method verifies whether the sentinel for the 322258945Sroberto /// list has been created and lazily makes it if not. 323258945Sroberto void CreateLazySentinel() const { 324258945Sroberto this->ensureHead(Head); 325258945Sroberto } 326258945Sroberto 327258945Sroberto static bool op_less(NodeTy &L, NodeTy &R) { return L < R; } 328258945Sroberto static bool op_equal(NodeTy &L, NodeTy &R) { return L == R; } 329258945Sroberto 330258945Sroberto // No fundamental reason why iplist can't be copyable, but the default 331258945Sroberto // copy/copy-assign won't do. 332258945Sroberto iplist(const iplist &); // do not implement 333258945Sroberto void operator=(const iplist &); // do not implement 334258945Sroberto 335258945Srobertopublic: 336258945Sroberto typedef NodeTy *pointer; 337258945Sroberto typedef const NodeTy *const_pointer; 338258945Sroberto typedef NodeTy &reference; 339258945Sroberto typedef const NodeTy &const_reference; 340258945Sroberto typedef NodeTy value_type; 341258945Sroberto typedef ilist_iterator<NodeTy> iterator; 342258945Sroberto typedef ilist_iterator<const NodeTy> const_iterator; 343258945Sroberto typedef size_t size_type; 344258945Sroberto typedef ptrdiff_t difference_type; 345258945Sroberto typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 346258945Sroberto typedef std::reverse_iterator<iterator> reverse_iterator; 347258945Sroberto 348258945Sroberto iplist() : Head(this->provideInitialHead()) {} 349258945Sroberto ~iplist() { 350258945Sroberto if (!Head) return; 351258945Sroberto clear(); 352258945Sroberto Traits::destroySentinel(getTail()); 353258945Sroberto } 354258945Sroberto 355258945Sroberto // Iterator creation methods. 356258945Sroberto iterator begin() { 357258945Sroberto CreateLazySentinel(); 358258945Sroberto return iterator(Head); 359258945Sroberto } 360258945Sroberto const_iterator begin() const { 361258945Sroberto CreateLazySentinel(); 362258945Sroberto return const_iterator(Head); 363258945Sroberto } 364258945Sroberto iterator end() { 365258945Sroberto CreateLazySentinel(); 366258945Sroberto return iterator(getTail()); 367258945Sroberto } 368 const_iterator end() const { 369 CreateLazySentinel(); 370 return const_iterator(getTail()); 371 } 372 373 // reverse iterator creation methods. 374 reverse_iterator rbegin() { return reverse_iterator(end()); } 375 const_reverse_iterator rbegin() const{ return const_reverse_iterator(end()); } 376 reverse_iterator rend() { return reverse_iterator(begin()); } 377 const_reverse_iterator rend() const { return const_reverse_iterator(begin());} 378 379 380 // Miscellaneous inspection routines. 381 size_type max_size() const { return size_type(-1); } 382 bool empty() const { return Head == 0 || Head == getTail(); } 383 384 // Front and back accessor functions... 385 reference front() { 386 assert(!empty() && "Called front() on empty list!"); 387 return *Head; 388 } 389 const_reference front() const { 390 assert(!empty() && "Called front() on empty list!"); 391 return *Head; 392 } 393 reference back() { 394 assert(!empty() && "Called back() on empty list!"); 395 return *this->getPrev(getTail()); 396 } 397 const_reference back() const { 398 assert(!empty() && "Called back() on empty list!"); 399 return *this->getPrev(getTail()); 400 } 401 402 void swap(iplist &RHS) { 403 assert(0 && "Swap does not use list traits callback correctly yet!"); 404 std::swap(Head, RHS.Head); 405 } 406 407 iterator insert(iterator where, NodeTy *New) { 408 NodeTy *CurNode = where.getNodePtrUnchecked(); 409 NodeTy *PrevNode = this->getPrev(CurNode); 410 this->setNext(New, CurNode); 411 this->setPrev(New, PrevNode); 412 413 if (CurNode != Head) // Is PrevNode off the beginning of the list? 414 this->setNext(PrevNode, New); 415 else 416 Head = New; 417 this->setPrev(CurNode, New); 418 419 this->addNodeToList(New); // Notify traits that we added a node... 420 return New; 421 } 422 423 iterator insertAfter(iterator where, NodeTy *New) { 424 if (empty()) 425 return insert(begin(), New); 426 else 427 return insert(++where, New); 428 } 429 430 NodeTy *remove(iterator &IT) { 431 assert(IT != end() && "Cannot remove end of list!"); 432 NodeTy *Node = &*IT; 433 NodeTy *NextNode = this->getNext(Node); 434 NodeTy *PrevNode = this->getPrev(Node); 435 436 if (Node != Head) // Is PrevNode off the beginning of the list? 437 this->setNext(PrevNode, NextNode); 438 else 439 Head = NextNode; 440 this->setPrev(NextNode, PrevNode); 441 IT = NextNode; 442 this->removeNodeFromList(Node); // Notify traits that we removed a node... 443 444 // Set the next/prev pointers of the current node to null. This isn't 445 // strictly required, but this catches errors where a node is removed from 446 // an ilist (and potentially deleted) with iterators still pointing at it. 447 // When those iterators are incremented or decremented, they will assert on 448 // the null next/prev pointer instead of "usually working". 449 this->setNext(Node, 0); 450 this->setPrev(Node, 0); 451 return Node; 452 } 453 454 NodeTy *remove(const iterator &IT) { 455 iterator MutIt = IT; 456 return remove(MutIt); 457 } 458 459 // erase - remove a node from the controlled sequence... and delete it. 460 iterator erase(iterator where) { 461 this->deleteNode(remove(where)); 462 return where; 463 } 464 465 466private: 467 // transfer - The heart of the splice function. Move linked list nodes from 468 // [first, last) into position. 469 // 470 void transfer(iterator position, iplist &L2, iterator first, iterator last) { 471 assert(first != last && "Should be checked by callers"); 472 473 if (position != last) { 474 // Note: we have to be careful about the case when we move the first node 475 // in the list. This node is the list sentinel node and we can't move it. 476 NodeTy *ThisSentinel = getTail(); 477 setTail(0); 478 NodeTy *L2Sentinel = L2.getTail(); 479 L2.setTail(0); 480 481 // Remove [first, last) from its old position. 482 NodeTy *First = &*first, *Prev = this->getPrev(First); 483 NodeTy *Next = last.getNodePtrUnchecked(), *Last = this->getPrev(Next); 484 if (Prev) 485 this->setNext(Prev, Next); 486 else 487 L2.Head = Next; 488 this->setPrev(Next, Prev); 489 490 // Splice [first, last) into its new position. 491 NodeTy *PosNext = position.getNodePtrUnchecked(); 492 NodeTy *PosPrev = this->getPrev(PosNext); 493 494 // Fix head of list... 495 if (PosPrev) 496 this->setNext(PosPrev, First); 497 else 498 Head = First; 499 this->setPrev(First, PosPrev); 500 501 // Fix end of list... 502 this->setNext(Last, PosNext); 503 this->setPrev(PosNext, Last); 504 505 this->transferNodesFromList(L2, First, PosNext); 506 507 // Now that everything is set, restore the pointers to the list sentinels. 508 L2.setTail(L2Sentinel); 509 setTail(ThisSentinel); 510 } 511 } 512 513public: 514 515 //===----------------------------------------------------------------------=== 516 // Functionality derived from other functions defined above... 517 // 518 519 size_type size() const { 520 if (Head == 0) return 0; // Don't require construction of sentinel if empty. 521 return std::distance(begin(), end()); 522 } 523 524 iterator erase(iterator first, iterator last) { 525 while (first != last) 526 first = erase(first); 527 return last; 528 } 529 530 void clear() { if (Head) erase(begin(), end()); } 531 532 // Front and back inserters... 533 void push_front(NodeTy *val) { insert(begin(), val); } 534 void push_back(NodeTy *val) { insert(end(), val); } 535 void pop_front() { 536 assert(!empty() && "pop_front() on empty list!"); 537 erase(begin()); 538 } 539 void pop_back() { 540 assert(!empty() && "pop_back() on empty list!"); 541 iterator t = end(); erase(--t); 542 } 543 544 // Special forms of insert... 545 template<class InIt> void insert(iterator where, InIt first, InIt last) { 546 for (; first != last; ++first) insert(where, *first); 547 } 548 549 // Splice members - defined in terms of transfer... 550 void splice(iterator where, iplist &L2) { 551 if (!L2.empty()) 552 transfer(where, L2, L2.begin(), L2.end()); 553 } 554 void splice(iterator where, iplist &L2, iterator first) { 555 iterator last = first; ++last; 556 if (where == first || where == last) return; // No change 557 transfer(where, L2, first, last); 558 } 559 void splice(iterator where, iplist &L2, iterator first, iterator last) { 560 if (first != last) transfer(where, L2, first, last); 561 } 562 563 564 565 //===----------------------------------------------------------------------=== 566 // High-Level Functionality that shouldn't really be here, but is part of list 567 // 568 569 // These two functions are actually called remove/remove_if in list<>, but 570 // they actually do the job of erase, rename them accordingly. 571 // 572 void erase(const NodeTy &val) { 573 for (iterator I = begin(), E = end(); I != E; ) { 574 iterator next = I; ++next; 575 if (*I == val) erase(I); 576 I = next; 577 } 578 } 579 template<class Pr1> void erase_if(Pr1 pred) { 580 for (iterator I = begin(), E = end(); I != E; ) { 581 iterator next = I; ++next; 582 if (pred(*I)) erase(I); 583 I = next; 584 } 585 } 586 587 template<class Pr2> void unique(Pr2 pred) { 588 if (empty()) return; 589 for (iterator I = begin(), E = end(), Next = begin(); ++Next != E;) { 590 if (pred(*I)) 591 erase(Next); 592 else 593 I = Next; 594 Next = I; 595 } 596 } 597 void unique() { unique(op_equal); } 598 599 template<class Pr3> void merge(iplist &right, Pr3 pred) { 600 iterator first1 = begin(), last1 = end(); 601 iterator first2 = right.begin(), last2 = right.end(); 602 while (first1 != last1 && first2 != last2) 603 if (pred(*first2, *first1)) { 604 iterator next = first2; 605 transfer(first1, right, first2, ++next); 606 first2 = next; 607 } else { 608 ++first1; 609 } 610 if (first2 != last2) transfer(last1, right, first2, last2); 611 } 612 void merge(iplist &right) { return merge(right, op_less); } 613 614 template<class Pr3> void sort(Pr3 pred); 615 void sort() { sort(op_less); } 616 void reverse(); 617}; 618 619 620template<typename NodeTy> 621struct ilist : public iplist<NodeTy> { 622 typedef typename iplist<NodeTy>::size_type size_type; 623 typedef typename iplist<NodeTy>::iterator iterator; 624 625 ilist() {} 626 ilist(const ilist &right) { 627 insert(this->begin(), right.begin(), right.end()); 628 } 629 explicit ilist(size_type count) { 630 insert(this->begin(), count, NodeTy()); 631 } 632 ilist(size_type count, const NodeTy &val) { 633 insert(this->begin(), count, val); 634 } 635 template<class InIt> ilist(InIt first, InIt last) { 636 insert(this->begin(), first, last); 637 } 638 639 // bring hidden functions into scope 640 using iplist<NodeTy>::insert; 641 using iplist<NodeTy>::push_front; 642 using iplist<NodeTy>::push_back; 643 644 // Main implementation here - Insert for a node passed by value... 645 iterator insert(iterator where, const NodeTy &val) { 646 return insert(where, this->createNode(val)); 647 } 648 649 650 // Front and back inserters... 651 void push_front(const NodeTy &val) { insert(this->begin(), val); } 652 void push_back(const NodeTy &val) { insert(this->end(), val); } 653 654 // Special forms of insert... 655 template<class InIt> void insert(iterator where, InIt first, InIt last) { 656 for (; first != last; ++first) insert(where, *first); 657 } 658 void insert(iterator where, size_type count, const NodeTy &val) { 659 for (; count != 0; --count) insert(where, val); 660 } 661 662 // Assign special forms... 663 void assign(size_type count, const NodeTy &val) { 664 iterator I = this->begin(); 665 for (; I != this->end() && count != 0; ++I, --count) 666 *I = val; 667 if (count != 0) 668 insert(this->end(), val, val); 669 else 670 erase(I, this->end()); 671 } 672 template<class InIt> void assign(InIt first1, InIt last1) { 673 iterator first2 = this->begin(), last2 = this->end(); 674 for ( ; first1 != last1 && first2 != last2; ++first1, ++first2) 675 *first1 = *first2; 676 if (first2 == last2) 677 erase(first1, last1); 678 else 679 insert(last1, first2, last2); 680 } 681 682 683 // Resize members... 684 void resize(size_type newsize, NodeTy val) { 685 iterator i = this->begin(); 686 size_type len = 0; 687 for ( ; i != this->end() && len < newsize; ++i, ++len) /* empty*/ ; 688 689 if (len == newsize) 690 erase(i, this->end()); 691 else // i == end() 692 insert(this->end(), newsize - len, val); 693 } 694 void resize(size_type newsize) { resize(newsize, NodeTy()); } 695}; 696 697} // End llvm namespace 698 699namespace std { 700 // Ensure that swap uses the fast list swap... 701 template<class Ty> 702 void swap(llvm::iplist<Ty> &Left, llvm::iplist<Ty> &Right) { 703 Left.swap(Right); 704 } 705} // End 'std' extensions... 706 707#endif // LLVM_ADT_ILIST_H 708