annotate DEPENDENCIES/generic/include/boost/multi_index/hashed_index.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@101 1 /* Copyright 2003-2015 Joaquin M Lopez Munoz.
Chris@16 2 * Distributed under the Boost Software License, Version 1.0.
Chris@16 3 * (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 4 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 5 *
Chris@16 6 * See http://www.boost.org/libs/multi_index for library home page.
Chris@16 7 */
Chris@16 8
Chris@16 9 #ifndef BOOST_MULTI_INDEX_HASHED_INDEX_HPP
Chris@16 10 #define BOOST_MULTI_INDEX_HASHED_INDEX_HPP
Chris@16 11
Chris@101 12 #if defined(_MSC_VER)
Chris@16 13 #pragma once
Chris@16 14 #endif
Chris@16 15
Chris@16 16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
Chris@16 17 #include <algorithm>
Chris@16 18 #include <boost/call_traits.hpp>
Chris@16 19 #include <boost/detail/allocator_utilities.hpp>
Chris@16 20 #include <boost/detail/no_exceptions_support.hpp>
Chris@16 21 #include <boost/detail/workaround.hpp>
Chris@16 22 #include <boost/foreach_fwd.hpp>
Chris@16 23 #include <boost/limits.hpp>
Chris@16 24 #include <boost/move/core.hpp>
Chris@16 25 #include <boost/mpl/bool.hpp>
Chris@101 26 #include <boost/mpl/if.hpp>
Chris@16 27 #include <boost/mpl/push_front.hpp>
Chris@16 28 #include <boost/multi_index/detail/access_specifier.hpp>
Chris@16 29 #include <boost/multi_index/detail/auto_space.hpp>
Chris@16 30 #include <boost/multi_index/detail/bucket_array.hpp>
Chris@16 31 #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
Chris@16 32 #include <boost/multi_index/detail/hash_index_iterator.hpp>
Chris@16 33 #include <boost/multi_index/detail/index_node_base.hpp>
Chris@16 34 #include <boost/multi_index/detail/modify_key_adaptor.hpp>
Chris@101 35 #include <boost/multi_index/detail/promotes_arg.hpp>
Chris@16 36 #include <boost/multi_index/detail/safe_mode.hpp>
Chris@16 37 #include <boost/multi_index/detail/scope_guard.hpp>
Chris@16 38 #include <boost/multi_index/detail/vartempl_support.hpp>
Chris@16 39 #include <boost/multi_index/hashed_index_fwd.hpp>
Chris@16 40 #include <boost/tuple/tuple.hpp>
Chris@101 41 #include <boost/type_traits/is_same.hpp>
Chris@101 42 #include <cmath>
Chris@16 43 #include <cstddef>
Chris@16 44 #include <functional>
Chris@101 45 #include <iterator>
Chris@16 46 #include <utility>
Chris@16 47
Chris@16 48 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
Chris@16 49 #include <initializer_list>
Chris@16 50 #endif
Chris@16 51
Chris@16 52 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
Chris@16 53 #include <boost/serialization/nvp.hpp>
Chris@16 54 #endif
Chris@16 55
Chris@16 56 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
Chris@16 57 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x) \
Chris@16 58 detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
Chris@16 59 detail::make_obj_guard(x,&hashed_index::check_invariant_); \
Chris@16 60 BOOST_JOIN(check_invariant_,__LINE__).touch();
Chris@16 61 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT \
Chris@16 62 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(*this)
Chris@16 63 #else
Chris@16 64 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x)
Chris@16 65 #define BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT
Chris@16 66 #endif
Chris@16 67
Chris@16 68 namespace boost{
Chris@16 69
Chris@16 70 namespace multi_index{
Chris@16 71
Chris@16 72 namespace detail{
Chris@16 73
Chris@16 74 /* hashed_index adds a layer of hashed indexing to a given Super */
Chris@16 75
Chris@16 76 /* Most of the implementation of unique and non-unique indices is
Chris@16 77 * shared. We tell from one another on instantiation time by using
Chris@101 78 * Category tags defined in hash_index_node.hpp.
Chris@16 79 */
Chris@16 80
Chris@16 81 template<
Chris@16 82 typename KeyFromValue,typename Hash,typename Pred,
Chris@16 83 typename SuperMeta,typename TagList,typename Category
Chris@16 84 >
Chris@16 85 class hashed_index:
Chris@16 86 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
Chris@16 87
Chris@16 88 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 89 ,public safe_mode::safe_container<
Chris@16 90 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category> >
Chris@16 91 #endif
Chris@16 92
Chris@16 93 {
Chris@16 94 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
Chris@16 95 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
Chris@16 96 /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
Chris@16 97 * lifetime of const references bound to temporaries --precisely what
Chris@16 98 * scopeguards are.
Chris@16 99 */
Chris@16 100
Chris@16 101 #pragma parse_mfunc_templ off
Chris@16 102 #endif
Chris@16 103
Chris@16 104 typedef typename SuperMeta::type super;
Chris@16 105
Chris@16 106 protected:
Chris@16 107 typedef hashed_index_node<
Chris@101 108 typename super::node_type,Category> node_type;
Chris@16 109
Chris@16 110 private:
Chris@101 111 typedef typename node_type::node_alg node_alg;
Chris@16 112 typedef typename node_type::impl_type node_impl_type;
Chris@16 113 typedef typename node_impl_type::pointer node_impl_pointer;
Chris@101 114 typedef typename node_impl_type::base_pointer node_impl_base_pointer;
Chris@16 115 typedef bucket_array<
Chris@16 116 typename super::final_allocator_type> bucket_array_type;
Chris@16 117
Chris@16 118 public:
Chris@16 119 /* types */
Chris@16 120
Chris@16 121 typedef typename KeyFromValue::result_type key_type;
Chris@16 122 typedef typename node_type::value_type value_type;
Chris@16 123 typedef KeyFromValue key_from_value;
Chris@16 124 typedef Hash hasher;
Chris@16 125 typedef Pred key_equal;
Chris@16 126 typedef tuple<std::size_t,
Chris@16 127 key_from_value,hasher,key_equal> ctor_args;
Chris@16 128 typedef typename super::final_allocator_type allocator_type;
Chris@16 129 typedef typename allocator_type::pointer pointer;
Chris@16 130 typedef typename allocator_type::const_pointer const_pointer;
Chris@16 131 typedef typename allocator_type::reference reference;
Chris@16 132 typedef typename allocator_type::const_reference const_reference;
Chris@16 133 typedef std::size_t size_type;
Chris@16 134 typedef std::ptrdiff_t difference_type;
Chris@16 135
Chris@16 136 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 137 typedef safe_mode::safe_iterator<
Chris@16 138 hashed_index_iterator<
Chris@101 139 node_type,bucket_array_type,
Chris@101 140 hashed_index_global_iterator_tag>,
Chris@16 141 hashed_index> iterator;
Chris@16 142 #else
Chris@16 143 typedef hashed_index_iterator<
Chris@101 144 node_type,bucket_array_type,
Chris@101 145 hashed_index_global_iterator_tag> iterator;
Chris@16 146 #endif
Chris@16 147
Chris@16 148 typedef iterator const_iterator;
Chris@16 149
Chris@101 150 typedef hashed_index_iterator<
Chris@101 151 node_type,bucket_array_type,
Chris@101 152 hashed_index_local_iterator_tag> local_iterator;
Chris@101 153 typedef local_iterator const_local_iterator;
Chris@101 154
Chris@16 155 typedef TagList tag_list;
Chris@16 156
Chris@16 157 protected:
Chris@16 158 typedef typename super::final_node_type final_node_type;
Chris@16 159 typedef tuples::cons<
Chris@16 160 ctor_args,
Chris@16 161 typename super::ctor_args_list> ctor_args_list;
Chris@16 162 typedef typename mpl::push_front<
Chris@16 163 typename super::index_type_list,
Chris@16 164 hashed_index>::type index_type_list;
Chris@16 165 typedef typename mpl::push_front<
Chris@16 166 typename super::iterator_type_list,
Chris@16 167 iterator>::type iterator_type_list;
Chris@16 168 typedef typename mpl::push_front<
Chris@16 169 typename super::const_iterator_type_list,
Chris@16 170 const_iterator>::type const_iterator_type_list;
Chris@16 171 typedef typename super::copy_map_type copy_map_type;
Chris@16 172
Chris@16 173 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
Chris@16 174 typedef typename super::index_saver_type index_saver_type;
Chris@16 175 typedef typename super::index_loader_type index_loader_type;
Chris@16 176 #endif
Chris@16 177
Chris@16 178 private:
Chris@16 179 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 180 typedef safe_mode::safe_container<
Chris@16 181 hashed_index> safe_super;
Chris@16 182 #endif
Chris@16 183
Chris@16 184 typedef typename call_traits<value_type>::param_type value_param_type;
Chris@16 185 typedef typename call_traits<
Chris@16 186 key_type>::param_type key_param_type;
Chris@16 187
Chris@16 188 /* Needed to avoid commas in BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL
Chris@16 189 * expansion.
Chris@16 190 */
Chris@16 191
Chris@16 192 typedef std::pair<iterator,bool> emplace_return_type;
Chris@16 193
Chris@16 194 public:
Chris@16 195
Chris@16 196 /* construct/destroy/copy
Chris@16 197 * Default and copy ctors are in the protected section as indices are
Chris@16 198 * not supposed to be created on their own. No range ctor either.
Chris@16 199 */
Chris@16 200
Chris@16 201 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& operator=(
Chris@16 202 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
Chris@16 203 {
Chris@16 204 this->final()=x.final();
Chris@16 205 return *this;
Chris@16 206 }
Chris@16 207
Chris@16 208 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
Chris@16 209 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& operator=(
Chris@16 210 std::initializer_list<value_type> list)
Chris@16 211 {
Chris@16 212 this->final()=list;
Chris@16 213 return *this;
Chris@16 214 }
Chris@16 215 #endif
Chris@16 216
Chris@101 217 allocator_type get_allocator()const BOOST_NOEXCEPT
Chris@16 218 {
Chris@16 219 return this->final().get_allocator();
Chris@16 220 }
Chris@16 221
Chris@16 222 /* size and capacity */
Chris@16 223
Chris@101 224 bool empty()const BOOST_NOEXCEPT{return this->final_empty_();}
Chris@101 225 size_type size()const BOOST_NOEXCEPT{return this->final_size_();}
Chris@101 226 size_type max_size()const BOOST_NOEXCEPT{return this->final_max_size_();}
Chris@16 227
Chris@16 228 /* iterators */
Chris@16 229
Chris@101 230 iterator begin()BOOST_NOEXCEPT
Chris@101 231 {return make_iterator(node_type::from_impl(header()->next()->prior()));}
Chris@101 232 const_iterator begin()const BOOST_NOEXCEPT
Chris@101 233 {return make_iterator(node_type::from_impl(header()->next()->prior()));}
Chris@101 234 iterator end()BOOST_NOEXCEPT{return make_iterator(header());}
Chris@101 235 const_iterator end()const BOOST_NOEXCEPT{return make_iterator(header());}
Chris@101 236 const_iterator cbegin()const BOOST_NOEXCEPT{return begin();}
Chris@101 237 const_iterator cend()const BOOST_NOEXCEPT{return end();}
Chris@16 238
Chris@16 239 iterator iterator_to(const value_type& x)
Chris@16 240 {
Chris@16 241 return make_iterator(node_from_value<node_type>(&x));
Chris@16 242 }
Chris@16 243
Chris@16 244 const_iterator iterator_to(const value_type& x)const
Chris@16 245 {
Chris@16 246 return make_iterator(node_from_value<node_type>(&x));
Chris@16 247 }
Chris@16 248
Chris@16 249 /* modifiers */
Chris@16 250
Chris@16 251 BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
Chris@16 252 emplace_return_type,emplace,emplace_impl)
Chris@16 253
Chris@16 254 BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG(
Chris@16 255 iterator,emplace_hint,emplace_hint_impl,iterator,position)
Chris@16 256
Chris@16 257 std::pair<iterator,bool> insert(const value_type& x)
Chris@16 258 {
Chris@16 259 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 260 std::pair<final_node_type*,bool> p=this->final_insert_(x);
Chris@16 261 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
Chris@16 262 }
Chris@16 263
Chris@16 264 std::pair<iterator,bool> insert(BOOST_RV_REF(value_type) x)
Chris@16 265 {
Chris@16 266 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 267 std::pair<final_node_type*,bool> p=this->final_insert_rv_(x);
Chris@16 268 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
Chris@16 269 }
Chris@16 270
Chris@16 271 iterator insert(iterator position,const value_type& x)
Chris@16 272 {
Chris@16 273 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 274 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 275 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 276 std::pair<final_node_type*,bool> p=this->final_insert_(
Chris@16 277 x,static_cast<final_node_type*>(position.get_node()));
Chris@16 278 return make_iterator(p.first);
Chris@16 279 }
Chris@16 280
Chris@16 281 iterator insert(iterator position,BOOST_RV_REF(value_type) x)
Chris@16 282 {
Chris@16 283 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 284 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 285 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 286 std::pair<final_node_type*,bool> p=this->final_insert_rv_(
Chris@16 287 x,static_cast<final_node_type*>(position.get_node()));
Chris@16 288 return make_iterator(p.first);
Chris@16 289 }
Chris@16 290
Chris@16 291 template<typename InputIterator>
Chris@16 292 void insert(InputIterator first,InputIterator last)
Chris@16 293 {
Chris@16 294 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 295 for(;first!=last;++first)this->final_insert_ref_(*first);
Chris@16 296 }
Chris@16 297
Chris@16 298 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
Chris@16 299 void insert(std::initializer_list<value_type> list)
Chris@16 300 {
Chris@16 301 insert(list.begin(),list.end());
Chris@16 302 }
Chris@16 303 #endif
Chris@16 304
Chris@16 305 iterator erase(iterator position)
Chris@16 306 {
Chris@16 307 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 308 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
Chris@16 309 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 310 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 311 this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
Chris@16 312 return position;
Chris@16 313 }
Chris@16 314
Chris@16 315 size_type erase(key_param_type k)
Chris@16 316 {
Chris@16 317 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 318
Chris@101 319 std::size_t buc=buckets.position(hash_(k));
Chris@101 320 for(node_impl_pointer x=buckets.at(buc)->prior();
Chris@101 321 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
Chris@101 322 if(eq_(k,key(node_type::from_impl(x)->value()))){
Chris@101 323 node_impl_pointer y=end_of_range(x);
Chris@101 324 size_type s=0;
Chris@16 325 do{
Chris@101 326 node_impl_pointer z=node_alg::after(x);
Chris@16 327 this->final_erase_(
Chris@101 328 static_cast<final_node_type*>(node_type::from_impl(x)));
Chris@101 329 x=z;
Chris@16 330 ++s;
Chris@101 331 }while(x!=y);
Chris@101 332 return s;
Chris@16 333 }
Chris@16 334 }
Chris@101 335 return 0;
Chris@16 336 }
Chris@16 337
Chris@16 338 iterator erase(iterator first,iterator last)
Chris@16 339 {
Chris@16 340 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
Chris@16 341 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
Chris@16 342 BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
Chris@16 343 BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
Chris@16 344 BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
Chris@16 345 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 346 while(first!=last){
Chris@16 347 first=erase(first);
Chris@16 348 }
Chris@16 349 return first;
Chris@16 350 }
Chris@16 351
Chris@16 352 bool replace(iterator position,const value_type& x)
Chris@16 353 {
Chris@16 354 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 355 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
Chris@16 356 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 357 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 358 return this->final_replace_(
Chris@16 359 x,static_cast<final_node_type*>(position.get_node()));
Chris@16 360 }
Chris@16 361
Chris@16 362 bool replace(iterator position,BOOST_RV_REF(value_type) x)
Chris@16 363 {
Chris@16 364 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 365 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
Chris@16 366 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 367 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 368 return this->final_replace_rv_(
Chris@16 369 x,static_cast<final_node_type*>(position.get_node()));
Chris@16 370 }
Chris@16 371
Chris@16 372 template<typename Modifier>
Chris@16 373 bool modify(iterator position,Modifier mod)
Chris@16 374 {
Chris@16 375 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 376 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
Chris@16 377 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 378 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 379
Chris@16 380 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 381 /* MSVC++ 6.0 optimizer on safe mode code chokes if this
Chris@16 382 * this is not added. Left it for all compilers as it does no
Chris@16 383 * harm.
Chris@16 384 */
Chris@16 385
Chris@16 386 position.detach();
Chris@16 387 #endif
Chris@16 388
Chris@16 389 return this->final_modify_(
Chris@16 390 mod,static_cast<final_node_type*>(position.get_node()));
Chris@16 391 }
Chris@16 392
Chris@16 393 template<typename Modifier,typename Rollback>
Chris@101 394 bool modify(iterator position,Modifier mod,Rollback back_)
Chris@16 395 {
Chris@16 396 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 397 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
Chris@16 398 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 399 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 400
Chris@16 401 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 402 /* MSVC++ 6.0 optimizer on safe mode code chokes if this
Chris@16 403 * this is not added. Left it for all compilers as it does no
Chris@16 404 * harm.
Chris@16 405 */
Chris@16 406
Chris@16 407 position.detach();
Chris@16 408 #endif
Chris@16 409
Chris@16 410 return this->final_modify_(
Chris@101 411 mod,back_,static_cast<final_node_type*>(position.get_node()));
Chris@16 412 }
Chris@16 413
Chris@16 414 template<typename Modifier>
Chris@16 415 bool modify_key(iterator position,Modifier mod)
Chris@16 416 {
Chris@16 417 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 418 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
Chris@16 419 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 420 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 421 return modify(
Chris@16 422 position,modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key));
Chris@16 423 }
Chris@16 424
Chris@16 425 template<typename Modifier,typename Rollback>
Chris@101 426 bool modify_key(iterator position,Modifier mod,Rollback back_)
Chris@16 427 {
Chris@16 428 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 429 BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
Chris@16 430 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 431 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 432 return modify(
Chris@16 433 position,
Chris@16 434 modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key),
Chris@101 435 modify_key_adaptor<Rollback,value_type,KeyFromValue>(back_,key));
Chris@16 436 }
Chris@16 437
Chris@101 438 void clear()BOOST_NOEXCEPT
Chris@16 439 {
Chris@16 440 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 441 this->final_clear_();
Chris@16 442 }
Chris@16 443
Chris@16 444 void swap(hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
Chris@16 445 {
Chris@16 446 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 447 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF(x);
Chris@16 448 this->final_swap_(x.final());
Chris@16 449 }
Chris@16 450
Chris@16 451 /* observers */
Chris@16 452
Chris@16 453 key_from_value key_extractor()const{return key;}
Chris@16 454 hasher hash_function()const{return hash_;}
Chris@16 455 key_equal key_eq()const{return eq_;}
Chris@16 456
Chris@16 457 /* lookup */
Chris@16 458
Chris@16 459 /* Internally, these ops rely on const_iterator being the same
Chris@16 460 * type as iterator.
Chris@16 461 */
Chris@16 462
Chris@101 463 /* Implementation note: When CompatibleKey is consistently promoted to
Chris@101 464 * KeyFromValue::result_type for equality comparison, the promotion is made
Chris@101 465 * once in advance to increase efficiency.
Chris@101 466 */
Chris@101 467
Chris@16 468 template<typename CompatibleKey>
Chris@16 469 iterator find(const CompatibleKey& k)const
Chris@16 470 {
Chris@16 471 return find(k,hash_,eq_);
Chris@16 472 }
Chris@16 473
Chris@16 474 template<
Chris@16 475 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
Chris@16 476 >
Chris@16 477 iterator find(
Chris@16 478 const CompatibleKey& k,
Chris@16 479 const CompatibleHash& hash,const CompatiblePred& eq)const
Chris@16 480 {
Chris@101 481 return find(
Chris@101 482 k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
Chris@16 483 }
Chris@16 484
Chris@16 485 template<typename CompatibleKey>
Chris@16 486 size_type count(const CompatibleKey& k)const
Chris@16 487 {
Chris@16 488 return count(k,hash_,eq_);
Chris@16 489 }
Chris@16 490
Chris@16 491 template<
Chris@16 492 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
Chris@16 493 >
Chris@16 494 size_type count(
Chris@16 495 const CompatibleKey& k,
Chris@16 496 const CompatibleHash& hash,const CompatiblePred& eq)const
Chris@16 497 {
Chris@101 498 return count(
Chris@101 499 k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
Chris@16 500 }
Chris@16 501
Chris@16 502 template<typename CompatibleKey>
Chris@16 503 std::pair<iterator,iterator> equal_range(const CompatibleKey& k)const
Chris@16 504 {
Chris@16 505 return equal_range(k,hash_,eq_);
Chris@16 506 }
Chris@16 507
Chris@16 508 template<
Chris@16 509 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
Chris@16 510 >
Chris@16 511 std::pair<iterator,iterator> equal_range(
Chris@16 512 const CompatibleKey& k,
Chris@16 513 const CompatibleHash& hash,const CompatiblePred& eq)const
Chris@16 514 {
Chris@101 515 return equal_range(
Chris@101 516 k,hash,eq,promotes_1st_arg<CompatiblePred,CompatibleKey,key_type>());
Chris@16 517 }
Chris@16 518
Chris@16 519 /* bucket interface */
Chris@16 520
Chris@101 521 size_type bucket_count()const BOOST_NOEXCEPT{return buckets.size();}
Chris@101 522 size_type max_bucket_count()const BOOST_NOEXCEPT{return static_cast<size_type>(-1);}
Chris@16 523
Chris@16 524 size_type bucket_size(size_type n)const
Chris@16 525 {
Chris@101 526 size_type res=0;
Chris@101 527 for(node_impl_pointer x=buckets.at(n)->prior();
Chris@101 528 x!=node_impl_pointer(0);x=node_alg::after_local(x)){
Chris@16 529 ++res;
Chris@16 530 }
Chris@16 531 return res;
Chris@16 532 }
Chris@16 533
Chris@16 534 size_type bucket(key_param_type k)const
Chris@16 535 {
Chris@16 536 return buckets.position(hash_(k));
Chris@16 537 }
Chris@16 538
Chris@16 539 local_iterator begin(size_type n)
Chris@16 540 {
Chris@16 541 return const_cast<const hashed_index*>(this)->begin(n);
Chris@16 542 }
Chris@16 543
Chris@16 544 const_local_iterator begin(size_type n)const
Chris@16 545 {
Chris@101 546 node_impl_pointer x=buckets.at(n)->prior();
Chris@101 547 if(x==node_impl_pointer(0))return end(n);
Chris@101 548 return make_local_iterator(node_type::from_impl(x));
Chris@16 549 }
Chris@16 550
Chris@16 551 local_iterator end(size_type n)
Chris@16 552 {
Chris@16 553 return const_cast<const hashed_index*>(this)->end(n);
Chris@16 554 }
Chris@16 555
Chris@101 556 const_local_iterator end(size_type)const
Chris@16 557 {
Chris@101 558 return make_local_iterator(0);
Chris@16 559 }
Chris@16 560
Chris@16 561 const_local_iterator cbegin(size_type n)const{return begin(n);}
Chris@16 562 const_local_iterator cend(size_type n)const{return end(n);}
Chris@16 563
Chris@16 564 local_iterator local_iterator_to(const value_type& x)
Chris@16 565 {
Chris@101 566 return make_local_iterator(node_from_value<node_type>(&x));
Chris@16 567 }
Chris@16 568
Chris@16 569 const_local_iterator local_iterator_to(const value_type& x)const
Chris@16 570 {
Chris@101 571 return make_local_iterator(node_from_value<node_type>(&x));
Chris@16 572 }
Chris@16 573
Chris@16 574 /* hash policy */
Chris@16 575
Chris@101 576 float load_factor()const BOOST_NOEXCEPT
Chris@101 577 {return static_cast<float>(size())/bucket_count();}
Chris@101 578 float max_load_factor()const BOOST_NOEXCEPT{return mlf;}
Chris@16 579 void max_load_factor(float z){mlf=z;calculate_max_load();}
Chris@16 580
Chris@16 581 void rehash(size_type n)
Chris@16 582 {
Chris@16 583 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@101 584 if(size()<=max_load&&n<=bucket_count())return;
Chris@16 585
Chris@16 586 size_type bc =(std::numeric_limits<size_type>::max)();
Chris@16 587 float fbc=static_cast<float>(1+size()/mlf);
Chris@16 588 if(bc>fbc){
Chris@16 589 bc=static_cast<size_type>(fbc);
Chris@16 590 if(bc<n)bc=n;
Chris@16 591 }
Chris@16 592 unchecked_rehash(bc);
Chris@16 593 }
Chris@16 594
Chris@101 595 void reserve(size_type n)
Chris@101 596 {
Chris@101 597 rehash(static_cast<size_type>(std::ceil(static_cast<double>(n)/mlf)));
Chris@101 598 }
Chris@101 599
Chris@16 600 BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
Chris@16 601 hashed_index(const ctor_args_list& args_list,const allocator_type& al):
Chris@16 602 super(args_list.get_tail(),al),
Chris@16 603 key(tuples::get<1>(args_list.get_head())),
Chris@16 604 hash_(tuples::get<2>(args_list.get_head())),
Chris@16 605 eq_(tuples::get<3>(args_list.get_head())),
Chris@16 606 buckets(al,header()->impl(),tuples::get<0>(args_list.get_head())),
Chris@101 607 mlf(1.0f)
Chris@16 608 {
Chris@16 609 calculate_max_load();
Chris@16 610 }
Chris@16 611
Chris@16 612 hashed_index(
Chris@16 613 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x):
Chris@16 614 super(x),
Chris@16 615
Chris@16 616 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 617 safe_super(),
Chris@16 618 #endif
Chris@16 619
Chris@16 620 key(x.key),
Chris@16 621 hash_(x.hash_),
Chris@16 622 eq_(x.eq_),
Chris@16 623 buckets(x.get_allocator(),header()->impl(),x.buckets.size()),
Chris@16 624 mlf(x.mlf),
Chris@101 625 max_load(x.max_load)
Chris@16 626 {
Chris@16 627 /* Copy ctor just takes the internal configuration objects from x. The rest
Chris@16 628 * is done in subsequent call to copy_().
Chris@16 629 */
Chris@16 630 }
Chris@16 631
Chris@16 632 hashed_index(
Chris@16 633 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
Chris@16 634 do_not_copy_elements_tag):
Chris@16 635 super(x,do_not_copy_elements_tag()),
Chris@16 636
Chris@16 637 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 638 safe_super(),
Chris@16 639 #endif
Chris@16 640
Chris@16 641 key(x.key),
Chris@16 642 hash_(x.hash_),
Chris@16 643 eq_(x.eq_),
Chris@16 644 buckets(x.get_allocator(),header()->impl(),0),
Chris@101 645 mlf(1.0f)
Chris@16 646 {
Chris@16 647 calculate_max_load();
Chris@16 648 }
Chris@16 649
Chris@16 650 ~hashed_index()
Chris@16 651 {
Chris@16 652 /* the container is guaranteed to be empty by now */
Chris@16 653 }
Chris@16 654
Chris@16 655 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 656 iterator make_iterator(node_type* node)
Chris@16 657 {
Chris@101 658 return iterator(node,this);
Chris@16 659 }
Chris@16 660
Chris@16 661 const_iterator make_iterator(node_type* node)const
Chris@16 662 {
Chris@101 663 return const_iterator(node,const_cast<hashed_index*>(this));
Chris@16 664 }
Chris@16 665 #else
Chris@16 666 iterator make_iterator(node_type* node)
Chris@16 667 {
Chris@101 668 return iterator(node);
Chris@16 669 }
Chris@16 670
Chris@16 671 const_iterator make_iterator(node_type* node)const
Chris@16 672 {
Chris@101 673 return const_iterator(node);
Chris@16 674 }
Chris@16 675 #endif
Chris@16 676
Chris@101 677 local_iterator make_local_iterator(node_type* node)
Chris@101 678 {
Chris@101 679 return local_iterator(node);
Chris@101 680 }
Chris@101 681
Chris@101 682 const_local_iterator make_local_iterator(node_type* node)const
Chris@101 683 {
Chris@101 684 return const_local_iterator(node);
Chris@101 685 }
Chris@101 686
Chris@16 687 void copy_(
Chris@16 688 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
Chris@16 689 const copy_map_type& map)
Chris@16 690 {
Chris@101 691 copy_(x,map,Category());
Chris@101 692 }
Chris@16 693
Chris@101 694 void copy_(
Chris@101 695 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
Chris@101 696 const copy_map_type& map,hashed_unique_tag)
Chris@101 697 {
Chris@101 698 if(x.size()!=0){
Chris@101 699 node_impl_pointer end_org=x.header()->impl(),
Chris@101 700 org=end_org,
Chris@101 701 cpy=header()->impl();
Chris@101 702 do{
Chris@101 703 node_impl_pointer prev_org=org->prior(),
Chris@101 704 prev_cpy=
Chris@101 705 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
Chris@101 706 node_type::from_impl(prev_org))))->impl();
Chris@101 707 cpy->prior()=prev_cpy;
Chris@101 708 if(node_alg::is_first_of_bucket(org)){
Chris@101 709 node_impl_base_pointer buc_org=prev_org->next(),
Chris@101 710 buc_cpy=
Chris@101 711 buckets.begin()+(buc_org-x.buckets.begin());
Chris@101 712 prev_cpy->next()=buc_cpy;
Chris@101 713 buc_cpy->prior()=cpy;
Chris@101 714 }
Chris@101 715 else{
Chris@101 716 prev_cpy->next()=node_impl_type::base_pointer_from(cpy);
Chris@101 717 }
Chris@101 718 org=prev_org;
Chris@101 719 cpy=prev_cpy;
Chris@101 720 }while(org!=end_org);
Chris@16 721 }
Chris@16 722
Chris@16 723 super::copy_(x,map);
Chris@16 724 }
Chris@16 725
Chris@101 726 void copy_(
Chris@101 727 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
Chris@101 728 const copy_map_type& map,hashed_non_unique_tag)
Chris@101 729 {
Chris@101 730 if(x.size()!=0){
Chris@101 731 node_impl_pointer end_org=x.header()->impl(),
Chris@101 732 org=end_org,
Chris@101 733 cpy=header()->impl();
Chris@101 734 do{
Chris@101 735 node_impl_pointer next_org=node_alg::after(org),
Chris@101 736 next_cpy=
Chris@101 737 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
Chris@101 738 node_type::from_impl(next_org))))->impl();
Chris@101 739 if(node_alg::is_first_of_bucket(next_org)){
Chris@101 740 node_impl_base_pointer buc_org=org->next(),
Chris@101 741 buc_cpy=
Chris@101 742 buckets.begin()+(buc_org-x.buckets.begin());
Chris@101 743 cpy->next()=buc_cpy;
Chris@101 744 buc_cpy->prior()=next_cpy;
Chris@101 745 next_cpy->prior()=cpy;
Chris@101 746 }
Chris@101 747 else{
Chris@101 748 if(org->next()==node_impl_type::base_pointer_from(next_org)){
Chris@101 749 cpy->next()=node_impl_type::base_pointer_from(next_cpy);
Chris@101 750 }
Chris@101 751 else{
Chris@101 752 cpy->next()=
Chris@101 753 node_impl_type::base_pointer_from(
Chris@101 754 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
Chris@101 755 node_type::from_impl(
Chris@101 756 node_impl_type::pointer_from(org->next())))))->impl());
Chris@101 757 }
Chris@101 758
Chris@101 759 if(next_org->prior()!=org){
Chris@101 760 next_cpy->prior()=
Chris@101 761 static_cast<node_type*>(map.find(static_cast<final_node_type*>(
Chris@101 762 node_type::from_impl(next_org->prior()))))->impl();
Chris@101 763 }
Chris@101 764 else{
Chris@101 765 next_cpy->prior()=cpy;
Chris@101 766 }
Chris@101 767 }
Chris@101 768 org=next_org;
Chris@101 769 cpy=next_cpy;
Chris@101 770 }while(org!=end_org);
Chris@101 771 }
Chris@101 772
Chris@101 773 super::copy_(x,map);
Chris@101 774 }
Chris@101 775
Chris@16 776 template<typename Variant>
Chris@101 777 final_node_type* insert_(
Chris@101 778 value_param_type v,final_node_type*& x,Variant variant)
Chris@16 779 {
Chris@101 780 reserve_for_insert(size()+1);
Chris@16 781
Chris@101 782 std::size_t buc=find_bucket(v);
Chris@101 783 link_info pos(buckets.at(buc));
Chris@101 784 if(!link_point(v,pos)){
Chris@101 785 return static_cast<final_node_type*>(
Chris@101 786 node_type::from_impl(node_impl_type::pointer_from(pos)));
Chris@101 787 }
Chris@16 788
Chris@101 789 final_node_type* res=super::insert_(v,x,variant);
Chris@101 790 if(res==x)link(static_cast<node_type*>(x),pos);
Chris@16 791 return res;
Chris@16 792 }
Chris@16 793
Chris@16 794 template<typename Variant>
Chris@101 795 final_node_type* insert_(
Chris@101 796 value_param_type v,node_type* position,final_node_type*& x,Variant variant)
Chris@16 797 {
Chris@101 798 reserve_for_insert(size()+1);
Chris@16 799
Chris@101 800 std::size_t buc=find_bucket(v);
Chris@101 801 link_info pos(buckets.at(buc));
Chris@101 802 if(!link_point(v,pos)){
Chris@101 803 return static_cast<final_node_type*>(
Chris@101 804 node_type::from_impl(node_impl_type::pointer_from(pos)));
Chris@101 805 }
Chris@16 806
Chris@101 807 final_node_type* res=super::insert_(v,position,x,variant);
Chris@101 808 if(res==x)link(static_cast<node_type*>(x),pos);
Chris@16 809 return res;
Chris@16 810 }
Chris@16 811
Chris@16 812 void erase_(node_type* x)
Chris@16 813 {
Chris@16 814 unlink(x);
Chris@16 815 super::erase_(x);
Chris@16 816
Chris@16 817 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 818 detach_iterators(x);
Chris@16 819 #endif
Chris@16 820 }
Chris@16 821
Chris@16 822 void delete_all_nodes_()
Chris@16 823 {
Chris@101 824 delete_all_nodes_(Category());
Chris@101 825 }
Chris@101 826
Chris@101 827 void delete_all_nodes_(hashed_unique_tag)
Chris@101 828 {
Chris@101 829 for(node_impl_pointer x_end=header()->impl(),x=x_end->prior();x!=x_end;){
Chris@101 830 node_impl_pointer y=x->prior();
Chris@101 831 this->final_delete_node_(
Chris@101 832 static_cast<final_node_type*>(node_type::from_impl(x)));
Chris@101 833 x=y;
Chris@101 834 }
Chris@101 835 }
Chris@101 836
Chris@101 837 void delete_all_nodes_(hashed_non_unique_tag)
Chris@101 838 {
Chris@101 839 for(node_impl_pointer x_end=header()->impl(),x=x_end->prior();x!=x_end;){
Chris@101 840 node_impl_pointer y=x->prior();
Chris@101 841 if(y->next()!=node_impl_type::base_pointer_from(x)&&
Chris@101 842 y->next()->prior()!=x){ /* n-1 of group */
Chris@101 843 /* Make the second node prior() pointer back-linked so that it won't
Chris@101 844 * refer to a deleted node when the time for its own destruction comes.
Chris@101 845 */
Chris@101 846
Chris@101 847 node_impl_pointer first=node_impl_type::pointer_from(y->next());
Chris@101 848 first->next()->prior()=first;
Chris@16 849 }
Chris@101 850 this->final_delete_node_(
Chris@101 851 static_cast<final_node_type*>(node_type::from_impl(x)));
Chris@101 852 x=y;
Chris@16 853 }
Chris@16 854 }
Chris@16 855
Chris@16 856 void clear_()
Chris@16 857 {
Chris@16 858 super::clear_();
Chris@101 859 buckets.clear(header()->impl());
Chris@16 860
Chris@16 861 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 862 safe_super::detach_dereferenceable_iterators();
Chris@16 863 #endif
Chris@16 864 }
Chris@16 865
Chris@16 866 void swap_(
Chris@16 867 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
Chris@16 868 {
Chris@16 869 std::swap(key,x.key);
Chris@16 870 std::swap(hash_,x.hash_);
Chris@16 871 std::swap(eq_,x.eq_);
Chris@16 872 buckets.swap(x.buckets);
Chris@16 873 std::swap(mlf,x.mlf);
Chris@16 874 std::swap(max_load,x.max_load);
Chris@16 875
Chris@16 876 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 877 safe_super::swap(x);
Chris@16 878 #endif
Chris@16 879
Chris@16 880 super::swap_(x);
Chris@16 881 }
Chris@16 882
Chris@16 883 void swap_elements_(
Chris@16 884 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x)
Chris@16 885 {
Chris@16 886 buckets.swap(x.buckets);
Chris@16 887 std::swap(mlf,x.mlf);
Chris@16 888 std::swap(max_load,x.max_load);
Chris@16 889
Chris@16 890 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 891 safe_super::swap(x);
Chris@16 892 #endif
Chris@16 893
Chris@16 894 super::swap_elements_(x);
Chris@16 895 }
Chris@16 896
Chris@16 897 template<typename Variant>
Chris@16 898 bool replace_(value_param_type v,node_type* x,Variant variant)
Chris@16 899 {
Chris@16 900 if(eq_(key(v),key(x->value()))){
Chris@16 901 return super::replace_(v,x,variant);
Chris@16 902 }
Chris@101 903
Chris@101 904 unlink_undo undo;
Chris@101 905 unlink(x,undo);
Chris@16 906
Chris@16 907 BOOST_TRY{
Chris@101 908 std::size_t buc=find_bucket(v);
Chris@101 909 link_info pos(buckets.at(buc));
Chris@101 910 if(link_point(v,pos)&&super::replace_(v,x,variant)){
Chris@16 911 link(x,pos);
Chris@16 912 return true;
Chris@16 913 }
Chris@101 914 undo();
Chris@16 915 return false;
Chris@16 916 }
Chris@16 917 BOOST_CATCH(...){
Chris@101 918 undo();
Chris@16 919 BOOST_RETHROW;
Chris@16 920 }
Chris@16 921 BOOST_CATCH_END
Chris@16 922 }
Chris@16 923
Chris@16 924 bool modify_(node_type* x)
Chris@16 925 {
Chris@16 926 std::size_t buc;
Chris@16 927 bool b;
Chris@16 928 BOOST_TRY{
Chris@16 929 buc=find_bucket(x->value());
Chris@101 930 b=in_place(x->impl(),key(x->value()),buc);
Chris@16 931 }
Chris@16 932 BOOST_CATCH(...){
Chris@16 933 erase_(x);
Chris@16 934 BOOST_RETHROW;
Chris@16 935 }
Chris@16 936 BOOST_CATCH_END
Chris@16 937 if(!b){
Chris@16 938 unlink(x);
Chris@16 939 BOOST_TRY{
Chris@101 940 link_info pos(buckets.at(buc));
Chris@101 941 if(!link_point(x->value(),pos)){
Chris@16 942 super::erase_(x);
Chris@16 943
Chris@16 944 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 945 detach_iterators(x);
Chris@16 946 #endif
Chris@16 947 return false;
Chris@16 948 }
Chris@16 949 link(x,pos);
Chris@16 950 }
Chris@16 951 BOOST_CATCH(...){
Chris@16 952 super::erase_(x);
Chris@16 953
Chris@16 954 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 955 detach_iterators(x);
Chris@16 956 #endif
Chris@16 957
Chris@16 958 BOOST_RETHROW;
Chris@16 959 }
Chris@16 960 BOOST_CATCH_END
Chris@16 961 }
Chris@16 962
Chris@16 963 BOOST_TRY{
Chris@16 964 if(!super::modify_(x)){
Chris@16 965 unlink(x);
Chris@101 966
Chris@16 967 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 968 detach_iterators(x);
Chris@16 969 #endif
Chris@16 970 return false;
Chris@16 971 }
Chris@16 972 else return true;
Chris@16 973 }
Chris@16 974 BOOST_CATCH(...){
Chris@16 975 unlink(x);
Chris@16 976
Chris@16 977 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 978 detach_iterators(x);
Chris@16 979 #endif
Chris@16 980
Chris@16 981 BOOST_RETHROW;
Chris@16 982 }
Chris@16 983 BOOST_CATCH_END
Chris@16 984 }
Chris@16 985
Chris@16 986 bool modify_rollback_(node_type* x)
Chris@16 987 {
Chris@16 988 std::size_t buc=find_bucket(x->value());
Chris@101 989 if(in_place(x->impl(),key(x->value()),buc)){
Chris@16 990 return super::modify_rollback_(x);
Chris@16 991 }
Chris@16 992
Chris@101 993 unlink_undo undo;
Chris@101 994 unlink(x,undo);
Chris@16 995
Chris@16 996 BOOST_TRY{
Chris@101 997 link_info pos(buckets.at(buc));
Chris@101 998 if(link_point(x->value(),pos)&&super::modify_rollback_(x)){
Chris@16 999 link(x,pos);
Chris@16 1000 return true;
Chris@16 1001 }
Chris@101 1002 undo();
Chris@16 1003 return false;
Chris@16 1004 }
Chris@16 1005 BOOST_CATCH(...){
Chris@101 1006 undo();
Chris@16 1007 BOOST_RETHROW;
Chris@16 1008 }
Chris@16 1009 BOOST_CATCH_END
Chris@16 1010 }
Chris@16 1011
Chris@101 1012 /* comparison */
Chris@101 1013
Chris@101 1014 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
Chris@101 1015 /* defect macro refers to class, not function, templates, but anyway */
Chris@101 1016
Chris@101 1017 template<typename K,typename H,typename P,typename S,typename T,typename C>
Chris@101 1018 friend bool operator==(
Chris@101 1019 const hashed_index<K,H,P,S,T,C>&,const hashed_index<K,H,P,S,T,C>& y);
Chris@101 1020 #endif
Chris@101 1021
Chris@101 1022 bool equals(const hashed_index& x)const{return equals(x,Category());}
Chris@101 1023
Chris@101 1024 bool equals(const hashed_index& x,hashed_unique_tag)const
Chris@101 1025 {
Chris@101 1026 if(size()!=x.size())return false;
Chris@101 1027 for(const_iterator it=begin(),it_end=end(),it2_end=x.end();
Chris@101 1028 it!=it_end;++it){
Chris@101 1029 const_iterator it2=x.find(key(*it));
Chris@101 1030 if(it2==it2_end||!(*it==*it2))return false;
Chris@101 1031 }
Chris@101 1032 return true;
Chris@101 1033 }
Chris@101 1034
Chris@101 1035 bool equals(const hashed_index& x,hashed_non_unique_tag)const
Chris@101 1036 {
Chris@101 1037 if(size()!=x.size())return false;
Chris@101 1038 for(const_iterator it=begin(),it_end=end();it!=it_end;){
Chris@101 1039 const_iterator it2,it2_last;
Chris@101 1040 boost::tie(it2,it2_last)=x.equal_range(key(*it));
Chris@101 1041 if(it2==it2_last)return false;
Chris@101 1042
Chris@101 1043 const_iterator it_last=make_iterator(
Chris@101 1044 node_type::from_impl(end_of_range(it.get_node()->impl())));
Chris@101 1045 if(std::distance(it,it_last)!=std::distance(it2,it2_last))return false;
Chris@101 1046
Chris@101 1047 /* From is_permutation code in
Chris@101 1048 * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3068.pdf
Chris@101 1049 */
Chris@101 1050
Chris@101 1051 for(;it!=it_last;++it,++it2){
Chris@101 1052 if(!(*it==*it2))break;
Chris@101 1053 }
Chris@101 1054 if(it!=it_last){
Chris@101 1055 for(const_iterator scan=it;scan!=it_last;++scan){
Chris@101 1056 if(std::find(it,scan,*scan)!=scan)continue;
Chris@101 1057 std::ptrdiff_t matches=std::count(it2,it2_last,*scan);
Chris@101 1058 if(matches==0||matches!=std::count(scan,it_last,*scan))return false;
Chris@101 1059 }
Chris@101 1060 it=it_last;
Chris@101 1061 }
Chris@101 1062 }
Chris@101 1063 return true;
Chris@101 1064 }
Chris@101 1065
Chris@16 1066 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
Chris@16 1067 /* serialization */
Chris@16 1068
Chris@16 1069 template<typename Archive>
Chris@16 1070 void save_(
Chris@16 1071 Archive& ar,const unsigned int version,const index_saver_type& sm)const
Chris@16 1072 {
Chris@16 1073 ar<<serialization::make_nvp("position",buckets);
Chris@16 1074 super::save_(ar,version,sm);
Chris@16 1075 }
Chris@16 1076
Chris@16 1077 template<typename Archive>
Chris@16 1078 void load_(Archive& ar,const unsigned int version,const index_loader_type& lm)
Chris@16 1079 {
Chris@16 1080 ar>>serialization::make_nvp("position",buckets);
Chris@16 1081 super::load_(ar,version,lm);
Chris@16 1082 }
Chris@16 1083 #endif
Chris@16 1084
Chris@16 1085 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
Chris@16 1086 /* invariant stuff */
Chris@16 1087
Chris@16 1088 bool invariant_()const
Chris@16 1089 {
Chris@16 1090 if(size()==0||begin()==end()){
Chris@16 1091 if(size()!=0||begin()!=end())return false;
Chris@16 1092 }
Chris@16 1093 else{
Chris@16 1094 size_type s0=0;
Chris@16 1095 for(const_iterator it=begin(),it_end=end();it!=it_end;++it,++s0){}
Chris@16 1096 if(s0!=size())return false;
Chris@16 1097
Chris@16 1098 size_type s1=0;
Chris@16 1099 for(size_type buc=0;buc<bucket_count();++buc){
Chris@16 1100 size_type ss1=0;
Chris@16 1101 for(const_local_iterator it=begin(buc),it_end=end(buc);
Chris@16 1102 it!=it_end;++it,++ss1){
Chris@16 1103 if(find_bucket(*it)!=buc)return false;
Chris@16 1104 }
Chris@16 1105 if(ss1!=bucket_size(buc))return false;
Chris@16 1106 s1+=ss1;
Chris@16 1107 }
Chris@16 1108 if(s1!=size())return false;
Chris@16 1109 }
Chris@16 1110
Chris@16 1111 return super::invariant_();
Chris@16 1112 }
Chris@16 1113
Chris@16 1114 /* This forwarding function eases things for the boost::mem_fn construct
Chris@16 1115 * in BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT. Actually,
Chris@16 1116 * final_check_invariant is already an inherited member function of index.
Chris@16 1117 */
Chris@16 1118 void check_invariant_()const{this->final_check_invariant_();}
Chris@16 1119 #endif
Chris@16 1120
Chris@16 1121 private:
Chris@16 1122 node_type* header()const{return this->final_header();}
Chris@16 1123
Chris@16 1124 std::size_t find_bucket(value_param_type v)const
Chris@16 1125 {
Chris@16 1126 return bucket(key(v));
Chris@16 1127 }
Chris@16 1128
Chris@101 1129 struct link_info_non_unique
Chris@101 1130 {
Chris@101 1131 link_info_non_unique(node_impl_base_pointer pos):
Chris@101 1132 first(pos),last(node_impl_base_pointer(0)){}
Chris@101 1133
Chris@101 1134 operator const node_impl_base_pointer&()const{return this->first;}
Chris@101 1135
Chris@101 1136 node_impl_base_pointer first,last;
Chris@101 1137 };
Chris@101 1138
Chris@101 1139 typedef typename mpl::if_<
Chris@101 1140 is_same<Category,hashed_unique_tag>,
Chris@101 1141 node_impl_base_pointer,
Chris@101 1142 link_info_non_unique
Chris@101 1143 >::type link_info;
Chris@101 1144
Chris@101 1145 bool link_point(value_param_type v,link_info& pos)
Chris@101 1146 {
Chris@101 1147 return link_point(v,pos,Category());
Chris@101 1148 }
Chris@101 1149
Chris@16 1150 bool link_point(
Chris@101 1151 value_param_type v,node_impl_base_pointer& pos,hashed_unique_tag)
Chris@16 1152 {
Chris@101 1153 for(node_impl_pointer x=pos->prior();x!=node_impl_pointer(0);
Chris@101 1154 x=node_alg::after_local(x)){
Chris@16 1155 if(eq_(key(v),key(node_type::from_impl(x)->value()))){
Chris@101 1156 pos=node_impl_type::base_pointer_from(x);
Chris@16 1157 return false;
Chris@16 1158 }
Chris@16 1159 }
Chris@16 1160 return true;
Chris@16 1161 }
Chris@16 1162
Chris@16 1163 bool link_point(
Chris@101 1164 value_param_type v,link_info_non_unique& pos,hashed_non_unique_tag)
Chris@16 1165 {
Chris@101 1166 for(node_impl_pointer x=pos.first->prior();x!=node_impl_pointer(0);
Chris@101 1167 x=node_alg::next_to_inspect(x)){
Chris@16 1168 if(eq_(key(v),key(node_type::from_impl(x)->value()))){
Chris@101 1169 pos.first=node_impl_type::base_pointer_from(x);
Chris@101 1170 pos.last=node_impl_type::base_pointer_from(last_of_range(x));
Chris@16 1171 return true;
Chris@16 1172 }
Chris@16 1173 }
Chris@16 1174 return true;
Chris@16 1175 }
Chris@101 1176
Chris@101 1177 node_impl_pointer last_of_range(node_impl_pointer x)const
Chris@16 1178 {
Chris@101 1179 return last_of_range(x,Category());
Chris@16 1180 }
Chris@16 1181
Chris@101 1182 node_impl_pointer last_of_range(node_impl_pointer x,hashed_unique_tag)const
Chris@16 1183 {
Chris@101 1184 return x;
Chris@16 1185 }
Chris@16 1186
Chris@101 1187 node_impl_pointer last_of_range(
Chris@101 1188 node_impl_pointer x,hashed_non_unique_tag)const
Chris@16 1189 {
Chris@101 1190 node_impl_base_pointer y=x->next();
Chris@101 1191 node_impl_pointer z=y->prior();
Chris@101 1192 if(z==x){ /* range of size 1 or 2 */
Chris@101 1193 node_impl_pointer yy=node_impl_type::pointer_from(y);
Chris@101 1194 return
Chris@101 1195 eq_(
Chris@101 1196 key(node_type::from_impl(x)->value()),
Chris@101 1197 key(node_type::from_impl(yy)->value()))?yy:x;
Chris@101 1198 }
Chris@101 1199 else if(z->prior()==x) /* last of bucket */
Chris@101 1200 return x;
Chris@101 1201 else /* group of size>2 */
Chris@101 1202 return z;
Chris@101 1203 }
Chris@101 1204
Chris@101 1205 node_impl_pointer end_of_range(node_impl_pointer x)const
Chris@101 1206 {
Chris@101 1207 return end_of_range(x,Category());
Chris@101 1208 }
Chris@101 1209
Chris@101 1210 node_impl_pointer end_of_range(node_impl_pointer x,hashed_unique_tag)const
Chris@101 1211 {
Chris@101 1212 return node_alg::after(last_of_range(x));
Chris@101 1213 }
Chris@101 1214
Chris@101 1215 node_impl_pointer end_of_range(
Chris@101 1216 node_impl_pointer x,hashed_non_unique_tag)const
Chris@101 1217 {
Chris@101 1218 node_impl_base_pointer y=x->next();
Chris@101 1219 node_impl_pointer z=y->prior();
Chris@101 1220 if(z==x){ /* range of size 1 or 2 */
Chris@101 1221 node_impl_pointer yy=node_impl_type::pointer_from(y);
Chris@101 1222 if(!eq_(
Chris@101 1223 key(node_type::from_impl(x)->value()),
Chris@101 1224 key(node_type::from_impl(yy)->value())))yy=x;
Chris@101 1225 return yy->next()->prior()==yy?
Chris@101 1226 node_impl_type::pointer_from(yy->next()):
Chris@101 1227 yy->next()->prior();
Chris@101 1228 }
Chris@101 1229 else if(z->prior()==x) /* last of bucket */
Chris@101 1230 return z;
Chris@101 1231 else /* group of size>2 */
Chris@101 1232 return z->next()->prior()==z?
Chris@101 1233 node_impl_type::pointer_from(z->next()):
Chris@101 1234 z->next()->prior();
Chris@101 1235 }
Chris@101 1236
Chris@101 1237 void link(node_type* x,const link_info& pos)
Chris@101 1238 {
Chris@101 1239 link(x,pos,Category());
Chris@101 1240 }
Chris@101 1241
Chris@101 1242 void link(node_type* x,node_impl_base_pointer pos,hashed_unique_tag)
Chris@101 1243 {
Chris@101 1244 node_alg::link(x->impl(),pos,header()->impl());
Chris@101 1245 }
Chris@101 1246
Chris@101 1247 void link(node_type* x,const link_info_non_unique& pos,hashed_non_unique_tag)
Chris@101 1248 {
Chris@101 1249 if(pos.last==node_impl_base_pointer(0)){
Chris@101 1250 node_alg::link(x->impl(),pos.first,header()->impl());
Chris@101 1251 }
Chris@101 1252 else{
Chris@101 1253 node_alg::link(
Chris@101 1254 x->impl(),
Chris@101 1255 node_impl_type::pointer_from(pos.first),
Chris@101 1256 node_impl_type::pointer_from(pos.last));
Chris@101 1257 }
Chris@101 1258 }
Chris@101 1259
Chris@101 1260 void unlink(node_type* x)
Chris@101 1261 {
Chris@101 1262 node_alg::unlink(x->impl());
Chris@101 1263 }
Chris@101 1264
Chris@101 1265 typedef typename node_alg::unlink_undo unlink_undo;
Chris@101 1266
Chris@101 1267 void unlink(node_type* x,unlink_undo& undo)
Chris@101 1268 {
Chris@101 1269 node_alg::unlink(x->impl(),undo);
Chris@16 1270 }
Chris@16 1271
Chris@16 1272 void calculate_max_load()
Chris@16 1273 {
Chris@101 1274 float fml=static_cast<float>(mlf*static_cast<float>(bucket_count()));
Chris@16 1275 max_load=(std::numeric_limits<size_type>::max)();
Chris@16 1276 if(max_load>fml)max_load=static_cast<size_type>(fml);
Chris@16 1277 }
Chris@16 1278
Chris@101 1279 void reserve_for_insert(size_type n)
Chris@16 1280 {
Chris@16 1281 if(n>max_load){
Chris@16 1282 size_type bc =(std::numeric_limits<size_type>::max)();
Chris@16 1283 float fbc=static_cast<float>(1+static_cast<double>(n)/mlf);
Chris@16 1284 if(bc>fbc)bc =static_cast<size_type>(fbc);
Chris@16 1285 unchecked_rehash(bc);
Chris@16 1286 }
Chris@16 1287 }
Chris@16 1288
Chris@101 1289 void unchecked_rehash(size_type n){unchecked_rehash(n,Category());}
Chris@101 1290
Chris@101 1291 void unchecked_rehash(size_type n,hashed_unique_tag)
Chris@16 1292 {
Chris@101 1293 node_impl_type cpy_end_node;
Chris@101 1294 node_impl_pointer cpy_end=node_impl_pointer(&cpy_end_node),
Chris@101 1295 end_=header()->impl();
Chris@101 1296 bucket_array_type buckets_cpy(get_allocator(),cpy_end,n);
Chris@16 1297
Chris@101 1298 if(size()!=0){
Chris@101 1299 auto_space<
Chris@101 1300 std::size_t,allocator_type> hashes(get_allocator(),size());
Chris@101 1301 auto_space<
Chris@101 1302 node_impl_pointer,allocator_type> node_ptrs(get_allocator(),size());
Chris@101 1303 std::size_t i=0,size_=size();
Chris@101 1304 bool within_bucket=false;
Chris@101 1305 BOOST_TRY{
Chris@101 1306 for(;i!=size_;++i){
Chris@101 1307 node_impl_pointer x=end_->prior();
Chris@101 1308
Chris@101 1309 /* only this can possibly throw */
Chris@101 1310 std::size_t h=hash_(key(node_type::from_impl(x)->value()));
Chris@101 1311
Chris@101 1312 hashes.data()[i]=h;
Chris@101 1313 node_ptrs.data()[i]=x;
Chris@101 1314 within_bucket=!node_alg::unlink_last(end_);
Chris@101 1315 node_alg::link(x,buckets_cpy.at(buckets_cpy.position(h)),cpy_end);
Chris@101 1316 }
Chris@16 1317 }
Chris@101 1318 BOOST_CATCH(...){
Chris@101 1319 if(i!=0){
Chris@101 1320 std::size_t prev_buc=buckets.position(hashes.data()[i-1]);
Chris@101 1321 if(!within_bucket)prev_buc=~prev_buc;
Chris@101 1322
Chris@101 1323 for(std::size_t j=i;j--;){
Chris@101 1324 std::size_t buc=buckets.position(hashes.data()[j]);
Chris@101 1325 node_impl_pointer x=node_ptrs.data()[j];
Chris@101 1326 if(buc==prev_buc)node_alg::append(x,end_);
Chris@101 1327 else node_alg::link(x,buckets.at(buc),end_);
Chris@101 1328 prev_buc=buc;
Chris@101 1329 }
Chris@101 1330 }
Chris@101 1331 BOOST_RETHROW;
Chris@101 1332 }
Chris@101 1333 BOOST_CATCH_END
Chris@16 1334 }
Chris@16 1335
Chris@101 1336 end_->prior()=cpy_end->prior()!=cpy_end?cpy_end->prior():end_;
Chris@101 1337 end_->next()=cpy_end->next();
Chris@101 1338 end_->prior()->next()->prior()=end_->next()->prior()->prior()=end_;
Chris@101 1339 buckets.swap(buckets_cpy);
Chris@101 1340 calculate_max_load();
Chris@101 1341 }
Chris@101 1342
Chris@101 1343 void unchecked_rehash(size_type n,hashed_non_unique_tag)
Chris@101 1344 {
Chris@101 1345 node_impl_type cpy_end_node;
Chris@101 1346 node_impl_pointer cpy_end=node_impl_pointer(&cpy_end_node),
Chris@101 1347 end_=header()->impl();
Chris@101 1348 bucket_array_type buckets_cpy(get_allocator(),cpy_end,n);
Chris@101 1349
Chris@101 1350 if(size()!=0){
Chris@101 1351 auto_space<
Chris@101 1352 std::size_t,allocator_type> hashes(get_allocator(),size());
Chris@101 1353 auto_space<
Chris@101 1354 node_impl_pointer,allocator_type> node_ptrs(get_allocator(),size());
Chris@101 1355 std::size_t i=0;
Chris@101 1356 bool within_bucket=false;
Chris@101 1357 BOOST_TRY{
Chris@101 1358 for(;;++i){
Chris@101 1359 node_impl_pointer x=end_->prior();
Chris@101 1360 if(x==end_)break;
Chris@101 1361
Chris@101 1362 /* only this can possibly throw */
Chris@101 1363 std::size_t h=hash_(key(node_type::from_impl(x)->value()));
Chris@101 1364
Chris@101 1365 hashes.data()[i]=h;
Chris@101 1366 node_ptrs.data()[i]=x;
Chris@101 1367 std::pair<node_impl_pointer,bool> p=
Chris@101 1368 node_alg::unlink_last_group(end_);
Chris@101 1369 node_alg::link_range(
Chris@101 1370 p.first,x,buckets_cpy.at(buckets_cpy.position(h)),cpy_end);
Chris@101 1371 within_bucket=!(p.second);
Chris@101 1372 }
Chris@16 1373 }
Chris@101 1374 BOOST_CATCH(...){
Chris@101 1375 if(i!=0){
Chris@101 1376 std::size_t prev_buc=buckets.position(hashes.data()[i-1]);
Chris@101 1377 if(!within_bucket)prev_buc=~prev_buc;
Chris@101 1378
Chris@101 1379 for(std::size_t j=i;j--;){
Chris@101 1380 std::size_t buc=buckets.position(hashes.data()[j]);
Chris@101 1381 node_impl_pointer x=node_ptrs.data()[j],
Chris@101 1382 y=
Chris@101 1383 x->prior()->next()!=node_impl_type::base_pointer_from(x)&&
Chris@101 1384 x->prior()->next()->prior()!=x?
Chris@101 1385 node_impl_type::pointer_from(x->prior()->next()):x;
Chris@101 1386 node_alg::unlink_range(y,x);
Chris@101 1387 if(buc==prev_buc)node_alg::append_range(y,x,end_);
Chris@101 1388 else node_alg::link_range(y,x,buckets.at(buc),end_);
Chris@101 1389 prev_buc=buc;
Chris@101 1390 }
Chris@101 1391 }
Chris@101 1392 BOOST_RETHROW;
Chris@101 1393 }
Chris@101 1394 BOOST_CATCH_END
Chris@16 1395 }
Chris@16 1396
Chris@101 1397 end_->prior()=cpy_end->prior()!=cpy_end?cpy_end->prior():end_;
Chris@101 1398 end_->next()=cpy_end->next();
Chris@101 1399 end_->prior()->next()->prior()=end_->next()->prior()->prior()=end_;
Chris@101 1400 buckets.swap(buckets_cpy);
Chris@16 1401 calculate_max_load();
Chris@101 1402 }
Chris@101 1403
Chris@101 1404 bool in_place(node_impl_pointer x,key_param_type k,std::size_t buc)const
Chris@101 1405 {
Chris@101 1406 return in_place(x,k,buc,Category());
Chris@16 1407 }
Chris@16 1408
Chris@16 1409 bool in_place(
Chris@16 1410 node_impl_pointer x,key_param_type k,std::size_t buc,
Chris@16 1411 hashed_unique_tag)const
Chris@16 1412 {
Chris@101 1413 bool found=false;
Chris@101 1414 for(node_impl_pointer y=buckets.at(buc)->prior();
Chris@101 1415 y!=node_impl_pointer(0);y=node_alg::after_local(y)){
Chris@101 1416 if(y==x)found=true;
Chris@101 1417 else if(eq_(k,key(node_type::from_impl(y)->value())))return false;
Chris@16 1418 }
Chris@101 1419 return found;
Chris@16 1420 }
Chris@16 1421
Chris@16 1422 bool in_place(
Chris@16 1423 node_impl_pointer x,key_param_type k,std::size_t buc,
Chris@16 1424 hashed_non_unique_tag)const
Chris@16 1425 {
Chris@101 1426 bool found=false;
Chris@101 1427 int range_size=0;
Chris@101 1428 for(node_impl_pointer y=buckets.at(buc)->prior();y!=node_impl_pointer(0);){
Chris@101 1429 if(node_alg::is_first_of_group(y)){ /* group of 3 or more */
Chris@101 1430 if(y==x){
Chris@101 1431 /* in place <-> equal to some other member of the group */
Chris@101 1432 return eq_(
Chris@101 1433 k,
Chris@101 1434 key(node_type::from_impl(
Chris@101 1435 node_impl_type::pointer_from(y->next()))->value()));
Chris@101 1436 }
Chris@101 1437 else{
Chris@101 1438 node_impl_pointer z=
Chris@101 1439 node_alg::after_local(y->next()->prior()); /* end of range */
Chris@101 1440 if(eq_(k,key(node_type::from_impl(y)->value()))){
Chris@101 1441 if(found)return false; /* x lies outside */
Chris@101 1442 do{
Chris@101 1443 if(y==x)return true;
Chris@101 1444 y=node_alg::after_local(y);
Chris@101 1445 }while(y!=z);
Chris@101 1446 return false; /* x not found */
Chris@101 1447 }
Chris@101 1448 else{
Chris@101 1449 if(range_size==1&&!found)return false;
Chris@101 1450 if(range_size==2)return found;
Chris@101 1451 range_size=0;
Chris@101 1452 y=z; /* skip range (and potentially x, too, which is fine) */
Chris@101 1453 }
Chris@16 1454 }
Chris@16 1455 }
Chris@101 1456 else{ /* group of 1 or 2 */
Chris@101 1457 if(y==x){
Chris@101 1458 if(range_size==1)return true;
Chris@101 1459 range_size=1;
Chris@101 1460 found=true;
Chris@16 1461 }
Chris@101 1462 else if(eq_(k,key(node_type::from_impl(y)->value()))){
Chris@101 1463 if(range_size==0&&found)return false;
Chris@101 1464 if(range_size==1&&!found)return false;
Chris@101 1465 if(range_size==2)return false;
Chris@101 1466 ++range_size;
Chris@101 1467 }
Chris@101 1468 else{
Chris@101 1469 if(range_size==1&&!found)return false;
Chris@101 1470 if(range_size==2)return found;
Chris@101 1471 range_size=0;
Chris@101 1472 }
Chris@101 1473 y=node_alg::after_local(y);
Chris@16 1474 }
Chris@16 1475 }
Chris@101 1476 return found;
Chris@16 1477 }
Chris@16 1478
Chris@16 1479 #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
Chris@16 1480 void detach_iterators(node_type* x)
Chris@16 1481 {
Chris@16 1482 iterator it=make_iterator(x);
Chris@16 1483 safe_mode::detach_equivalent_iterators(it);
Chris@16 1484 }
Chris@16 1485 #endif
Chris@16 1486
Chris@16 1487 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
Chris@16 1488 std::pair<iterator,bool> emplace_impl(BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
Chris@16 1489 {
Chris@16 1490 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 1491 std::pair<final_node_type*,bool>p=
Chris@16 1492 this->final_emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
Chris@16 1493 return std::pair<iterator,bool>(make_iterator(p.first),p.second);
Chris@16 1494 }
Chris@16 1495
Chris@16 1496 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
Chris@16 1497 iterator emplace_hint_impl(
Chris@16 1498 iterator position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
Chris@16 1499 {
Chris@16 1500 BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
Chris@16 1501 BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
Chris@16 1502 BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT;
Chris@16 1503 std::pair<final_node_type*,bool>p=
Chris@16 1504 this->final_emplace_hint_(
Chris@16 1505 static_cast<final_node_type*>(position.get_node()),
Chris@16 1506 BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
Chris@16 1507 return make_iterator(p.first);
Chris@16 1508 }
Chris@16 1509
Chris@101 1510 template<
Chris@101 1511 typename CompatibleHash,typename CompatiblePred
Chris@101 1512 >
Chris@101 1513 iterator find(
Chris@101 1514 const key_type& k,
Chris@101 1515 const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
Chris@101 1516 {
Chris@101 1517 return find(k,hash,eq,mpl::false_());
Chris@101 1518 }
Chris@101 1519
Chris@101 1520 template<
Chris@101 1521 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
Chris@101 1522 >
Chris@101 1523 iterator find(
Chris@101 1524 const CompatibleKey& k,
Chris@101 1525 const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
Chris@101 1526 {
Chris@101 1527 std::size_t buc=buckets.position(hash(k));
Chris@101 1528 for(node_impl_pointer x=buckets.at(buc)->prior();
Chris@101 1529 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
Chris@101 1530 if(eq(k,key(node_type::from_impl(x)->value()))){
Chris@101 1531 return make_iterator(node_type::from_impl(x));
Chris@101 1532 }
Chris@101 1533 }
Chris@101 1534 return end();
Chris@101 1535 }
Chris@101 1536
Chris@101 1537 template<
Chris@101 1538 typename CompatibleHash,typename CompatiblePred
Chris@101 1539 >
Chris@101 1540 size_type count(
Chris@101 1541 const key_type& k,
Chris@101 1542 const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
Chris@101 1543 {
Chris@101 1544 return count(k,hash,eq,mpl::false_());
Chris@101 1545 }
Chris@101 1546
Chris@101 1547 template<
Chris@101 1548 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
Chris@101 1549 >
Chris@101 1550 size_type count(
Chris@101 1551 const CompatibleKey& k,
Chris@101 1552 const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
Chris@101 1553 {
Chris@101 1554 std::size_t buc=buckets.position(hash(k));
Chris@101 1555 for(node_impl_pointer x=buckets.at(buc)->prior();
Chris@101 1556 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
Chris@101 1557 if(eq(k,key(node_type::from_impl(x)->value()))){
Chris@101 1558 size_type res=0;
Chris@101 1559 node_impl_pointer y=end_of_range(x);
Chris@101 1560 do{
Chris@101 1561 ++res;
Chris@101 1562 x=node_alg::after(x);
Chris@101 1563 }while(x!=y);
Chris@101 1564 return res;
Chris@101 1565 }
Chris@101 1566 }
Chris@101 1567 return 0;
Chris@101 1568 }
Chris@101 1569
Chris@101 1570 template<
Chris@101 1571 typename CompatibleHash,typename CompatiblePred
Chris@101 1572 >
Chris@101 1573 std::pair<iterator,iterator> equal_range(
Chris@101 1574 const key_type& k,
Chris@101 1575 const CompatibleHash& hash,const CompatiblePred& eq,mpl::true_)const
Chris@101 1576 {
Chris@101 1577 return equal_range(k,hash,eq,mpl::false_());
Chris@101 1578 }
Chris@101 1579
Chris@101 1580 template<
Chris@101 1581 typename CompatibleKey,typename CompatibleHash,typename CompatiblePred
Chris@101 1582 >
Chris@101 1583 std::pair<iterator,iterator> equal_range(
Chris@101 1584 const CompatibleKey& k,
Chris@101 1585 const CompatibleHash& hash,const CompatiblePred& eq,mpl::false_)const
Chris@101 1586 {
Chris@101 1587 std::size_t buc=buckets.position(hash(k));
Chris@101 1588 for(node_impl_pointer x=buckets.at(buc)->prior();
Chris@101 1589 x!=node_impl_pointer(0);x=node_alg::next_to_inspect(x)){
Chris@101 1590 if(eq(k,key(node_type::from_impl(x)->value()))){
Chris@101 1591 return std::pair<iterator,iterator>(
Chris@101 1592 make_iterator(node_type::from_impl(x)),
Chris@101 1593 make_iterator(node_type::from_impl(end_of_range(x))));
Chris@101 1594 }
Chris@101 1595 }
Chris@101 1596 return std::pair<iterator,iterator>(end(),end());
Chris@101 1597 }
Chris@101 1598
Chris@16 1599 key_from_value key;
Chris@16 1600 hasher hash_;
Chris@16 1601 key_equal eq_;
Chris@16 1602 bucket_array_type buckets;
Chris@16 1603 float mlf;
Chris@16 1604 size_type max_load;
Chris@16 1605
Chris@16 1606 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
Chris@16 1607 BOOST_WORKAROUND(__MWERKS__,<=0x3003)
Chris@16 1608 #pragma parse_mfunc_templ reset
Chris@16 1609 #endif
Chris@16 1610 };
Chris@16 1611
Chris@101 1612 /* comparison */
Chris@101 1613
Chris@101 1614 template<
Chris@101 1615 typename KeyFromValue,typename Hash,typename Pred,
Chris@101 1616 typename SuperMeta,typename TagList,typename Category
Chris@101 1617 >
Chris@101 1618 bool operator==(
Chris@101 1619 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
Chris@101 1620 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
Chris@101 1621 {
Chris@101 1622 return x.equals(y);
Chris@101 1623 }
Chris@101 1624
Chris@101 1625 template<
Chris@101 1626 typename KeyFromValue,typename Hash,typename Pred,
Chris@101 1627 typename SuperMeta,typename TagList,typename Category
Chris@101 1628 >
Chris@101 1629 bool operator!=(
Chris@101 1630 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
Chris@101 1631 const hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
Chris@101 1632 {
Chris@101 1633 return !(x==y);
Chris@101 1634 }
Chris@101 1635
Chris@16 1636 /* specialized algorithms */
Chris@16 1637
Chris@16 1638 template<
Chris@16 1639 typename KeyFromValue,typename Hash,typename Pred,
Chris@16 1640 typename SuperMeta,typename TagList,typename Category
Chris@16 1641 >
Chris@16 1642 void swap(
Chris@16 1643 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& x,
Chris@16 1644 hashed_index<KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>& y)
Chris@16 1645 {
Chris@16 1646 x.swap(y);
Chris@16 1647 }
Chris@16 1648
Chris@16 1649 } /* namespace multi_index::detail */
Chris@16 1650
Chris@16 1651 /* hashed index specifiers */
Chris@16 1652
Chris@16 1653 template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
Chris@16 1654 struct hashed_unique
Chris@16 1655 {
Chris@16 1656 typedef typename detail::hashed_index_args<
Chris@16 1657 Arg1,Arg2,Arg3,Arg4> index_args;
Chris@16 1658 typedef typename index_args::tag_list_type::type tag_list_type;
Chris@16 1659 typedef typename index_args::key_from_value_type key_from_value_type;
Chris@16 1660 typedef typename index_args::hash_type hash_type;
Chris@16 1661 typedef typename index_args::pred_type pred_type;
Chris@16 1662
Chris@16 1663 template<typename Super>
Chris@16 1664 struct node_class
Chris@16 1665 {
Chris@101 1666 typedef detail::hashed_index_node<Super,detail::hashed_unique_tag> type;
Chris@16 1667 };
Chris@16 1668
Chris@16 1669 template<typename SuperMeta>
Chris@16 1670 struct index_class
Chris@16 1671 {
Chris@16 1672 typedef detail::hashed_index<
Chris@16 1673 key_from_value_type,hash_type,pred_type,
Chris@16 1674 SuperMeta,tag_list_type,detail::hashed_unique_tag> type;
Chris@16 1675 };
Chris@16 1676 };
Chris@16 1677
Chris@16 1678 template<typename Arg1,typename Arg2,typename Arg3,typename Arg4>
Chris@16 1679 struct hashed_non_unique
Chris@16 1680 {
Chris@16 1681 typedef typename detail::hashed_index_args<
Chris@16 1682 Arg1,Arg2,Arg3,Arg4> index_args;
Chris@16 1683 typedef typename index_args::tag_list_type::type tag_list_type;
Chris@16 1684 typedef typename index_args::key_from_value_type key_from_value_type;
Chris@16 1685 typedef typename index_args::hash_type hash_type;
Chris@16 1686 typedef typename index_args::pred_type pred_type;
Chris@16 1687
Chris@16 1688 template<typename Super>
Chris@16 1689 struct node_class
Chris@16 1690 {
Chris@101 1691 typedef detail::hashed_index_node<
Chris@101 1692 Super,detail::hashed_non_unique_tag> type;
Chris@16 1693 };
Chris@16 1694
Chris@16 1695 template<typename SuperMeta>
Chris@16 1696 struct index_class
Chris@16 1697 {
Chris@16 1698 typedef detail::hashed_index<
Chris@16 1699 key_from_value_type,hash_type,pred_type,
Chris@16 1700 SuperMeta,tag_list_type,detail::hashed_non_unique_tag> type;
Chris@16 1701 };
Chris@16 1702 };
Chris@16 1703
Chris@16 1704 } /* namespace multi_index */
Chris@16 1705
Chris@16 1706 } /* namespace boost */
Chris@16 1707
Chris@16 1708 /* Boost.Foreach compatibility */
Chris@16 1709
Chris@16 1710 template<
Chris@16 1711 typename KeyFromValue,typename Hash,typename Pred,
Chris@16 1712 typename SuperMeta,typename TagList,typename Category
Chris@16 1713 >
Chris@16 1714 inline boost::mpl::true_* boost_foreach_is_noncopyable(
Chris@16 1715 boost::multi_index::detail::hashed_index<
Chris@16 1716 KeyFromValue,Hash,Pred,SuperMeta,TagList,Category>*&,
Chris@16 1717 boost::foreach::tag)
Chris@16 1718 {
Chris@16 1719 return 0;
Chris@16 1720 }
Chris@16 1721
Chris@16 1722 #undef BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT
Chris@16 1723 #undef BOOST_MULTI_INDEX_HASHED_INDEX_CHECK_INVARIANT_OF
Chris@16 1724
Chris@16 1725 #endif