//===- llvm/ADT/SparseSet.h - Sparse set ------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// This file defines the SparseSet class derived from the version described in /// Briggs, Torczon, "An efficient representation for sparse sets", ACM Letters /// on Programming Languages and Systems, Volume 2 Issue 1-4, March-Dec. 1993. /// /// A sparse set holds a small number of objects identified by integer keys from /// a moderately sized universe. The sparse set uses more memory than other /// containers in order to provide faster operations. /// //===----------------------------------------------------------------------===// #ifndef LLVM_ADT_SPARSESET_H #define LLVM_ADT_SPARSESET_H #include "llvm/ADT/identity.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/AllocatorBase.h" #include <cassert> #include <cstdint> #include <cstdlib> #include <limits> #include <utility> namespace llvm { /// SparseSetValTraits - Objects in a SparseSet are identified by keys that can /// be uniquely converted to a small integer less than the set's universe. This /// class allows the set to hold values that differ from the set's key type as /// long as an index can still be derived from the value. SparseSet never /// directly compares ValueT, only their indices, so it can map keys to /// arbitrary values. SparseSetValTraits computes the index from the value /// object. To compute the index from a key, SparseSet uses a separate /// KeyFunctorT template argument. /// /// A simple type declaration, SparseSet<Type>, handles these cases: /// - unsigned key, identity index, identity value /// - unsigned key, identity index, fat value providing getSparseSetIndex() /// /// The type declaration SparseSet<Type, UnaryFunction> handles: /// - unsigned key, remapped index, identity value (virtual registers) /// - pointer key, pointer-derived index, identity value (node+ID) /// - pointer key, pointer-derived index, fat value with getSparseSetIndex() /// /// Only other, unexpected cases require specializing SparseSetValTraits. /// /// For best results, ValueT should not require a destructor. /// template<typename ValueT> struct SparseSetValTraits { … }; /// SparseSetValFunctor - Helper class for selecting SparseSetValTraits. The /// generic implementation handles ValueT classes which either provide /// getSparseSetIndex() or specialize SparseSetValTraits<>. /// template<typename KeyT, typename ValueT, typename KeyFunctorT> struct SparseSetValFunctor { … }; /// SparseSetValFunctor<KeyT, KeyT> - Helper class for the common case of /// identity key/value sets. SparseSetValFunctor<KeyT, KeyT, KeyFunctorT>; /// SparseSet - Fast set implementation for objects that can be identified by /// small unsigned keys. /// /// SparseSet allocates memory proportional to the size of the key universe, so /// it is not recommended for building composite data structures. It is useful /// for algorithms that require a single set with fast operations. /// /// Compared to DenseSet and DenseMap, SparseSet provides constant-time fast /// clear() and iteration as fast as a vector. The find(), insert(), and /// erase() operations are all constant time, and typically faster than a hash /// table. The iteration order doesn't depend on numerical key values, it only /// depends on the order of insert() and erase() operations. When no elements /// have been erased, the iteration order is the insertion order. /// /// Compared to BitVector, SparseSet<unsigned> uses 8x-40x more memory, but /// offers constant-time clear() and size() operations as well as fast /// iteration independent on the size of the universe. /// /// SparseSet contains a dense vector holding all the objects and a sparse /// array holding indexes into the dense vector. Most of the memory is used by /// the sparse array which is the size of the key universe. The SparseT /// template parameter provides a space/speed tradeoff for sets holding many /// elements. /// /// When SparseT is uint32_t, find() only touches 2 cache lines, but the sparse /// array uses 4 x Universe bytes. /// /// When SparseT is uint8_t (the default), find() touches up to 2+[N/256] cache /// lines, but the sparse array is 4x smaller. N is the number of elements in /// the set. /// /// For sets that may grow to thousands of elements, SparseT should be set to /// uint16_t or uint32_t. /// /// @tparam ValueT The type of objects in the set. /// @tparam KeyFunctorT A functor that computes an unsigned index from KeyT. /// @tparam SparseT An unsigned integer type. See above. /// template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t> class SparseSet { … }; } // end namespace llvm #endif // LLVM_ADT_SPARSESET_H