# Boost.MultiIndex Ordered indices reference

Index reference
Boost.MultiIndex reference
Ranked indices

## Contents

- Header `"boost/multi_index/ordered_index_fwd.hpp"` synopsis
- Header `"boost/multi_index/ordered_index.hpp"` synopsis
  - Index specifiers `ordered_unique` and `ordered_non_unique`
  - Ordered indices
    - Complexity signature
    - Instantiation types
    - Nested types
    - Constructors, copy and assignment
    - Iterators
    - Modifiers
    - Observers
    - Set operations
    - Range operations
    - Serialization

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

>     namespace boost{

>     namespace multi_index{

>     // index specifiers ordered_unique and ordered_non_unique

>     template<consult ordered_unique reference for arguments>
>     struct ordered_unique;
>     template<consult ordered_non_unique reference for arguments>
>     struct ordered_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

`ordered_index_fwd.hpp` provides forward declarations for index specifiers `ordered_unique` and `ordered_non_unique` and their associated ordered index classes.

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

>     #include <initializer_list>

>     namespace boost{

>     namespace multi_index{

>     // index specifiers ordered_unique and ordered_non_unique

>     template<consult ordered_unique reference for arguments>
>     struct ordered_unique;
>     template<consult ordered_non_unique reference for arguments>
>     struct ordered_non_unique;

>     // indices

>     namespace detail{

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

>     // index comparison:

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

>     template<arg set 1,arg set 2>
>     bool operator OP(
>       const index class name<arg set 1>& x,const index class name<arg set 2>& 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 `ordered_unique` and `ordered_non_unique`

These index specifiers allow for insertion of ordered indices without and with allowance of duplicate elements, respectively. The syntax of `ordered_unique` and `ordered_non_unique` coincide, thus we describe them in a grouped manner. `ordered_unique` and `ordered_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 Compare=std::less<KeyFromValue::result_type>
>     >
>     struct (ordered_unique | ordered_non_unique);

>     template<
>       typename TagList,
>       typename KeyFromValue,
>       typename Compare=std::less<KeyFromValue::result_type>
>     >
>     struct (ordered_unique | ordered_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 ordered indices reference section for further explanations on their acceptable type values.

### Ordered indices

An ordered index provides a set-like interface to the underlying heap of elements contained in a `multi_index_container`. An ordered index is particularized according to a given `Key Extractor` that retrieves keys from elements of `multi_index_container` and a comparison predicate.

There are two variants of ordered indices: unique, which do not allow duplicate elements (with respect to its associated comparison 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, ordered indices (both unique and non-unique) satisfy the C++ requirements for associative containers at [associative.reqmts] (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. 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{

>     implementation defined unbounded; // see range()

>     namespace detail{

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

>       typedef typename KeyFromValue::result_type         key_type;
>       typedef Value                                      value_type;
>       typedef KeyFromValue                               key_from_value;
>       typedef Compare                                    key_compare;
>       typedef implementation defined                     value_compare;
>       typedef boost::tuple<key_from_value,key_compare>   ctor_args;
>       typedef TagList                                    tag_list;
>       typedef Allocator                                  allocator_type;
>       typedef typename Allocator::reference              reference;
>       typedef typename Allocator::const_reference        const_reference;
>       typedef implementation defined                     iterator;
>       typedef implementation defined                     const_iterator;
>       typedef implementation defined                     size_type;
>       typedef implementation defined                     difference_type;
>       typedef typename Allocator::pointer                pointer;
>       typedef typename Allocator::const_pointer          const_pointer;
>       typedef equivalent to
>         std::reverse_iterator<iterator>                  reverse_iterator;
>       typedef equivalent to
>         std::reverse_iterator<const_iterator>            const_reverse_iterator;
>       typedef same as owning container                   node_type;
>       typedef following [container.insert.return] spec   insert_return_type;

>       // construct/copy/destroy:

>       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;

>       // iterators:

>       iterator               begin()noexcept;
>       const_iterator         begin()const noexcept;
>       iterator               end()noexcept;
>       const_iterator         end()const noexcept;
>       reverse_iterator       rbegin()noexcept;
>       const_reverse_iterator rbegin()const noexcept;
>       reverse_iterator       rend()noexcept;
>       const_reverse_iterator rend()const noexcept;
>       const_iterator         cbegin()const noexcept;
>       const_iterator         cend()const noexcept;
>       const_reverse_iterator crbegin()const noexcept;
>       const_reverse_iterator crend()const noexcept;

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

>       // capacity:

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

>       // 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 swap(index class name& x);
>       void clear()noexcept;

>       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;
>       key_compare    key_comp()const;
>       value_compare  value_comp()const;

>       // set operations:

>       template<typename CompatibleKey>
>       iterator find(const CompatibleKey& x)const;
>       template<typename CompatibleKey,typename CompatibleCompare>
>       iterator find(
>         const CompatibleKey& x,const CompatibleCompare& comp)const;

>       template<typename CompatibleKey>
>       size_type count(const CompatibleKey& x)const;
>       template<typename CompatibleKey,typename CompatibleCompare>
>       size_type count(const CompatibleKey& x,const CompatibleCompare& comp)const;

>       template<typename CompatibleKey>
>       bool contains(const CompatibleKey& x)const;
>       template<typename CompatibleKey,typename CompatibleCompare>
>       bool contains(const CompatibleKey& x,const CompatibleCompare& comp)const;

>       template<typename CompatibleKey>
>       iterator lower_bound(const CompatibleKey& x)const;
>       template<typename CompatibleKey,typename CompatibleCompare>
>       iterator lower_bound(
>         const CompatibleKey& x,const CompatibleCompare& comp)const;

>       template<typename CompatibleKey>
>       iterator upper_bound(const CompatibleKey& x)const;
>       template<typename CompatibleKey,typename CompatibleCompare>
>       iterator upper_bound(
>         const CompatibleKey& x,const CompatibleCompare& comp)const;

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

>       // range:

>       template<typename LowerBounder,typename UpperBounder>
>       std::pair<iterator,iterator> range(
>         LowerBounder lower,UpperBounder upper)const;
>     };

>     // index comparison:

>     template<arg set 1,arg set 2>
>     bool operator==(
>       const index class name<arg set 1>& x,
>       const index class name<arg set 2>& y)
>     {
>       return x.size()==y.size()&&std::equal(x.begin(),x.end(),y.begin());
>     }

>     template<arg set 1,arg set 2>
>     bool operator<(
>       const index class name<arg set 1>& x,
>       const index class name<arg set 2>& y)
>     {
>       return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
>     }

>     template<arg set 1,arg set 2>
>     bool operator!=(
>       const index class name<arg set 1>& x,
>       const index class name<arg set 2>& y)
>     {
>       return !(x==y);
>     }

>     template<arg set 1,arg set 2>
>     bool operator>(
>       const index class name<arg set 1>& x,
>       const index class name<arg set 2>& y)
>     {
>       return y<x;
>     }

>     template<arg set 1,arg set 2>
>     bool operator>=(
>       const index class name<arg set 1>& x,
>       const index class name<arg set 2>& y)
>     {
>       return !(x<y);
>     }

>     template<arg set 1,arg set 2>
>     bool operator<=(
>       const index class name<arg set 1>& x,
>       const index class name<arg set 2>& 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 ordered indices, we adopt the scheme outlined in the complexity signature section. The complexity signature of ordered indices is:

- copying: `c(n)=n*log(n)`,
- insertion: `i(n)=log(n)`,
- hinted insertion: `h(n)=1` (amortized constant) if the hint element is immediately after the point of insertion, `h(n)=log(n)` otherwise,
- deletion: `d(n)=1` (amortized constant),
- replacement: `r(n)=1` (constant) if the element position does not change, `r(n)=log(n)` otherwise,
- modifying: `m(n)=1` (constant) if the element position does not change, `m(n)=log(n)` otherwise.

#### Instantiation types

Ordered indices are instantiated internally to `multi_index_container` and specified by means of `indexed_by` with index specifiers `ordered_unique` and `ordered_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,
- `Compare` 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`. `Compare` is a `CopyConstructible` binary predicate inducing a strict weak order on elements of `KeyFromValue::result_type`.

#### Nested types

`iterator
const_iterator`

> These types depend only on `node_type` and the position of the index in the `multi_index_container`.

#### 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.

`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.

`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. If successful, insertion happens as close as possible to the location just prior to `position`.
> 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.

`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.

`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. If successful, insertion happens as close as possible to the location just prior to `position`.
> 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.

`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*H(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. 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. If successful, insertion happens as close as possible to the location just prior to `position`.
> 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. 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: `O(log(n) + 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: `O(log(n) + 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(log(n) + 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 `key_comp` and `value_comp`, ordered 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.

#### Set operations

Ordered indices provide the full lookup functionality required by [associative.reqmts], namely `find`, `count`, `lower_bound`, `upper_bound` 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 binary predicate `Compare` inducing a strict weak order over values of type `Key`. A pair of types (`CompatibleKey`, `CompatibleCompare`) is said to be a compatible extension of `Compare` if

1. `CompatibleCompare` is a binary predicate over (`Key`, `CompatibleKey`),
2. `CompatibleCompare` is a binary predicate over (`CompatibleKey`, `Key`),
3. if `c_comp(ck,k1)` then `!c_comp(k1,ck)`,
4. if `!c_comp(ck,k1)` and `!comp(k1,k2)` then `!c_comp(ck,k2)`,
5. if `!c_comp(k1,ck)` and `!comp(k2,k1)` then `!c_comp(k2,ck)`,

for every `c_comp` of type `CompatibleCompare`, `comp` of type `Compare`, `ck` of type `CompatibleKey` and `k1`, `k2` of type `Key`.

Additionally, a type `CompatibleKey` is said to be a compatible key of `Compare` if (`CompatibleKey`, `Compare`) is a compatible extension of `Compare`. This implies that `Compare`, as well as being a strict weak ordering, accepts arguments of type `CompatibleKey`, which usually means it has several overloads of `operator()`.

In the context of a compatible extension or a compatible key, the expressions "equivalent", "less than" and "greater than" take on their obvious interpretations.

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

> Requires: `CompatibleKey` is a compatible key of `key_compare`.
> Effects: Returns a pointer to an element whose key is equivalent to `x`, or `end()` if such an element does not exist.
> Complexity: `O(log(n))`.

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

> Requires: (`CompatibleKey`, `CompatibleCompare`) is a compatible extension of `key_compare`.
> Effects: Returns a pointer to an element whose key is equivalent to `x`, or `end()` if such an element does not exist.
> Complexity: `O(log(n))`.

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

> Requires: `CompatibleKey` is a compatible key of `key_compare`.
> Effects: Returns the number of elements with key equivalent to `x`.
> Complexity: `O(log(n) + count(x))`.

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

> Requires: (`CompatibleKey`, `CompatibleCompare`) is a compatible extension of `key_compare`.
> Effects: Returns the number of elements with key equivalent to `x`.
> Complexity: `O(log(n) + count(x,comp))`.

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

> Requires: `CompatibleKey` is a compatible key of `key_compare`.
> Effects: Returns `true` iff there is some element with key equivalent to `x`.
> Complexity: `O(log(n))`.

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

> Requires: (`CompatibleKey`, `CompatibleCompare`) is a compatible extension of `key_compare`.
> Effects: Returns `true` iff there is some element with key equivalent to `x`.
> Complexity: `O(log(n))`.

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

> Requires: `CompatibleKey` is a compatible key of `key_compare`.
> Effects: Returns an iterator pointing to the first element with key not less than `x`, or `end()` if such an element does not exist.
> Complexity: `O(log(n))`.

`template<typename CompatibleKey,typename CompatibleCompare>
iterator lower_bound(const CompatibleKey& x,const CompatibleCompare& comp)const;`

> Requires: (`CompatibleKey`, `CompatibleCompare`) is a compatible extension of `key_compare`.
> Effects: Returns an iterator pointing to the first element with key not less than `x`, or `end()` if such an element does not exist.
> Complexity: `O(log(n))`.

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

> Requires: `CompatibleKey` is a compatible key of `key_compare`.
> Effects: Returns an iterator pointing to the first element with key greater than `x`, or `end()` if such an element does not exist.
> Complexity: `O(log(n))`.

`template<typename CompatibleKey,typename CompatibleCompare>
iterator upper_bound(const CompatibleKey& x,const CompatibleCompare& comp)const;`

> Requires: (`CompatibleKey`, `CompatibleCompare`) is a compatible extension of `key_compare`.
> Effects: Returns an iterator pointing to the first element with key greater than `x`, or `end()` if such an element does not exist.
> Complexity: `O(log(n))`.

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

> Requires: `CompatibleKey` is a compatible key of `key_compare`.
> Effects: Equivalent to `make_pair(lower_bound(x),upper_bound(x))`.
> Complexity: `O(log(n))`.

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

> Requires: (`CompatibleKey`, `CompatibleCompare`) is a compatible extension of `key_compare`.
> Effects: Equivalent to `make_pair(lower_bound(x,comp),upper_bound(x,comp))`.
> Complexity: `O(log(n))`.

#### Range operations

The member function `range` is not defined for sorted associative containers, but ordered indices provide it as a convenient utility. A range or interval is defined by two conditions for the lower and upper bounds, which are modeled after the following concepts.

Consider a binary predicate `Compare` inducing a strict weak order over values of type `Key`. A type `LowerBounder` is said to be a lower bounder of `Compare` if

1. `LowerBounder` is a predicate over `Key`,
2. if `lower(k1)` and `!comp(k2,k1)` then `lower(k2)`,

for every `lower` of type `LowerBounder`, `comp` of type `Compare`, and `k1`, `k2` of type `Key`. Similarly, an upper bounder is a type `UpperBounder` such that

1. `UpperBounder` is a predcate over `Key`,
2. if `upper(k1)` and `!comp(k1,k2)` then `upper(k2)`,

for every `upper` of type `UpperBounder`, `comp` of type `Compare`, and `k1`, `k2` of type `Key`. `template<typename LowerBounder,typename UpperBounder>
std::pair<iterator,iterator> range(
LowerBounder lower,UpperBounder upper)const;`

> Requires: `LowerBounder` and `UpperBounder` are a lower and upper bounder of `key_compare`, respectively.
> Effects: Returns a pair of iterators pointing to the beginning and one past the end of the subsequence of elements satisfying `lower` and `upper` simultaneously. If no such elements exist, the iterators both point to the first element satisfying `lower`, or else are equal to `end()` if this latter element does not exist.
> Complexity: `O(log(n))`.
> Variants: In place of `lower` or `upper` (or both), the singular value `boost::multi_index::unbounded` can be provided. This acts as a predicate which all values of type `key_type` satisfy.

#### 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 ordered 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, `value_comp()` must be serialization-compatible with `m.get<i>().value_comp()`, where `i` is the position of the ordered index in the container.
> Postconditions: On successful loading, each of the elements of [`begin()`, `end()`) is a restored copy of the corresponding element in [`m.get<i>().begin()`, `m.get<i>().end()`).

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 viceversa.

Index reference
Boost.MultiIndex reference
Ranked indices

Revised February 5th 2022

© Copyright 2003-2022 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)