1249259Sdim//===--- llvm/ADT/SparseMultiSet.h - Sparse multiset ------------*- C++ -*-===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim// 10249259Sdim// This file defines the SparseMultiSet class, which adds multiset behavior to 11249259Sdim// the SparseSet. 12249259Sdim// 13249259Sdim// A sparse multiset holds a small number of objects identified by integer keys 14249259Sdim// from a moderately sized universe. The sparse multiset uses more memory than 15249259Sdim// other containers in order to provide faster operations. Any key can map to 16249259Sdim// multiple values. A SparseMultiSetNode class is provided, which serves as a 17249259Sdim// convenient base class for the contents of a SparseMultiSet. 18249259Sdim// 19249259Sdim//===----------------------------------------------------------------------===// 20249259Sdim 21249259Sdim#ifndef LLVM_ADT_SPARSEMULTISET_H 22249259Sdim#define LLVM_ADT_SPARSEMULTISET_H 23249259Sdim 24249259Sdim#include "llvm/ADT/SparseSet.h" 25249259Sdim 26249259Sdimnamespace llvm { 27249259Sdim 28249259Sdim/// Fast multiset implementation for objects that can be identified by small 29249259Sdim/// unsigned keys. 30249259Sdim/// 31249259Sdim/// SparseMultiSet allocates memory proportional to the size of the key 32249259Sdim/// universe, so it is not recommended for building composite data structures. 33249259Sdim/// It is useful for algorithms that require a single set with fast operations. 34249259Sdim/// 35249259Sdim/// Compared to DenseSet and DenseMap, SparseMultiSet provides constant-time 36249259Sdim/// fast clear() as fast as a vector. The find(), insert(), and erase() 37249259Sdim/// operations are all constant time, and typically faster than a hash table. 38249259Sdim/// The iteration order doesn't depend on numerical key values, it only depends 39249259Sdim/// on the order of insert() and erase() operations. Iteration order is the 40249259Sdim/// insertion order. Iteration is only provided over elements of equivalent 41249259Sdim/// keys, but iterators are bidirectional. 42249259Sdim/// 43249259Sdim/// Compared to BitVector, SparseMultiSet<unsigned> uses 8x-40x more memory, but 44249259Sdim/// offers constant-time clear() and size() operations as well as fast iteration 45249259Sdim/// independent on the size of the universe. 46249259Sdim/// 47249259Sdim/// SparseMultiSet contains a dense vector holding all the objects and a sparse 48249259Sdim/// array holding indexes into the dense vector. Most of the memory is used by 49249259Sdim/// the sparse array which is the size of the key universe. The SparseT template 50249259Sdim/// parameter provides a space/speed tradeoff for sets holding many elements. 51249259Sdim/// 52249259Sdim/// When SparseT is uint32_t, find() only touches up to 3 cache lines, but the 53249259Sdim/// sparse array uses 4 x Universe bytes. 54249259Sdim/// 55249259Sdim/// When SparseT is uint8_t (the default), find() touches up to 3+[N/256] cache 56249259Sdim/// lines, but the sparse array is 4x smaller. N is the number of elements in 57249259Sdim/// the set. 58249259Sdim/// 59249259Sdim/// For sets that may grow to thousands of elements, SparseT should be set to 60249259Sdim/// uint16_t or uint32_t. 61249259Sdim/// 62249259Sdim/// Multiset behavior is provided by providing doubly linked lists for values 63249259Sdim/// that are inlined in the dense vector. SparseMultiSet is a good choice when 64249259Sdim/// one desires a growable number of entries per key, as it will retain the 65249259Sdim/// SparseSet algorithmic properties despite being growable. Thus, it is often a 66249259Sdim/// better choice than a SparseSet of growable containers or a vector of 67249259Sdim/// vectors. SparseMultiSet also keeps iterators valid after erasure (provided 68249259Sdim/// the iterators don't point to the element erased), allowing for more 69249259Sdim/// intuitive and fast removal. 70249259Sdim/// 71249259Sdim/// @tparam ValueT The type of objects in the set. 72249259Sdim/// @tparam KeyFunctorT A functor that computes an unsigned index from KeyT. 73249259Sdim/// @tparam SparseT An unsigned integer type. See above. 74249259Sdim/// 75249259Sdimtemplate<typename ValueT, 76249259Sdim typename KeyFunctorT = llvm::identity<unsigned>, 77249259Sdim typename SparseT = uint8_t> 78249259Sdimclass SparseMultiSet { 79249259Sdim /// The actual data that's stored, as a doubly-linked list implemented via 80249259Sdim /// indices into the DenseVector. The doubly linked list is implemented 81249259Sdim /// circular in Prev indices, and INVALID-terminated in Next indices. This 82249259Sdim /// provides efficient access to list tails. These nodes can also be 83249259Sdim /// tombstones, in which case they are actually nodes in a single-linked 84249259Sdim /// freelist of recyclable slots. 85249259Sdim struct SMSNode { 86249259Sdim static const unsigned INVALID = ~0U; 87249259Sdim 88249259Sdim ValueT Data; 89249259Sdim unsigned Prev; 90249259Sdim unsigned Next; 91249259Sdim 92249259Sdim SMSNode(ValueT D, unsigned P, unsigned N) : Data(D), Prev(P), Next(N) { } 93249259Sdim 94249259Sdim /// List tails have invalid Nexts. 95249259Sdim bool isTail() const { 96249259Sdim return Next == INVALID; 97249259Sdim } 98249259Sdim 99249259Sdim /// Whether this node is a tombstone node, and thus is in our freelist. 100249259Sdim bool isTombstone() const { 101249259Sdim return Prev == INVALID; 102249259Sdim } 103249259Sdim 104249259Sdim /// Since the list is circular in Prev, all non-tombstone nodes have a valid 105249259Sdim /// Prev. 106249259Sdim bool isValid() const { return Prev != INVALID; } 107249259Sdim }; 108249259Sdim 109249259Sdim typedef typename KeyFunctorT::argument_type KeyT; 110249259Sdim typedef SmallVector<SMSNode, 8> DenseT; 111249259Sdim DenseT Dense; 112249259Sdim SparseT *Sparse; 113249259Sdim unsigned Universe; 114249259Sdim KeyFunctorT KeyIndexOf; 115249259Sdim SparseSetValFunctor<KeyT, ValueT, KeyFunctorT> ValIndexOf; 116249259Sdim 117249259Sdim /// We have a built-in recycler for reusing tombstone slots. This recycler 118249259Sdim /// puts a singly-linked free list into tombstone slots, allowing us quick 119249259Sdim /// erasure, iterator preservation, and dense size. 120249259Sdim unsigned FreelistIdx; 121249259Sdim unsigned NumFree; 122249259Sdim 123249259Sdim unsigned sparseIndex(const ValueT &Val) const { 124249259Sdim assert(ValIndexOf(Val) < Universe && 125249259Sdim "Invalid key in set. Did object mutate?"); 126249259Sdim return ValIndexOf(Val); 127249259Sdim } 128249259Sdim unsigned sparseIndex(const SMSNode &N) const { return sparseIndex(N.Data); } 129249259Sdim 130249259Sdim // Disable copy construction and assignment. 131249259Sdim // This data structure is not meant to be used that way. 132249259Sdim SparseMultiSet(const SparseMultiSet&) LLVM_DELETED_FUNCTION; 133249259Sdim SparseMultiSet &operator=(const SparseMultiSet&) LLVM_DELETED_FUNCTION; 134249259Sdim 135249259Sdim /// Whether the given entry is the head of the list. List heads's previous 136249259Sdim /// pointers are to the tail of the list, allowing for efficient access to the 137249259Sdim /// list tail. D must be a valid entry node. 138249259Sdim bool isHead(const SMSNode &D) const { 139249259Sdim assert(D.isValid() && "Invalid node for head"); 140249259Sdim return Dense[D.Prev].isTail(); 141249259Sdim } 142249259Sdim 143249259Sdim /// Whether the given entry is a singleton entry, i.e. the only entry with 144249259Sdim /// that key. 145249259Sdim bool isSingleton(const SMSNode &N) const { 146249259Sdim assert(N.isValid() && "Invalid node for singleton"); 147249259Sdim // Is N its own predecessor? 148249259Sdim return &Dense[N.Prev] == &N; 149249259Sdim } 150249259Sdim 151249259Sdim /// Add in the given SMSNode. Uses a free entry in our freelist if 152249259Sdim /// available. Returns the index of the added node. 153249259Sdim unsigned addValue(const ValueT& V, unsigned Prev, unsigned Next) { 154249259Sdim if (NumFree == 0) { 155249259Sdim Dense.push_back(SMSNode(V, Prev, Next)); 156249259Sdim return Dense.size() - 1; 157249259Sdim } 158249259Sdim 159249259Sdim // Peel off a free slot 160249259Sdim unsigned Idx = FreelistIdx; 161249259Sdim unsigned NextFree = Dense[Idx].Next; 162249259Sdim assert(Dense[Idx].isTombstone() && "Non-tombstone free?"); 163249259Sdim 164249259Sdim Dense[Idx] = SMSNode(V, Prev, Next); 165249259Sdim FreelistIdx = NextFree; 166249259Sdim --NumFree; 167249259Sdim return Idx; 168249259Sdim } 169249259Sdim 170249259Sdim /// Make the current index a new tombstone. Pushes it onto the freelist. 171249259Sdim void makeTombstone(unsigned Idx) { 172249259Sdim Dense[Idx].Prev = SMSNode::INVALID; 173249259Sdim Dense[Idx].Next = FreelistIdx; 174249259Sdim FreelistIdx = Idx; 175249259Sdim ++NumFree; 176249259Sdim } 177249259Sdim 178249259Sdimpublic: 179249259Sdim typedef ValueT value_type; 180249259Sdim typedef ValueT &reference; 181249259Sdim typedef const ValueT &const_reference; 182249259Sdim typedef ValueT *pointer; 183249259Sdim typedef const ValueT *const_pointer; 184249259Sdim 185249259Sdim SparseMultiSet() 186249259Sdim : Sparse(0), Universe(0), FreelistIdx(SMSNode::INVALID), NumFree(0) { } 187249259Sdim 188249259Sdim ~SparseMultiSet() { free(Sparse); } 189249259Sdim 190249259Sdim /// Set the universe size which determines the largest key the set can hold. 191249259Sdim /// The universe must be sized before any elements can be added. 192249259Sdim /// 193249259Sdim /// @param U Universe size. All object keys must be less than U. 194249259Sdim /// 195249259Sdim void setUniverse(unsigned U) { 196249259Sdim // It's not hard to resize the universe on a non-empty set, but it doesn't 197249259Sdim // seem like a likely use case, so we can add that code when we need it. 198249259Sdim assert(empty() && "Can only resize universe on an empty map"); 199249259Sdim // Hysteresis prevents needless reallocations. 200249259Sdim if (U >= Universe/4 && U <= Universe) 201249259Sdim return; 202249259Sdim free(Sparse); 203249259Sdim // The Sparse array doesn't actually need to be initialized, so malloc 204249259Sdim // would be enough here, but that will cause tools like valgrind to 205249259Sdim // complain about branching on uninitialized data. 206249259Sdim Sparse = reinterpret_cast<SparseT*>(calloc(U, sizeof(SparseT))); 207249259Sdim Universe = U; 208249259Sdim } 209249259Sdim 210249259Sdim /// Our iterators are iterators over the collection of objects that share a 211249259Sdim /// key. 212249259Sdim template<typename SMSPtrTy> 213249259Sdim class iterator_base : public std::iterator<std::bidirectional_iterator_tag, 214249259Sdim ValueT> { 215249259Sdim friend class SparseMultiSet; 216249259Sdim SMSPtrTy SMS; 217249259Sdim unsigned Idx; 218249259Sdim unsigned SparseIdx; 219249259Sdim 220249259Sdim iterator_base(SMSPtrTy P, unsigned I, unsigned SI) 221249259Sdim : SMS(P), Idx(I), SparseIdx(SI) { } 222249259Sdim 223249259Sdim /// Whether our iterator has fallen outside our dense vector. 224249259Sdim bool isEnd() const { 225249259Sdim if (Idx == SMSNode::INVALID) 226249259Sdim return true; 227249259Sdim 228249259Sdim assert(Idx < SMS->Dense.size() && "Out of range, non-INVALID Idx?"); 229249259Sdim return false; 230249259Sdim } 231249259Sdim 232249259Sdim /// Whether our iterator is properly keyed, i.e. the SparseIdx is valid 233249259Sdim bool isKeyed() const { return SparseIdx < SMS->Universe; } 234249259Sdim 235249259Sdim unsigned Prev() const { return SMS->Dense[Idx].Prev; } 236249259Sdim unsigned Next() const { return SMS->Dense[Idx].Next; } 237249259Sdim 238249259Sdim void setPrev(unsigned P) { SMS->Dense[Idx].Prev = P; } 239249259Sdim void setNext(unsigned N) { SMS->Dense[Idx].Next = N; } 240249259Sdim 241249259Sdim public: 242249259Sdim typedef std::iterator<std::bidirectional_iterator_tag, ValueT> super; 243249259Sdim typedef typename super::value_type value_type; 244249259Sdim typedef typename super::difference_type difference_type; 245249259Sdim typedef typename super::pointer pointer; 246249259Sdim typedef typename super::reference reference; 247249259Sdim 248249259Sdim iterator_base(const iterator_base &RHS) 249249259Sdim : SMS(RHS.SMS), Idx(RHS.Idx), SparseIdx(RHS.SparseIdx) { } 250249259Sdim 251249259Sdim const iterator_base &operator=(const iterator_base &RHS) { 252249259Sdim SMS = RHS.SMS; 253249259Sdim Idx = RHS.Idx; 254249259Sdim SparseIdx = RHS.SparseIdx; 255249259Sdim return *this; 256249259Sdim } 257249259Sdim 258249259Sdim reference operator*() const { 259249259Sdim assert(isKeyed() && SMS->sparseIndex(SMS->Dense[Idx].Data) == SparseIdx && 260249259Sdim "Dereferencing iterator of invalid key or index"); 261249259Sdim 262249259Sdim return SMS->Dense[Idx].Data; 263249259Sdim } 264249259Sdim pointer operator->() const { return &operator*(); } 265249259Sdim 266249259Sdim /// Comparison operators 267249259Sdim bool operator==(const iterator_base &RHS) const { 268249259Sdim // end compares equal 269249259Sdim if (SMS == RHS.SMS && Idx == RHS.Idx) { 270249259Sdim assert((isEnd() || SparseIdx == RHS.SparseIdx) && 271249259Sdim "Same dense entry, but different keys?"); 272249259Sdim return true; 273249259Sdim } 274249259Sdim 275249259Sdim return false; 276249259Sdim } 277249259Sdim 278249259Sdim bool operator!=(const iterator_base &RHS) const { 279249259Sdim return !operator==(RHS); 280249259Sdim } 281249259Sdim 282249259Sdim /// Increment and decrement operators 283249259Sdim iterator_base &operator--() { // predecrement - Back up 284249259Sdim assert(isKeyed() && "Decrementing an invalid iterator"); 285249259Sdim assert((isEnd() || !SMS->isHead(SMS->Dense[Idx])) && 286249259Sdim "Decrementing head of list"); 287249259Sdim 288249259Sdim // If we're at the end, then issue a new find() 289249259Sdim if (isEnd()) 290249259Sdim Idx = SMS->findIndex(SparseIdx).Prev(); 291249259Sdim else 292249259Sdim Idx = Prev(); 293249259Sdim 294249259Sdim return *this; 295249259Sdim } 296249259Sdim iterator_base &operator++() { // preincrement - Advance 297249259Sdim assert(!isEnd() && isKeyed() && "Incrementing an invalid/end iterator"); 298249259Sdim Idx = Next(); 299249259Sdim return *this; 300249259Sdim } 301249259Sdim iterator_base operator--(int) { // postdecrement 302249259Sdim iterator_base I(*this); 303249259Sdim --*this; 304249259Sdim return I; 305249259Sdim } 306249259Sdim iterator_base operator++(int) { // postincrement 307249259Sdim iterator_base I(*this); 308249259Sdim ++*this; 309249259Sdim return I; 310249259Sdim } 311249259Sdim }; 312249259Sdim typedef iterator_base<SparseMultiSet *> iterator; 313249259Sdim typedef iterator_base<const SparseMultiSet *> const_iterator; 314249259Sdim 315249259Sdim // Convenience types 316249259Sdim typedef std::pair<iterator, iterator> RangePair; 317249259Sdim 318249259Sdim /// Returns an iterator past this container. Note that such an iterator cannot 319249259Sdim /// be decremented, but will compare equal to other end iterators. 320249259Sdim iterator end() { return iterator(this, SMSNode::INVALID, SMSNode::INVALID); } 321249259Sdim const_iterator end() const { 322249259Sdim return const_iterator(this, SMSNode::INVALID, SMSNode::INVALID); 323249259Sdim } 324249259Sdim 325249259Sdim /// Returns true if the set is empty. 326249259Sdim /// 327249259Sdim /// This is not the same as BitVector::empty(). 328249259Sdim /// 329249259Sdim bool empty() const { return size() == 0; } 330249259Sdim 331249259Sdim /// Returns the number of elements in the set. 332249259Sdim /// 333249259Sdim /// This is not the same as BitVector::size() which returns the size of the 334249259Sdim /// universe. 335249259Sdim /// 336249259Sdim unsigned size() const { 337249259Sdim assert(NumFree <= Dense.size() && "Out-of-bounds free entries"); 338249259Sdim return Dense.size() - NumFree; 339249259Sdim } 340249259Sdim 341249259Sdim /// Clears the set. This is a very fast constant time operation. 342249259Sdim /// 343249259Sdim void clear() { 344249259Sdim // Sparse does not need to be cleared, see find(). 345249259Sdim Dense.clear(); 346249259Sdim NumFree = 0; 347249259Sdim FreelistIdx = SMSNode::INVALID; 348249259Sdim } 349249259Sdim 350249259Sdim /// Find an element by its index. 351249259Sdim /// 352249259Sdim /// @param Idx A valid index to find. 353249259Sdim /// @returns An iterator to the element identified by key, or end(). 354249259Sdim /// 355249259Sdim iterator findIndex(unsigned Idx) { 356249259Sdim assert(Idx < Universe && "Key out of range"); 357249259Sdim assert(std::numeric_limits<SparseT>::is_integer && 358249259Sdim !std::numeric_limits<SparseT>::is_signed && 359249259Sdim "SparseT must be an unsigned integer type"); 360249259Sdim const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u; 361249259Sdim for (unsigned i = Sparse[Idx], e = Dense.size(); i < e; i += Stride) { 362249259Sdim const unsigned FoundIdx = sparseIndex(Dense[i]); 363249259Sdim // Check that we're pointing at the correct entry and that it is the head 364249259Sdim // of a valid list. 365249259Sdim if (Idx == FoundIdx && Dense[i].isValid() && isHead(Dense[i])) 366249259Sdim return iterator(this, i, Idx); 367249259Sdim // Stride is 0 when SparseT >= unsigned. We don't need to loop. 368249259Sdim if (!Stride) 369249259Sdim break; 370249259Sdim } 371249259Sdim return end(); 372249259Sdim } 373249259Sdim 374249259Sdim /// Find an element by its key. 375249259Sdim /// 376249259Sdim /// @param Key A valid key to find. 377249259Sdim /// @returns An iterator to the element identified by key, or end(). 378249259Sdim /// 379249259Sdim iterator find(const KeyT &Key) { 380249259Sdim return findIndex(KeyIndexOf(Key)); 381249259Sdim } 382249259Sdim 383249259Sdim const_iterator find(const KeyT &Key) const { 384249259Sdim iterator I = const_cast<SparseMultiSet*>(this)->findIndex(KeyIndexOf(Key)); 385249259Sdim return const_iterator(I.SMS, I.Idx, KeyIndexOf(Key)); 386249259Sdim } 387249259Sdim 388249259Sdim /// Returns the number of elements identified by Key. This will be linear in 389249259Sdim /// the number of elements of that key. 390249259Sdim unsigned count(const KeyT &Key) const { 391249259Sdim unsigned Ret = 0; 392249259Sdim for (const_iterator It = find(Key); It != end(); ++It) 393249259Sdim ++Ret; 394249259Sdim 395249259Sdim return Ret; 396249259Sdim } 397249259Sdim 398249259Sdim /// Returns true if this set contains an element identified by Key. 399249259Sdim bool contains(const KeyT &Key) const { 400249259Sdim return find(Key) != end(); 401249259Sdim } 402249259Sdim 403249259Sdim /// Return the head and tail of the subset's list, otherwise returns end(). 404249259Sdim iterator getHead(const KeyT &Key) { return find(Key); } 405249259Sdim iterator getTail(const KeyT &Key) { 406249259Sdim iterator I = find(Key); 407249259Sdim if (I != end()) 408249259Sdim I = iterator(this, I.Prev(), KeyIndexOf(Key)); 409249259Sdim return I; 410249259Sdim } 411249259Sdim 412249259Sdim /// The bounds of the range of items sharing Key K. First member is the head 413249259Sdim /// of the list, and the second member is a decrementable end iterator for 414249259Sdim /// that key. 415249259Sdim RangePair equal_range(const KeyT &K) { 416249259Sdim iterator B = find(K); 417249259Sdim iterator E = iterator(this, SMSNode::INVALID, B.SparseIdx); 418249259Sdim return make_pair(B, E); 419249259Sdim } 420249259Sdim 421249259Sdim /// Insert a new element at the tail of the subset list. Returns an iterator 422249259Sdim /// to the newly added entry. 423249259Sdim iterator insert(const ValueT &Val) { 424249259Sdim unsigned Idx = sparseIndex(Val); 425249259Sdim iterator I = findIndex(Idx); 426249259Sdim 427249259Sdim unsigned NodeIdx = addValue(Val, SMSNode::INVALID, SMSNode::INVALID); 428249259Sdim 429249259Sdim if (I == end()) { 430249259Sdim // Make a singleton list 431249259Sdim Sparse[Idx] = NodeIdx; 432249259Sdim Dense[NodeIdx].Prev = NodeIdx; 433249259Sdim return iterator(this, NodeIdx, Idx); 434249259Sdim } 435249259Sdim 436249259Sdim // Stick it at the end. 437249259Sdim unsigned HeadIdx = I.Idx; 438249259Sdim unsigned TailIdx = I.Prev(); 439249259Sdim Dense[TailIdx].Next = NodeIdx; 440249259Sdim Dense[HeadIdx].Prev = NodeIdx; 441249259Sdim Dense[NodeIdx].Prev = TailIdx; 442249259Sdim 443249259Sdim return iterator(this, NodeIdx, Idx); 444249259Sdim } 445249259Sdim 446249259Sdim /// Erases an existing element identified by a valid iterator. 447249259Sdim /// 448249259Sdim /// This invalidates iterators pointing at the same entry, but erase() returns 449249259Sdim /// an iterator pointing to the next element in the subset's list. This makes 450249259Sdim /// it possible to erase selected elements while iterating over the subset: 451249259Sdim /// 452249259Sdim /// tie(I, E) = Set.equal_range(Key); 453249259Sdim /// while (I != E) 454249259Sdim /// if (test(*I)) 455249259Sdim /// I = Set.erase(I); 456249259Sdim /// else 457249259Sdim /// ++I; 458249259Sdim /// 459249259Sdim /// Note that if the last element in the subset list is erased, this will 460249259Sdim /// return an end iterator which can be decremented to get the new tail (if it 461249259Sdim /// exists): 462249259Sdim /// 463249259Sdim /// tie(B, I) = Set.equal_range(Key); 464249259Sdim /// for (bool isBegin = B == I; !isBegin; /* empty */) { 465249259Sdim /// isBegin = (--I) == B; 466249259Sdim /// if (test(I)) 467249259Sdim /// break; 468249259Sdim /// I = erase(I); 469249259Sdim /// } 470249259Sdim iterator erase(iterator I) { 471249259Sdim assert(I.isKeyed() && !I.isEnd() && !Dense[I.Idx].isTombstone() && 472249259Sdim "erasing invalid/end/tombstone iterator"); 473249259Sdim 474249259Sdim // First, unlink the node from its list. Then swap the node out with the 475249259Sdim // dense vector's last entry 476249259Sdim iterator NextI = unlink(Dense[I.Idx]); 477249259Sdim 478249259Sdim // Put in a tombstone. 479249259Sdim makeTombstone(I.Idx); 480249259Sdim 481249259Sdim return NextI; 482249259Sdim } 483249259Sdim 484249259Sdim /// Erase all elements with the given key. This invalidates all 485249259Sdim /// iterators of that key. 486249259Sdim void eraseAll(const KeyT &K) { 487249259Sdim for (iterator I = find(K); I != end(); /* empty */) 488249259Sdim I = erase(I); 489249259Sdim } 490249259Sdim 491249259Sdimprivate: 492249259Sdim /// Unlink the node from its list. Returns the next node in the list. 493249259Sdim iterator unlink(const SMSNode &N) { 494249259Sdim if (isSingleton(N)) { 495249259Sdim // Singleton is already unlinked 496249259Sdim assert(N.Next == SMSNode::INVALID && "Singleton has next?"); 497249259Sdim return iterator(this, SMSNode::INVALID, ValIndexOf(N.Data)); 498249259Sdim } 499249259Sdim 500249259Sdim if (isHead(N)) { 501249259Sdim // If we're the head, then update the sparse array and our next. 502249259Sdim Sparse[sparseIndex(N)] = N.Next; 503249259Sdim Dense[N.Next].Prev = N.Prev; 504249259Sdim return iterator(this, N.Next, ValIndexOf(N.Data)); 505249259Sdim } 506249259Sdim 507249259Sdim if (N.isTail()) { 508249259Sdim // If we're the tail, then update our head and our previous. 509249259Sdim findIndex(sparseIndex(N)).setPrev(N.Prev); 510249259Sdim Dense[N.Prev].Next = N.Next; 511249259Sdim 512249259Sdim // Give back an end iterator that can be decremented 513249259Sdim iterator I(this, N.Prev, ValIndexOf(N.Data)); 514249259Sdim return ++I; 515249259Sdim } 516249259Sdim 517249259Sdim // Otherwise, just drop us 518249259Sdim Dense[N.Next].Prev = N.Prev; 519249259Sdim Dense[N.Prev].Next = N.Next; 520249259Sdim return iterator(this, N.Next, ValIndexOf(N.Data)); 521249259Sdim } 522249259Sdim}; 523249259Sdim 524249259Sdim} // end namespace llvm 525249259Sdim 526249259Sdim#endif 527