piranha
0.10
|
Hash set. More...
#include <piranha/hash_set.hpp>
Public Types | |
using | hasher = Hash |
Functor type for the calculation of hash values. | |
using | key_equal = Pred |
Functor type for comparing the items in the set. | |
using | key_type = T |
Key type. | |
using | size_type = std::size_t |
Size type. More... | |
using | iterator = iterator_impl< key_type const > |
Iterator type. More... | |
using | const_iterator = iterator |
Const iterator type. More... | |
using | local_iterator = typename list::const_iterator |
Local iterator. More... | |
Public Member Functions | |
hash_set (const hasher &h=hasher{}, const key_equal &k=key_equal{}) | |
Default constructor. More... | |
hash_set (const size_type &n_buckets, const hasher &h=hasher{}, const key_equal &k=key_equal{}, unsigned n_threads=1u) | |
Constructor from number of buckets. More... | |
hash_set (const hash_set &other) | |
Copy constructor. More... | |
hash_set (hash_set &&other) noexcept | |
Move constructor. More... | |
template<typename InputIterator > | |
hash_set (const InputIterator &begin, const InputIterator &end, const size_type &n_buckets=0u, const hasher &h=hasher{}, const key_equal &k=key_equal{}) | |
Constructor from range. More... | |
template<typename U > | |
hash_set (std::initializer_list< U > list) | |
Constructor from initializer list. More... | |
~hash_set () | |
Destructor. More... | |
hash_set & | operator= (const hash_set &other) |
Copy assignment operator. More... | |
hash_set & | operator= (hash_set &&other) noexcept |
Move assignment operator. More... | |
const_iterator | begin () const |
Const begin iterator. More... | |
const_iterator | end () const |
Const end iterator. More... | |
iterator | begin () |
Begin iterator. More... | |
iterator | end () |
End iterator. More... | |
size_type | size () const |
Number of elements contained in the set. More... | |
bool | empty () const |
Test for empty set. More... | |
size_type | bucket_count () const |
Number of buckets. More... | |
double | load_factor () const |
Load factor. More... | |
size_type | bucket (const key_type &k) const |
Index of destination bucket. More... | |
const_iterator | find (const key_type &k) const |
Find element. More... | |
iterator | find (const key_type &k) |
Find element. More... | |
double | max_load_factor () const |
Maximum load factor. More... | |
template<typename U , insert_enabler< U > = 0> | |
std::pair< iterator, bool > | insert (U &&k) |
Insert element. More... | |
iterator | erase (const_iterator it) |
Erase element. More... | |
void | clear () |
Remove all elements. More... | |
void | swap (hash_set &other) |
Swap content. More... | |
void | rehash (const size_type &new_size, unsigned n_threads=1u) |
Rehash set. More... | |
std::map< size_type, size_type > | evaluate_sparsity () const |
Get information on the sparsity of the set. More... | |
Low-level interface | |
using | _m_iterator = iterator_impl< key_type > |
Mutable iterator. More... | |
_m_iterator | _m_begin () |
Mutable begin iterator. More... | |
_m_iterator | _m_end () |
Mutable end iterator. More... | |
template<typename U , insert_enabler< U > = 0> | |
iterator | _unique_insert (U &&k, const size_type &bucket_idx) |
Insert unique element (low-level). More... | |
const_iterator | _find (const key_type &k, const size_type &bucket_idx) const |
Find element (low-level). More... | |
size_type | _bucket_from_hash (const std::size_t &hash) const |
Index of destination bucket from hash value. More... | |
size_type | _bucket (const key_type &k) const |
Index of destination bucket (low-level). More... | |
void | _update_size (const size_type &new_size) |
Force update of the number of elements. More... | |
void | _increase_size () |
Increase bucket count. More... | |
const list & | _get_bucket_list (const size_type &idx) const |
Const reference to list in bucket. More... | |
local_iterator | _erase (const_iterator it) |
Erase element. More... | |
Hash set.
Hash set class with interface similar to std::unordered_set
. The main points of difference with respect to std::unordered_set
are the following:
The implementation employs a separate chaining strategy consisting of an array of buckets, each one a singly linked list with the first node stored directly within the array (so that the first insertion in a bucket does not require any heap allocation).
An additional set of low-level methods is provided: such methods are suitable for use in high-performance and multi-threaded contexts, and, if misused, could lead to data corruption and other unpredictable errors.
Note that for performance reasons the implementation employs sizes that are powers of two. Hence, particular care should be taken that the hash function does not exhibit commensurabilities with powers of 2.
T
must satisfy piranha::is_container_element,Hash
must satisfy piranha::is_hash_function_object,Pred
must satisfy piranha::is_equality_function_object.This class provides the strong exception safety guarantee for all operations apart from methods involving insertion, which provide the basic guarantee (after a failed insertion, the set will be left in an unspecified but valid state).
Move construction and move assignment will leave the moved-from object equivalent to an empty set whose hasher and equality predicate have been moved-from.
Definition at line 122 of file hash_set.hpp.
using piranha::hash_set< T, Hash, Pred >::_m_iterator = iterator_impl<key_type> |
Mutable iterator.
This iterator type provides non-const access to the elements of the set. Please note that modifications to an existing element of the set might invalidate the relation between the element and its position in the set. After such modifications of one or more elements, the only valid operation is hash_set::clear() (destruction of the set before calling hash_set::clear() will lead to assertion failures in debug mode).
Definition at line 1212 of file hash_set.hpp.
using piranha::hash_set< T, Hash, Pred >::const_iterator = iterator |
using piranha::hash_set< T, Hash, Pred >::iterator = iterator_impl<key_type const> |
using piranha::hash_set< T, Hash, Pred >::local_iterator = typename list::const_iterator |
Local iterator.
Const iterator that can be used to iterate through a single bucket.
Definition at line 668 of file hash_set.hpp.
using piranha::hash_set< T, Hash, Pred >::size_type = std::size_t |
|
inline |
Default constructor.
If not specified, it will default-initialise the hasher and the equality predicate. The resulting hash set will be empty.
h | hasher functor. |
k | equality predicate. |
unspecified | any exception thrown by the copy constructors of Hash or Pred . |
Definition at line 679 of file hash_set.hpp.
|
inlineexplicit |
Constructor from number of buckets.
Will construct a set whose number of buckets is at least equal to n_buckets
. If n_threads
is not 1, then the first n_threads
threads from piranha::thread_pool will be used concurrently for the initialisation of the set.
n_buckets | desired number of buckets. |
h | hasher functor. |
k | equality predicate. |
n_threads | number of threads to use during initialisation. |
std::bad_alloc | if the desired number of buckets is greater than an implementation-defined maximum, or in case of memory errors. |
std::invalid_argument | if n_threads is zero. |
unspecified | any exception thrown by:
|
Definition at line 702 of file hash_set.hpp.
|
inline |
Copy constructor.
The hasher, the equality comparator and the allocator will also be copied.
other | piranha::hash_set that will be copied into this . |
unspecified | any exception thrown by memory allocation errors, the copy constructor of the stored type, Hash or Pred . |
Definition at line 717 of file hash_set.hpp.
|
inlinenoexcept |
Move constructor.
After the move, other
will have zero buckets and zero elements, and its hasher and equality predicate will have been used to move-construct their counterparts in this
.
other | set to be moved. |
Definition at line 756 of file hash_set.hpp.
|
inlineexplicit |
Constructor from range.
Create a set with a copy of a range.
begin | begin of range. |
end | end of range. |
n_buckets | number of initial buckets. |
h | hash functor. |
k | key equality predicate. |
std::bad_alloc | if the desired number of buckets is greater than an implementation-defined maximum. |
unspecified | any exception thrown by the copy constructors of Hash or Pred , or arising from calling insert() on the elements of the range. |
Definition at line 780 of file hash_set.hpp.
|
inlineexplicit |
Constructor from initializer list.
Will insert() all the elements of the initializer list, ignoring the return value of the operation. Hash functor and equality predicate will be default-constructed.
list | initializer list of elements to be inserted. |
std::bad_alloc | if the desired number of buckets is greater than an implementation-defined maximum. |
unspecified | any exception thrown by either insert() or of the default constructor of Hash or Pred . |
Definition at line 801 of file hash_set.hpp.
|
inline |
|
inline |
Index of destination bucket (low-level).
Equivalent to bucket(), with the exception that this method will not check if the number of buckets is zero.
k | input argument. |
k
.unspecified | any exception thrown by the call operator of the hasher. |
Definition at line 1331 of file hash_set.hpp.
|
inline |
Index of destination bucket from hash value.
Note that this method will not check if the number of buckets is zero.
hash | input hash value. |
hash
. Definition at line 1315 of file hash_set.hpp.
|
inline |
Erase element.
Erase the element to which it
points. it
must be a valid iterator pointing to an element of the set.
Erasing an element invalidates all iterators pointing to elements in the same bucket as the erased element.
This method will not update the number of elements in the set, nor it will try to access elements outside the bucket to which it
refers.
it | iterator to the element of the set to be removed. |
it
prior to the element being erased, or local end() if no such element exists. Definition at line 1394 of file hash_set.hpp.
|
inline |
Find element (low-level).
Locate element in the set. The parameter bucket_idx
is the index of the destination bucket for k
and, for a set with a nonzero number of buckets, must be equal to the output of bucket() before the insertion. This method will not check if the value of bucket_idx
is correct.
k | element to be located. |
bucket_idx | index of the destination bucket for k . |
k
's position in the set, or end() if k
is not in the set.unspecified | any exception thrown by calling the equality predicate. |
Definition at line 1291 of file hash_set.hpp.
|
inline |
Const reference to list in bucket.
idx | index of the bucket whose list will be returned. |
idx
. Definition at line 1372 of file hash_set.hpp.
|
inline |
Increase bucket count.
Increase the number of buckets to the next implementation-defined value.
std::bad_alloc | if the operation results in a resize of the set past an implementation-defined maximum number of buckets. |
unspecified | any exception thrown by rehash(). |
Definition at line 1353 of file hash_set.hpp.
|
inline |
Mutable begin iterator.
Definition at line 1217 of file hash_set.hpp.
|
inline |
Mutable end iterator.
Definition at line 1241 of file hash_set.hpp.
|
inline |
Insert unique element (low-level).
T
and U
are the same type, aside from cv qualifications and references.The parameter bucket_idx
is the index of the destination bucket for k
and, for a set with a nonzero number of buckets, must be equal to the output of bucket() before the insertion.
This method will not check if a key equivalent to k
already exists in the set, it will not update the number of elements present in the set after the insertion, it will not resize the set in case the maximum load factor is exceeded, nor it will check if the value of bucket_idx
is correct.
k | object that will be inserted into the set. |
bucket_idx | destination bucket for k . |
unspecified | any exception thrown by the copy constructor of hash_set::key_type or by memory allocation errors. |
Definition at line 1269 of file hash_set.hpp.
|
inline |
Force update of the number of elements.
After this call, size() will return new_size
regardless of the true number of elements in the set.
new_size | new set size. |
Definition at line 1341 of file hash_set.hpp.
|
inline |
Const begin iterator.
Definition at line 859 of file hash_set.hpp.
|
inline |
Begin iterator.
Definition at line 891 of file hash_set.hpp.
|
inline |
Index of destination bucket.
Index to which k
would belong, were it to be inserted into the set. The index of the destination bucket is the hash value reduced modulo the bucket count.
k | input argument. |
k
.piranha::zero_division_error | if bucket_count() returns zero. |
unspecified | any exception thrown by _bucket(). |
Definition at line 948 of file hash_set.hpp.
|
inline |
Number of buckets.
Definition at line 923 of file hash_set.hpp.
|
inline |
Remove all elements.
After this call, size() and bucket_count() will both return zero.
Definition at line 1106 of file hash_set.hpp.
|
inline |
Test for empty set.
true
if size() returns 0, false
otherwise. Definition at line 915 of file hash_set.hpp.
|
inline |
Const end iterator.
Definition at line 883 of file hash_set.hpp.
|
inline |
End iterator.
Definition at line 899 of file hash_set.hpp.
|
inline |
Erase element.
Erase the element to which it
points. it
must be a valid iterator pointing to an element of the set.
Erasing an element invalidates all iterators pointing to elements in the same bucket as the erased element.
After the operation has taken place, the size() of the set will be decreased by one.
it | iterator to the element of the set to be removed. |
it
prior to the element being erased, or end() if no such element exists. Definition at line 1065 of file hash_set.hpp.
|
inline |
Get information on the sparsity of the set.
std::map<size_type,size_type>
in which the key is the number of elements stored in a bucket and the mapped type the number of buckets containing those many elements.unspecified | any exception thrown by memory errors in standard containers. |
Definition at line 1186 of file hash_set.hpp.
|
inline |
Find element.
k | element to be located. |
k
's position in the set, or end() if k
is not in the set.Definition at line 963 of file hash_set.hpp.
|
inline |
Find element.
k | element to be located. |
k
's position in the set, or end() if k
is not in the set.unspecified | any exception thrown by _find(). |
Definition at line 978 of file hash_set.hpp.
|
inline |
Insert element.
T
and U
are the same type, aside from cv qualifications and references.If no other key equivalent to k
exists in the set, the insertion is successful and returns the (it,true)
pair - where it
is the position in the set into which the object has been inserted. Otherwise, the return value will be (it,false)
- where it
is the position of the existing equivalent object.
k | object that will be inserted into the set. |
(hash_set::iterator,bool)
pair containing an iterator to the newly-inserted object (or its existing equivalent) and the result of the operation.unspecified | any exception thrown by:
|
std::overflow_error | if a successful insertion would result in size() exceeding the maximum value representable by type piranha::hash_set::size_type. |
std::bad_alloc | if the operation results in a resize of the set past an implementation-defined maximum number of buckets. |
Definition at line 1020 of file hash_set.hpp.
|
inline |
Load factor.
(double)size() / bucket_count()
, or 0 if the set is empty. Definition at line 931 of file hash_set.hpp.
|
inline |
Maximum load factor.
Definition at line 986 of file hash_set.hpp.
|
inline |
Copy assignment operator.
other | assignment argument. |
this
.unspecified | any exception thrown by the copy constructor. |
Definition at line 827 of file hash_set.hpp.
|
inlinenoexcept |
Move assignment operator.
other | set to be moved into this . |
this
. Definition at line 841 of file hash_set.hpp.
|
inline |
Rehash set.
Change the number of buckets in the set to at least new_size
. No rehash is performed if rehashing would lead to exceeding the maximum load factor. If n_threads
is not 1, then the first n_threads
threads from piranha::thread_pool will be used concurrently during the rehash operation.
new_size | new desired number of buckets. |
n_threads | number of threads to use. |
std::invalid_argument | if n_threads is zero. |
unspecified | any exception thrown by the constructor from number of buckets, _unique_insert() or _bucket(). |
Definition at line 1142 of file hash_set.hpp.
|
inline |
Number of elements contained in the set.
Definition at line 907 of file hash_set.hpp.
|
inline |
Swap content.
Will use std::swap
to swap hasher and equality predicate.
other | swap argument. |
unspecified | any exception thrown by swapping hasher or equality predicate via std::swap . |
Definition at line 1122 of file hash_set.hpp.