Class Template unordered_flat_map
boost::unordered_flat_map
— An open-addressing unordered associative container that associates unique keys with another value.
The performance of boost::unordered_flat_map
is much better than that of boost::unordered_map
or other implementations of std::unordered_map
. Unlike standard unordered associative containers,
which are node-based, the elements of a boost::unordered_flat_map
are held directly in the bucket
array, and insertions into an already occupied bucket are diverted to available buckets in the
vicinity of the original position. This type of data layout is known as open addressing.
As a result of its using open addressing, the interface of boost::unordered_flat_map
deviates in
a number of aspects from that of boost::unordered_map
/std::unordered_map
:
-
value_type
must be move-constructible. -
Pointer stability is not kept under rehashing.
-
begin()
is not constant-time. -
There is no API for bucket handling (except
bucket_count
) or node extraction/insertion. -
The maximum load factor of the container is managed internally and can’t be set by the user.
Other than this, boost::unordered_flat_map
is mostly a drop-in replacement of node-based standard
unordered associative containers.
Synopsis
// #include <boost/unordered/unordered_flat_map.hpp>
namespace boost {
namespace unordered {
template<class Key,
class T,
class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
class unordered_flat_map {
public:
// types
using key_type = Key;
using mapped_type = T;
using value_type = std::pair<const Key, T>;
using init_type = std::pair<
typename std::remove_const<Key>::type,
typename std::remove_const<T>::type
>;
using hasher = Hash;
using key_equal = Pred;
using allocator_type = Allocator;
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using reference = value_type&;
using const_reference = const value_type&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using iterator = implementation-defined;
using const_iterator = implementation-defined;
using stats = stats-type; // if statistics are enabled
// construct/copy/destroy
unordered_flat_map();
explicit unordered_flat_map(size_type n,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& a = allocator_type());
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l,
size_type n = implementation-defined,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& a = allocator_type());
unordered_flat_map(const unordered_flat_map& other);
unordered_flat_map(unordered_flat_map&& other);
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l, const allocator_type& a);
explicit unordered_flat_map(const Allocator& a);
unordered_flat_map(const unordered_flat_map& other, const Allocator& a);
unordered_flat_map(unordered_flat_map&& other, const Allocator& a);
unordered_flat_map(concurrent_flat_map<Key, T, Hash, Pred, Allocator>&& other);
unordered_flat_map(std::initializer_list<value_type> il,
size_type n = implementation-defined
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& a = allocator_type());
unordered_flat_map(size_type n, const allocator_type& a);
unordered_flat_map(size_type n, const hasher& hf, const allocator_type& a);
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l, size_type n, const allocator_type& a);
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l, size_type n, const hasher& hf,
const allocator_type& a);
unordered_flat_map(std::initializer_list<value_type> il, const allocator_type& a);
unordered_flat_map(std::initializer_list<value_type> il, size_type n,
const allocator_type& a);
unordered_flat_map(std::initializer_list<value_type> il, size_type n, const hasher& hf,
const allocator_type& a);
~unordered_flat_map();
unordered_flat_map& operator=(const unordered_flat_map& other);
unordered_flat_map& operator=(unordered_flat_map&& other) noexcept(
(boost::allocator_traits<Allocator>::is_always_equal::value ||
boost::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) &&
std::is_same<pointer, value_type*>::value);
unordered_flat_map& operator=(std::initializer_list<value_type>);
allocator_type get_allocator() const noexcept;
// iterators
iterator begin() noexcept;
const_iterator begin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
// capacity
[[nodiscard]] bool empty() const noexcept;
size_type size() const noexcept;
size_type max_size() const noexcept;
// modifiers
template<class... Args> std::pair<iterator, bool> emplace(Args&&... args);
template<class... Args> iterator emplace_hint(const_iterator position, Args&&... args);
std::pair<iterator, bool> insert(const value_type& obj);
std::pair<iterator, bool> insert(const init_type& obj);
std::pair<iterator, bool> insert(value_type&& obj);
std::pair<iterator, bool> insert(init_type&& obj);
iterator insert(const_iterator hint, const value_type& obj);
iterator insert(const_iterator hint, const init_type& obj);
iterator insert(const_iterator hint, value_type&& obj);
iterator insert(const_iterator hint, init_type&& obj);
template<class InputIterator> void insert(InputIterator first, InputIterator last);
void insert(std::initializer_list<value_type>);
template<class... Args>
std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);
template<class... Args>
std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args);
template<class K, class... Args>
std::pair<iterator, bool> try_emplace(K&& k, Args&&... args);
template<class... Args>
iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args);
template<class... Args>
iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args);
template<class K, class... Args>
iterator try_emplace(const_iterator hint, K&& k, Args&&... args);
template<class M>
std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj);
template<class M>
std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj);
template<class K, class M>
std::pair<iterator, bool> insert_or_assign(K&& k, M&& obj);
template<class M>
iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj);
template<class M>
iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj);
template<class K, class M>
iterator insert_or_assign(const_iterator hint, K&& k, M&& obj);
convertible-to-iterator erase(iterator position);
convertible-to-iterator erase(const_iterator position);
size_type erase(const key_type& k);
template<class K> size_type erase(K&& k);
iterator erase(const_iterator first, const_iterator last);
void swap(unordered_flat_map& other)
noexcept(boost::allocator_traits<Allocator>::is_always_equal::value ||
boost::allocator_traits<Allocator>::propagate_on_container_swap::value);
void clear() noexcept;
template<class H2, class P2>
void merge(unordered_flat_map<Key, T, H2, P2, Allocator>& source);
template<class H2, class P2>
void merge(unordered_flat_map<Key, T, H2, P2, Allocator>&& source);
// observers
hasher hash_function() const;
key_equal key_eq() const;
// map operations
iterator find(const key_type& k);
const_iterator find(const key_type& k) const;
template<class K>
iterator find(const K& k);
template<class K>
const_iterator find(const K& k) const;
size_type count(const key_type& k) const;
template<class K>
size_type count(const K& k) const;
bool contains(const key_type& k) const;
template<class K>
bool contains(const K& k) const;
std::pair<iterator, iterator> equal_range(const key_type& k);
std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
template<class K>
std::pair<iterator, iterator> equal_range(const K& k);
template<class K>
std::pair<const_iterator, const_iterator> equal_range(const K& k) const;
// element access
mapped_type& operator[](const key_type& k);
mapped_type& operator[](key_type&& k);
template<class K> mapped_type& operator[](K&& k);
mapped_type& at(const key_type& k);
const mapped_type& at(const key_type& k) const;
template<class K> mapped_type& at(const K& k);
template<class K> const mapped_type& at(const K& k) const;
// bucket interface
size_type bucket_count() const noexcept;
// hash policy
float load_factor() const noexcept;
float max_load_factor() const noexcept;
void max_load_factor(float z);
size_type max_load() const noexcept;
void rehash(size_type n);
void reserve(size_type n);
// statistics (if enabled)
stats get_stats() const;
void reset_stats() noexcept;
};
// Deduction Guides
template<class InputIterator,
class Hash = boost::hash<iter-key-type<InputIterator>>,
class Pred = std::equal_to<iter-key-type<InputIterator>>,
class Allocator = std::allocator<iter-to-alloc-type<InputIterator>>>
unordered_flat_map(InputIterator, InputIterator, typename see below::size_type = see below,
Hash = Hash(), Pred = Pred(), Allocator = Allocator())
-> unordered_flat_map<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>, Hash,
Pred, Allocator>;
template<class Key, class T, class Hash = boost::hash<Key>,
class Pred = std::equal_to<Key>,
class Allocator = std::allocator<std::pair<const Key, T>>>
unordered_flat_map(std::initializer_list<std::pair<Key, T>>,
typename see below::size_type = see below, Hash = Hash(),
Pred = Pred(), Allocator = Allocator())
-> unordered_flat_map<Key, T, Hash, Pred, Allocator>;
template<class InputIterator, class Allocator>
unordered_flat_map(InputIterator, InputIterator, typename see below::size_type, Allocator)
-> unordered_flat_map<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>,
boost::hash<iter-key-type<InputIterator>>,
std::equal_to<iter-key-type<InputIterator>>, Allocator>;
template<class InputIterator, class Allocator>
unordered_flat_map(InputIterator, InputIterator, Allocator)
-> unordered_flat_map<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>,
boost::hash<iter-key-type<InputIterator>>,
std::equal_to<iter-key-type<InputIterator>>, Allocator>;
template<class InputIterator, class Hash, class Allocator>
unordered_flat_map(InputIterator, InputIterator, typename see below::size_type, Hash,
Allocator)
-> unordered_flat_map<iter-key-type<InputIterator>, iter-mapped-type<InputIterator>, Hash,
std::equal_to<iter-key-type<InputIterator>>, Allocator>;
template<class Key, class T, class Allocator>
unordered_flat_map(std::initializer_list<std::pair<Key, T>>, typename see below::size_type,
Allocator)
-> unordered_flat_map<Key, T, boost::hash<Key>, std::equal_to<Key>, Allocator>;
template<class Key, class T, class Allocator>
unordered_flat_map(std::initializer_list<std::pair<Key, T>>, Allocator)
-> unordered_flat_map<Key, T, boost::hash<Key>, std::equal_to<Key>, Allocator>;
template<class Key, class T, class Hash, class Allocator>
unordered_flat_map(std::initializer_list<std::pair<Key, T>>, typename see below::size_type,
Hash, Allocator)
-> unordered_flat_map<Key, T, Hash, std::equal_to<Key>, Allocator>;
} // namespace unordered
} // namespace boost
Description
Template Parameters
Key |
|
T |
|
Hash |
A unary function object type that acts a hash function for a |
Pred |
A binary function object that induces an equivalence relation on values of type |
Allocator |
An allocator whose value type is the same as the container’s value type. Allocators using fancy pointers are supported. |
The elements of the container are held into an internal bucket array. An element is inserted into a bucket determined by its hash code, but if the bucket is already occupied (a collision), an available one in the vicinity of the original position is used.
The size of the bucket array can be automatically increased by a call to insert
/emplace
, or as a result of calling
rehash
/reserve
. The load factor of the container (number of elements divided by number of buckets) is never
greater than max_load_factor()
, except possibly for small sizes where the implementation may decide to
allow for higher loads.
If hash_is_avalanching<Hash>::value
is true
, the hash function
is used as-is; otherwise, a bit-mixing post-processing stage is added to increase the quality of hashing
at the expense of extra computational cost.
Configuration Macros
BOOST_UNORDERED_ENABLE_STATS
Globally define this macro to enable statistics calculation for the container. Note that this option decreases the overall performance of many operations.
Typedefs
typedef implementation-defined iterator;
An iterator whose value type is value_type
.
The iterator category is at least a forward iterator.
Convertible to const_iterator
.
typedef implementation-defined const_iterator;
A constant iterator whose value type is value_type
.
The iterator category is at least a forward iterator.
Constructors
Default Constructor
unordered_flat_map();
Constructs an empty container using hasher()
as the hash function,
key_equal()
as the key equality predicate and allocator_type()
as the allocator.
Postconditions: |
|
Requires: |
If the defaults are used, |
Bucket Count Constructor
explicit unordered_flat_map(size_type n,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& a = allocator_type());
Constructs an empty container with at least n
buckets, using hf
as the hash
function, eql
as the key equality predicate, and a
as the allocator.
Postconditions: |
|
Requires: |
If the defaults are used, |
Iterator Range Constructor
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l,
size_type n = implementation-defined,
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& a = allocator_type());
Constructs an empty container with at least n
buckets, using hf
as the hash function, eql
as the key equality predicate and a
as the allocator, and inserts the elements from [f, l)
into it.
Requires: |
If the defaults are used, |
Copy Constructor
unordered_flat_map(unordered_flat_map const& other);
The copy constructor. Copies the contained elements, hash function, predicate and allocator.
If Allocator::select_on_container_copy_construction
exists and has the right signature, the allocator will be constructed from its result.
Requires: |
|
Move Constructor
unordered_flat_map(unordered_flat_map&& other);
The move constructor. The internal bucket array of other
is transferred directly to the new container.
The hash function, predicate and allocator are moved-constructed from other
.
If statistics are enabled,
transfers the internal statistical information from other
and calls other.reset_stats()
.
Iterator Range Constructor with Allocator
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l, const allocator_type& a);
Constructs an empty container using a
as the allocator, with the default hash function and key equality predicate and inserts the elements from [f, l)
into it.
Requires: |
|
Allocator Constructor
explicit unordered_flat_map(Allocator const& a);
Constructs an empty container, using allocator a
.
Copy Constructor with Allocator
unordered_flat_map(unordered_flat_map const& other, Allocator const& a);
Constructs a container, copying other
's contained elements, hash function, and predicate, but using allocator a
.
Move Constructor with Allocator
unordered_flat_map(unordered_flat_map&& other, Allocator const& a);
If a == other.get_allocator()
, the elements of other
are transferred directly to the new container;
otherwise, elements are moved-constructed from those of other
. The hash function and predicate are moved-constructed
from other
, and the allocator is copy-constructed from a
.
If statistics are enabled,
transfers the internal statistical information from other
iff a == other.get_allocator()
,
and always calls other.reset_stats()
.
Move Constructor from concurrent_flat_map
unordered_flat_map(concurrent_flat_map<Key, T, Hash, Pred, Allocator>&& other);
Move construction from a concurrent_flat_map
.
The internal bucket array of other
is transferred directly to the new container.
The hash function, predicate and allocator are moved-constructed from other
.
If statistics are enabled,
transfers the internal statistical information from other
and calls other.reset_stats()
.
Complexity: |
Constant time. |
Concurrency: |
Blocking on |
Initializer List Constructor
unordered_flat_map(std::initializer_list<value_type> il,
size_type n = implementation-defined
const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type& a = allocator_type());
Constructs an empty container with at least n
buckets, using hf
as the hash function, eql
as the key equality predicate and a
, and inserts the elements from il
into it.
Requires: |
If the defaults are used, |
Bucket Count Constructor with Allocator
unordered_flat_map(size_type n, allocator_type const& a);
Constructs an empty container with at least n
buckets, using hf
as the hash function, the default hash function and key equality predicate and a
as the allocator.
Postconditions: |
|
Requires: |
|
Bucket Count Constructor with Hasher and Allocator
unordered_flat_map(size_type n, hasher const& hf, allocator_type const& a);
Constructs an empty container with at least n
buckets, using hf
as the hash function, the default key equality predicate and a
as the allocator.
Postconditions: |
|
Requires: |
|
Iterator Range Constructor with Bucket Count and Allocator
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l, size_type n, const allocator_type& a);
Constructs an empty container with at least n
buckets, using a
as the allocator and default hash function and key equality predicate, and inserts the elements from [f, l)
into it.
Requires: |
|
Iterator Range Constructor with Bucket Count and Hasher
template<class InputIterator>
unordered_flat_map(InputIterator f, InputIterator l, size_type n, const hasher& hf,
const allocator_type& a);
Constructs an empty container with at least n
buckets, using hf
as the hash function, a
as the allocator, with the default key equality predicate, and inserts the elements from [f, l)
into it.
Requires: |
|
initializer_list Constructor with Allocator
unordered_flat_map(std::initializer_list<value_type> il, const allocator_type& a);
Constructs an empty container using a
and default hash function and key equality predicate, and inserts the elements from il
into it.
Requires: |
|
initializer_list Constructor with Bucket Count and Allocator
unordered_flat_map(std::initializer_list<value_type> il, size_type n, const allocator_type& a);
Constructs an empty container with at least n
buckets, using a
and default hash function and key equality predicate, and inserts the elements from il
into it.
Requires: |
|
initializer_list Constructor with Bucket Count and Hasher and Allocator
unordered_flat_map(std::initializer_list<value_type> il, size_type n, const hasher& hf,
const allocator_type& a);
Constructs an empty container with at least n
buckets, using hf
as the hash function, a
as the allocator and default key equality predicate,and inserts the elements from il
into it.
Requires: |
|
Destructor
~unordered_flat_map();
Note: |
The destructor is applied to every element, and all memory is deallocated |
Assignment
Copy Assignment
unordered_flat_map& operator=(unordered_flat_map const& other);
The assignment operator. Destroys previously existing elements, copy-assigns the hash function and predicate from other
,
copy-assigns the allocator from other
if Alloc::propagate_on_container_copy_assignment
exists and Alloc::propagate_on_container_copy_assignment::value
is true
,
and finally inserts copies of the elements of other
.
Requires: |
|
Move Assignment
unordered_flat_map& operator=(unordered_flat_map&& other)
noexcept((boost::allocator_traits<Allocator>::is_always_equal::value ||
boost::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) &&
std::is_same<pointer, value_type*>::value);
The move assignment operator. Destroys previously existing elements, swaps the hash function and predicate from other
,
and move-assigns the allocator from other
if Alloc::propagate_on_container_move_assignment
exists and Alloc::propagate_on_container_move_assignment::value
is true
.
If at this point the allocator is equal to other.get_allocator()
, the internal bucket array of other
is transferred directly to the new container;
otherwise, inserts move-constructed copies of the elements of other
.
If statistics are enabled,
transfers the internal statistical information from other
iff the final allocator is equal to other.get_allocator()
,
and always calls other.reset_stats()
.
Initializer List Assignment
unordered_flat_map& operator=(std::initializer_list<value_type> il);
Assign from values in initializer list. All previously existing elements are destroyed.
Requires: |
|
Iterators
begin
iterator begin() noexcept;
const_iterator begin() const noexcept;
Returns: |
An iterator referring to the first element of the container, or if the container is empty the past-the-end value for the container. |
Complexity: |
O( |
end
iterator end() noexcept;
const_iterator end() const noexcept;
Returns: |
An iterator which refers to the past-the-end value for the container. |
Modifiers
emplace
template<class... Args> std::pair<iterator, bool> emplace(Args&&... args);
Inserts an object, constructed with the arguments args
, in the container if and only if there is no element in the container with an equivalent key.
Requires: |
|
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. If |
emplace_hint
template<class... Args> iterator emplace_hint(const_iterator position, Args&&... args);
Inserts an object, constructed with the arguments args
, in the container if and only if there is no element in the container with an equivalent key.
position
is a suggestion to where the element should be inserted. This implementation ignores it.
Requires: |
|
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. If |
Copy Insert
std::pair<iterator, bool> insert(const value_type& obj);
std::pair<iterator, bool> insert(const init_type& obj);
Inserts obj
in the container if and only if there is no element in the container with an equivalent key.
Requires: |
|
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. A call of the form |
Move Insert
std::pair<iterator, bool> insert(value_type&& obj);
std::pair<iterator, bool> insert(init_type&& obj);
Inserts obj
in the container if and only if there is no element in the container with an equivalent key.
Requires: |
|
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. A call of the form |
Copy Insert with Hint
iterator insert(const_iterator hint, const value_type& obj);
iterator insert(const_iterator hint, const init_type& obj);
Inserts obj
in the container if and only if there is no element in the container with an equivalent key.
hint
is a suggestion to where the element should be inserted. This implementation ignores it.
Requires: |
|
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. A call of the form |
Move Insert with Hint
iterator insert(const_iterator hint, value_type&& obj);
iterator insert(const_iterator hint, init_type&& obj);
Inserts obj
in the container if and only if there is no element in the container with an equivalent key.
hint
is a suggestion to where the element should be inserted. This implementation ignores it.
Requires: |
|
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. A call of the form |
Insert Iterator Range
template<class InputIterator> void insert(InputIterator first, InputIterator last);
Inserts a range of elements into the container. Elements are inserted if and only if there is no element in the container with an equivalent key.
Requires: |
|
Throws: |
When inserting a single element, if an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. |
Insert Initializer List
void insert(std::initializer_list<value_type>);
Inserts a range of elements into the container. Elements are inserted if and only if there is no element in the container with an equivalent key.
Requires: |
|
Throws: |
When inserting a single element, if an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. |
try_emplace
template<class... Args>
std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);
template<class... Args>
std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args);
template<class K, class... Args>
std::pair<iterator, bool> try_emplace(K&& k, Args&&... args);
Inserts a new element into the container if there is no existing element with key k
contained within it.
If there is an existing element with key k
this function does nothing.
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
This function is similiar to emplace, with the difference that no
unlike emplace, which simply forwards all arguments to Can invalidate iterators pointers and references, but only if the insert causes the load to be greater than the maximum load. The |
try_emplace with Hint
template<class... Args>
iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args);
template<class... Args>
iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args);
template<class K, class... Args>
iterator try_emplace(const_iterator hint, K&& k, Args&&... args);
Inserts a new element into the container if there is no existing element with key k
contained within it.
If there is an existing element with key k
this function does nothing.
hint
is a suggestion to where the element should be inserted. This implementation ignores it.
Returns: |
If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
This function is similiar to emplace_hint, with the difference that no
unlike emplace_hint, which simply forwards all arguments to Can invalidate iterators pointers and references, but only if the insert causes the load to be greater than the maximum load. The |
insert_or_assign
template<class M>
std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj);
template<class M>
std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj);
template<class K, class M>
std::pair<iterator, bool> insert_or_assign(K&& k, M&& obj);
Inserts a new element into the container or updates an existing one by assigning to the contained value.
If there is an element with key k
, then it is updated by assigning std::forward<M>(obj)
.
If there is no such element, it is added to the container as:
// first two overloads
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Key>(k)),
std::forward_as_tuple(std::forward<M>(obj)))
// third overload
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<M>(obj)))
Returns: |
The If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators pointers and references, but only if the insert causes the load to be greater than the maximum load. The |
insert_or_assign with Hint
template<class M>
iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj);
template<class M>
iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj);
template<class K, class M>
iterator insert_or_assign(const_iterator hint, K&& k, M&& obj);
Inserts a new element into the container or updates an existing one by assigning to the contained value.
If there is an element with key k
, then it is updated by assigning std::forward<M>(obj)
.
If there is no such element, it is added to the container as:
// first two overloads
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<Key>(k)),
std::forward_as_tuple(std::forward<M>(obj)))
// third overload
value_type(std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<M>(obj)))
hint
is a suggestion to where the element should be inserted. This implementation ignores it.
Returns: |
If an insert took place, then the iterator points to the newly inserted element. Otherwise, it points to the element with equivalent key. |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. The |
Erase by Position
convertible-to-iterator erase(iterator position);
convertible-to-iterator erase(const_iterator position);
Erase the element pointed to by position
.
Returns: |
An opaque object implicitly convertible to the |
Throws: |
Nothing. |
Notes: |
The opaque object returned must only be discarded or immediately converted to |
Erase by Key
size_type erase(const key_type& k);
template<class K> size_type erase(K&& k);
Erase all elements with key equivalent to k
.
Returns: |
The number of elements erased. |
Throws: |
Only throws an exception if it is thrown by |
Notes: |
The |
Erase Range
iterator erase(const_iterator first, const_iterator last);
Erases the elements in the range from first
to last
.
Returns: |
The iterator following the erased elements - i.e. |
Throws: |
Nothing in this implementation (neither the |
swap
void swap(unordered_flat_map& other)
noexcept(boost::allocator_traits<Allocator>::is_always_equal::value ||
boost::allocator_traits<Allocator>::propagate_on_container_swap::value);
Swaps the contents of the container with the parameter.
If Allocator::propagate_on_container_swap
is declared and Allocator::propagate_on_container_swap::value
is true
then the containers' allocators are swapped. Otherwise, swapping with unequal allocators results in undefined behavior.
Throws: |
Nothing unless |
clear
void clear() noexcept;
Erases all elements in the container.
Postconditions: |
|
merge
template<class H2, class P2>
void merge(unordered_flat_map<Key, T, H2, P2, Allocator>& source);
template<class H2, class P2>
void merge(unordered_flat_map<Key, T, H2, P2, Allocator>&& source);
Move-inserts all the elements from source
whose key is not already present in *this
, and erases them from source
.
Lookup
find
iterator find(const key_type& k);
const_iterator find(const key_type& k) const;
template<class K>
iterator find(const K& k);
Returns: |
An iterator pointing to an element with key equivalent to |
Notes: |
The |
count
size_type count(const key_type& k) const;
template<class K>
size_type count(const K& k) const;
Returns: |
The number of elements with key equivalent to |
Notes: |
The |
contains
bool contains(const key_type& k) const;
template<class K>
bool contains(const K& k) const;
Returns: |
A boolean indicating whether or not there is an element with key equal to |
Notes: |
The |
equal_range
std::pair<iterator, iterator> equal_range(const key_type& k);
std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
template<class K>
std::pair<iterator, iterator> equal_range(const K& k);
template<class K>
std::pair<const_iterator, const_iterator> equal_range(const K& k) const;
Returns: |
A range containing all elements with key equivalent to |
Notes: |
The |
operator[]
mapped_type& operator[](const key_type& k);
mapped_type& operator[](key_type&& k);
template<class K> mapped_type& operator[](K&& k);
Effects: |
If the container does not already contain an element with a key equivalent to |
Returns: |
A reference to |
Throws: |
If an exception is thrown by an operation other than a call to |
Notes: |
Can invalidate iterators, pointers and references, but only if the insert causes the load to be greater than the maximum load. The |
at
mapped_type& at(const key_type& k);
const mapped_type& at(const key_type& k) const;
template<class K> mapped_type& at(const K& k);
template<class K> const mapped_type& at(const K& k) const;
Returns: |
A reference to |
Throws: |
An exception object of type |
Notes: |
The |
Hash Policy
load_factor
float load_factor() const noexcept;
Returns: |
|
max_load_factor
float max_load_factor() const noexcept;
Returns: |
Returns the container’s maximum load factor. |
Set max_load_factor
void max_load_factor(float z);
Effects: |
Does nothing, as the user is not allowed to change this parameter. Kept for compatibility with |
max_load
size_type max_load() const noexcept;
Returns: |
The maximum number of elements the container can hold without rehashing, assuming that no further elements will be erased. |
Note: |
After construction, rehash or clearance, the container’s maximum load is at least |
rehash
void rehash(size_type n);
Changes if necessary the size of the bucket array so that there are at least n
buckets, and so that the load factor is less than or equal to the maximum load factor. When applicable, this will either grow or shrink the bucket_count()
associated with the container.
When size() == 0
, rehash(0)
will deallocate the underlying buckets array. If the provided Allocator uses fancy pointers, a default allocation is subsequently performed.
Invalidates iterators, pointers and references, and changes the order of elements.
Throws: |
The function has no effect if an exception is thrown, unless it is thrown by the container’s hash function or comparison function. |
reserve
void reserve(size_type n);
Equivalent to a.rehash(ceil(n / a.max_load_factor()))
.
Similar to rehash
, this function can be used to grow or shrink the number of buckets in the container.
Invalidates iterators, pointers and references, and changes the order of elements.
Throws: |
The function has no effect if an exception is thrown, unless it is thrown by the container’s hash function or comparison function. |
Statistics
get_stats
stats get_stats() const;
Returns: |
A statistical description of the insertion and lookup operations performed by the container so far. |
Notes: |
Only available if statistics calculation is enabled. |
reset_stats
void reset_stats() noexcept;
Effects: |
Sets to zero the internal statistics kept by the container. |
Notes: |
Only available if statistics calculation is enabled. |
Deduction Guides
A deduction guide will not participate in overload resolution if any of the following are true:
-
It has an
InputIterator
template parameter and a type that does not qualify as an input iterator is deduced for that parameter. -
It has an
Allocator
template parameter and a type that does not qualify as an allocator is deduced for that parameter. -
It has a
Hash
template parameter and an integral type or a type that qualifies as an allocator is deduced for that parameter. -
It has a
Pred
template parameter and a type that qualifies as an allocator is deduced for that parameter.
A size_type
parameter type in a deduction guide refers to the size_type
member type of the
container type deduced by the deduction guide. Its default value coincides with the default value
of the constructor selected.
iter-value-type
template<class InputIterator> using iter-value-type = typename std::iterator_traits<InputIterator>::value_type; // exposition only
iter-key-type
template<class InputIterator> using iter-key-type = std::remove_const_t< std::tuple_element_t<0, iter-value-type<InputIterator>>>; // exposition only
iter-mapped-type
template<class InputIterator> using iter-mapped-type = std::tuple_element_t<1, iter-value-type<InputIterator>>; // exposition only
iter-to-alloc-type
template<class InputIterator> using iter-to-alloc-type = std::pair< std::add_const_t<std::tuple_element_t<0, iter-value-type<InputIterator>>>, std::tuple_element_t<1, iter-value-type<InputIterator>>>; // exposition only
Equality Comparisons
operator==
template<class Key, class T, class Hash, class Pred, class Alloc>
bool operator==(const unordered_flat_map<Key, T, Hash, Pred, Alloc>& x,
const unordered_flat_map<Key, T, Hash, Pred, Alloc>& y);
Return true
if x.size() == y.size()
and for every element in x
, there is an element in y
with the same key, with an equal value (using operator==
to compare the value types).
Notes: |
Behavior is undefined if the two containers don’t have equivalent equality predicates. |
operator!=
template<class Key, class T, class Hash, class Pred, class Alloc>
bool operator!=(const unordered_flat_map<Key, T, Hash, Pred, Alloc>& x,
const unordered_flat_map<Key, T, Hash, Pred, Alloc>& y);
Return false
if x.size() == y.size()
and for every element in x
, there is an element in y
with the same key, with an equal value (using operator==
to compare the value types).
Notes: |
Behavior is undefined if the two containers don’t have equivalent equality predicates. |
Swap
template<class Key, class T, class Hash, class Pred, class Alloc>
void swap(unordered_flat_map<Key, T, Hash, Pred, Alloc>& x,
unordered_flat_map<Key, T, Hash, Pred, Alloc>& y)
noexcept(noexcept(x.swap(y)));
Swaps the contents of x
and y
.
If Allocator::propagate_on_container_swap
is declared and Allocator::propagate_on_container_swap::value
is true
then the containers' allocators are swapped. Otherwise, swapping with unequal allocators results in undefined behavior.
Effects: |
|
Throws: |
Nothing unless |
erase_if
template<class K, class T, class H, class P, class A, class Predicate>
typename unordered_flat_map<K, T, H, P, A>::size_type
erase_if(unordered_flat_map<K, T, H, P, A>& c, Predicate pred);
Traverses the container c
and removes all elements for which the supplied predicate returns true
.
Returns: |
The number of erased elements. |
Notes: |
Equivalent to:
|
Serialization
unordered_flat_map
s can be archived/retrieved by means of
Boost.Serialization using the API provided
by this library. Both regular and XML archives are supported.
Saving an unordered_flat_map to an archive
Saves all the elements of an unordered_flat_map
x
to an archive (XML archive) ar
.
Requires: |
|
Loading an unordered_flat_map from an archive
Deletes all preexisting elements of an unordered_flat_map
x
and inserts
from an archive (XML archive) ar
restored copies of the elements of the
original unordered_flat_map
other
saved to the storage read by ar
.
Requires: |
|
Saving an iterator/const_iterator to an archive
Saves the positional information of an iterator
(const_iterator
) it
to an archive (XML archive) ar
. it
can be and end()
iterator.
Requires: |
The |
Loading an iterator/const_iterator from an archive
Makes an iterator
(const_iterator
) it
point to the restored position of
the original iterator
(const_iterator
) saved to the storage read by
an archive (XML archive) ar
.
Requires: |
If |