...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
"boost/multi_index/ordered_index_fwd.hpp"
synopsis"boost/multi_index/ordered_index.hpp"
synopsis
"boost/multi_index/ordered_index_fwd.hpp"
synopsisnamespace 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.
"boost/multi_index/ordered_index.hpp"
synopsisnamespace 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
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.
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{ 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; const_iterator cbegin()const; const_iterator cend()const; const_reverse_iterator crbegin()const; const_reverse_iterator crend()const; iterator iterator_to(const value_type& x); const_iterator iterator_to(const value_type& x)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); 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); 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(); // 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
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:
c(n)=n*log(n)
,i(n)=log(n)
,h(n)=1
(constant) if the hint element
is immediately after the point of insertion, h(n)=log(n)
otherwise,d(n)=1
(amortized constant),r(n)=1
(constant) if the element position does not
change, r(n)=log(n)
otherwise,m(n)=1
(constant) if the element position does not
change, m(n)=log(n)
otherwise.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
.
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:wherea=b;a
andb
are themulti_index_container
objects to which*this
andx
belong, respectively.
Returns:*this
.
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 tox
.
Complexity: Constant.
Exception safety:nothrow
.
std::pair<iterator,bool> insert(const value_type& x);
Effects: Insertsx
into themulti_index_container
to which the index belongs ifReturns: The return value is a pair
- 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
.p
.p.second
istrue
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: Insertsx
into themulti_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 ofInput Iterator
over elements of typevalue_type
or a type convertible tovalue_type
.first
andlast
are not iterators into any index of themulti_index_container
to which this index belongs.last
is reachable fromfirst
.
Effects:Complexity:iterator hint=end(); while(first!=last)hint=insert(hint,*first++);O(m*H(n+m))
, wherem
is the number of elements in [first
,last
).
Exception safety: Basic.
iterator erase(iterator position);
Requires:position
is a valid dereferenceable iterator of the index.
Effects: Deletes the element pointed to byposition
.
Returns: An iterator pointing to the element immediately following the one that was deleted, orend()
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 tox
.
Returns: Number of elements deleted.
Complexity:O(log(n) + m*D(n))
, wherem
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))
, wherem
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 valuex
to the element pointed to byposition
into themulti_index_container
to which the index belongs if, for the valuex
Postconditions: Validity of
- 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
.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 themulti_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 ofUnary Function
accepting arguments of typevalue_type&
.position
is a valid dereferenceable iterator of the index.
Effects: Callsmod(e)
wheree
is the element pointed to byposition
and rearranges*position
into all the indices of themulti_index_container
. Rearrangement is successful ifIf the rearrangement fails, the element is erased.
- 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
.
Postconditions: Validity ofposition
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 (except possiblymod
), then the element pointed to byposition
is erased.
template<typename Modifier,typename Rollback>
bool modify(iterator position,Modifier mod,Rollback back);
Requires:Modifier
andRollback
are models ofUnary Function
accepting arguments of typevalue_type&
.position
is a valid dereferenceable iterator of the index. The sequence of operationsmod(e)
,back(e)
, wheree
is the element pointed to byposition
, restores all keys of the element to their original state.
Effects: Callsmod(e)
wheree
is the element pointed to byposition
and tries to rearrange*position
into all the indices of themulti_index_container
. Rearrangement is successful ifIf the rearrangement fails,
- 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
.back(e)
is invoked and the element is kept at its original position in all indices.
Postconditions: Validity ofposition
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 ifback
throws an exception, in which case the modified element is erased. Ifback
throws inside the handling code executing after some other user-provided operation has thrown, it is the exception generated byback
that is rethrown.
template<typename Modifier> bool modify_key(iterator position,Modifier mod);
Requires:key_from_value
is a read/writeKey Extractor
fromvalue_type
.Modifier
is a model ofUnary Function
accepting arguments of typekey_type&
.position
is a valid dereferenceable iterator of the index.
Effects: Equivalent tomodify(position,mod')
, withmod'
defined in such a way thatmod'(x)
is the same asmod(key(x))
, wherekey
is the internalKeyFromValue
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/writeKey Extractor
fromvalue_type
.Modifier
andRollback
are models ofUnary Function
accepting arguments of typekey_type&
.position
is a valid dereferenceable iterator of the index. The sequence of operationsmod(k)
,back(k)
, wherek
is the key of the element pointed to byposition
, restores k to its original state.
Effects: Equivalent tomodify(position,mod',back')
, withmod'
andback
defined in such a way thatmod'(x)
is the same asmod(key(x))
andback'(x)
is the same asback(key(x))
, wherekey
is the internalKeyFromValue
object of the index.
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 thekey_from_value
object used to construct the index.
Complexity: Constant.
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
CompatibleCompare
is a
Binary Predicate
over (Key
,
CompatibleKey
),CompatibleCompare
is a
Binary Predicate
over (CompatibleKey
,
Key
),c_comp(ck,k1)
then !c_comp(k1,ck)
,!c_comp(ck,k1)
and !comp(k1,k2)
then
!c_comp(ck,k2)
,!c_comp(k1,ck)
and !comp(k2,k1)
then
!c_comp(k2,ck)
,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 ofkey_compare
.
Effects: Returns a pointer to an element whose key is equivalent tox
, orend()
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 ofkey_compare
.
Effects: Returns a pointer to an element whose key is equivalent tox
, orend()
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 ofkey_compare
.
Effects: Returns the number of elements with key equivalent tox
.
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 ofkey_compare
.
Effects: Returns the number of elements with key equivalent tox
.
Complexity:O(log(n) + count(x,comp))
.
template<typename CompatibleKey>
iterator lower_bound(const CompatibleKey& x)const;
Requires:CompatibleKey
is a compatible key ofkey_compare
.
Effects: Returns an iterator pointing to the first element with key not less thanx
, orend()
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 ofkey_compare
.
Effects: Returns an iterator pointing to the first element with key not less thanx
, orend()
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 ofkey_compare
.
Effects: Returns an iterator pointing to the first element with key greater thanx
, orend()
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 ofkey_compare
.
Effects: Returns an iterator pointing to the first element with key greater thanx
, orend()
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 ofkey_compare
.
Effects: Equivalent tomake_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 ofkey_compare
.
Effects: Equivalent tomake_pair(lower_bound(x,comp),upper_bound(x,comp))
.
Complexity:O(log(n))
.
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
LowerBounder
is a
Predicate
over Key
,lower(k1)
and !comp(k2,k1)
then
lower(k2)
,lower
of type LowerBounder
,
comp
of type Compare
, and k1
,
k2
of type Key
. Similarly, an upper bounder
is a type UpperBounder
such that
UpperBounder
is a
Predicate
over Key
,upper(k1)
and !comp(k1,k2)
then
upper(k2)
,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
andUpperBounder
are a lower and upper bounder ofkey_compare
, respectively.
Effects: Returns a pair of iterators pointing to the beginning and one past the end of the subsequence of elements satisfyinglower
andupper
simultaneously. If no such elements exist, the iterators both point to the first element satisfyinglower
, or else are equal toend()
if this latter element does not exist.
Complexity:O(log(n))
.
Variants: In place oflower
orupper
(or both), the singular valueboost::multi_index::unbounded
can be provided. This acts as a predicate which all values of typekey_type
satisfy.
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.
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,Operation: saving of anvalue_comp()
must be serialization-compatible withm.get<i>().value_comp()
, wherei
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()
).
iterator
or const_iterator
it
to an output archive (XML archive) ar
.
Requires:Operation: loading of anit
is a valid iterator of the index. The associatedmulti_index_container
has been previously saved.
iterator
or const_iterator
it'
from an input archive (XML archive) ar
.
Postconditions: On succesful loading, ifit
was dereferenceable then*it'
is the restored copy of*it
, otherwiseit'==end()
.
Note: It is allowed thatit
be aconst_iterator
and the restoredit'
aniterator
, or viceversa.
Revised June 25th 2008
© Copyright 2003-2008 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)