random_access_index.hpp
来自「support vector clustering for vc++」· HPP 代码 · 共 921 行 · 第 1/2 页
HPP
921 行
/* Copyright 2003-2007 Joaqu韓 M L髉ez Mu駉z.
* 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)
*
* See http://www.boost.org/libs/multi_index for library home page.
*/
#ifndef BOOST_MULTI_INDEX_RANDOM_ACCESS_INDEX_HPP
#define BOOST_MULTI_INDEX_RANDOM_ACCESS_INDEX_HPP
#if defined(_MSC_VER)&&(_MSC_VER>=1200)
#pragma once
#endif
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <algorithm>
#include <boost/call_traits.hpp>
#include <boost/detail/no_exceptions_support.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/iterator/reverse_iterator.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/multi_index/detail/access_specifier.hpp>
#include <boost/multi_index/detail/index_node_base.hpp>
#include <boost/multi_index/detail/rnd_node_iterator.hpp>
#include <boost/multi_index/detail/rnd_index_node.hpp>
#include <boost/multi_index/detail/rnd_index_ops.hpp>
#include <boost/multi_index/detail/rnd_index_ptr_array.hpp>
#include <boost/multi_index/detail/safe_ctr_proxy.hpp>
#include <boost/multi_index/detail/safe_mode.hpp>
#include <boost/multi_index/detail/scope_guard.hpp>
#include <boost/multi_index/random_access_index_fwd.hpp>
#include <boost/throw_exception.hpp>
#include <boost/tuple/tuple.hpp>
#include <cstddef>
#include <functional>
#include <stdexcept>
#include <utility>
#if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
#include <boost/bind.hpp>
#include <boost/multi_index/detail/rnd_index_loader.hpp>
#endif
#if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
#define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT \
detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
detail::make_obj_guard(*this,&random_access_index::check_invariant_); \
BOOST_JOIN(check_invariant_,__LINE__).touch();
#else
#define BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT
#endif
namespace boost{
namespace multi_index{
namespace detail{
/* random_access_index adds a layer of random access indexing
* to a given Super
*/
template<typename SuperMeta,typename TagList>
class random_access_index:
BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
#if BOOST_WORKAROUND(BOOST_MSVC,<1300)
,public safe_ctr_proxy_impl<
rnd_node_iterator<
random_access_index_node<typename SuperMeta::type::node_type> >,
random_access_index<SuperMeta,TagList> >
#else
,public safe_mode::safe_container<
random_access_index<SuperMeta,TagList> >
#endif
#endif
{
#if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
BOOST_WORKAROUND(__MWERKS__,<=0x3003)
/* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
* lifetime of const references bound to temporaries --precisely what
* scopeguards are.
*/
#pragma parse_mfunc_templ off
#endif
typedef typename SuperMeta::type super;
protected:
typedef random_access_index_node<
typename super::node_type> node_type;
public:
/* types */
typedef typename node_type::value_type value_type;
typedef tuples::null_type ctor_args;
typedef typename super::final_allocator_type allocator_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
#if BOOST_WORKAROUND(BOOST_MSVC,<1300)
typedef safe_mode::safe_iterator<
rnd_node_iterator<node_type>,
safe_ctr_proxy<
rnd_node_iterator<node_type> > > iterator;
#else
typedef safe_mode::safe_iterator<
rnd_node_iterator<node_type>,
random_access_index> iterator;
#endif
#else
typedef rnd_node_iterator<node_type> iterator;
#endif
typedef iterator 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 typename
boost::reverse_iterator<iterator> reverse_iterator;
typedef typename
boost::reverse_iterator<const_iterator> const_reverse_iterator;
typedef TagList tag_list;
protected:
typedef typename super::final_node_type final_node_type;
typedef tuples::cons<
ctor_args,
typename super::ctor_args_list> ctor_args_list;
typedef typename mpl::push_front<
typename super::index_type_list,
random_access_index>::type index_type_list;
typedef typename mpl::push_front<
typename super::iterator_type_list,
iterator>::type iterator_type_list;
typedef typename mpl::push_front<
typename super::const_iterator_type_list,
const_iterator>::type const_iterator_type_list;
typedef typename super::copy_map_type copy_map_type;
#if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
typedef typename super::index_saver_type index_saver_type;
typedef typename super::index_loader_type index_loader_type;
#endif
private:
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
#if BOOST_WORKAROUND(BOOST_MSVC,<1300)
typedef safe_ctr_proxy_impl<
rnd_node_iterator<node_type>,
random_access_index> safe_super;
#else
typedef safe_mode::safe_container<
random_access_index> safe_super;
#endif
#endif
typedef typename call_traits<
value_type>::param_type value_param_type;
public:
/* construct/copy/destroy
* Default and copy ctors are in the protected section as indices are
* not supposed to be created on their own. No range ctor either.
*/
random_access_index<SuperMeta,TagList>& operator=(
const random_access_index<SuperMeta,TagList>& x)
{
this->final()=x.final();
return *this;
}
template <class InputIterator>
void assign(InputIterator first,InputIterator last)
{
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
clear();
for(;first!=last;++first)push_back(*first);
}
void assign(size_type n,value_param_type value)
{
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
clear();
for(size_type i=0;i<n;++i)push_back(value);
}
allocator_type get_allocator()const
{
return this->final().get_allocator();
}
/* iterators */
iterator begin()
{return make_iterator(node_type::from_impl(*ptrs.begin()));}
const_iterator begin()const
{return make_iterator(node_type::from_impl(*ptrs.begin()));}
iterator end(){return make_iterator(header());}
const_iterator end()const{return make_iterator(header());}
reverse_iterator rbegin(){return make_reverse_iterator(end());}
const_reverse_iterator rbegin()const{return make_reverse_iterator(end());}
reverse_iterator rend(){return make_reverse_iterator(begin());}
const_reverse_iterator rend()const{return make_reverse_iterator(begin());}
/* capacity */
bool empty()const{return this->final_empty_();}
size_type size()const{return this->final_size_();}
size_type max_size()const{return this->final_max_size_();}
size_type capacity()const{return ptrs.capacity();}
void reserve(size_type n){ptrs.reserve(n);}
void resize(size_type n,value_param_type x=value_type())
{
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
if(n>size())insert(end(),n-size(),x);
else if(n<size())erase(begin()+n,end());
}
/* access: no non-const versions provided as random_access_index
* handles const elements.
*/
const_reference operator[](size_type n)const
{
BOOST_MULTI_INDEX_SAFE_MODE_ASSERT(n<size(),safe_mode::out_of_bounds);
return node_type::from_impl(*ptrs.at(n))->value();
}
const_reference at(size_type n)const
{
if(n>=size())throw_exception(std::out_of_range("random access index"));
return node_type::from_impl(*ptrs.at(n))->value();
}
const_reference front()const{return operator[](0);}
const_reference back()const{return operator[](size()-1);}
/* modifiers */
std::pair<iterator,bool> push_front(value_param_type x)
{return insert(begin(),x);}
void pop_front(){erase(begin());}
std::pair<iterator,bool> push_back(value_param_type x)
{return insert(end(),x);}
void pop_back(){erase(--end());}
std::pair<iterator,bool> insert(iterator position,value_param_type x)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
std::pair<final_node_type*,bool> p=this->final_insert_(x);
if(p.second&&position.get_node()!=header()){
relocate(position.get_node(),p.first);
}
return std::pair<iterator,bool>(make_iterator(p.first),p.second);
}
void insert(iterator position,size_type n,value_param_type x)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
size_type s=0;
BOOST_TRY{
while(n--){
if(push_back(x).second)++s;
}
}
BOOST_CATCH(...){
relocate(position,end()-s,end());
BOOST_RETHROW;
}
BOOST_CATCH_END
relocate(position,end()-s,end());
}
template<typename InputIterator>
void insert(iterator position,InputIterator first,InputIterator last)
{
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
size_type s=0;
BOOST_TRY{
for(;first!=last;++first){
if(push_back(*first).second)++s;
}
}
BOOST_CATCH(...){
relocate(position,end()-s,end());
BOOST_RETHROW;
}
BOOST_CATCH_END
relocate(position,end()-s,end());
}
iterator erase(iterator position)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
return position;
}
iterator erase(iterator first,iterator last)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
difference_type n=last-first;
relocate(end(),first,last);
while(n--)pop_back();
return last;
}
bool replace(iterator position,value_param_type x)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
return this->final_replace_(
x,static_cast<final_node_type*>(position.get_node()));
}
template<typename Modifier>
bool modify(iterator position,Modifier mod)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
/* MSVC++ 6.0 optimizer on safe mode code chokes if this
* this is not added. Left it for all compilers as it does no
* harm.
*/
position.detach();
#endif
return this->final_modify_(
mod,static_cast<final_node_type*>(position.get_node()));
}
void swap(random_access_index<SuperMeta,TagList>& x)
{
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
this->final_swap_(x.final());
}
void clear()
{
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
this->final_clear_();
}
/* list operations */
void splice(iterator position,random_access_index<SuperMeta,TagList>& x)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_CHECK_DIFFERENT_CONTAINER(*this,x);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
iterator first=x.begin(),last=x.end();
size_type n=0;
BOOST_TRY{
while(first!=last){
if(push_back(*first).second){
first=x.erase(first);
++n;
}
else ++first;
}
}
BOOST_CATCH(...){
relocate(position,end()-n,end());
BOOST_RETHROW;
}
BOOST_CATCH_END
relocate(position,end()-n,end());
}
void splice(
iterator position,random_access_index<SuperMeta,TagList>& x,iterator i)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(i);
BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(i);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(i,x);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
if(&x==this)relocate(position,i);
else{
if(insert(position,*i).second){
#if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
/* MSVC++ 6.0 optimizer has a hard time with safe mode, and the following
* workaround is needed. Left it for all compilers as it does no
* harm.
*/
i.detach();
x.erase(x.make_iterator(i.get_node()));
#else
x.erase(i);
#endif
}
}
}
void splice(
iterator position,random_access_index<SuperMeta,TagList>& x,
iterator first,iterator last)
{
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,x);
BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,x);
BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
BOOST_MULTI_INDEX_RND_INDEX_CHECK_INVARIANT;
if(&x==this)relocate(position,first,last);
else{
size_type n=0;
BOOST_TRY{
while(first!=last){
if(push_back(*first).second){
first=x.erase(first);
++n;
}
else ++first;
}
}
BOOST_CATCH(...){
relocate(position,end()-n,end());
BOOST_RETHROW;
}
BOOST_CATCH_END
relocate(position,end()-n,end());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?