...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/random_access_index_fwd.hpp"
synopsis"boost/multi_index/random_access_index.hpp"
synopsis
"boost/multi_index/random_access_index_fwd.hpp"
synopsisnamespace boost{ namespace multi_index{ // random_access index specifier template<typename TagList=tag<> > struct random_access; // indices namespace detail{ template<implementation defined> class index class name implementation defined; } // namespace boost::multi_index::detail } // namespace boost::multi_index } // namespace boost
random_access_index_fwd.hpp
provides forward declarations for the
random_access
index specifier and
its associated random access index class.
"boost/multi_index/random_access_index.hpp"
synopsisnamespace boost{ namespace multi_index{ // random_access index specifier template<typename TagList=tag<> > struct random_access; // 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
random_access
index specifier
This index specifier allows for insertion of a random access index.
template<typename TagList=tag<> > struct random_access;
If provided, TagList
must be an instantiation of
tag
.
Random access indices are free-order sequences with constant time
positional access and random access iterators. Elements in a
random access index are by default sorted according to their order of
insertion: this means that new elements inserted through a different index
of the multi_index_container
are appended to the end of the
random access index; additionally, facilities are provided
for further rearrangement of the elements. The public interface of
random access indices includes that of
sequenced indices, with differences in
the complexity of the operations, plus extra operations for
positional access (operator[]
and at()
) and
for capacity handling. Validity of iterators and references to elements
is preserved in all operations, regardless of the capacity status.
As is the case with sequenced indices, random access indices have the following limitations with respect to STL sequence containers:
Assignable
(like any other index.)replace
and
modify
member functions.
Random Access Container
and
Back Insertion Sequence
. Although these indices do
not model
Front Insertion Sequence
, because front insertion
and deletion take linear time, front operations are nonetheless provided
to match the interface of sequenced indices.
We only describe 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 detail{ template<implementation defined: dependent on types Value, Allocator, TagList> class name is implementation defined { public: // types: typedef typename node_type::value_type value_type; typedef tuples::null_type ctor_args; typedef typename Allocator allocator_type; typedef typename allocator_type::reference reference; typedef typename allocator_type::const_reference const_reference; typedef implementation defined iterator; typedef implementation defined const_iterator; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef typename allocator_type::pointer pointer; typedef typename allocator_type::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); template <class InputIterator> void assign(InputIterator first,InputIterator last); void assign(size_type n,const value_type& value); 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; size_type capacity()const; void reserve(size_type m); void resize(size_type n,const value_type& x=value_type()); // access: const_reference operator[](size_type n)const; const_reference at(size_type n)const; const_reference front()const; const_reference back()const; // modifiers: std::pair<iterator,bool> push_front(const value_type& x); void pop_front(); std::pair<iterator,bool> push_back(const value_type& x); void pop_back(); std::pair<iterator,bool> insert(iterator position,const value_type& x); void insert(iterator position,size_type m,const value_type& x); template<typename InputIterator> void insert(iterator position,InputIterator first,InputIterator last); iterator erase(iterator position); 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); void swap(index class name& x); void clear(); // list operations: void splice(iterator position,index class name& x); void splice(iterator position,index class name& x,iterator i); void splice( iterator position,index class name& x,iterator first,iterator last); void remove(const value_type& value); template<typename Predicate> void remove_if(Predicate pred); void unique(); template <class BinaryPredicate> void unique(BinaryPredicate binary_pred); void merge(index class name& x); template <typename Compare> void merge(index class name& x,Compare comp); void sort(); template <typename Compare> void sort(Compare comp); void reverse(); // rearrange operations: void relocate(iterator position,iterator i); void relocate(iterator position,iterator first,iterator last); template<typename InputIterator> void rearrange(InputIterator first); } // 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 random access indices, we adopt the scheme outlined in the complexity signature section. The complexity signature of random access indices is:
c(n)=n*log(n)
,i(n)=1
(amortized constant),h(n)=1
(amortized constant),d(n)=m
, where m
is the distance
from the deleted element to the end of the sequence,r(n)=1
(constant),m(n)=1
(constant).shl(a,b)
=a+b
if a is nonzero,0
otherwise.
rel(a,b,c)
= ifa<b
,c-a
, elsea-b
,
(shl
and rel
stand for shift left and
relocate, respectively.)
Random access indices are instantiated internally to multi_index_container
and specified by means of
indexed_by
with the random_access
index specifier. Instantiations are dependent on the following types:
Value
from multi_index_container
,Allocator
from multi_index_container
,TagList
from the index specifier (if provided).TagList
must be an instantiation of
tag
.
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
.
template <class InputIterator>
void assign(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:clear(); insert(end(),first,last);
void assign(size_type n,const value_type& value);
Effects:clear(); for(size_type i=0;i<n;++n)push_back(v);
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
.
size_type capacity()const;
Returns: The total number of elementsc
such that, whensize()<c
, back insertions happen in constant time (the general case as described byi(n)
is amortized constant time.)
Note: Validity of iterators and references to elements is preserved in all insertions, regardless of the capacity status.
void reserve(size_type m);
Effects: If the previous value ofcapacity()
was greater than or equal tom
, nothing is done; otherwise, the internal capacity is changed so thatcapacity()>=m
.
Complexity: If the capacity is not changed, constant; otherwiseO(n)
.
Exception safety: If the capacity is not changed,nothrow
; otherwise, strong.
void resize(size_type n,const value_type& x=value_type());
Effects:Note: If an expansion is requested, the size of the index is not guaranteed to beif(n>size())insert(end(),n-size(),x); else if(n<size())erase(begin()+n,end());n
after this operation (other indices may ban insertions.)
std::pair<iterator,bool> push_front(const value_type& x);
Effects: Insertsx
at the beginning of the sequence if no other index of themulti_index_container
bans the insertion.
Returns: The return value is a pairp
.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(n+I(n))
.
Exception safety: Strong.
std::pair<iterator,bool> push_back(const value_type& x);
Effects: Insertsx
at the end of the sequence if no other index of themulti_index_container
bans the insertion.
Returns: The return value is a pairp
.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.
std::pair<iterator,bool> insert(iterator position,const value_type& x);
Requires:position
is a valid iterator of the index.
Effects: Insertsx
beforeposition
if insertion is allowed by all other indices of themulti_index_container
.
Returns: The return value is a pairp
.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(shl(end()-position,1) + I(n))
.
Exception safety: Strong.
void insert(iterator position,size_type m,const value_type& x);
Requires:position
is a valid iterator of the index.
Effects:Complexity:for(size_type i=0;i<m;++i)insert(position,x);O(shl(end()-position,m) + m*I(n+m))
.
template<typename InputIterator>
void insert(iterator position,InputIterator first,InputIterator last);
Requires:position
is a valid iterator of the index.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:while(first!=last)insert(position,*first++);O(shl(end()-position,m) + m*I(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
.
iterator erase(iterator first,iterator last);
Requires: [first
,last
) is a valid range of the index.
Effects: Deletes the elements in [first
,last
).
Returns:last
.
Complexity:O(m*D(n))
, 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 replacing is allowed by all other indices of themulti_index_container
.
Postconditions: Validity ofposition
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 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 on random access indices does not change the position of the element with respect to the index; rearrangement on other indices may or might not succeed. If the rearrangement fails, the element is erased.
Postconditions: Validity ofposition
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 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 on random access indices does not change the position of the element with respect to the index; rearrangement on other indices may or might not succeed. If the rearrangement fails,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.
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.
Random access indices replicate the interface of sequenced indices, which
in turn includes the list operations provided by std::list
.
The syntax and behavior of these operations exactly matches those
of sequenced indices, but the associated complexity bounds differ in general.
void splice(iterator position,index class name& x);
Requires:position
is a valid iterator of the index.&x!=this
.
Effects: Inserts the contents ofx
beforeposition
, in the same order as they were inx
. Those elements succesfully inserted are erased fromx
.
Complexity:O(shl(end()-position,x.size()) + x.size()*I(n+x.size()) + x.size()*D(x.size()))
.
Exception safety: Basic.
void splice(iterator position,index class name& x,iterator i);
Requires:position
is a valid iterator of the index.i
is a valid dereferenceable iteratorx
.
Effects: Inserts the element pointed to byi
beforeposition
: if insertion is succesful, the element is erased fromx
. In the special case&x==this
, no copy or deletion is performed, and the operation is always succesful. Ifposition==i
, no operation is performed.
Postconditions: If&x==this
, no iterator or reference is invalidated.
Complexity: If&x==this
,O(rel(position,i,i+1))
; otherwiseO(shl(end()-position,1) + I(n) + D(n))
.
Exception safety: If&x==this
,nothrow
; otherwise, strong.
void splice(iterator position,index class name& x,iterator first,iterator last);
Requires:position
is a valid iterator of the index.first
andlast
are valid iterators ofx
.last
is reachable fromfirst
.position
is not in the range [first
,last
).
Effects: For each element in the range [first
,last
), insertion is tried beforeposition
; if the operation is succesful, the element is erased fromx
. In the special case&x==this
, no copy or deletion is performed, and insertions are always succesful.
Postconditions: If&x==this
, no iterator or reference is invalidated.
Complexity: If&x==this
,O(rel(position,first,last))
; otherwiseO(shl(end()-position,m) + m*I(n+m) + m*D(x.size()))
wherem
is the number of elements in [first
,last
).
Exception safety: If&x==this
,nothrow
; otherwise, basic.
void remove(const value_type& value);
Effects: Erases all elements of the index which compare equal tovalue
.
Complexity:O(n + m*D(n))
, wherem
is the number of elements erased.
Exception safety: Basic.
template<typename Predicate> void remove_if(Predicate pred);
Effects: Erases all elementsx
of the index for whichpred(x)
holds.
Complexity:O(n + m*D(n))
, wherem
is the number of elements erased.
Exception safety: Basic.
void unique();
Effects: Eliminates all but the first element from every consecutive group of equal elements referred to by the iteratori
in the range [first+1
,last
) for which*i==*(i-1)
.
Complexity:O(n + m*D(n))
, wherem
is the number of elements erased.
Exception safety: Basic.
template <class BinaryPredicate> void unique(BinaryPredicate binary_pred);
Effects: Eliminates all but the first element from every consecutive group of elements referred to by the iteratori
in the range [first+1
,last
) for whichbinary_pred(*i,*(i-1))
holds.
Complexity:O(n + m*D(n))
, wherem
is the number of elements erased.
Exception safety: Basic.
void merge(index class name& x);
Requires:std::less<value_type>
is aStrict Weak Ordering
overvalue_type
. Both the index andx
are sorted according tostd::less<value_type>
.
Effects: Attempts to insert every element ofx
into the corresponding position of the index (according to the order). Elements successfully inserted are erased fromx
. The resulting sequence is stable, i.e. equivalent elements of either container preserve their relative position. In the special case&x==this
, no operation is performed.
Postconditions: Elements in the index and remaining elements inx
are sorted. Validity of iterators to the index and of non-erased elements ofx
references is preserved.
Complexity: If&x==this
, constant; otherwiseO(n + x.size()*I(n+x.size()) + x.size()*D(x.size()))
.
Exception safety: If&x==this
,nothrow
; otherwise, basic.
template <typename Compare> void merge(index class name& x,Compare comp);
Requires:Compare
is aStrict Weak Ordering
overvalue_type
. Both the index andx
are sorted according tocomp
.
Effects: Attempts to insert every element ofx
into the corresponding position of the index (according tocomp
). Elements successfully inserted are erased fromx
. The resulting sequence is stable, i.e. equivalent elements of either container preserve their relative position. In the special case&x==this
, no operation is performed.
Postconditions: Elements in the index and remaining elements inx
are sorted according tocomp
. Validity of iterators to the index and of non-erased elements ofx
references is preserved.
Complexity: If&x==this
, constant; otherwiseO(n + x.size()*I(n+x.size()) + x.size()*D(x.size()))
.
Exception safety: If&x==this
,nothrow
; otherwise, basic.
void sort();
Requires:std::less<value_type>
is aStrict Weak Ordering
overvalue_type
.
Effects: Sorts the index according tostd::less<value_type>
. The sorting is stable, i.e. equivalent elements preserve their relative position.
Postconditions: Validity of iterators and references is preserved.
Complexity:O(n*log(n))
.
Exception safety: Basic.
template <typename Compare> void sort(Compare comp);
Requires:Compare
is aStrict Weak Ordering
overvalue_type
.
Effects: Sorts the index according tocomp
. The sorting is stable, i.e. equivalent elements preserve their relative position.
Postconditions: Validity of iterators and references is preserved.
Complexity:O(n*log(n))
.
Exception safety: Basic.
void reverse();
Effects: Reverses the order of the elements in the index.
Postconditions: Validity of iterators and references is preserved.
Complexity:O(n)
.
Exception safety:nothrow
.
These operations, without counterpart in STL sequence containers
(although std::list::splice
provides partially overlapping
functionality), perform individual and global repositioning of elements
inside the index.
void relocate(iterator position,iterator i);
Requires:position
is a valid iterator of the index.i
is a valid dereferenceable iterator of the index.
Effects: Inserts the element pointed to byi
beforeposition
. Ifposition==i
, no operation is performed.
Postconditions: No iterator or reference is invalidated.
Complexity:O(rel(position,i,i+1))
.
Exception safety:nothrow
.
void relocate(iterator position,iterator first,iterator last);
Requires:position
is a valid iterator of the index.first
andlast
are valid iterators of the index.last
is reachable fromfirst
.position
is not in the range [first
,last
).
Effects: The range of elements [first
,last
) is repositioned just beforeposition
.
Postconditions: No iterator or reference is invalidated.
Complexity:O(rel(position,first,last))
.
Exception safety:nothrow
.
template<typename InputIterator> void rearrange(InputIterator first);
Requires: The range [first
,std::advance(first,n)
), wheren
is the size of the index, is a free view of the index.
Effects: The elements are rearranged so as to match the order of the previously described view.
Postconditions: No iterator or reference is invalidated.
Complexity:O(n)
.
Exception safety: Basic.
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 random access 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: No additional requirements to those imposed by the container.Operation: saving of an
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()
), wherei
is the position of the random access index in the container.
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)