7#ifndef PXR_BASE_TF_DENSE_HASH_SET_H
8#define PXR_BASE_TF_DENSE_HASH_SET_H
14#include "pxr/base/tf/hashmap.h"
19PXR_NAMESPACE_OPEN_SCOPE
35 class EqualElement = std::equal_to<Element>,
36 unsigned Threshold = 128
42 typedef Element value_type;
49 typedef std::vector<Element> _Vector;
52 typedef TfHashMap<Element, size_t, HashFn, EqualElement> _HashMap;
61 typedef typename _Vector::const_iterator
iterator;
74 const HashFn &hashFn = HashFn(),
75 const EqualElement &equalElement = EqualElement())
78 _equ() = equalElement;
84 : _storage(rhs._storage) {
86 _h = std::make_unique<_HashMap>(*rhs._h);
96 template <
class Iterator>
104 insert(l.begin(), l.end());
125 insert(l.begin(), l.end());
140 if (!rhs.
count(*iter))
148 return !(*
this == rhs);
161 _storage.swap(rhs._storage);
168 return _vec().empty();
174 return _vec().size();
180 return _vec().begin();
194 typename _HashMap::const_iterator iter = _h->find(k);
195 if (iter == _h->end())
198 return _vec().begin() + iter->second;
201 typename _Vector::const_iterator iter,
end = _vec().end();
203 for(iter = _vec().
begin(); iter !=
end; ++iter)
204 if (_equ()(*iter, k))
212 size_t count(
const Element &k)
const {
226 std::pair<typename _HashMap::iterator, bool> res =
227 _h->insert(std::make_pair(v,
size()));
242 _CreateTableIfNeeded();
250 template<
class IteratorType>
251 void insert(IteratorType i0, IteratorType i1) {
256 if (
size() + std::distance(i0, i1) >= Threshold)
260 for (IteratorType iter = i0; iter != i1; ++iter)
267 template <
class Iterator>
272 _CreateTableIfNeeded();
300 if (iter != std::prev(
end())) {
306 swap(*
const_cast<Element *
>(&(*iter)), _vec().back());
310 (*_h)[*iter] = iter - _vec().begin();
322 _h->erase(iter->first);
328 for(; vremain != _vec().end(); ++vremain)
329 (*_h)[*vremain] = vremain - _vec().begin();
338 _vec().shrink_to_fit();
346 if (sz < Threshold) {
353 _h.reset(
new _HashMap(sz, _hash(), _equ()));
354 for(
size_t i=0; i<sz; ++i)
355 (*_h)[_vec()[i]] = i;
363 return _vec()[index];
372 return _storage.vector;
381 EqualElement &_equ() {
386 const _Vector &_vec()
const {
387 return _storage.vector;
391 const HashFn &_hash()
const {
396 const EqualElement &_equ()
const {
401 inline void _CreateTableIfNeeded() {
402 if (
size() >= Threshold) {
409 inline void _CreateTable() {
411 _h.reset(
new _HashMap(Threshold, _hash(), _equ()));
412 for(
size_t i=0; i <
size(); ++i)
413 (*_h)[_vec()[i]] = i;
422 private EqualElement,
private HashFn {
423 static_assert(!std::is_same<EqualElement, HashFn>::value,
424 "EqualElement and HashFn must be distinct types.");
425 _CompressedStorage() =
default;
426 _CompressedStorage(
const EqualElement& equal,
const HashFn& hashFn)
427 : EqualElement(equal), HashFn(hashFn) {}
429 void swap(_CompressedStorage& other) {
431 vector.swap(other.vector);
432 swap(
static_cast<EqualElement&
>(*
this),
433 static_cast<EqualElement&
>(other));
434 swap(
static_cast<HashFn&
>(*
this),
static_cast<HashFn&
>(other));
439 _CompressedStorage _storage;
442 std::unique_ptr<_HashMap> _h;
445PXR_NAMESPACE_CLOSE_SCOPE
Define function attributes.
#define ARCH_EMPTY_BASES
Macro to begin the definition of a class that is using private inheritance to take advantage of the e...
This is a space efficient container that mimics the TfHashSet API that uses a vector for storage when...
size_t erase(const Element &k)
Erase element with key k.
size_t size() const
Returns the size of the set.
const_iterator begin() const
Returns an const_iterator pointing to the beginning of the set.
TfDenseHashSet(TfDenseHashSet &&rhs)=default
Move Ctor.
TfDenseHashSet(Iterator begin, Iterator end)
Construct from range.
const Element & operator[](size_t index) const
Index into set via index.
void insert(IteratorType i0, IteratorType i1)
Insert a range into the hash set.
size_t count(const Element &k) const
Returns the number of elements with key k.
TfDenseHashSet(const HashFn &hashFn=HashFn(), const EqualElement &equalElement=EqualElement())
Ctor.
void shrink_to_fit()
Optimize storage space.
bool empty() const
true if the set's size is 0.
void erase(const iterator &i0, const iterator &i1)
Erases a range from the set.
void erase(const iterator &iter)
Erases element pointed to by iter.
TfDenseHashSet & operator=(TfDenseHashSet &&rhs)=default
Move assignment operator.
void swap(TfDenseHashSet &rhs)
Swaps the contents of two sets.
std::pair< const_iterator, bool > insert_result
Return type for insert() method.
const_iterator find(const Element &k) const
Finds the element with key k.
TfDenseHashSet(std::initializer_list< Element > l)
Construct from an initializer_list.
_Vector::const_iterator const_iterator
A const_iterator type for this set.
bool operator==(const TfDenseHashSet &rhs) const
Equality operator.
insert_result insert(const value_type &v)
Returns a pair of <iterator, bool> where iterator points to the element in the list and bool is true ...
void clear()
Erases all of the elements.
TfDenseHashSet & operator=(const TfDenseHashSet &rhs)
Copy assignment operator.
void insert_unique(Iterator begin, Iterator end)
Insert a range of unique elements into the container.
const_iterator end() const
Returns an const_iterator pointing to the end of the set.
_Vector::const_iterator iterator
An iterator type for this set.
TfDenseHashSet(const TfDenseHashSet &rhs)
Copy Ctor.
TfDenseHashSet & operator=(std::initializer_list< Element > l)
Assignment from an initializer_list.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.