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