Boost.MultiIndex Documentation - Hashed indices reference

# Boost.MultiIndex Hashed indices reference

Ranked indices
Boost.MultiIndex reference
Sequenced indices

## Contents

- Header `"boost/multi_index/hashed_index_fwd.hpp"` synopsis
- Header `"boost/multi_index/hashed_index.hpp"` synopsis
  - Index specifiers `hashed_unique` and `hashed_non_unique`
  - Hashed indices
    - Complexity signature
    - Instantiation types
    - Nested types
    - Constructors, copy and assignment
    - Iterators
    - Modifiers
    - Observers
    - Lookup
    - Bucket interface
    - Hash policy
    - Comparison
    - Serialization

## Header `"boost/multi_index/hashed_index_fwd.hpp"` synopsis

>     namespace boost{

>     namespace multi_index{

>     // index specifiers hashed_unique and hashed_non_unique

>     template<consult hashed_unique reference for arguments>
>     struct hashed_unique;
>     template<consult hashed_non_unique reference for arguments>
>     struct hashed_non_unique;

>     // indices

>     namespace detail{

>     template<implementation defined> class index name is implementation defined;

>     } // namespace boost::multi_index::detail

>     } // namespace boost::multi_index

>     } // namespace boost

`hashed_index_fwd.hpp` provides forward declarations for index specifiers `hashed_unique` and `hashed_non_unique` and their associated hashed index classes.

## Header `"boost/multi_index/hashed_index.hpp"` synopsis

>     #include <initializer_list>

>     namespace boost{

>     namespace multi_index{

>     // index specifiers hashed_unique and hashed_non_unique

>     template<consult hashed_unique reference for arguments>
>     struct hashed_unique;
>     template<consult hashed_non_unique reference for arguments>
>     struct hashed_non_unique;

>     // indices

>     namespace detail{

>     template<implementation defined> class index class name implementation defined;

>     // index comparison:

>     // OP is any of ==,!=

>     template<implementation defined>
>     bool operator OP(const index class name& x,const index class name& y);

>     // index specialized algorithms:

>     template<implementation defined>
>     void swap(index class name& x,index class name& y);

>     } // namespace boost::multi_index::detail

>     } // namespace boost::multi_index

>     } // namespace boost

### Index specifiers `hashed_unique` and `hashed_non_unique`

These index specifiers allow for insertion of hashed indices without and with allowance of duplicate elements, respectively. The syntax of `hashed_unique` and `hashed_non_unique` coincide, thus we describe them in a grouped manner. `hashed_unique` and `hashed_non_unique` can be instantiated in two different forms, according to whether a tag list for the index is provided or not:

>     template<
>       typename KeyFromValue,
>       typename Hash=boost::hash<KeyFromValue::result_type>,
>       typename Pred=std::equal_to<KeyFromValue::result_type>
>     >
>     struct (hashed_unique | hashed_non_unique);

>     template<
>       typename TagList,
>       typename KeyFromValue,
>       typename Hash=boost::hash<KeyFromValue::result_type>,
>       typename Pred=std::equal_to<KeyFromValue::result_type>
>     >
>     struct (hashed_unique | hashed_non_unique);

If provided, `TagList` must be an instantiation of the class template `tag`. The template arguments are used by the corresponding index implementation, refer to the hashed indices reference section for further explanations on their acceptable type values.

### Hashed indices

A hashed index provides fast retrieval of elements of a `multi_index_container` through hashing techniques. A hashed index is particularized according to a given `Key Extractor` that retrieves keys from elements of `multi_index_container`, a `Hash` function object which returns hash values for the keys and a binary predicate `Pred` acting as an equivalence relation on values of `Key`.

There are two variants of hashed indices: unique, which do not allow duplicate elements (with respect to its associated equality predicate) and non-unique, which accept those duplicates. The interface of these two variants is the same, so they are documented together, with minor differences explicitly stated when they exist.

Except where noted or if the corresponding interface does not exist, hashed indices (both unique and non-unique) satisfy the C++ requirements for unordered associative containers at [unord.req] (supporting unique and equivalent keys, respectively.) Iterators (including to the end of the index) and pointers and references to an element remain valid during the lifetime of the associated container (which can change upon swapping), or until the referred-to element is erased or extracted; pointers and references to an extracted element, but not so for iterators, become valid again once the element is re-inserted. Occasionally, the exception safety guarantees provided are actually stronger than required by the standard. We only provide descriptions of those types and operations that do not exactly conform to or are not mandated by the standard requirements.

>     namespace boost{

>     namespace multi_index{

>     namespace detail{

>     template<implementation defined: dependent on types Value, Allocator,
>       TagList, KeyFromValue, Hash, Pred>
>     class name is implementation defined
>     {
>     public:
>       // types:

>       typedef typename KeyFromValue::result_type         key_type;
>       typedef Value                                      value_type;
>       typedef KeyFromValue                               key_from_value;
>       typedef Hash                                       hasher;
>       typedef Pred                                       key_equal;
>       typedef boost::tuple<
>         size_type,key_from_value,hasher,key_equal>       ctor_args;
>       typedef TagList                                    tag_list;
>       typedef Allocator                                  allocator_type;
>       typedef typename Allocator::pointer                pointer;
>       typedef typename Allocator::const_pointer          const_pointer;
>       typedef typename Allocator::reference              reference;
>       typedef typename Allocator::const_reference        const_reference;
>       typedef implementation defined                     size_type;
>       typedef implementation defined                     difference_type;
>       typedef implementation defined                     iterator;
>       typedef implementation defined                     const_iterator;
>       typedef implementation defined                     local_iterator;
>       typedef implementation defined                     const_local_iterator;
>       typedef same as owning container                   node_type;
>       typedef following [container.insert.return] spec   insert_return_type;

>       // construct/destroy/copy:

>       index class name& operator=(const index class name& x);
>       index class name& operator=(std::initializer_list<value_type> list);

>       allocator_type get_allocator()const noexcept;

>       // size and capacity:

>       bool      empty()const noexcept;
>       size_type size()const noexcept;
>       size_type max_size()const noexcept;

>       // iterators:

>       iterator       begin()noexcept;
>       const_iterator begin()const noexcept;
>       iterator       end()noexcept;
>       const_iterator end()const noexcept;
>       const_iterator cbegin()const;
>       const_iterator cend()const;

>       iterator       iterator_to(const value_type& x);
>       const_iterator iterator_to(const value_type& x)const;

>       // modifiers:

>       template<typename... Args>
>       std::pair<iterator,bool> emplace(Args&&... args);
>       template<typename... Args>
>       iterator emplace_hint(iterator position,Args&&... args);
>       std::pair<iterator,bool> insert(const value_type& x);
>       std::pair<iterator,bool> insert(value_type&& x);
>       iterator insert(iterator position,const value_type& x);
>       iterator insert(iterator position,value_type&& x);
>       template<typename InputIterator>
>       void insert(InputIterator first,InputIterator last);
>       void insert(std::initializer_list<value_type> list);
>       insert_return_type insert(node_type&& nh);
>       iterator insert(const_iterator position,node_type&& nh);

>       node_type extract(const_iterator position);
>       node_type extract(const key_type& x);

>       iterator  erase(iterator position);
>       size_type erase(const key_type& x);
>       iterator  erase(iterator first,iterator last);

>       bool replace(iterator position,const value_type& x);
>       bool replace(iterator position,value_type&& x);
>       template<typename Modifier> bool modify(iterator position,Modifier mod);
>       template<typename Modifier,typename Rollback>
>       bool modify(iterator position,Modifier mod,Rollback back);
>       template<typename Modifier> bool modify_key(iterator position,Modifier mod);
>       template<typename Modifier,typename Rollback>
>       bool modify_key(iterator position,Modifier mod,Rollback back);

>       void clear()noexcept;
>       void swap(index class name& x);

>       template<typename Index> void merge(Index&& x);
>       template<typename Index>
>       std::pair<iterator,bool> merge(
>         Index&& x,typename std::remove_reference_t<Index>::const_iterator i);
>       template<typename Index>
>       void merge(
>         Index&& x,
>         typename std::remove_reference_t<Index>::const_iterator first,
>         typename std::remove_reference_t<Index>::const_iterator last);

>       // observers:

>       key_from_value key_extractor()const;
>       hasher         hash_function()const;
>       key_equal      key_eq()const;

>       // lookup:

>       template<typename CompatibleKey>
>       iterator find(const CompatibleKey& x)const;
>       template<
>         typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>       >
>       iterator find(
>         const CompatibleKey& x,
>         const CompatibleHash& hash,const CompatiblePred& eq)const;

>       template<typename CompatibleKey>
>       size_type count(const CompatibleKey& x)const;
>       template<
>         typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>       >
>       size_type count(
>         const CompatibleKey& x,
>         const CompatibleHash& hash,const CompatiblePred& eq)const;

>       template<typename CompatibleKey>
>       bool contains(const CompatibleKey& x)const;
>       template<
>         typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>       >
>       bool contains(
>         const CompatibleKey& x,
>         const CompatibleHash& hash,const CompatiblePred& eq)const;

>       template<typename CompatibleKey>
>       std::pair<iterator,iterator> equal_range(const CompatibleKey& x)const;
>       template<
>         typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>       >
>       std::pair<iterator,iterator> equal_range(
>         const CompatibleKey& x,
>         const CompatibleHash& hash,const CompatiblePred& eq)const;

>       // bucket interface:

>       size_type bucket_count()const noexcept;
>       size_type max_bucket_count()const noexcept;
>       size_type bucket_size(size_type n)const;
>       size_type bucket(const key_type& k)const;

>       local_iterator       begin(size_type n);
>       const_local_iterator begin(size_type n)const;
>       local_iterator       end(size_type n);
>       const_local_iterator end(size_type n)const;
>       const_local_iterator cbegin(size_type n)const;
>       const_local_iterator cend(size_type n)const;

>       local_iterator       local_iterator_to(const value_type& x);
>       const_local_iterator local_iterator_to(const value_type& x)const;

>       // hash policy:

>       float load_factor()const noexcept;
>       float max_load_factor()const noexcept;
>       void  max_load_factor(float z);
>       void  rehash(size_type n);
>       void  reserve(size_type n);
>     };

>     // index comparison:

>     template<implementation defined>
>     bool operator==(const index class name& x,const index class name& y);

>     template<implementation defined>
>     bool operator!=(const index class name& x,const index class name& y)
>     {
>       return !(x==y);
>     }

>     // index specialized algorithms:

>     template<implementation defined>
>     void swap(index class name& x,index class name& y);

>     } // namespace boost::multi_index::detail

>     } // namespace boost::multi_index

>     } // namespace boost

#### Complexity signature

Here and in the descriptions of operations of hashed indices, we adopt the scheme outlined in the complexity signature section. The complexity signature of hashed indices is:

- copying: `c(n)=n*log(n)`,
- insertion: average case `i(n)=1` (amortized constant), worst case `i(n)=ndist`,
- hinted insertion: average case `h(n)=1` (amortized constant), worst case `h(n)=ndist`,
- deletion: `d(n)=1` (constant),
- replacement:
  - if the new element key is equivalent to the original, `r(n)=1` (constant),
  - otherwise, average case `r(n)=1` (constant), worst case `r(n)=ndist`,
- modifying: average case `m(n)=1` (constant), worst case `m(n)=ndist`,

where `ndist` is the number of non-equivalent elements out of the total `n`.

#### Instantiation types

Hashed indices are instantiated internally to `multi_index_container` and specified by means of `indexed_by` with index specifiers `hashed_unique` and `hashed_non_unique`. Instantiations are dependent on the following types:

- `Value` from `multi_index_container`,
- `Allocator` from `multi_index_container`,
- `TagList` from the index specifier (if provided, otherwise `tag<>` is assumed),
- `KeyFromValue` from the index specifier,
- `Hash` from the index specifier,
- `Pred` from the index specifier.

`TagList` must be an instantiation of `tag`. The type `KeyFromValue`, which determines the mechanism for extracting a key from `Value`, must be a model of `Key Extractor` from `Value`. `Hash` is a `CopyConstructible`unary function object taking a single argument of type `KeyFromValue::result_type` and returning a value of type `std::size_t` in the range `[0, std::numeric_limits<std::size_t>::max())`. `Pred` is a `CopyConstructible` binary predicate inducing an equivalence relation on elements of `KeyFromValue::result_type`. It is required that the `Hash` object return the same value for keys equivalent under `Pred`.

#### Nested types

`ctor_args`

> The first element of this tuple indicates the minimum number of buckets set up by the index on construction time. If the default value 0 is used, an implementation defined number is used instead.

`iterator
const_iterator
local_iterator
const_local_iterator`

> These types are forward iterators. They depend only on `node_type`, the position of the index in the `multi_index_container`, and whether the index is unique or not (this implies that, for instance, iterators to elements transferred from a unique index to a non-unique one will become invalid).

#### Constructors, copy and assignment

As explained in the index concepts section, indices do not have public constructors or destructors. Assignment, on the other hand, is provided. Upon construction, `max_load_factor()` is 1.0.

`index class name& operator=(const index class name& x);`

> Effects:

> >     a=b;

> where `a` and `b` are the `multi_index_container` objects to which `*this` and `x` belong, respectively.
> Returns: `*this`.

`index class name& operator=(std::initializer_list<value_type> list);`

> Effects:

> >     a=list;

> where `a` is the `multi_index_container` object to which `*this` belongs.
> Returns: `*this`.

#### Iterators

`iterator iterator_to(const value_type& x);
const_iterator iterator_to(const value_type& x)const;`

> Requires: `x` is a reference to an element of the container.
> Returns: An iterator to `x`.
> Complexity: Constant.
> Exception safety: `nothrow`.

#### Modifiers

`template<typename... Args>
std::pair<iterator,bool> emplace(Args&&... args);`

> Requires: `value_type` is `EmplaceConstructible` into `multi_index_container` from `args`.
> Effects: Inserts a `value_type` object constructed with `std::forward<Args>(args)...` into the `multi_index_container` to which the index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> Returns: The return value is a pair `p`. `p.second` is `true` if and only if insertion took place. On successful insertion, `p.first` points to the element inserted; otherwise, `p.first` points to an element that caused the insertion to be banned. Note that more than one element can be causing insertion not to be allowed.
> Complexity: `O(I(n))`.
> Exception safety: Strong, except that rehashing may occur even if the operation fails.

`template<typename... Args>
iterator emplace_hint(iterator position, Args&&... args);`

> Requires: `value_type` is `EmplaceConstructible` into `multi_index_container` from `args`. `position` is a valid iterator of the index.
> Effects: Inserts a `value_type` object constructed with `std::forward<Args>(args)...` into the `multi_index_container` to which the index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> `position` is used as a hint to improve the efficiency of the operation.
> Returns: On successful insertion, an iterator to the newly inserted element. Otherwise, an iterator to an element that caused the insertion to be banned. Note that more than one element can be causing insertion not to be allowed.
> Complexity: `O(H(n))`.
> Exception safety: Strong, except that rehashing may occur even if the operation fails.

`std::pair<iterator,bool> insert(const value_type& x);`
`std::pair<iterator,bool> insert(value_type&& x);`

> Requires (first version): `value_type` is `CopyInsertable` into `multi_index_container`.
> Requires (second version): `value_type` is `MoveInsertable` into `multi_index_container`.
> Effects: Inserts `x` into the `multi_index_container` to which the index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> Returns: The return value is a pair `p`. `p.second` is `true` if and only if insertion took place. On successful insertion, `p.first` points to the element inserted; otherwise, `p.first` points to an element that caused the insertion to be banned. Note that more than one element can be causing insertion not to be allowed.
> Complexity: `O(I(n))`.
> Exception safety: Strong, except that rehashing may occur even if the operation fails.

`iterator insert(iterator position,const value_type& x);`
`iterator insert(iterator position,value_type&& x);`

> Requires (first version): `value_type` is `CopyInsertable` into `multi_index_container`. `position` is a valid iterator of the index.
> Requires (second version): `value_type` is `MoveInsertable` into `multi_index_container`. `position` is a valid iterator of the index.
> Effects: Inserts `x` into the `multi_index_container` to which the index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> `position` is used as a hint to improve the efficiency of the operation.
> Returns: On successful insertion, an iterator to the newly inserted element. Otherwise, an iterator to an element that caused the insertion to be banned. Note that more than one element can be causing insertion not to be allowed.
> Complexity: `O(H(n))`.
> Exception safety: Strong, except that rehashing may occur even if the operation fails.

`template<typename InputIterator>
void insert(InputIterator first,InputIterator last);`

> Requires: `InputIterator` is an input iterator. `value_type` is `EmplaceConstructible` into `multi_index_container` from `*first`. `first` and `last` are not iterators into any index of the `multi_index_container` to which this index belongs. `last` is reachable from `first`.
> Effects: For each element of [`first`, `last`), in this order, inserts it into the `multi_index_container` to which this index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> Complexity: `O(m*I(n+m))`, where `m` is the number of elements in [`first`, `last`).
> Exception safety: Basic.

`void insert(std::initializer_list<value_type> list);`

> Effects:

> >     insert(list.begin(),list.end());

`insert_return_type insert(node_type&& nh);`

> Requires: `nh.empty() || get_allocator()==nh.get_allocator()`.
> Effects: Does nothing if `nh` is empty; otherwise, inserts the node owned by `nh` into the `multi_index_container` to which the index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> Postconditions: `nh` is empty.
> Returns: A value `p` of type `insert_return_type`. If `nh` is empty, `p.position` is `end()`, `p.inserted` is `false` and `p.node` is empty; on successful insertion, `p.position` points to the element inserted, `p.inserted` is `true` and `p.node` is empty; if the insertion failed, `p.position` points to an element that caused the insertion to be banned, `p.inserted` is `false` and `p.node` owns the original node. Note that more than one element can be causing insertion not to be allowed.
> Complexity: `O(I(n))`.
> Exception safety: Strong, except that rehashing may occur even if the operation fails. If an exception is thrown, `nh` is not changed.

`iterator insert(const_iterator position,node_type&& nh);`

> Requires: `nh.empty() || get_allocator()==nh.get_allocator()`. `position` is a valid iterator of the index.
> Effects: Does nothing if `nh` is empty; otherwise, inserts the node owned by `nh` into the `multi_index_container` to which the index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> `position` is used as a hint to improve the efficiency of the operation.
> Postconditions: `nh` is empty if insertion succeeds, and is not changed otherwise.
> Returns: `end()` if `nh` is empty. On successful insertion, an iterator to the newly inserted element; otherwise, an iterator to an element that caused the insertion to be banned. Note that more than one element can be causing insertion not to be allowed.
> Complexity: `O(H(n))`.
> Exception safety: Strong, except that rehashing may occur even if the operation fails. If an exception is thrown, `nh` is not changed.

`node_type extract(const_iterator position);`

> Requires: `position` is a valid dereferenceable iterator of the index.
> Effects: Extracts the node of the element pointed to by `position`.
> Returns: A node handle owning the extracted node.
> Complexity: `O(D(n))`.
> Exception safety: `nothrow`.

`node_type extract(const key_type& x);`

> Effects: Extracts the node of the first element with key equivalent to `x`, if there is any.
> Returns: A node handle owning the extracted node, or empty otherwise.
> Complexity: Average case `O(1 + D(n))`, worst case `O(ndist + D(n))`.
> Exception safety: Strong.

`iterator erase(iterator position);`

> Requires: `position` is a valid dereferenceable iterator of the index.
> Effects: Deletes the element pointed to by `position`.
> Returns: An iterator pointing to the element immediately following the one that was deleted, or `end()` if no such element exists.
> Complexity: `O(D(n))`.
> Exception safety: `nothrow`.

`size_type erase(const key_type& x);`

> Effects: Deletes the elements with key equivalent to `x`.
> Returns: Number of elements deleted.
> Complexity: Average case `O(1 + m*D(n))`, worst case `O(ndist + m*D(n))`, where `m` is the number of elements deleted.
> Exception safety: Basic.

`iterator erase(iterator first,iterator last);`

> Requires: [`first`,`last`) is a valid range of the index.
> Effects: Deletes the elements in [`first`,`last`).
> Returns: `last`.
> Complexity: `O(m*D(n))`, where `m` is the number of elements in [`first`,`last`).
> Exception safety: `nothrow`.

`bool replace(iterator position,const value_type& x);`
`bool replace(iterator position,value_type&& x);`

> Requires (first version): `value_type` is `CopyAssignable`. `position` is a valid dereferenceable iterator of the index.
> Requires (second version): `value_type` is `MoveAssignable`. `position` is a valid dereferenceable iterator of the index.
> Effects: Assigns the value `x` to the element pointed to by `position` into the `multi_index_container` to which the index belongs if, for the value `x`
> - the index is non-unique OR no other element exists (except possibly `*position`) with equivalent key,
> - AND replacing is allowed by all other indices of the `multi_index_container`.
> Postconditions: Validity of `position` is preserved in all cases. If the key of the new value is equivalent to that of the replaced value, the position of the element does not change.
> Returns: `true` if the replacement took place, `false` otherwise.
> Complexity: `O(R(n))`.
> Exception safety: Strong. If an exception is thrown by some user-provided operation the `multi_index_container` to which the index belongs remains in its original state.

`template<typename Modifier> bool modify(iterator position,Modifier mod);`

> Requires: `mod` is a unary function object accepting arguments of type `value_type&`. `position` is a valid dereferenceable iterator of the index. The execution of `mod(e)`, where `e` is the element pointed to by `position`, does not invoke any operation of the `multi_index_container` after `e` is directly modified or, before modification, if the operation would invalidate `position`.
> Effects: Calls `mod(e)` where `e` is the element pointed to by `position` and rearranges `*position` into all the indices of the `multi_index_container`. Rearrangement is successful if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND rearrangement is allowed by all other indices of the `multi_index_container`.
> If the rearrangement fails, the element is erased.
> Postconditions: Validity of `position` is preserved if the operation succeeds. If the key of the modified value is equivalent to that of the original value, the position of the element does not change.
> Returns: `true` if the operation succeeded, `false` otherwise.
> Complexity: `O(M(n))`.
> Exception safety: Basic. If an exception is thrown by some user-provided operation (including `mod`), then the element pointed to by `position` is erased.

`template<typename Modifier,typename Rollback>
bool modify(iterator position,Modifier mod,Rollback back);`

> Requires: `mod` and `back` are unary function objects accepting arguments of type `value_type&`. `position` is a valid dereferenceable iterator of the index. The execution of `mod(e)`, where `e` is the element pointed to by `position`, does not invoke any operation of the `multi_index_container` after `e` is directly modified or, before modification, if the operation would invalidate `position`. `back(e)` does not invoke any operation of the `multi_index_container`.
> Effects: Calls `mod(e)` where `e` is the element pointed to by `position` and tries to rearrange `*position` into all the indices of the `multi_index_container`. Rearrangement is successful if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND rearrangement is allowed by all other indices of the `multi_index_container`.
> If the rearrangement fails, `back(e)` is invoked: if the resulting value of `e` is consistent with its original position and constraints in all indices, the element is kept, otherwise it is erased.
> Postconditions: Validity of `position` is preserved except if the element is erased under the conditions described below. If the key of the modified value is equivalent to that of the original value, the position of the element does not change.
> Returns: `true` if the operation succeeded, `false` otherwise.
> Complexity: `O(M(n))`.
> Exception safety: Strong, except if `mod` or `back` throw an exception or `back(e)` fails to properly restore the element or there is a throwing user-provided operation after invoking `back(e)`, in which cases the modified element is erased. If `back` throws inside the handling code executing after some other user-provided operation has thrown, it is the exception generated by `back` that is rethrown.

`template<typename Modifier> bool modify_key(iterator position,Modifier mod);`

> Requires: `key_from_value` is a read/write `Key Extractor` from `value_type`. `mod` is a unary function object accepting arguments of type `key_type&`. `position` is a valid dereferenceable iterator of the index. The execution of `mod(k)`, where `k` is the key of the element pointed to by `position`, does not invoke any operation of the `multi_index_container` after `k` is directly modified or, before modification, if the operation would invalidate `position`.
> Effects: Equivalent to `modify(position,mod')`, with `mod'` defined in such a way that `mod'(x)` is the same as `mod(key(x))`, where `key` is the internal `KeyFromValue` object of the index.

`template<typename Modifier,typename Rollback>
bool modify_key(iterator position,Modifier mod,Rollback back);`

> Requires: `key_from_value` is a read/write `Key Extractor` from `value_type`. `mod` and `back` are unary function objects accepting arguments of type `key_type&`. `position` is a valid dereferenceable iterator of the index. The execution of `mod(k)`, where `k` is the key of the element pointed to by `position`, does not invoke any operation of the `multi_index_container` after `k` is directly modified or, before modification, if the operation would invalidate `position`. `back(k)` does not invoke any operation of the `multi_index_container`.
> Effects: Equivalent to `modify(position,mod',back')`, with `mod'` and `back` defined in such a way that `mod'(x)` is the same as `mod(key(x))` and `back'(x)` is the same as `back(key(x))`, where `key` is the internal `KeyFromValue` object of the index.

`template<typename Index> void merge(Index&& x);`

> Requires: `x` is a non-const reference to an index of a node-compatible `multi_index_container`. `get_allocator()==x.get_allocator()`.
> Effects:

> >     merge(x,x.begin(),x.end());

`template<typename Index> std::pair<iterator,bool> merge(
Index&& x,typename std::remove_reference_t<Index>::const_iterator i);`

> Requires: `x` is a non-const reference to an index of a node-compatible `multi_index_container`. `get_allocator()==x.get_allocator()`. `i` is a valid dereferenceable iterator of `x`.
> Effects: Does nothing if the source and destination containers are the same; otherwise, transfers the node of the element referred to by `i` into the `multi_index_container` to which the destination index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> Note that no element is copied or destroyed in the process.
> Postconditions: If transfer succeeds, for any index in the source container having the same `iterator`/`const_iterator` types as the corresponding index in the destination container, iterators referring to `*i` remain valid and behave as iterators of the destination index.
> Returns: The return value is a pair `p`. `p.second` is `true` if and only if transfer took place or the source and destination containers are the same. If `p.second` is `true`, `p.first` points to `*i`; otherwise, `p.first` points to an element that caused the insertion to be banned. Note that more than one element can be causing insertion not to be allowed.
> Complexity: If the source and destination containers are the same, constant; otherwise, `O(I(n)+D(x.size()))`.
> Exception safety: If the source and destination containers are the same, `nothrow`; otherwise strong.

`template<typename Index> void merge(
Index&& x,
typename std::remove_reference_t<Index>::const_iterator first,
typename std::remove_reference_t<Index>::const_iterator last);`

> Requires: `x` is a non-const reference to an index of a node-compatible `multi_index_container`. `get_allocator()==x.get_allocator()`. [`first`,`last`) is a valid range of `x`.
> Effects: Does nothing if the source and destination containers are the same; otherwise, for each node in [`first`,`last`), in this order, the node is transferred to the `multi_index_container` to which the destination index belongs if
> - the index is non-unique OR no other element exists with equivalent key,
> - AND insertion is allowed by all other indices of the `multi_index_container`.
> Note that no element is copied or destroyed in the process.
> Postconditions: For any index in the source container having the same `iterator`/`const_iterator` types as the corresponding index in the destination container, iterators referring to the transferred elements remain valid and behave as iterators of the destination index.
> Complexity: If the source and destination containers are the same, constant; otherwise, `O(m*(I(n+m)+D(x.size())))`, where `m` is the number of elements in [`first`, `last`).
> Exception safety: If the source and destination containers are the same, `nothrow`; otherwise basic.

#### Observers

Apart from standard `hash_function` and `key_eq`, hashed indices have a member function for retrieving the internal key extractor used.

`key_from_value key_extractor()const;`

> Returns a copy of the `key_from_value` object used to construct the index.
> Complexity: Constant.

#### Lookup

Hashed indices provide the full lookup functionality required by [unord.req], namely `find`, `count`, and `equal_range`. Additionally, these member functions are templatized to allow for non-standard arguments, so extending the types of search operations allowed. The kind of arguments permissible when invoking the lookup member functions is defined by the following concept.

Consider a pair (`Hash`, `Pred`) where `Hash` is a hash functor over values of type `Key` and `Pred` is a binary predicate inducing an equivalence relation on `Key`, with the additional constraint that equivalent keys have the same hash value. A triplet of types (`CompatibleKey`, `CompatibleHash`, `CompatiblePred`) is said to be a compatible extension of (`Hash`, `Pred`) if

1. `CompatibleHash` is a hash functor on values of type `CompatibleKey`,
2. `CompatiblePred` is a binary predicate over (`Key`, `CompatibleKey`),
3. `CompatiblePred` is a binary predicate over (`CompatibleKey`, `Key`),
4. if `c_eq(ck,k1)` then `c_eq(k1,ck)`,
5. if `c_eq(ck,k1)` and `eq(k1,k2)` then `c_eq(ck,k2)`,
6. if `c_eq(ck,k1)` and `c_eq(ck,k2)` then `eq(k1,k2)`,
7. if `c_eq(ck,k1)` then `c_hash(ck)==hash(k1)`,

for every `c_hash` of type `CompatibleHash`, `c_eq` of type `CompatiblePred`, `hash` of type `Hash`, `eq` of type `Pred`, `ck` of type `CompatibleKey` and `k1`, `k2` of type `Key`.

Additionally, a type `CompatibleKey` is said to be a compatible key of (`Hash`, `Pred`) if (`CompatibleKey`, `Hash`, `Pred`) is a compatible extension of (`Hash`, `Pred`). This implies that `Hash` and `Pred` accept arguments of type `CompatibleKey`, which usually means they have several overloads of their corresponding `operator()` member functions.

In the context of a compatible extension or a compatible key, the expression "equivalent key" takes on its obvious interpretation.

`template<typename CompatibleKey> iterator find(const CompatibleKey& x)const;`

> Requires: `CompatibleKey` is a compatible key of (`hasher`, `key_equal`).
> Effects: Returns a pointer to an element whose key is equivalent to `x`, or `end()` if such an element does not exist.
> Complexity: Average case `O(1)` (constant), worst case `O(ndist)`.

`template<
typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>
iterator find(
const CompatibleKey& x,
const CompatibleHash& hash,const CompatiblePred& eq)const;`

> Requires: (`CompatibleKey`, `CompatibleHash`, `CompatiblePred`) is a compatible extension of (`hasher`, `key_equal`).
> Effects: Returns a pointer to an element whose key is equivalent to `x`, or `end()` if such an element does not exist.
> Complexity: Average case `O(1)` (constant), worst case `O(ndist)`.

`template<typename CompatibleKey>
size_type count(const CompatibleKey& x)const;`

> Requires: `CompatibleKey` is a compatible key of (`hasher`, `key_equal`).
> Effects: Returns the number of elements with key equivalent to `x`.
> Complexity: Average case `O(count(x))`, worst case `O(count(x)+ndist)`.

`template<
typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>
size_type count(
const CompatibleKey& x,
const CompatibleHash& hash,const CompatiblePred& eq)const;`

> Requires: (`CompatibleKey`, `CompatibleHash`, `CompatiblePred`) is a compatible extension of (`hasher`, `key_equal`).
> Effects: Returns the number of elements with key equivalent to `x`.
> Complexity: Average case `O(count(x,hash,eq))`, worst case `O(count(x,hash,eq)+ndist)`.

`template<typename CompatibleKey>
bool contains(const CompatibleKey& x)const;`

> Requires: `CompatibleKey` is a compatible key of (`hasher`, `key_equal`).
> Effects: Returns `true` iff there is some element with key equivalent to `x`.
> Complexity: Average case `O(1)` (constant), worst case `O(ndist)`.

`template<
typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>
bool contains(
const CompatibleKey& x,
const CompatibleHash& hash,const CompatiblePred& eq)const;`

> Requires: (`CompatibleKey`, `CompatibleHash`, `CompatiblePred`) is a compatible extension of (`hasher`, `key_equal`).
> Effects: Returns `true` iff there is some element with key equivalent to `x`.
> Complexity: Average case `O(1)` (constant), worst case `O(ndist)`.

`template<typename CompatibleKey>
std::pair<iterator,iterator> equal_range(const CompatibleKey& x)const;`

> Requires: `CompatibleKey` is a compatible key of (`hasher`, `key_equal`).
> Effects: Returns a range containing all elements with keys equivalent to `x` (and only those), or (`end()`,`end()`) if no such elements exist.
> Complexity: Average case `O(1)` (constant), worst case `O(ndist)`.

`template<
typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>
std::pair<iterator,iterator> equal_range(
const CompatibleKey& x,
const CompatibleHash& hash,const CompatiblePred& eq)const;`

> Requires: (`CompatibleKey`, `CompatibleHash`, `CompatiblePred`) is a compatible extension of (`hasher`, `key_equal`).
> Effects: Returns a range containing all elements with keys equivalent to `x` (and only those), or (`end()`,`end()`) if no such elements exist.
> Complexity: Average case `O(1)` (constant), worst case `O(ndist)`.

#### Bucket interface

`local_iterator local_iterator_to(const value_type& x);
const_local_iterator local_iterator_to(const value_type& x)const;`

> Requires: `x` is a reference to an element of the container.
> Returns: An iterator to `x`.
> Complexity: Constant.
> Exception safety: `nothrow`.

#### Hash policy

`void rehash(size_type n);`

> Effects: Increases if necessary the number of internal buckets so that `size()/bucket_count()` does not exceed the maximum load factor, and `bucket_count()>=n`.
> Postconditions: Validity of iterators and references to the elements contained is preserved.
> Complexity: `O(m)`, where `m` is the number of non-equivalent elements in the index.
> Exception safety: Strong.

`void reserve(size_type n);`

> Effects:

> >     rehash(std::ceil(n/max_load_factor()));

#### Comparison

`template<implementation defined>
bool operator==(const index class name& x,const index class name& y);`

> Requires: `x.key_extractor()`, `x.hash_function()` and `x.key_eq()` have the same behavior as the corresponding objects in `y`. For any two elements `e1`, `e2` in `x` or `y`, if `e1==e2` then their keys are equivalent.
> Returns: `true` iff `x` and `y` have the same size and for each key `k` present in `x` the range `x.equal_range(k)` is equal (considering the `==` operator of `value_type`) to `y.equal_range(k)` under permutations of the elements.
> Complexity: Let `k1`,...,`km` be the different keys present in `x`:
> - If, for each `ki`, `x.equal_range(ki)` is arranged in the same order as `y.equal_range(ki)`, average case is `O(x.size())`, worst case `O(x.size()2)`.
> - Otherwise, average case is `O(Σ(x.count(ki)2))`, worst case `O(x.size()2)`.
> (For unique indices, the formulas above reduce to average case `O(x.size())`, worst case `O(x.size()2)`.)

#### Serialization

Indices cannot be serialized on their own, but only as part of the `multi_index_container` into which they are embedded. In describing the additional preconditions and guarantees associated to hashed indices with respect to serialization of their embedding containers, we use the concepts defined in the `multi_index_container` serialization section.

Operation: saving of a `multi_index_container` `m` to an output archive (XML archive) `ar`.

> Requires: No additional requirements to those imposed by the container.

Operation: loading of a `multi_index_container` `m'` from an input archive (XML archive) `ar`.

> Requires: Additionally to the general requirements, `key_eq()` must be serialization-compatible with `m.get<i>().key_eq()`, where `i` is the position of the hashed index in the container.
> Postconditions: On successful loading, the range [`begin()`, `end()`) contains restored copies of every element in [`m.get<i>().begin()`, `m.get<i>().end()`), though not necessarily in the same order.

Operation: saving of an `iterator` or `const_iterator` `it` to an output archive (XML archive) `ar`.

> Requires: `it` is a valid iterator of the index. The associated `multi_index_container` has been previously saved.

Operation: loading of an `iterator` or `const_iterator` `it'` from an input archive (XML archive) `ar`.

> Postconditions: On successful loading, if `it` was dereferenceable then `*it'` is the restored copy of `*it`, otherwise `it'==end()`.
> Note: It is allowed that `it` be a `const_iterator` and the restored `it'` an `iterator`, or vice versa.

Operation: saving of a `local_iterator` or `const_local_iterator` `it` to an output archive (XML archive) `ar`.

> Requires: `it` is a valid local iterator of the index. The associated `multi_index_container` has been previously saved.

Operation: loading of a `local_iterator` or `const_local_iterator` `it'` from an input archive (XML archive) `ar`.

> Postconditions: On successful loading, if `it` was dereferenceable then `*it'` is the restored copy of `*it`; if `it` was `m.get<i>().end(n)` for some `n`, then `it'==m'.get<i>().end(n)` (where `m` is the original `multi_index_container`, `m'` its restored copy and `i` is the ordinal of the index.)
> Note: It is allowed that `it` be a `const_local_iterator` and the restored `it'` a `local_iterator`, or vice versa.

Ranked indices
Boost.MultiIndex reference
Sequenced indices

Revised August 30th 2021

© Copyright 2003-2021 Joaquín M López Muñoz. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)