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