Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

526 lines
18 KiB

  1. //===--- llvm/ADT/SparseMultiSet.h - Sparse multiset ------------*- 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 the SparseMultiSet class, which adds multiset behavior to
  11. // the SparseSet.
  12. //
  13. // A sparse multiset holds a small number of objects identified by integer keys
  14. // from a moderately sized universe. The sparse multiset uses more memory than
  15. // other containers in order to provide faster operations. Any key can map to
  16. // multiple values. A SparseMultiSetNode class is provided, which serves as a
  17. // convenient base class for the contents of a SparseMultiSet.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ADT_SPARSEMULTISET_H
  21. #define LLVM_ADT_SPARSEMULTISET_H
  22. #include "llvm/ADT/SparseSet.h"
  23. namespace llvm {
  24. /// Fast multiset implementation for objects that can be identified by small
  25. /// unsigned keys.
  26. ///
  27. /// SparseMultiSet allocates memory proportional to the size of the key
  28. /// universe, so it is not recommended for building composite data structures.
  29. /// It is useful for algorithms that require a single set with fast operations.
  30. ///
  31. /// Compared to DenseSet and DenseMap, SparseMultiSet provides constant-time
  32. /// fast clear() as fast as a vector. The find(), insert(), and erase()
  33. /// operations are all constant time, and typically faster than a hash table.
  34. /// The iteration order doesn't depend on numerical key values, it only depends
  35. /// on the order of insert() and erase() operations. Iteration order is the
  36. /// insertion order. Iteration is only provided over elements of equivalent
  37. /// keys, but iterators are bidirectional.
  38. ///
  39. /// Compared to BitVector, SparseMultiSet<unsigned> uses 8x-40x more memory, but
  40. /// offers constant-time clear() and size() operations as well as fast iteration
  41. /// independent on the size of the universe.
  42. ///
  43. /// SparseMultiSet contains a dense vector holding all the objects and a sparse
  44. /// array holding indexes into the dense vector. Most of the memory is used by
  45. /// the sparse array which is the size of the key universe. The SparseT template
  46. /// parameter provides a space/speed tradeoff for sets holding many elements.
  47. ///
  48. /// When SparseT is uint32_t, find() only touches up to 3 cache lines, but the
  49. /// sparse array uses 4 x Universe bytes.
  50. ///
  51. /// When SparseT is uint8_t (the default), find() touches up to 3+[N/256] cache
  52. /// lines, but the sparse array is 4x smaller. N is the number of elements in
  53. /// the set.
  54. ///
  55. /// For sets that may grow to thousands of elements, SparseT should be set to
  56. /// uint16_t or uint32_t.
  57. ///
  58. /// Multiset behavior is provided by providing doubly linked lists for values
  59. /// that are inlined in the dense vector. SparseMultiSet is a good choice when
  60. /// one desires a growable number of entries per key, as it will retain the
  61. /// SparseSet algorithmic properties despite being growable. Thus, it is often a
  62. /// better choice than a SparseSet of growable containers or a vector of
  63. /// vectors. SparseMultiSet also keeps iterators valid after erasure (provided
  64. /// the iterators don't point to the element erased), allowing for more
  65. /// intuitive and fast removal.
  66. ///
  67. /// @tparam ValueT The type of objects in the set.
  68. /// @tparam KeyFunctorT A functor that computes an unsigned index from KeyT.
  69. /// @tparam SparseT An unsigned integer type. See above.
  70. ///
  71. template<typename ValueT,
  72. typename KeyFunctorT = llvm::identity<unsigned>,
  73. typename SparseT = uint8_t>
  74. class SparseMultiSet {
  75. /// The actual data that's stored, as a doubly-linked list implemented via
  76. /// indices into the DenseVector. The doubly linked list is implemented
  77. /// circular in Prev indices, and INVALID-terminated in Next indices. This
  78. /// provides efficient access to list tails. These nodes can also be
  79. /// tombstones, in which case they are actually nodes in a single-linked
  80. /// freelist of recyclable slots.
  81. struct SMSNode {
  82. static const unsigned INVALID = ~0U;
  83. ValueT Data;
  84. unsigned Prev;
  85. unsigned Next;
  86. SMSNode(ValueT D, unsigned P, unsigned N) : Data(D), Prev(P), Next(N) { }
  87. /// List tails have invalid Nexts.
  88. bool isTail() const {
  89. return Next == INVALID;
  90. }
  91. /// Whether this node is a tombstone node, and thus is in our freelist.
  92. bool isTombstone() const {
  93. return Prev == INVALID;
  94. }
  95. /// Since the list is circular in Prev, all non-tombstone nodes have a valid
  96. /// Prev.
  97. bool isValid() const { return Prev != INVALID; }
  98. };
  99. typedef typename KeyFunctorT::argument_type KeyT;
  100. typedef SmallVector<SMSNode, 8> DenseT;
  101. DenseT Dense;
  102. SparseT *Sparse;
  103. unsigned Universe;
  104. KeyFunctorT KeyIndexOf;
  105. SparseSetValFunctor<KeyT, ValueT, KeyFunctorT> ValIndexOf;
  106. /// We have a built-in recycler for reusing tombstone slots. This recycler
  107. /// puts a singly-linked free list into tombstone slots, allowing us quick
  108. /// erasure, iterator preservation, and dense size.
  109. unsigned FreelistIdx;
  110. unsigned NumFree;
  111. unsigned sparseIndex(const ValueT &Val) const {
  112. assert(ValIndexOf(Val) < Universe &&
  113. "Invalid key in set. Did object mutate?");
  114. return ValIndexOf(Val);
  115. }
  116. unsigned sparseIndex(const SMSNode &N) const { return sparseIndex(N.Data); }
  117. // Disable copy construction and assignment.
  118. // This data structure is not meant to be used that way.
  119. SparseMultiSet(const SparseMultiSet&) LLVM_DELETED_FUNCTION;
  120. SparseMultiSet &operator=(const SparseMultiSet&) LLVM_DELETED_FUNCTION;
  121. /// Whether the given entry is the head of the list. List heads's previous
  122. /// pointers are to the tail of the list, allowing for efficient access to the
  123. /// list tail. D must be a valid entry node.
  124. bool isHead(const SMSNode &D) const {
  125. assert(D.isValid() && "Invalid node for head");
  126. return Dense[D.Prev].isTail();
  127. }
  128. /// Whether the given entry is a singleton entry, i.e. the only entry with
  129. /// that key.
  130. bool isSingleton(const SMSNode &N) const {
  131. assert(N.isValid() && "Invalid node for singleton");
  132. // Is N its own predecessor?
  133. return &Dense[N.Prev] == &N;
  134. }
  135. /// Add in the given SMSNode. Uses a free entry in our freelist if
  136. /// available. Returns the index of the added node.
  137. unsigned addValue(const ValueT& V, unsigned Prev, unsigned Next) {
  138. if (NumFree == 0) {
  139. Dense.push_back(SMSNode(V, Prev, Next));
  140. return Dense.size() - 1;
  141. }
  142. // Peel off a free slot
  143. unsigned Idx = FreelistIdx;
  144. unsigned NextFree = Dense[Idx].Next;
  145. assert(Dense[Idx].isTombstone() && "Non-tombstone free?");
  146. Dense[Idx] = SMSNode(V, Prev, Next);
  147. FreelistIdx = NextFree;
  148. --NumFree;
  149. return Idx;
  150. }
  151. /// Make the current index a new tombstone. Pushes it onto the freelist.
  152. void makeTombstone(unsigned Idx) {
  153. Dense[Idx].Prev = SMSNode::INVALID;
  154. Dense[Idx].Next = FreelistIdx;
  155. FreelistIdx = Idx;
  156. ++NumFree;
  157. }
  158. public:
  159. typedef ValueT value_type;
  160. typedef ValueT &reference;
  161. typedef const ValueT &const_reference;
  162. typedef ValueT *pointer;
  163. typedef const ValueT *const_pointer;
  164. SparseMultiSet()
  165. : Sparse(0), Universe(0), FreelistIdx(SMSNode::INVALID), NumFree(0) { }
  166. ~SparseMultiSet() { free(Sparse); }
  167. /// Set the universe size which determines the largest key the set can hold.
  168. /// The universe must be sized before any elements can be added.
  169. ///
  170. /// @param U Universe size. All object keys must be less than U.
  171. ///
  172. void setUniverse(unsigned U) {
  173. // It's not hard to resize the universe on a non-empty set, but it doesn't
  174. // seem like a likely use case, so we can add that code when we need it.
  175. assert(empty() && "Can only resize universe on an empty map");
  176. // Hysteresis prevents needless reallocations.
  177. if (U >= Universe/4 && U <= Universe)
  178. return;
  179. free(Sparse);
  180. // The Sparse array doesn't actually need to be initialized, so malloc
  181. // would be enough here, but that will cause tools like valgrind to
  182. // complain about branching on uninitialized data.
  183. Sparse = reinterpret_cast<SparseT*>(calloc(U, sizeof(SparseT)));
  184. Universe = U;
  185. }
  186. /// Our iterators are iterators over the collection of objects that share a
  187. /// key.
  188. template<typename SMSPtrTy>
  189. class iterator_base : public std::iterator<std::bidirectional_iterator_tag,
  190. ValueT> {
  191. friend class SparseMultiSet;
  192. SMSPtrTy SMS;
  193. unsigned Idx;
  194. unsigned SparseIdx;
  195. iterator_base(SMSPtrTy P, unsigned I, unsigned SI)
  196. : SMS(P), Idx(I), SparseIdx(SI) { }
  197. /// Whether our iterator has fallen outside our dense vector.
  198. bool isEnd() const {
  199. if (Idx == SMSNode::INVALID)
  200. return true;
  201. assert(Idx < SMS->Dense.size() && "Out of range, non-INVALID Idx?");
  202. return false;
  203. }
  204. /// Whether our iterator is properly keyed, i.e. the SparseIdx is valid
  205. bool isKeyed() const { return SparseIdx < SMS->Universe; }
  206. unsigned Prev() const { return SMS->Dense[Idx].Prev; }
  207. unsigned Next() const { return SMS->Dense[Idx].Next; }
  208. void setPrev(unsigned P) { SMS->Dense[Idx].Prev = P; }
  209. void setNext(unsigned N) { SMS->Dense[Idx].Next = N; }
  210. public:
  211. typedef std::iterator<std::bidirectional_iterator_tag, ValueT> super;
  212. typedef typename super::value_type value_type;
  213. typedef typename super::difference_type difference_type;
  214. typedef typename super::pointer pointer;
  215. typedef typename super::reference reference;
  216. iterator_base(const iterator_base &RHS)
  217. : SMS(RHS.SMS), Idx(RHS.Idx), SparseIdx(RHS.SparseIdx) { }
  218. const iterator_base &operator=(const iterator_base &RHS) {
  219. SMS = RHS.SMS;
  220. Idx = RHS.Idx;
  221. SparseIdx = RHS.SparseIdx;
  222. return *this;
  223. }
  224. reference operator*() const {
  225. assert(isKeyed() && SMS->sparseIndex(SMS->Dense[Idx].Data) == SparseIdx &&
  226. "Dereferencing iterator of invalid key or index");
  227. return SMS->Dense[Idx].Data;
  228. }
  229. pointer operator->() const { return &operator*(); }
  230. /// Comparison operators
  231. bool operator==(const iterator_base &RHS) const {
  232. // end compares equal
  233. if (SMS == RHS.SMS && Idx == RHS.Idx) {
  234. assert((isEnd() || SparseIdx == RHS.SparseIdx) &&
  235. "Same dense entry, but different keys?");
  236. return true;
  237. }
  238. return false;
  239. }
  240. bool operator!=(const iterator_base &RHS) const {
  241. return !operator==(RHS);
  242. }
  243. /// Increment and decrement operators
  244. iterator_base &operator--() { // predecrement - Back up
  245. assert(isKeyed() && "Decrementing an invalid iterator");
  246. assert((isEnd() || !SMS->isHead(SMS->Dense[Idx])) &&
  247. "Decrementing head of list");
  248. // If we're at the end, then issue a new find()
  249. if (isEnd())
  250. Idx = SMS->findIndex(SparseIdx).Prev();
  251. else
  252. Idx = Prev();
  253. return *this;
  254. }
  255. iterator_base &operator++() { // preincrement - Advance
  256. assert(!isEnd() && isKeyed() && "Incrementing an invalid/end iterator");
  257. Idx = Next();
  258. return *this;
  259. }
  260. iterator_base operator--(int) { // postdecrement
  261. iterator_base I(*this);
  262. --*this;
  263. return I;
  264. }
  265. iterator_base operator++(int) { // postincrement
  266. iterator_base I(*this);
  267. ++*this;
  268. return I;
  269. }
  270. };
  271. typedef iterator_base<SparseMultiSet *> iterator;
  272. typedef iterator_base<const SparseMultiSet *> const_iterator;
  273. // Convenience types
  274. typedef std::pair<iterator, iterator> RangePair;
  275. /// Returns an iterator past this container. Note that such an iterator cannot
  276. /// be decremented, but will compare equal to other end iterators.
  277. iterator end() { return iterator(this, SMSNode::INVALID, SMSNode::INVALID); }
  278. const_iterator end() const {
  279. return const_iterator(this, SMSNode::INVALID, SMSNode::INVALID);
  280. }
  281. /// Returns true if the set is empty.
  282. ///
  283. /// This is not the same as BitVector::empty().
  284. ///
  285. bool empty() const { return size() == 0; }
  286. /// Returns the number of elements in the set.
  287. ///
  288. /// This is not the same as BitVector::size() which returns the size of the
  289. /// universe.
  290. ///
  291. unsigned size() const {
  292. assert(NumFree <= Dense.size() && "Out-of-bounds free entries");
  293. return Dense.size() - NumFree;
  294. }
  295. /// Clears the set. This is a very fast constant time operation.
  296. ///
  297. void clear() {
  298. // Sparse does not need to be cleared, see find().
  299. Dense.clear();
  300. NumFree = 0;
  301. FreelistIdx = SMSNode::INVALID;
  302. }
  303. /// Find an element by its index.
  304. ///
  305. /// @param Idx A valid index to find.
  306. /// @returns An iterator to the element identified by key, or end().
  307. ///
  308. iterator findIndex(unsigned Idx) {
  309. assert(Idx < Universe && "Key out of range");
  310. assert(std::numeric_limits<SparseT>::is_integer &&
  311. !std::numeric_limits<SparseT>::is_signed &&
  312. "SparseT must be an unsigned integer type");
  313. const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
  314. for (unsigned i = Sparse[Idx], e = Dense.size(); i < e; i += Stride) {
  315. const unsigned FoundIdx = sparseIndex(Dense[i]);
  316. // Check that we're pointing at the correct entry and that it is the head
  317. // of a valid list.
  318. if (Idx == FoundIdx && Dense[i].isValid() && isHead(Dense[i]))
  319. return iterator(this, i, Idx);
  320. // Stride is 0 when SparseT >= unsigned. We don't need to loop.
  321. if (!Stride)
  322. break;
  323. }
  324. return end();
  325. }
  326. /// Find an element by its key.
  327. ///
  328. /// @param Key A valid key to find.
  329. /// @returns An iterator to the element identified by key, or end().
  330. ///
  331. iterator find(const KeyT &Key) {
  332. return findIndex(KeyIndexOf(Key));
  333. }
  334. const_iterator find(const KeyT &Key) const {
  335. iterator I = const_cast<SparseMultiSet*>(this)->findIndex(KeyIndexOf(Key));
  336. return const_iterator(I.SMS, I.Idx, KeyIndexOf(Key));
  337. }
  338. /// Returns the number of elements identified by Key. This will be linear in
  339. /// the number of elements of that key.
  340. unsigned count(const KeyT &Key) const {
  341. unsigned Ret = 0;
  342. for (const_iterator It = find(Key); It != end(); ++It)
  343. ++Ret;
  344. return Ret;
  345. }
  346. /// Returns true if this set contains an element identified by Key.
  347. bool contains(const KeyT &Key) const {
  348. return find(Key) != end();
  349. }
  350. /// Return the head and tail of the subset's list, otherwise returns end().
  351. iterator getHead(const KeyT &Key) { return find(Key); }
  352. iterator getTail(const KeyT &Key) {
  353. iterator I = find(Key);
  354. if (I != end())
  355. I = iterator(this, I.Prev(), KeyIndexOf(Key));
  356. return I;
  357. }
  358. /// The bounds of the range of items sharing Key K. First member is the head
  359. /// of the list, and the second member is a decrementable end iterator for
  360. /// that key.
  361. RangePair equal_range(const KeyT &K) {
  362. iterator B = find(K);
  363. iterator E = iterator(this, SMSNode::INVALID, B.SparseIdx);
  364. return make_pair(B, E);
  365. }
  366. /// Insert a new element at the tail of the subset list. Returns an iterator
  367. /// to the newly added entry.
  368. iterator insert(const ValueT &Val) {
  369. unsigned Idx = sparseIndex(Val);
  370. iterator I = findIndex(Idx);
  371. unsigned NodeIdx = addValue(Val, SMSNode::INVALID, SMSNode::INVALID);
  372. if (I == end()) {
  373. // Make a singleton list
  374. Sparse[Idx] = NodeIdx;
  375. Dense[NodeIdx].Prev = NodeIdx;
  376. return iterator(this, NodeIdx, Idx);
  377. }
  378. // Stick it at the end.
  379. unsigned HeadIdx = I.Idx;
  380. unsigned TailIdx = I.Prev();
  381. Dense[TailIdx].Next = NodeIdx;
  382. Dense[HeadIdx].Prev = NodeIdx;
  383. Dense[NodeIdx].Prev = TailIdx;
  384. return iterator(this, NodeIdx, Idx);
  385. }
  386. /// Erases an existing element identified by a valid iterator.
  387. ///
  388. /// This invalidates iterators pointing at the same entry, but erase() returns
  389. /// an iterator pointing to the next element in the subset's list. This makes
  390. /// it possible to erase selected elements while iterating over the subset:
  391. ///
  392. /// tie(I, E) = Set.equal_range(Key);
  393. /// while (I != E)
  394. /// if (test(*I))
  395. /// I = Set.erase(I);
  396. /// else
  397. /// ++I;
  398. ///
  399. /// Note that if the last element in the subset list is erased, this will
  400. /// return an end iterator which can be decremented to get the new tail (if it
  401. /// exists):
  402. ///
  403. /// tie(B, I) = Set.equal_range(Key);
  404. /// for (bool isBegin = B == I; !isBegin; /* empty */) {
  405. /// isBegin = (--I) == B;
  406. /// if (test(I))
  407. /// break;
  408. /// I = erase(I);
  409. /// }
  410. iterator erase(iterator I) {
  411. assert(I.isKeyed() && !I.isEnd() && !Dense[I.Idx].isTombstone() &&
  412. "erasing invalid/end/tombstone iterator");
  413. // First, unlink the node from its list. Then swap the node out with the
  414. // dense vector's last entry
  415. iterator NextI = unlink(Dense[I.Idx]);
  416. // Put in a tombstone.
  417. makeTombstone(I.Idx);
  418. return NextI;
  419. }
  420. /// Erase all elements with the given key. This invalidates all
  421. /// iterators of that key.
  422. void eraseAll(const KeyT &K) {
  423. for (iterator I = find(K); I != end(); /* empty */)
  424. I = erase(I);
  425. }
  426. private:
  427. /// Unlink the node from its list. Returns the next node in the list.
  428. iterator unlink(const SMSNode &N) {
  429. if (isSingleton(N)) {
  430. // Singleton is already unlinked
  431. assert(N.Next == SMSNode::INVALID && "Singleton has next?");
  432. return iterator(this, SMSNode::INVALID, ValIndexOf(N.Data));
  433. }
  434. if (isHead(N)) {
  435. // If we're the head, then update the sparse array and our next.
  436. Sparse[sparseIndex(N)] = N.Next;
  437. Dense[N.Next].Prev = N.Prev;
  438. return iterator(this, N.Next, ValIndexOf(N.Data));
  439. }
  440. if (N.isTail()) {
  441. // If we're the tail, then update our head and our previous.
  442. findIndex(sparseIndex(N)).setPrev(N.Prev);
  443. Dense[N.Prev].Next = N.Next;
  444. // Give back an end iterator that can be decremented
  445. iterator I(this, N.Prev, ValIndexOf(N.Data));
  446. return ++I;
  447. }
  448. // Otherwise, just drop us
  449. Dense[N.Next].Prev = N.Prev;
  450. Dense[N.Prev].Next = N.Next;
  451. return iterator(this, N.Next, ValIndexOf(N.Data));
  452. }
  453. };
  454. } // end namespace llvm
  455. #endif