...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/hashed_index_fwd.hpp"
synopsis"boost/multi_index/hashed_index.hpp"
synopsis
"boost/multi_index/hashed_index_fwd.hpp"
synopsisnamespace boost{ namespace multi_index{ // index specifiers hashed_unique and hashed_non_unique template<consult hashed_unique reference for arguments> struct hashed_unique; template<consult hashed_non_unique reference for arguments> struct hashed_non_unique; // indices namespace detail{ template<implementation defined> class index name is implementation defined; } // namespace boost::multi_index::detail } // namespace boost::multi_index } // namespace boost
hashed_index_fwd.hpp
provides forward declarations for index specifiers
hashed_unique
and hashed_non_unique
and
their associated hashed index classes.
"boost/multi_index/hashed_index.hpp"
synopsisnamespace boost{ namespace multi_index{ // index specifiers hashed_unique and hashed_non_unique template<consult hashed_unique reference for arguments> struct hashed_unique; template<consult hashed_non_unique reference for arguments> struct hashed_non_unique; // indices namespace detail{ template<implementation defined> class index class name implementation defined; // index 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
hashed_unique
and hashed_non_unique
These index specifiers allow
for insertion of hashed indices without and with
allowance of duplicate elements, respectively. The syntax of hashed_unique
and hashed_non_unique
coincide, thus we describe them in a grouped manner.
hashed_unique
and hashed_non_unique
can be instantiated in
two different forms, according to whether a tag list for the index is provided or not:
template< typename KeyFromValue, typename Hash=boost::hash<KeyFromValue::result_type>, typename Pred=std::equal_to<KeyFromValue::result_type> > struct (hashed_unique | hashed_non_unique); template< typename TagList, typename KeyFromValue, typename Hash=boost::hash<KeyFromValue::result_type>, typename Pred=std::equal_to<KeyFromValue::result_type> > struct (hashed_unique | hashed_non_unique);
If provided, TagList
must be an instantiation of the class template
tag
.
The template arguments are used by the corresponding index implementation,
refer to the hashed indices reference section for further
explanations on their acceptable type values.
A hashed index provides fast retrieval of elements of a multi_index_container
through hashing tecnhiques. The interface and semantics of hashed indices are modeled according
to the proposal for unordered associative containers given in the C++
Proposed
Draft Tecnhical Report on Standard Library Extensions, also known as TR1. A hashed
index is particularized according to a given
Key Extractor
that retrieves keys from elements of multi_index_container
, a Hash
function object which returns hash values for the keys and a binary predicate Pred
acting as an equivalence relation on values of Key
.
There are two variants of hashed indices: unique, which do not allow duplicate elements (with respect to its associated equality predicate) and non-unique, which accept those duplicates. The interface of these two variants is the same, so they are documented together, with minor differences explicitly stated when they exist.
Except where noted, hashed indices (both unique and non-unique) are models of
Unordered Associative Container
, in the spirit of
std::tr1::unordered_set
s. Validity of iterators and references to
elements is preserved in all cases. Occasionally, the exception safety guarantees provided
are actually stronger than required by the extension draft. 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 unordered associative containers.
namespace boost{ namespace multi_index{ namespace detail{ template<implementation defined: dependent on types Value, Allocator, TagList, KeyFromValue, Hash, Pred> class name is implementation defined { public: // types: typedef typename KeyFromValue::result_type key_type; typedef Value value_type; typedef KeyFromValue key_from_value; typedef Hash hasher; typedef Pred key_equal; typedef tuple< size_type,key_from_value,hasher,key_equal> ctor_args; typedef Allocator allocator_type; typedef typename Allocator::pointer pointer; typedef typename Allocator::const_pointer const_pointer; typedef typename Allocator::reference reference; typedef typename Allocator::const_reference const_reference; typedef implementation defined size_type; typedef implementation defined difference_type; typedef implementation defined iterator; typedef implementation defined const_iterator; typedef implementation defined local_iterator; typedef implementation defined const_local_iterator; // construct/destroy/copy: index class name& operator=(const index class name& x); allocator_type get_allocator()const; // size and capacity: bool empty()const; size_type size()const; size_type max_size()const; // iterators: iterator begin(); const_iterator begin()const; iterator end(); const_iterator end()const; const_iterator cbegin()const; const_iterator cend()const; iterator iterator_to(const value_type& x); const_iterator iterator_to(const value_type& x)const; // modifiers: 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 clear(); void swap(index class name& x); // observers: key_from_value key_extractor()const; hasher hash_function()const; key_equal key_eq()const; // lookup: template<typename CompatibleKey> iterator find(const CompatibleKey& x)const; template< typename CompatibleKey,typename CompatibleHash, typename CompatiblePred > iterator find( const CompatibleKey& x, const CompatibleHash& hash,const CompatiblePred& eq)const; template<typename CompatibleKey> size_type count(const CompatibleKey& x)const; template< typename CompatibleKey,typename CompatibleHash, typename CompatiblePred > size_type count( const CompatibleKey& x, const CompatibleHash& hash,const CompatiblePred& eq)const; template<typename CompatibleKey> std::pair<iterator,iterator> equal_range(const CompatibleKey& x)const; template< typename CompatibleKey,typename CompatibleHash, typename CompatiblePred > std::pair<iterator,iterator> equal_range( const CompatibleKey& x, const CompatibleHash& hash,const CompatiblePred& eq)const; // bucket interface: size_type bucket_count()const; size_type max_bucket_count()const; size_type bucket_size(size_type n)const; size_type bucket(const key_type& k)const; local_iterator begin(size_type n); const_local_iterator begin(size_type n)const; local_iterator end(size_type n); const_local_iterator end(size_type n)const; const_local_iterator cbegin(size_type n)const; const_local_iterator cend(size_type n)const; local_iterator local_iterator_to(const value_type& x); const_local_iterator local_iterator_to(const value_type& x)const; // hash policy: float load_factor()const; float max_load_factor()const; void max_load_factor(float z); void rehash(size_type n); }; // 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 hashed indices, we adopt the scheme outlined in the complexity signature section. The complexity signature of hashed indices is:
c(n)=n*log(n)
,i(n)=1
(constant),
worst case i(n)=n
,h(n)=1
(constant),
worst case h(n)=n
,d(n)=1
(constant),
worst case d(n)=n
,r(n)=1
(constant),r(n)=1
(constant),
worst case r(n)=n
,m(n)=1
(constant),
worst case m(n)=n
.Hashed indices are instantiated internally to multi_index_container
and
specified by means of indexed_by
with index specifiers hashed_unique
and hashed_non_unique
. Instantiations are dependent on the
following types:
Value
from multi_index_container
,Allocator
from multi_index_container
,TagList
from the index specifier (if provided),KeyFromValue
from the index specifier,Hash
from the index specifier,Pred
from the index specifier.TagList
must be an instantiation of
tag
. The type KeyFromValue
,
which determines the mechanism for extracting a key from Value
,
must be a model of
Key Extractor
from Value
. Hash
is a
Unary Function
taking a single argument of type KeyFromValue::result_type
and returning a
value of type std::size_t
in the range
[0, std::numeric_limits<std::size_t>::max())
.
Pred
is a
Binary Predicate
inducing an equivalence relation
on elements of KeyFromValue::result_type
. It is required that
the Hash
object return the same value for keys
equivalent under Pred
.
ctor_args
The first element of this tuple indicates the minimum number of buckets set up by the index on construction time. If the default value 0 is used, an implementation defined number is used instead.
iterator
const_iterator
local_iterator
const_local_iterator
These types are models of
Forward
Iterator
.
As explained in the index
concepts section, indices do not have public constructors or destructors.
Assignment, on the other hand, is provided. Upon construction,
max_load_factor()
is 1.0.
index class name& operator=(const index class name& x);
Effects: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: Average case,O(1 + m*D(n))
, worst caseO(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(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 hash_function
and key_eq
,
hashed indices have a member function for retrieving the internal key extractor
used.
key_from_value key_extractor()const;
Returns a copy of thekey_from_value
object used to construct the index.
Complexity: Constant.
Hashed indices provide the full lookup functionality required by
unordered associative containers, namely find
,
count
, and equal_range
. Additionally,
these member functions are templatized to allow for non-standard
arguments, so extending the types of search operations allowed.
The kind of arguments permissible when invoking the lookup member
functions is defined by the following concept.
Consider a pair (Hash
, Pred
) where
Hash
is a hash functor over values of type Key
and Pred
is a
Binary Predicate
inducing an equivalence relation
on Key
, with the additional constraint that equivalent
keys have the same hash value.
A triplet of types (CompatibleKey
, CompatibleHash
,
CompatiblePred
) is said to be a compatible extension
of (Hash
, Pred
) if
CompatibleHash
is a hash functor on values of
type CompatibleKey
,CompatiblePred
is a
Binary Predicate
over (Key
,
CompatibleKey
),CompatiblePred
is a
Binary Predicate
over (CompatibleKey
,
Key
),c_eq(ck,k1)
then c_eq(k1,ck)
,c_eq(ck,k1)
and eq(k1,k2)
then
c_eq(ck,k2)
,c_eq(ck,k1)
and c_eq(ck,k2)
then
eq(k1,k2)
,c_eq(ck,k1)
then c_hash(ck)==hash(k1)
,c_hash
of type CompatibleHash
,
c_eq
of type CompatiblePred
,
hash
of type Hash
,
eq
of type Pred
, ck
of type
CompatibleKey
and k1
, k2
of type
Key
.
Additionally, a type CompatibleKey
is said to be a
compatible key of (Hash
, Pred
) if
(CompatibleKey
, Hash
, Pred
)
is a compatible extension of (Hash
, Pred
).
This implies that Hash
and Pred
accept arguments
of type CompatibleKey
, which usually means they have
several overloads of therir corresponding operator()
member functions.
In the context of a compatible extension or a compatible key, the expression "equivalent key" takes on its obvious interpretation.
template<typename CompatibleKey> iterator find(const CompatibleKey& x)const;
Requires:CompatibleKey
is a compatible key of (hasher
,key_equal
).
Effects: Returns a pointer to an element whose key is equivalent tox
, orend()
if such an element does not exist.
Complexity: Average caseO(1)
(constant), worst caseO(n)
.
template<
typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>
iterator find(
const CompatibleKey& x,
const CompatibleHash& hash,const CompatiblePred& eq)const;
Requires: (CompatibleKey
,CompatibleHash
,CompatiblePred
) is a compatible extension of (hasher
,key_equal
).
Effects: Returns a pointer to an element whose key is equivalent tox
, orend()
if such an element does not exist.
Complexity: Average caseO(1)
(constant), worst caseO(n)
.
template<typename CompatibleKey>
size_type count(const CompatibleKey& x)const;
Requires:CompatibleKey
is a compatible key of (hasher
,key_equal
).
Effects: Returns the number of elements with key equivalent tox
.
Complexity: Average caseO(count(x))
, worst caseO(n)
.
template<
typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>
size_type count(
const CompatibleKey& x,
const CompatibleHash& hash,const CompatiblePred& eq)const;
Requires: (CompatibleKey
,CompatibleHash
,CompatiblePred
) is a compatible extension of (hasher
,key_equal
).
Effects: Returns the number of elements with key equivalent tox
.
Complexity: Average caseO(count(x,hash,eq))
, worst caseO(n)
.
template<typename CompatibleKey>
std::pair<iterator,iterator> equal_range(const CompatibleKey& x)const;
Requires:CompatibleKey
is a compatible key of (hasher
,key_equal
).
Effects: Returns a range containing all elements with keys equivalent tox
(and only those), or (end()
,end()
) if no such elements exist.
Complexity: Average caseO(count(x))
, worst caseO(n)
.
template<
typename CompatibleKey,typename CompatibleHash, typename CompatiblePred
>
std::pair<iterator,iterator> equal_range(
const CompatibleKey& x,
const CompatibleHash& hash,const CompatiblePred& eq)const;
Requires: (CompatibleKey
,CompatibleHash
,CompatiblePred
) is a compatible extension of (hasher
,key_equal
).
Effects: Returns a range containing all elements with keys equivalent tox
(and only those), or (end()
,end()
) if no such elements exist.
Complexity: Average caseO(count(x,hash,eq))
, worst caseO(n)
.
local_iterator local_iterator_to(const value_type& x);
const_local_iterator local_iterator_to(const value_type& x)const;
Requires:x
is a reference to an element of the container.
Returns: An iterator tox
.
Complexity: Constant.
Exception safety:nothrow
.
void rehash(size_type n);
Effects: Increases if necessary the number of internal buckets so thatsize()/bucket_count()
does not exceed the maximum load factor, andbucket_count()>=n
.
Postconditions: Validity of iterators and references to the elements contained is preserved.
Complexity: Average caseO(size())
, worst caseO(size(n)2)
.
Exception safety: Strong.
Indices cannot be serialized on their own, but only as part of the
multi_index_container
into which they are embedded. In describing
the additional preconditions and guarantees associated to hashed indices
with respect to serialization of their embedding containers, we
use the concepts defined in the multi_index_container
serialization section.
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 ankey_eq()
must be serialization-compatible withm.get<i>().key_eq()
, wherei
is the position of the hashed index in the container.
Postconditions: On succesful loading, the range [begin()
,end()
) contains restored copies of every element in [m.get<i>().begin()
,m.get<i>().end()
), though not necessarily in the same order.
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, ifOperation: saving of ait
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.
local_iterator
or
const_local_iterator
it
to an output archive (XML archive) ar
.
Requires:Operation: loading of ait
is a valid local iterator of the index. The associatedmulti_index_container
has been previously saved.
local_iterator
or
const_local_iterator
it'
from an input archive (XML archive) ar
.
Postconditions: On succesful loading, ifit
was dereferenceable then*it'
is the restored copy of*it
; ifit
wasm.get<i>().end(n)
for somen
, thenit'==m'.get<i>().end(n)
(wherem
is the originalmulti_index_container
,m'
its restored copy andi
is the ordinal of the index.)
Note: It is allowed thatit
be aconst_local_iterator
and the restoredit'
alocal_iterator
, 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)