# Boost.MultiIndex Ordered indices reference

Index reference
Boost.MultiIndex reference
Hashed 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
    - Constructors, copy and assignment
    - 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

>     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, ordered indices (both unique and non-unique) are models of `Sorted Associative Container` and `Unique Associative Container`, much as `std::set`s are. Accordingly, validity of iterators and references to elements is preserved. We only provide descriptions of those types and operations that are either not present in the concepts modeled or do not exactly conform to the requirements for these types of containers.

>     namespace boost{

>     namespace multi_index{

>     namespace{ 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 tuple<key_from_value,key_compare>          ctor_args;
>       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;

>       // construct/copy/destroy:

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

>       allocator_type get_allocator()const;

>       // iterators:

>       iterator               begin();
>       const_iterator         begin()const;
>       iterator               end();
>       const_iterator         end()const;
>       reverse_iterator       rbegin();
>       const_reverse_iterator rbegin()const;
>       reverse_iterator       rend();
>       const_reverse_iterator rend()const;

>       // capacity:

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

>       // modifiers:

>       std::pair<iterator,bool> insert(const value_type& x);
>       iterator insert(iterator position,const value_type& x);
>       template<typename InputIterator>
>       void insert(InputIterator first,InputIterator last);

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

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

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

>       // 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>
>       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` (constant) if the hint element precedes the point of insertion, `h(n)=log(n)` otherwise,
- deletion: `d(n)=1` (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),
- `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 `Strict Weak Ordering` on elements of `KeyFromValue::result_type`.

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

#### Modifiers

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

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

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

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

> Requires: `InputIterator` is a model of `Input Iterator` over elements of type `value_type` or a type convertible to `value_type`. `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:

> >     iterator hint=end();
> >     while(first!=last)hint=insert(hint,*first++);

> Complexity: `O(m*H(n+m))`, where `m` is the number of elements in [`first`, `last`).
> Exception safety: Basic.

`void erase(iterator position);`

> Requires: `position` is a valid dereferenceable iterator of the index. Effects: Deletes the element pointed to by `position`.
> 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.

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

> Requires: [`first`,`last`) is a valid range of the index.
> Effects: Deletes the elements in [`first`,`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);`

> Requires: `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.
> 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: `Modifier` is a model of `Unary Function` accepting arguments of type `value_type&`. `position` is a valid dereferenceable iterator of the index. 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.
> 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 (except possibly `mod`), then the element pointed to by `position` is erased.

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

> Requires: `key_from_value` is a read/write `Key Extractor` from `value_type`. `Modifier` is a model of `Unary Function` accepting arguments of type `key_type&`. `position` is a valid dereferenceable iterator of the index. Effects: Calls `mod(k)` where `k` is the key obtained by the internal `KeyFromValue` object of the index from 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.
> 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 (except possibly `mod`), then the element pointed to by `position` is erased.

#### 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 `Sorted Associative Containers` and `Unique Associative Containers`, 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 `Strict Weak Ordering` `Compare` 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>
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 `Strict Weak Ordering` `Compare` 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 `Predicate` 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 succesful 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 succesful 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
Hashed indices

Revised February 7th 2005

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