annotate DEPENDENCIES/generic/include/boost/intrusive/hashtable.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 /////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2006-2013
Chris@16 4 //
Chris@16 5 // Distributed under the Boost Software License, Version 1.0.
Chris@16 6 // (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 //
Chris@16 9 // See http://www.boost.org/libs/intrusive for documentation.
Chris@16 10 //
Chris@16 11 /////////////////////////////////////////////////////////////////////////////
Chris@16 12 #ifndef BOOST_INTRUSIVE_HASHTABLE_HPP
Chris@16 13 #define BOOST_INTRUSIVE_HASHTABLE_HPP
Chris@16 14
Chris@16 15 #include <boost/intrusive/detail/config_begin.hpp>
Chris@16 16 //std C++
Chris@16 17 #include <functional> //std::equal_to
Chris@16 18 #include <utility> //std::pair
Chris@16 19 #include <algorithm> //std::swap, std::lower_bound, std::upper_bound
Chris@16 20 #include <cstddef> //std::size_t
Chris@16 21 //boost
Chris@16 22 #include <boost/intrusive/detail/assert.hpp>
Chris@16 23 #include <boost/static_assert.hpp>
Chris@16 24 #include <boost/functional/hash.hpp>
Chris@16 25 #include <boost/pointer_cast.hpp>
Chris@16 26 //General intrusive utilities
Chris@16 27 #include <boost/intrusive/intrusive_fwd.hpp>
Chris@16 28 #include <boost/intrusive/detail/hashtable_node.hpp>
Chris@16 29 #include <boost/intrusive/detail/transform_iterator.hpp>
Chris@16 30 #include <boost/intrusive/link_mode.hpp>
Chris@16 31 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
Chris@16 32 #include <boost/intrusive/detail/clear_on_destructor_base.hpp>
Chris@16 33 #include <boost/intrusive/detail/utilities.hpp>
Chris@16 34 //Implementation utilities
Chris@16 35 #include <boost/intrusive/unordered_set_hook.hpp>
Chris@16 36 #include <boost/intrusive/slist.hpp>
Chris@16 37 #include <boost/intrusive/pointer_traits.hpp>
Chris@16 38 #include <boost/intrusive/detail/mpl.hpp>
Chris@16 39 #include <boost/move/move.hpp>
Chris@16 40
Chris@16 41 namespace boost {
Chris@16 42 namespace intrusive {
Chris@16 43
Chris@16 44 /// @cond
Chris@16 45
Chris@16 46 struct hash_bool_flags
Chris@16 47 {
Chris@16 48 static const std::size_t unique_keys_pos = 1u;
Chris@16 49 static const std::size_t constant_time_size_pos = 2u;
Chris@16 50 static const std::size_t power_2_buckets_pos = 4u;
Chris@16 51 static const std::size_t cache_begin_pos = 8u;
Chris@16 52 static const std::size_t compare_hash_pos = 16u;
Chris@16 53 static const std::size_t incremental_pos = 32u;
Chris@16 54 };
Chris@16 55
Chris@16 56 namespace detail {
Chris@16 57
Chris@16 58 template<class SupposedValueTraits>
Chris@16 59 struct get_slist_impl_from_supposed_value_traits
Chris@16 60 {
Chris@16 61 typedef typename detail::get_real_value_traits
Chris@16 62 <SupposedValueTraits>::type real_value_traits;
Chris@16 63 typedef typename detail::get_node_traits
Chris@16 64 <real_value_traits>::type node_traits;
Chris@16 65 typedef typename get_slist_impl
Chris@16 66 <typename reduced_slist_node_traits
Chris@16 67 <node_traits>::type
Chris@16 68 >::type type;
Chris@16 69 };
Chris@16 70
Chris@16 71 template<class SupposedValueTraits>
Chris@16 72 struct unordered_bucket_impl
Chris@16 73 {
Chris@16 74 typedef typename
Chris@16 75 get_slist_impl_from_supposed_value_traits
Chris@16 76 <SupposedValueTraits>::type slist_impl;
Chris@16 77 typedef detail::bucket_impl<slist_impl> implementation_defined;
Chris@16 78 typedef implementation_defined type;
Chris@16 79 };
Chris@16 80
Chris@16 81 template<class SupposedValueTraits>
Chris@16 82 struct unordered_bucket_ptr_impl
Chris@16 83 {
Chris@16 84 typedef typename detail::get_node_traits
Chris@16 85 <SupposedValueTraits>::type::node_ptr node_ptr;
Chris@16 86 typedef typename unordered_bucket_impl
Chris@16 87 <SupposedValueTraits>::type bucket_type;
Chris@16 88
Chris@16 89 typedef typename pointer_traits
Chris@16 90 <node_ptr>::template rebind_pointer
Chris@16 91 < bucket_type >::type implementation_defined;
Chris@16 92 typedef implementation_defined type;
Chris@16 93 };
Chris@16 94
Chris@16 95 template <class T>
Chris@16 96 struct store_hash_bool
Chris@16 97 {
Chris@16 98 template<bool Add>
Chris@16 99 struct two_or_three {one _[2 + Add];};
Chris@16 100 template <class U> static one test(...);
Chris@16 101 template <class U> static two_or_three<U::store_hash> test (int);
Chris@16 102 static const std::size_t value = sizeof(test<T>(0));
Chris@16 103 };
Chris@16 104
Chris@16 105 template <class T>
Chris@16 106 struct store_hash_is_true
Chris@16 107 {
Chris@16 108 static const bool value = store_hash_bool<T>::value > sizeof(one)*2;
Chris@16 109 };
Chris@16 110
Chris@16 111 template <class T>
Chris@16 112 struct optimize_multikey_bool
Chris@16 113 {
Chris@16 114 template<bool Add>
Chris@16 115 struct two_or_three {one _[2 + Add];};
Chris@16 116 template <class U> static one test(...);
Chris@16 117 template <class U> static two_or_three<U::optimize_multikey> test (int);
Chris@16 118 static const std::size_t value = sizeof(test<T>(0));
Chris@16 119 };
Chris@16 120
Chris@16 121 template <class T>
Chris@16 122 struct optimize_multikey_is_true
Chris@16 123 {
Chris@16 124 static const bool value = optimize_multikey_bool<T>::value > sizeof(one)*2;
Chris@16 125 };
Chris@16 126
Chris@16 127 struct insert_commit_data_impl
Chris@16 128 {
Chris@16 129 std::size_t hash;
Chris@16 130 };
Chris@16 131
Chris@16 132 template<class Node, class SlistNodePtr>
Chris@16 133 inline typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type
Chris@16 134 dcast_bucket_ptr(const SlistNodePtr &p)
Chris@16 135 {
Chris@16 136 typedef typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type node_ptr;
Chris@16 137 return pointer_traits<node_ptr>::pointer_to(static_cast<Node&>(*p));
Chris@16 138 }
Chris@16 139
Chris@16 140 template<class NodeTraits>
Chris@16 141 struct group_functions
Chris@16 142 {
Chris@16 143 typedef NodeTraits node_traits;
Chris@16 144 typedef unordered_group_adapter<node_traits> group_traits;
Chris@16 145 typedef typename node_traits::node_ptr node_ptr;
Chris@16 146 typedef typename node_traits::node node;
Chris@16 147 typedef typename reduced_slist_node_traits
Chris@16 148 <node_traits>::type reduced_node_traits;
Chris@16 149 typedef typename reduced_node_traits::node_ptr slist_node_ptr;
Chris@16 150 typedef typename reduced_node_traits::node slist_node;
Chris@16 151 typedef circular_slist_algorithms<group_traits> group_algorithms;
Chris@16 152
Chris@16 153 static slist_node_ptr get_bucket_before_begin
Chris@16 154 (const slist_node_ptr &bucket_beg, const slist_node_ptr &bucket_end, const node_ptr &p)
Chris@16 155 {
Chris@16 156 //First find the last node of p's group.
Chris@16 157 //This requires checking the first node of the next group or
Chris@16 158 //the bucket node.
Chris@16 159 node_ptr prev_node = p;
Chris@16 160 node_ptr nxt(node_traits::get_next(p));
Chris@16 161 while(!(bucket_beg <= nxt && nxt <= bucket_end) &&
Chris@16 162 (group_traits::get_next(nxt) == prev_node)){
Chris@16 163 prev_node = nxt;
Chris@16 164 nxt = node_traits::get_next(nxt);
Chris@16 165 }
Chris@16 166
Chris@16 167 //If we've reached the bucket node just return it.
Chris@16 168 if(bucket_beg <= nxt && nxt <= bucket_end){
Chris@16 169 return nxt;
Chris@16 170 }
Chris@16 171
Chris@16 172 //Otherwise, iterate using group links until the bucket node
Chris@16 173 node_ptr first_node_of_group = nxt;
Chris@16 174 node_ptr last_node_group = group_traits::get_next(first_node_of_group);
Chris@16 175 slist_node_ptr possible_end = node_traits::get_next(last_node_group);
Chris@16 176
Chris@16 177 while(!(bucket_beg <= possible_end && possible_end <= bucket_end)){
Chris@16 178 first_node_of_group = detail::dcast_bucket_ptr<node>(possible_end);
Chris@16 179 last_node_group = group_traits::get_next(first_node_of_group);
Chris@16 180 possible_end = node_traits::get_next(last_node_group);
Chris@16 181 }
Chris@16 182 return possible_end;
Chris@16 183 }
Chris@16 184
Chris@16 185 static node_ptr get_prev_to_first_in_group(const slist_node_ptr &bucket_node, const node_ptr &first_in_group)
Chris@16 186 {
Chris@16 187 //Just iterate using group links and obtain the node
Chris@16 188 //before "first_in_group)"
Chris@16 189 node_ptr prev_node = detail::dcast_bucket_ptr<node>(bucket_node);
Chris@16 190 node_ptr nxt(node_traits::get_next(prev_node));
Chris@16 191 while(nxt != first_in_group){
Chris@16 192 prev_node = group_traits::get_next(nxt);
Chris@16 193 nxt = node_traits::get_next(prev_node);
Chris@16 194 }
Chris@16 195 return prev_node;
Chris@16 196 }
Chris@16 197
Chris@16 198 static node_ptr get_first_in_group_of_last_in_group(const node_ptr &last_in_group)
Chris@16 199 {
Chris@16 200 //Just iterate using group links and obtain the node
Chris@16 201 //before "last_in_group"
Chris@16 202 node_ptr possible_first = group_traits::get_next(last_in_group);
Chris@16 203 node_ptr possible_first_prev = group_traits::get_next(possible_first);
Chris@16 204 // The deleted node is at the end of the group, so the
Chris@16 205 // node in the group pointing to it is at the beginning
Chris@16 206 // of the group. Find that to change its pointer.
Chris@16 207 while(possible_first_prev != last_in_group){
Chris@16 208 possible_first = possible_first_prev;
Chris@16 209 possible_first_prev = group_traits::get_next(possible_first);
Chris@16 210 }
Chris@16 211 return possible_first;
Chris@16 212 }
Chris@16 213
Chris@16 214 static void erase_from_group(const slist_node_ptr &end_ptr, const node_ptr &to_erase_ptr, detail::true_)
Chris@16 215 {
Chris@16 216 node_ptr nxt_ptr(node_traits::get_next(to_erase_ptr));
Chris@16 217 node_ptr prev_in_group_ptr(group_traits::get_next(to_erase_ptr));
Chris@16 218 bool last_in_group = (end_ptr == nxt_ptr) ||
Chris@16 219 (group_traits::get_next(nxt_ptr) != to_erase_ptr);
Chris@16 220 bool is_first_in_group = node_traits::get_next(prev_in_group_ptr) != to_erase_ptr;
Chris@16 221
Chris@16 222 if(is_first_in_group && last_in_group){
Chris@16 223 group_algorithms::init(to_erase_ptr);
Chris@16 224 }
Chris@16 225 else if(is_first_in_group){
Chris@16 226 group_algorithms::unlink_after(nxt_ptr);
Chris@16 227 }
Chris@16 228 else if(last_in_group){
Chris@16 229 node_ptr first_in_group =
Chris@16 230 get_first_in_group_of_last_in_group(to_erase_ptr);
Chris@16 231 group_algorithms::unlink_after(first_in_group);
Chris@16 232 }
Chris@16 233 else{
Chris@16 234 group_algorithms::unlink_after(nxt_ptr);
Chris@16 235 }
Chris@16 236 }
Chris@16 237
Chris@16 238 static void erase_from_group(const slist_node_ptr&, const node_ptr&, detail::false_)
Chris@16 239 {}
Chris@16 240
Chris@16 241 static node_ptr get_last_in_group(const node_ptr &first_in_group, detail::true_)
Chris@16 242 { return group_traits::get_next(first_in_group); }
Chris@16 243
Chris@16 244 static node_ptr get_last_in_group(const node_ptr &n, detail::false_)
Chris@16 245 { return n; }
Chris@16 246
Chris@16 247 static void init_group(const node_ptr &n, true_)
Chris@16 248 { group_algorithms::init(n); }
Chris@16 249
Chris@16 250 static void init_group(const node_ptr &, false_)
Chris@16 251 {}
Chris@16 252
Chris@16 253 static void insert_in_group(const node_ptr &first_in_group, const node_ptr &n, true_)
Chris@16 254 {
Chris@16 255 if(first_in_group){
Chris@16 256 if(group_algorithms::unique(first_in_group))
Chris@16 257 group_algorithms::link_after(first_in_group, n);
Chris@16 258 else{
Chris@16 259 group_algorithms::link_after(group_algorithms::node_traits::get_next(first_in_group), n);
Chris@16 260 }
Chris@16 261 }
Chris@16 262 else{
Chris@16 263 group_algorithms::init_header(n);
Chris@16 264 }
Chris@16 265 }
Chris@16 266
Chris@16 267 static slist_node_ptr get_previous_and_next_in_group
Chris@16 268 ( const slist_node_ptr &i, node_ptr &nxt_in_group
Chris@16 269 //If first_end_ptr == last_end_ptr, then first_end_ptr is the bucket of i
Chris@16 270 //Otherwise first_end_ptr is the first bucket and last_end_ptr the last one.
Chris@16 271 , const slist_node_ptr &first_end_ptr, const slist_node_ptr &last_end_ptr)
Chris@16 272 {
Chris@16 273 slist_node_ptr prev;
Chris@16 274 node_ptr elem(detail::dcast_bucket_ptr<node>(i));
Chris@16 275
Chris@16 276 //It's the last in group if the next_node is a bucket
Chris@16 277 slist_node_ptr nxt(node_traits::get_next(elem));
Chris@16 278 bool last_in_group = (first_end_ptr <= nxt && nxt <= last_end_ptr)/* ||
Chris@16 279 (group_traits::get_next(nxt) != elem)*/;
Chris@16 280 //It's the first in group if group_previous's next_node is not
Chris@16 281 //itself, as group list does not link bucket
Chris@16 282 node_ptr prev_in_group(group_traits::get_next(elem));
Chris@16 283 bool first_in_group = node_traits::get_next(prev_in_group) != elem;
Chris@16 284
Chris@16 285 if(first_in_group){
Chris@16 286 node_ptr start_pos;
Chris@16 287 if(last_in_group){
Chris@16 288 start_pos = elem;
Chris@16 289 nxt_in_group = node_ptr();
Chris@16 290 }
Chris@16 291 else{
Chris@16 292 start_pos = prev_in_group;
Chris@16 293 nxt_in_group = node_traits::get_next(elem);
Chris@16 294 }
Chris@16 295 slist_node_ptr bucket_node;
Chris@16 296 if(first_end_ptr != last_end_ptr){
Chris@16 297 bucket_node = group_functions::get_bucket_before_begin
Chris@16 298 (first_end_ptr, last_end_ptr, start_pos);
Chris@16 299 }
Chris@16 300 else{
Chris@16 301 bucket_node = first_end_ptr;
Chris@16 302 }
Chris@16 303 prev = group_functions::get_prev_to_first_in_group(bucket_node, elem);
Chris@16 304 }
Chris@16 305 else{
Chris@16 306 if(last_in_group){
Chris@16 307 nxt_in_group = group_functions::get_first_in_group_of_last_in_group(elem);
Chris@16 308 }
Chris@16 309 else{
Chris@16 310 nxt_in_group = node_traits::get_next(elem);
Chris@16 311 }
Chris@16 312 prev = group_traits::get_next(elem);
Chris@16 313 }
Chris@16 314 return prev;
Chris@16 315 }
Chris@16 316
Chris@16 317 static void insert_in_group(const node_ptr&, const node_ptr&, false_)
Chris@16 318 {}
Chris@16 319 };
Chris@16 320
Chris@16 321 template<class BucketType, class SplitTraits>
Chris@16 322 class incremental_rehash_rollback
Chris@16 323 {
Chris@16 324 private:
Chris@16 325 typedef BucketType bucket_type;
Chris@16 326 typedef SplitTraits split_traits;
Chris@16 327
Chris@16 328 incremental_rehash_rollback();
Chris@16 329 incremental_rehash_rollback & operator=(const incremental_rehash_rollback &);
Chris@16 330 incremental_rehash_rollback (const incremental_rehash_rollback &);
Chris@16 331
Chris@16 332 public:
Chris@16 333 incremental_rehash_rollback
Chris@16 334 (bucket_type &source_bucket, bucket_type &destiny_bucket, split_traits &split_traits)
Chris@16 335 : source_bucket_(source_bucket), destiny_bucket_(destiny_bucket)
Chris@16 336 , split_traits_(split_traits), released_(false)
Chris@16 337 {}
Chris@16 338
Chris@16 339 void release()
Chris@16 340 { released_ = true; }
Chris@16 341
Chris@16 342 ~incremental_rehash_rollback()
Chris@16 343 {
Chris@16 344 if(!released_){
Chris@16 345 //If an exception is thrown, just put all moved nodes back in the old bucket
Chris@16 346 //and move back the split mark.
Chris@16 347 destiny_bucket_.splice_after(destiny_bucket_.before_begin(), source_bucket_);
Chris@16 348 split_traits_.decrement();
Chris@16 349 }
Chris@16 350 }
Chris@16 351
Chris@16 352 private:
Chris@16 353 bucket_type &source_bucket_;
Chris@16 354 bucket_type &destiny_bucket_;
Chris@16 355 split_traits &split_traits_;
Chris@16 356 bool released_;
Chris@16 357 };
Chris@16 358
Chris@16 359 template<class NodeTraits>
Chris@16 360 struct node_functions
Chris@16 361 {
Chris@16 362 static void store_hash(typename NodeTraits::node_ptr p, std::size_t h, true_)
Chris@16 363 { return NodeTraits::set_hash(p, h); }
Chris@16 364
Chris@16 365 static void store_hash(typename NodeTraits::node_ptr, std::size_t, false_)
Chris@16 366 {}
Chris@16 367 };
Chris@16 368
Chris@16 369 inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::false_)
Chris@16 370 { return hash_value % bucket_cnt; }
Chris@16 371
Chris@16 372 inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::true_)
Chris@16 373 { return hash_value & (bucket_cnt - 1); }
Chris@16 374
Chris@16 375 template<bool Power2Buckets, bool Incremental>
Chris@16 376 inline std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t bucket_cnt, std::size_t split)
Chris@16 377 {
Chris@16 378 std::size_t bucket_number = detail::hash_to_bucket(hash_value, bucket_cnt, detail::bool_<Power2Buckets>());
Chris@16 379 if(Incremental)
Chris@16 380 if(bucket_number >= split)
Chris@16 381 bucket_number -= bucket_cnt/2;
Chris@16 382 return bucket_number;
Chris@16 383 }
Chris@16 384
Chris@16 385 } //namespace detail {
Chris@16 386
Chris@16 387 //!This metafunction will obtain the type of a bucket
Chris@16 388 //!from the value_traits or hook option to be used with
Chris@16 389 //!a hash container.
Chris@16 390 template<class ValueTraitsOrHookOption>
Chris@16 391 struct unordered_bucket
Chris@16 392 : public detail::unordered_bucket_impl
Chris@16 393 <typename ValueTraitsOrHookOption::
Chris@16 394 template pack<none>::proto_value_traits
Chris@16 395 >
Chris@16 396 {};
Chris@16 397
Chris@16 398 //!This metafunction will obtain the type of a bucket pointer
Chris@16 399 //!from the value_traits or hook option to be used with
Chris@16 400 //!a hash container.
Chris@16 401 template<class ValueTraitsOrHookOption>
Chris@16 402 struct unordered_bucket_ptr
Chris@16 403 : public detail::unordered_bucket_ptr_impl
Chris@16 404 <typename ValueTraitsOrHookOption::
Chris@16 405 template pack<none>::proto_value_traits
Chris@16 406 >
Chris@16 407 {};
Chris@16 408
Chris@16 409 //!This metafunction will obtain the type of the default bucket traits
Chris@16 410 //!(when the user does not specify the bucket_traits<> option) from the
Chris@16 411 //!value_traits or hook option to be used with
Chris@16 412 //!a hash container.
Chris@16 413 template<class ValueTraitsOrHookOption>
Chris@16 414 struct unordered_default_bucket_traits
Chris@16 415 {
Chris@16 416 typedef typename ValueTraitsOrHookOption::
Chris@16 417 template pack<none>::proto_value_traits supposed_value_traits;
Chris@16 418 typedef typename detail::
Chris@16 419 get_slist_impl_from_supposed_value_traits
Chris@16 420 <supposed_value_traits>::type slist_impl;
Chris@16 421 typedef detail::bucket_traits_impl
Chris@16 422 <slist_impl> implementation_defined;
Chris@16 423 typedef implementation_defined type;
Chris@16 424 };
Chris@16 425
Chris@16 426 struct default_bucket_traits;
Chris@16 427
Chris@16 428 struct hashtable_defaults
Chris@16 429 {
Chris@16 430 typedef detail::default_hashtable_hook proto_value_traits;
Chris@16 431 typedef std::size_t size_type;
Chris@16 432 typedef void equal;
Chris@16 433 typedef void hash;
Chris@16 434 typedef default_bucket_traits bucket_traits;
Chris@16 435 static const bool constant_time_size = true;
Chris@16 436 static const bool power_2_buckets = false;
Chris@16 437 static const bool cache_begin = false;
Chris@16 438 static const bool compare_hash = false;
Chris@16 439 static const bool incremental = false;
Chris@16 440 };
Chris@16 441
Chris@16 442 template<class RealValueTraits, bool IsConst>
Chris@16 443 struct downcast_node_to_value_t
Chris@16 444 : public detail::node_to_value<RealValueTraits, IsConst>
Chris@16 445 {
Chris@16 446 typedef detail::node_to_value<RealValueTraits, IsConst> base_t;
Chris@16 447 typedef typename base_t::result_type result_type;
Chris@16 448 typedef RealValueTraits real_value_traits;
Chris@16 449 typedef typename detail::get_slist_impl
Chris@16 450 <typename detail::reduced_slist_node_traits
Chris@16 451 <typename real_value_traits::node_traits>::type
Chris@16 452 >::type slist_impl;
Chris@16 453 typedef typename detail::add_const_if_c
Chris@16 454 <typename slist_impl::node, IsConst>::type & first_argument_type;
Chris@16 455 typedef typename detail::add_const_if_c
Chris@16 456 < typename RealValueTraits::node_traits::node
Chris@16 457 , IsConst>::type & intermediate_argument_type;
Chris@16 458 typedef typename pointer_traits
Chris@16 459 <typename RealValueTraits::pointer>::
Chris@16 460 template rebind_pointer
Chris@16 461 <const RealValueTraits>::type const_real_value_traits_ptr;
Chris@16 462
Chris@16 463 downcast_node_to_value_t(const const_real_value_traits_ptr &ptr)
Chris@16 464 : base_t(ptr)
Chris@16 465 {}
Chris@16 466
Chris@16 467 result_type operator()(first_argument_type arg) const
Chris@16 468 { return this->base_t::operator()(static_cast<intermediate_argument_type>(arg)); }
Chris@16 469 };
Chris@16 470
Chris@16 471 template<class F, class SlistNodePtr, class NodePtr>
Chris@16 472 struct node_cast_adaptor
Chris@16 473 : private detail::ebo_functor_holder<F>
Chris@16 474 {
Chris@16 475 typedef detail::ebo_functor_holder<F> base_t;
Chris@16 476
Chris@16 477 typedef typename pointer_traits<SlistNodePtr>::element_type slist_node;
Chris@16 478 typedef typename pointer_traits<NodePtr>::element_type node;
Chris@16 479
Chris@16 480 template<class ConvertibleToF, class RealValuTraits>
Chris@16 481 node_cast_adaptor(const ConvertibleToF &c2f, const RealValuTraits *traits)
Chris@16 482 : base_t(base_t(c2f, traits))
Chris@16 483 {}
Chris@16 484
Chris@16 485 typename base_t::node_ptr operator()(const slist_node &to_clone)
Chris@16 486 { return base_t::operator()(static_cast<const node &>(to_clone)); }
Chris@16 487
Chris@16 488 void operator()(SlistNodePtr to_clone)
Chris@16 489 {
Chris@16 490 base_t::operator()(pointer_traits<NodePtr>::pointer_to(static_cast<node &>(*to_clone)));
Chris@16 491 }
Chris@16 492 };
Chris@16 493
Chris@16 494 static const std::size_t hashtable_data_bool_flags_mask =
Chris@16 495 ( hash_bool_flags::cache_begin_pos
Chris@16 496 | hash_bool_flags::constant_time_size_pos
Chris@16 497 | hash_bool_flags::incremental_pos
Chris@16 498 );
Chris@16 499
Chris@16 500 template<class ValueTraits, class BucketTraits>
Chris@16 501 struct bucket_plus_vtraits : public ValueTraits
Chris@16 502 {
Chris@16 503 typedef BucketTraits bucket_traits;
Chris@16 504 typedef ValueTraits value_traits;
Chris@16 505
Chris@16 506 static const bool external_value_traits =
Chris@16 507 detail::external_value_traits_bool_is_true<ValueTraits>::value;
Chris@16 508
Chris@16 509 static const bool external_bucket_traits =
Chris@16 510 detail::external_bucket_traits_bool_is_true<bucket_traits>::value;
Chris@16 511
Chris@16 512 typedef typename detail::get_real_value_traits<ValueTraits>::type real_value_traits;
Chris@16 513
Chris@16 514 static const bool safemode_or_autounlink = is_safe_autounlink<real_value_traits::link_mode>::value;
Chris@16 515
Chris@16 516 typedef typename detail::eval_if_c
Chris@16 517 < external_bucket_traits
Chris@16 518 , detail::eval_bucket_traits<bucket_traits>
Chris@16 519 , detail::identity<bucket_traits>
Chris@16 520 >::type real_bucket_traits;
Chris@16 521 typedef typename
Chris@16 522 detail::get_slist_impl_from_supposed_value_traits
Chris@16 523 <real_value_traits>::type slist_impl;
Chris@16 524
Chris@16 525 template<class BucketTraitsType>
Chris@16 526 bucket_plus_vtraits(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits)
Chris@16 527 : ValueTraits(val_traits), bucket_traits_(::boost::forward<BucketTraitsType>(b_traits))
Chris@16 528 {}
Chris@16 529
Chris@16 530 bucket_plus_vtraits & operator =(const bucket_plus_vtraits &x)
Chris@16 531 {
Chris@16 532 bucket_traits_ = x.bucket_traits_;
Chris@16 533 return *this;
Chris@16 534 }
Chris@16 535
Chris@16 536 //real_value_traits
Chris@16 537 //
Chris@16 538 const real_value_traits &priv_real_value_traits(detail::false_) const
Chris@16 539 { return *this; }
Chris@16 540
Chris@16 541 const real_value_traits &priv_real_value_traits(detail::true_) const
Chris@16 542 { return this->get_value_traits(*this); }
Chris@16 543
Chris@16 544 real_value_traits &priv_real_value_traits(detail::false_)
Chris@16 545 { return *this; }
Chris@16 546
Chris@16 547 real_value_traits &priv_real_value_traits(detail::true_)
Chris@16 548 { return this->get_value_traits(*this); }
Chris@16 549
Chris@16 550 const real_value_traits &priv_real_value_traits() const
Chris@16 551 { return this->priv_real_value_traits(detail::bool_<external_value_traits>()); }
Chris@16 552
Chris@16 553 real_value_traits &priv_real_value_traits()
Chris@16 554 { return this->priv_real_value_traits(detail::bool_<external_value_traits>()); }
Chris@16 555
Chris@16 556 typedef typename pointer_traits<typename real_value_traits::pointer>::
Chris@16 557 template rebind_pointer<const real_value_traits>::type const_real_value_traits_ptr;
Chris@16 558
Chris@16 559 const_real_value_traits_ptr real_value_traits_ptr() const
Chris@16 560 { return pointer_traits<const_real_value_traits_ptr>::pointer_to(this->priv_real_value_traits()); }
Chris@16 561
Chris@16 562 //real_bucket_traits
Chris@16 563 //
Chris@16 564 const real_bucket_traits &priv_real_bucket_traits(detail::false_) const
Chris@16 565 { return this->bucket_traits_; }
Chris@16 566
Chris@16 567 const real_bucket_traits &priv_real_bucket_traits(detail::true_) const
Chris@16 568 { return this->bucket_traits_.get_bucket_traits(*this); }
Chris@16 569
Chris@16 570 real_bucket_traits &priv_real_bucket_traits(detail::false_)
Chris@16 571 { return bucket_traits_; }
Chris@16 572
Chris@16 573 real_bucket_traits &priv_real_bucket_traits(detail::true_)
Chris@16 574 { return this->get_bucket_traits(*this); }
Chris@16 575
Chris@16 576 const real_bucket_traits &priv_real_bucket_traits() const
Chris@16 577 { return this->priv_real_bucket_traits(detail::bool_<external_bucket_traits>()); }
Chris@16 578
Chris@16 579 real_bucket_traits &priv_real_bucket_traits()
Chris@16 580 { return this->priv_real_bucket_traits(detail::bool_<external_bucket_traits>()); }
Chris@16 581
Chris@16 582 //bucket_value_traits
Chris@16 583 //
Chris@16 584 const bucket_plus_vtraits &get_bucket_value_traits() const
Chris@16 585 { return *this; }
Chris@16 586
Chris@16 587 bucket_plus_vtraits &get_bucket_value_traits()
Chris@16 588 { return *this; }
Chris@16 589
Chris@16 590 typedef typename pointer_traits<typename real_value_traits::pointer>::
Chris@16 591 template rebind_pointer<const bucket_plus_vtraits>::type const_bucket_value_traits_ptr;
Chris@16 592
Chris@16 593 const_bucket_value_traits_ptr bucket_value_traits_ptr() const
Chris@16 594 { return pointer_traits<const_bucket_value_traits_ptr>::pointer_to(this->get_bucket_value_traits()); }
Chris@16 595
Chris@16 596 //value traits
Chris@16 597 //
Chris@16 598 const value_traits &priv_value_traits() const
Chris@16 599 { return *this; }
Chris@16 600
Chris@16 601 value_traits &priv_value_traits()
Chris@16 602 { return *this; }
Chris@16 603
Chris@16 604 //bucket_traits
Chris@16 605 //
Chris@16 606 const bucket_traits &priv_bucket_traits() const
Chris@16 607 { return this->bucket_traits_; }
Chris@16 608
Chris@16 609 bucket_traits &priv_bucket_traits()
Chris@16 610 { return this->bucket_traits_; }
Chris@16 611
Chris@16 612 //operations
Chris@16 613 typedef typename detail::unordered_bucket_ptr_impl<real_value_traits>::type bucket_ptr;
Chris@16 614
Chris@16 615 bucket_ptr priv_bucket_pointer() const
Chris@16 616 { return this->priv_real_bucket_traits().bucket_begin(); }
Chris@16 617
Chris@16 618 typename slist_impl::size_type priv_bucket_count() const
Chris@16 619 { return this->priv_real_bucket_traits().bucket_count(); }
Chris@16 620
Chris@16 621 bucket_ptr priv_invalid_bucket() const
Chris@16 622 {
Chris@16 623 const real_bucket_traits &rbt = this->priv_real_bucket_traits();
Chris@16 624 return rbt.bucket_begin() + rbt.bucket_count();
Chris@16 625 }
Chris@16 626
Chris@16 627 typedef typename real_value_traits::node_traits node_traits;
Chris@16 628 typedef unordered_group_adapter<node_traits> group_traits;
Chris@16 629 typedef typename slist_impl::iterator siterator;
Chris@16 630 typedef typename slist_impl::size_type size_type;
Chris@16 631 typedef detail::bucket_impl<slist_impl> bucket_type;
Chris@16 632 typedef detail::group_functions<node_traits> group_functions_t;
Chris@16 633 typedef typename slist_impl::node_algorithms node_algorithms;
Chris@16 634 typedef typename slist_impl::node_ptr slist_node_ptr;
Chris@16 635 typedef typename node_traits::node_ptr node_ptr;
Chris@16 636 typedef typename node_traits::node node;
Chris@16 637 typedef typename real_value_traits::value_type value_type;
Chris@16 638 typedef circular_slist_algorithms<group_traits> group_algorithms;
Chris@16 639
Chris@16 640
Chris@16 641 /*
Chris@16 642 siterator priv_invalid_local_it() const
Chris@16 643 { return this->priv_invalid_bucket()->end(); }
Chris@16 644 */
Chris@16 645 siterator priv_invalid_local_it() const
Chris@16 646 {
Chris@16 647 return this->priv_real_bucket_traits().bucket_begin()->before_begin();
Chris@16 648 }
Chris@16 649
Chris@16 650 ///
Chris@16 651 static siterator priv_get_last(bucket_type &b, detail::true_) //optimize multikey
Chris@16 652 {
Chris@16 653 //First find the last node of p's group.
Chris@16 654 //This requires checking the first node of the next group or
Chris@16 655 //the bucket node.
Chris@16 656 slist_node_ptr end_ptr(b.end().pointed_node());
Chris@16 657 node_ptr possible_end(node_traits::get_next( detail::dcast_bucket_ptr<node>(end_ptr)));
Chris@16 658 node_ptr last_node_group(possible_end);
Chris@16 659
Chris@16 660 while(end_ptr != possible_end){
Chris@16 661 last_node_group = group_traits::get_next(detail::dcast_bucket_ptr<node>(possible_end));
Chris@16 662 possible_end = node_traits::get_next(last_node_group);
Chris@16 663 }
Chris@16 664 return bucket_type::s_iterator_to(*last_node_group);
Chris@16 665 }
Chris@16 666
Chris@16 667 static siterator priv_get_last(bucket_type &b, detail::false_) //NOT optimize multikey
Chris@16 668 { return b.previous(b.end()); }
Chris@16 669
Chris@16 670 static siterator priv_get_previous(bucket_type &b, siterator i, detail::true_) //optimize multikey
Chris@16 671 {
Chris@16 672 node_ptr elem(detail::dcast_bucket_ptr<node>(i.pointed_node()));
Chris@16 673 node_ptr prev_in_group(group_traits::get_next(elem));
Chris@16 674 bool first_in_group = node_traits::get_next(prev_in_group) != elem;
Chris@16 675 typename bucket_type::node &n = first_in_group
Chris@16 676 ? *group_functions_t::get_prev_to_first_in_group(b.end().pointed_node(), elem)
Chris@16 677 : *group_traits::get_next(elem)
Chris@16 678 ;
Chris@16 679 return bucket_type::s_iterator_to(n);
Chris@16 680 }
Chris@16 681
Chris@16 682 static siterator priv_get_previous(bucket_type &b, siterator i, detail::false_) //NOT optimize multikey
Chris@16 683 { return b.previous(i); }
Chris@16 684
Chris@16 685 static void priv_clear_group_nodes(bucket_type &b, detail::true_) //optimize multikey
Chris@16 686 {
Chris@16 687 siterator it(b.begin()), itend(b.end());
Chris@16 688 while(it != itend){
Chris@16 689 node_ptr to_erase(detail::dcast_bucket_ptr<node>(it.pointed_node()));
Chris@16 690 ++it;
Chris@16 691 group_algorithms::init(to_erase);
Chris@16 692 }
Chris@16 693 }
Chris@16 694
Chris@16 695 static void priv_clear_group_nodes(bucket_type &, detail::false_) //NOT optimize multikey
Chris@16 696 {}
Chris@16 697
Chris@16 698 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::true_) //optimize multikey
Chris@16 699 {
Chris@16 700 const bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
Chris@16 701 slist_node_ptr bb = group_functions_t::get_bucket_before_begin
Chris@16 702 ( f->end().pointed_node()
Chris@16 703 , l->end().pointed_node()
Chris@16 704 , detail::dcast_bucket_ptr<node>(it.pointed_node()));
Chris@16 705 //Now get the bucket_impl from the iterator
Chris@16 706 const bucket_type &b = static_cast<const bucket_type&>
Chris@16 707 (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb)));
Chris@16 708 //Now just calculate the index b has in the bucket array
Chris@16 709 return static_cast<size_type>(&b - &*f);
Chris@16 710 }
Chris@16 711
Chris@16 712 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::false_) //NO optimize multikey
Chris@16 713 {
Chris@16 714 bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
Chris@16 715 slist_node_ptr first_ptr(f->cend().pointed_node())
Chris@16 716 , last_ptr(l->cend().pointed_node());
Chris@16 717
Chris@16 718 //The end node is embedded in the singly linked list:
Chris@16 719 //iterate until we reach it.
Chris@16 720 while(!(first_ptr <= it.pointed_node() && it.pointed_node() <= last_ptr)){
Chris@16 721 ++it;
Chris@16 722 }
Chris@16 723 //Now get the bucket_impl from the iterator
Chris@16 724 const bucket_type &b = static_cast<const bucket_type&>
Chris@16 725 (bucket_type::container_from_end_iterator(it));
Chris@16 726
Chris@16 727 //Now just calculate the index b has in the bucket array
Chris@16 728 return static_cast<std::size_t>(&b - &*f);
Chris@16 729 }
Chris@16 730
Chris@16 731 static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) //store_hash
Chris@16 732 { return node_traits::get_hash(detail::dcast_bucket_ptr<node>(n)); }
Chris@16 733
Chris@16 734 static std::size_t priv_stored_hash(slist_node_ptr, detail::false_) //NO store_hash
Chris@16 735 {
Chris@16 736 //This code should never be reached!
Chris@16 737 BOOST_INTRUSIVE_INVARIANT_ASSERT(0);
Chris@16 738 return 0;
Chris@16 739 }
Chris@16 740
Chris@16 741 node &priv_value_to_node(value_type &v)
Chris@16 742 { return *this->priv_real_value_traits().to_node_ptr(v); }
Chris@16 743
Chris@16 744 const node &priv_value_to_node(const value_type &v) const
Chris@16 745 { return *this->priv_real_value_traits().to_node_ptr(v); }
Chris@16 746
Chris@16 747 value_type &priv_value_from_slist_node(slist_node_ptr n)
Chris@16 748 { return *this->priv_real_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
Chris@16 749
Chris@16 750 const value_type &priv_value_from_slist_node(slist_node_ptr n) const
Chris@16 751 { return *this->priv_real_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
Chris@16 752
Chris@16 753 bucket_traits bucket_traits_;
Chris@16 754 };
Chris@16 755
Chris@16 756 template<class VoidOrKeyHash, class ValueTraits, class BucketTraits>
Chris@16 757 struct bucket_hash_t
Chris@16 758 : public detail::ebo_functor_holder
Chris@16 759 <typename get_hash< VoidOrKeyHash
Chris@16 760 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
Chris@16 761 >::type
Chris@16 762 >
Chris@16 763 , bucket_plus_vtraits<ValueTraits, BucketTraits>
Chris@16 764 {
Chris@16 765 typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits real_value_traits;
Chris@16 766 typedef typename real_value_traits::value_type value_type;
Chris@16 767 typedef typename real_value_traits::node_traits node_traits;
Chris@16 768 typedef typename get_hash< VoidOrKeyHash, value_type>::type hasher;
Chris@16 769 typedef BucketTraits bucket_traits;
Chris@16 770 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
Chris@16 771
Chris@16 772 template<class BucketTraitsType>
Chris@16 773 bucket_hash_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h)
Chris@16 774 : detail::ebo_functor_holder<hasher>(h), bucket_plus_vtraits_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits))
Chris@16 775 {}
Chris@16 776
Chris@16 777 const hasher &priv_hasher() const
Chris@16 778 { return this->detail::ebo_functor_holder<hasher>::get(); }
Chris@16 779
Chris@16 780 hasher &priv_hasher()
Chris@16 781 { return this->detail::ebo_functor_holder<hasher>::get(); }
Chris@16 782
Chris@16 783 std::size_t priv_stored_or_compute_hash(const value_type &v, detail::true_) const //For store_hash == true
Chris@16 784 { return node_traits::get_hash(this->priv_real_value_traits().to_node_ptr(v)); }
Chris@16 785
Chris@16 786 std::size_t priv_stored_or_compute_hash(const value_type &v, detail::false_) const //For store_hash == false
Chris@16 787 { return this->priv_hasher()(v); }
Chris@16 788 };
Chris@16 789
Chris@16 790 template<class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits, bool>
Chris@16 791 struct bucket_hash_equal_t
Chris@16 792 : public detail::ebo_functor_holder //equal
Chris@16 793 <typename get_equal_to< VoidOrKeyEqual
Chris@16 794 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
Chris@16 795 >::type
Chris@16 796 >
Chris@16 797 , bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits>
Chris@16 798 {
Chris@16 799 typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> bucket_hash_type;
Chris@16 800 typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits real_value_traits;
Chris@16 801 typedef typename get_equal_to< VoidOrKeyEqual
Chris@16 802 , typename real_value_traits::value_type
Chris@16 803 >::type value_equal;
Chris@16 804 typedef typename bucket_hash_type::hasher hasher;
Chris@16 805 typedef BucketTraits bucket_traits;
Chris@16 806 typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> buckethash_t;
Chris@16 807 typedef typename bucket_hash_type::real_bucket_traits real_bucket_traits;
Chris@16 808 typedef typename bucket_hash_type::slist_impl slist_impl;
Chris@16 809 typedef typename slist_impl::size_type size_type;
Chris@16 810 typedef typename slist_impl::iterator siterator;
Chris@16 811 typedef detail::bucket_impl<slist_impl> bucket_type;
Chris@16 812 typedef typename detail::unordered_bucket_ptr_impl<real_value_traits>::type bucket_ptr;
Chris@16 813
Chris@16 814 template<class BucketTraitsType>
Chris@16 815 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
Chris@16 816 : detail::ebo_functor_holder<value_equal>(e)
Chris@16 817 , buckethash_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
Chris@16 818 {}
Chris@16 819
Chris@16 820 bucket_ptr priv_get_cache()
Chris@16 821 { return this->priv_bucket_pointer(); }
Chris@16 822
Chris@16 823 void priv_set_cache(const bucket_ptr &)
Chris@16 824 {}
Chris@16 825
Chris@16 826 size_type priv_get_cache_bucket_num()
Chris@16 827 { return 0u; }
Chris@16 828
Chris@16 829 void priv_initialize_cache()
Chris@16 830 {}
Chris@16 831
Chris@16 832 void priv_swap_cache(bucket_hash_equal_t &)
Chris@16 833 {}
Chris@16 834
Chris@16 835 siterator priv_begin() const
Chris@16 836 {
Chris@16 837 size_type n = 0;
Chris@16 838 size_type bucket_cnt = this->priv_bucket_count();
Chris@16 839 for (n = 0; n < bucket_cnt; ++n){
Chris@16 840 bucket_type &b = this->priv_bucket_pointer()[n];
Chris@16 841 if(!b.empty()){
Chris@16 842 return b.begin();
Chris@16 843 }
Chris@16 844 }
Chris@16 845 return this->priv_invalid_local_it();
Chris@16 846 }
Chris@16 847
Chris@16 848 void priv_insertion_update_cache(size_type)
Chris@16 849 {}
Chris@16 850
Chris@16 851 void priv_erasure_update_cache_range(size_type, size_type)
Chris@16 852 {}
Chris@16 853
Chris@16 854 void priv_erasure_update_cache()
Chris@16 855 {}
Chris@16 856
Chris@16 857 const value_equal &priv_equal() const
Chris@16 858 { return this->detail::ebo_functor_holder<value_equal>::get(); }
Chris@16 859
Chris@16 860 value_equal &priv_equal()
Chris@16 861 { return this->detail::ebo_functor_holder<value_equal>::get(); }
Chris@16 862 };
Chris@16 863
Chris@16 864 template<class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits> //cache_begin == true version
Chris@16 865 struct bucket_hash_equal_t<VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits, true>
Chris@16 866 : public detail::ebo_functor_holder //equal
Chris@16 867 <typename get_equal_to< VoidOrKeyEqual
Chris@16 868 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
Chris@16 869 >::type
Chris@16 870 >
Chris@16 871 , public bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits>
Chris@16 872 {
Chris@16 873 typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> bucket_hash_type;
Chris@16 874 typedef typename get_equal_to< VoidOrKeyEqual
Chris@16 875 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
Chris@16 876 >::type value_equal;
Chris@16 877 typedef typename bucket_hash_type::hasher hasher;
Chris@16 878 typedef BucketTraits bucket_traits;
Chris@16 879 typedef typename bucket_hash_type::slist_impl::size_type size_type;
Chris@16 880 typedef typename bucket_hash_type::slist_impl::iterator siterator;
Chris@16 881
Chris@16 882 template<class BucketTraitsType>
Chris@16 883 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
Chris@16 884 : detail::ebo_functor_holder<value_equal>(e)
Chris@16 885 , bucket_hash_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
Chris@16 886 {}
Chris@16 887
Chris@16 888 typedef typename detail::unordered_bucket_ptr_impl
Chris@16 889 <typename bucket_hash_type::real_value_traits>::type bucket_ptr;
Chris@16 890
Chris@16 891 bucket_ptr &priv_get_cache()
Chris@16 892 { return cached_begin_; }
Chris@16 893
Chris@16 894 const bucket_ptr &priv_get_cache() const
Chris@16 895 { return cached_begin_; }
Chris@16 896
Chris@16 897 void priv_set_cache(const bucket_ptr &p)
Chris@16 898 { cached_begin_ = p; }
Chris@16 899
Chris@16 900 std::size_t priv_get_cache_bucket_num()
Chris@16 901 { return this->cached_begin_ - this->priv_bucket_pointer(); }
Chris@16 902
Chris@16 903 void priv_initialize_cache()
Chris@16 904 { this->cached_begin_ = this->priv_invalid_bucket(); }
Chris@16 905
Chris@16 906 void priv_swap_cache(bucket_hash_equal_t &other)
Chris@16 907 {
Chris@16 908 std::swap(this->cached_begin_, other.cached_begin_);
Chris@16 909 }
Chris@16 910
Chris@16 911 siterator priv_begin() const
Chris@16 912 {
Chris@16 913 if(this->cached_begin_ == this->priv_invalid_bucket()){
Chris@16 914 return this->priv_invalid_local_it();
Chris@16 915 }
Chris@16 916 else{
Chris@16 917 return this->cached_begin_->begin();
Chris@16 918 }
Chris@16 919 }
Chris@16 920
Chris@16 921 void priv_insertion_update_cache(size_type insertion_bucket)
Chris@16 922 {
Chris@16 923 bucket_ptr p = this->priv_bucket_pointer() + insertion_bucket;
Chris@16 924 if(p < this->cached_begin_){
Chris@16 925 this->cached_begin_ = p;
Chris@16 926 }
Chris@16 927 }
Chris@16 928
Chris@16 929 const value_equal &priv_equal() const
Chris@16 930 { return this->detail::ebo_functor_holder<value_equal>::get(); }
Chris@16 931
Chris@16 932 value_equal &priv_equal()
Chris@16 933 { return this->detail::ebo_functor_holder<value_equal>::get(); }
Chris@16 934
Chris@16 935 void priv_erasure_update_cache_range(size_type first_bucket_num, size_type last_bucket_num)
Chris@16 936 {
Chris@16 937 //If the last bucket is the end, the cache must be updated
Chris@16 938 //to the last position if all
Chris@16 939 if(this->priv_get_cache_bucket_num() == first_bucket_num &&
Chris@16 940 this->priv_bucket_pointer()[first_bucket_num].empty() ){
Chris@16 941 this->priv_set_cache(this->priv_bucket_pointer() + last_bucket_num);
Chris@16 942 this->priv_erasure_update_cache();
Chris@16 943 }
Chris@16 944 }
Chris@16 945
Chris@16 946 void priv_erasure_update_cache()
Chris@16 947 {
Chris@16 948 if(this->cached_begin_ != this->priv_invalid_bucket()){
Chris@16 949 size_type current_n = this->priv_get_cache() - this->priv_bucket_pointer();
Chris@16 950 for( const size_type num_buckets = this->priv_bucket_count()
Chris@16 951 ; current_n < num_buckets
Chris@16 952 ; ++current_n, ++this->priv_get_cache()){
Chris@16 953 if(!this->priv_get_cache()->empty()){
Chris@16 954 return;
Chris@16 955 }
Chris@16 956 }
Chris@16 957 this->priv_initialize_cache();
Chris@16 958 }
Chris@16 959 }
Chris@16 960
Chris@16 961 private:
Chris@16 962 bucket_ptr cached_begin_;
Chris@16 963 };
Chris@16 964
Chris@16 965 template<class SizeType, std::size_t BoolFlags, class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits>
Chris@16 966 struct hashdata_internal
Chris@16 967 : public detail::size_holder< 0 != (BoolFlags & hash_bool_flags::incremental_pos), SizeType, int> //split_traits
Chris@16 968 , public bucket_hash_equal_t
Chris@16 969 < VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits
Chris@16 970 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
Chris@16 971 >
Chris@16 972 {
Chris@16 973 typedef bucket_hash_equal_t
Chris@16 974 < VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits
Chris@16 975 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
Chris@16 976 > bucket_hash_equal_type;
Chris@16 977
Chris@16 978 typedef typename bucket_hash_equal_type::value_equal value_equal;
Chris@16 979 typedef typename bucket_hash_equal_type::hasher hasher;
Chris@16 980 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
Chris@16 981 typedef typename bucket_plus_vtraits_t::size_type size_type;
Chris@16 982 typedef typename bucket_plus_vtraits_t::bucket_ptr bucket_ptr;
Chris@16 983 static const bool optimize_multikey
Chris@16 984 = detail::optimize_multikey_is_true<typename bucket_plus_vtraits_t::real_value_traits::node_traits>::value;
Chris@16 985
Chris@16 986 typedef detail::bool_<optimize_multikey> optimize_multikey_t;
Chris@16 987
Chris@16 988 template<class BucketTraitsType>
Chris@16 989 hashdata_internal(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
Chris@16 990 : bucket_hash_equal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
Chris@16 991 {}
Chris@16 992
Chris@16 993 typedef detail::size_holder
Chris@16 994 <0 != (BoolFlags & hash_bool_flags::incremental_pos), SizeType, int> split_traits;
Chris@16 995
Chris@16 996 split_traits &priv_split_traits()
Chris@16 997 { return *this; }
Chris@16 998
Chris@16 999 const split_traits &priv_split_traits() const
Chris@16 1000 { return *this; }
Chris@16 1001 };
Chris@16 1002
Chris@16 1003 template<class SizeType, std::size_t BoolFlags, class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits>
Chris@16 1004 struct hashtable_data_t
Chris@16 1005 : public detail::size_holder< 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos), SizeType> //size_traits
Chris@16 1006 , public hashdata_internal
Chris@16 1007 < SizeType, BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos)
Chris@16 1008 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits>
Chris@16 1009 {
Chris@16 1010 static const std::size_t bool_flags = BoolFlags;
Chris@16 1011 typedef detail::size_holder
Chris@16 1012 < 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos)
Chris@16 1013 , SizeType> size_traits;
Chris@16 1014
Chris@16 1015 typedef hashdata_internal
Chris@16 1016 < SizeType, BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos)
Chris@16 1017 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> internal_type;
Chris@16 1018
Chris@16 1019 typedef ValueTraits value_traits;
Chris@16 1020 typedef typename internal_type::value_equal value_equal;
Chris@16 1021 typedef typename internal_type::hasher hasher;
Chris@16 1022 typedef BucketTraits bucket_traits;
Chris@16 1023 typedef bucket_plus_vtraits
Chris@16 1024 <ValueTraits,BucketTraits> bucket_plus_vtraits_t;
Chris@16 1025
Chris@16 1026 static const bool external_value_traits =
Chris@16 1027 detail::external_value_traits_bool_is_true<ValueTraits>::value;
Chris@16 1028 static const bool external_bucket_traits = bucket_plus_vtraits_t::external_bucket_traits;
Chris@16 1029
Chris@16 1030 typedef typename bucket_plus_vtraits_t::real_value_traits real_value_traits;
Chris@16 1031 typedef typename bucket_plus_vtraits_t::real_bucket_traits real_bucket_traits;
Chris@16 1032
Chris@16 1033 size_traits &priv_size_traits()
Chris@16 1034 { return *this; }
Chris@16 1035
Chris@16 1036 const size_traits &priv_size_traits() const
Chris@16 1037 { return *this; }
Chris@16 1038
Chris@16 1039 template<class BucketTraitsType>
Chris@16 1040 hashtable_data_t( BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h
Chris@16 1041 , const value_equal &e, const value_traits &val_traits)
Chris@16 1042 : size_traits()
Chris@16 1043 , internal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
Chris@16 1044 {}
Chris@16 1045 };
Chris@16 1046
Chris@16 1047 /// @endcond
Chris@16 1048
Chris@16 1049 //! The class template hashtable is an intrusive hash table container, that
Chris@16 1050 //! is used to construct intrusive unordered_set and unordered_multiset containers. The
Chris@16 1051 //! no-throw guarantee holds only, if the VoidOrKeyEqual object and Hasher don't throw.
Chris@16 1052 //!
Chris@16 1053 //! hashtable is a semi-intrusive container: each object to be stored in the
Chris@16 1054 //! container must contain a proper hook, but the container also needs
Chris@16 1055 //! additional auxiliary memory to work: hashtable needs a pointer to an array
Chris@16 1056 //! of type `bucket_type` to be passed in the constructor. This bucket array must
Chris@16 1057 //! have at least the same lifetime as the container. This makes the use of
Chris@16 1058 //! hashtable more complicated than purely intrusive containers.
Chris@16 1059 //! `bucket_type` is default-constructible, copyable and assignable
Chris@16 1060 //!
Chris@16 1061 //! The template parameter \c T is the type to be managed by the container.
Chris@16 1062 //! The user can specify additional options and if no options are provided
Chris@16 1063 //! default options are used.
Chris@16 1064 //!
Chris@16 1065 //! The container supports the following options:
Chris@16 1066 //! \c base_hook<>/member_hook<>/value_traits<>,
Chris@16 1067 //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<>
Chris@16 1068 //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>.
Chris@16 1069 //!
Chris@16 1070 //! hashtable only provides forward iterators but it provides 4 iterator types:
Chris@16 1071 //! iterator and const_iterator to navigate through the whole container and
Chris@16 1072 //! local_iterator and const_local_iterator to navigate through the values
Chris@16 1073 //! stored in a single bucket. Local iterators are faster and smaller.
Chris@16 1074 //!
Chris@16 1075 //! It's not recommended to use non constant-time size hashtables because several
Chris@16 1076 //! key functions, like "empty()", become non-constant time functions. Non
Chris@16 1077 //! constant_time size hashtables are mainly provided to support auto-unlink hooks.
Chris@16 1078 //!
Chris@16 1079 //! hashtables, does not make automatic rehashings nor
Chris@16 1080 //! offers functions related to a load factor. Rehashing can be explicitly requested
Chris@16 1081 //! and the user must provide a new bucket array that will be used from that moment.
Chris@16 1082 //!
Chris@16 1083 //! Since no automatic rehashing is done, iterators are never invalidated when
Chris@16 1084 //! inserting or erasing elements. Iterators are only invalidated when rehashing.
Chris@16 1085 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
Chris@16 1086 template<class T, class ...Options>
Chris@16 1087 #else
Chris@16 1088 template<class ValueTraits, class VoidOrKeyHash, class VoidOrKeyEqual, class SizeType, class BucketTraits, std::size_t BoolFlags>
Chris@16 1089 #endif
Chris@16 1090 class hashtable_impl
Chris@16 1091 : public hashtable_data_t
Chris@16 1092 < SizeType
Chris@16 1093 , BoolFlags & hashtable_data_bool_flags_mask
Chris@16 1094 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits>
Chris@16 1095 , private detail::clear_on_destructor_base
Chris@16 1096 < hashtable_impl<ValueTraits, VoidOrKeyHash, VoidOrKeyEqual, SizeType, BucketTraits, BoolFlags>
Chris@16 1097 , true //To always clear the bucket array
Chris@16 1098 //is_safe_autounlink<detail::get_real_value_traits<ValueTraits>::type::link_mode>::value
Chris@16 1099 >
Chris@16 1100 {
Chris@16 1101 template<class C, bool> friend class detail::clear_on_destructor_base;
Chris@16 1102 public:
Chris@16 1103 typedef ValueTraits value_traits;
Chris@16 1104
Chris@16 1105 typedef hashtable_data_t
Chris@16 1106 < SizeType
Chris@16 1107 , BoolFlags & hashtable_data_bool_flags_mask
Chris@16 1108 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> data_type;
Chris@16 1109
Chris@16 1110 /// @cond
Chris@16 1111 static const bool external_value_traits = data_type::external_value_traits;
Chris@16 1112 static const bool external_bucket_traits = data_type::external_bucket_traits;
Chris@16 1113
Chris@16 1114 typedef BucketTraits bucket_traits;
Chris@16 1115 typedef typename data_type::real_bucket_traits real_bucket_traits;
Chris@16 1116 typedef typename data_type::real_value_traits real_value_traits;
Chris@16 1117
Chris@16 1118
Chris@16 1119 typedef typename detail::get_slist_impl
Chris@16 1120 <typename detail::reduced_slist_node_traits
Chris@16 1121 <typename real_value_traits::node_traits>::type
Chris@16 1122 >::type slist_impl;
Chris@16 1123 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
Chris@16 1124 typedef typename bucket_plus_vtraits_t::const_real_value_traits_ptr const_real_value_traits_ptr;
Chris@16 1125
Chris@16 1126
Chris@16 1127 /// @endcond
Chris@16 1128
Chris@16 1129 typedef typename real_value_traits::pointer pointer;
Chris@16 1130 typedef typename real_value_traits::const_pointer const_pointer;
Chris@16 1131 typedef typename real_value_traits::value_type value_type;
Chris@16 1132 typedef typename pointer_traits<pointer>::reference reference;
Chris@16 1133 typedef typename pointer_traits<const_pointer>::reference const_reference;
Chris@16 1134 typedef typename pointer_traits<pointer>::difference_type difference_type;
Chris@16 1135 typedef SizeType size_type;
Chris@16 1136 typedef value_type key_type;
Chris@16 1137 typedef typename data_type::value_equal key_equal;
Chris@16 1138 typedef typename data_type::hasher hasher;
Chris@16 1139 typedef detail::bucket_impl<slist_impl> bucket_type;
Chris@16 1140 typedef typename pointer_traits
Chris@16 1141 <pointer>::template rebind_pointer
Chris@16 1142 < bucket_type >::type bucket_ptr;
Chris@16 1143 typedef typename slist_impl::iterator siterator;
Chris@16 1144 typedef typename slist_impl::const_iterator const_siterator;
Chris@16 1145 typedef hashtable_iterator<bucket_plus_vtraits_t, false> iterator;
Chris@16 1146 typedef hashtable_iterator<bucket_plus_vtraits_t, true> const_iterator;
Chris@16 1147 typedef typename real_value_traits::node_traits node_traits;
Chris@16 1148 typedef typename node_traits::node node;
Chris@16 1149 typedef typename pointer_traits
Chris@16 1150 <pointer>::template rebind_pointer
Chris@16 1151 < node >::type node_ptr;
Chris@16 1152 typedef typename pointer_traits
Chris@16 1153 <pointer>::template rebind_pointer
Chris@16 1154 < const node >::type const_node_ptr;
Chris@16 1155 typedef typename slist_impl::node_algorithms node_algorithms;
Chris@16 1156
Chris@16 1157 static const bool stateful_value_traits = detail::is_stateful_value_traits<real_value_traits>::value;
Chris@16 1158 static const bool store_hash = detail::store_hash_is_true<node_traits>::value;
Chris@16 1159
Chris@16 1160 static const bool unique_keys = 0 != (BoolFlags & hash_bool_flags::unique_keys_pos);
Chris@16 1161 static const bool constant_time_size = 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos);
Chris@16 1162 static const bool cache_begin = 0 != (BoolFlags & hash_bool_flags::cache_begin_pos);
Chris@16 1163 static const bool compare_hash = 0 != (BoolFlags & hash_bool_flags::compare_hash_pos);
Chris@16 1164 static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos);
Chris@16 1165 static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos));
Chris@16 1166
Chris@16 1167 static const bool optimize_multikey
Chris@16 1168 = detail::optimize_multikey_is_true<node_traits>::value && !unique_keys;
Chris@16 1169
Chris@16 1170 /// @cond
Chris@16 1171 private:
Chris@16 1172
Chris@16 1173 //Configuration error: compare_hash<> can't be specified without store_hash<>
Chris@16 1174 //See documentation for more explanations
Chris@16 1175 BOOST_STATIC_ASSERT((!compare_hash || store_hash));
Chris@16 1176
Chris@16 1177 typedef typename slist_impl::node_ptr slist_node_ptr;
Chris@16 1178 typedef typename pointer_traits
Chris@16 1179 <slist_node_ptr>::template rebind_pointer
Chris@16 1180 < void >::type void_pointer;
Chris@16 1181 //We'll define group traits, but these won't be instantiated if
Chris@16 1182 //optimize_multikey is not true
Chris@16 1183 typedef unordered_group_adapter<node_traits> group_traits;
Chris@16 1184 typedef circular_slist_algorithms<group_traits> group_algorithms;
Chris@16 1185 typedef detail::bool_<store_hash> store_hash_t;
Chris@16 1186 typedef detail::bool_<optimize_multikey> optimize_multikey_t;
Chris@16 1187 typedef detail::bool_<cache_begin> cache_begin_t;
Chris@16 1188 typedef detail::bool_<power_2_buckets> power_2_buckets_t;
Chris@16 1189 typedef detail::size_holder<constant_time_size, size_type> size_traits;
Chris@16 1190 typedef detail::size_holder<incremental, size_type, int> split_traits;
Chris@16 1191 typedef detail::group_functions<node_traits> group_functions_t;
Chris@16 1192 typedef detail::node_functions<node_traits> node_functions_t;
Chris@16 1193
Chris@16 1194 private:
Chris@16 1195 //noncopyable, movable
Chris@16 1196 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_impl)
Chris@16 1197
Chris@16 1198 static const bool safemode_or_autounlink = is_safe_autounlink<real_value_traits::link_mode>::value;
Chris@16 1199
Chris@16 1200 //Constant-time size is incompatible with auto-unlink hooks!
Chris@16 1201 BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink)));
Chris@16 1202 //Cache begin is incompatible with auto-unlink hooks!
Chris@16 1203 BOOST_STATIC_ASSERT(!(cache_begin && ((int)real_value_traits::link_mode == (int)auto_unlink)));
Chris@16 1204
Chris@16 1205 template<class Disposer>
Chris@16 1206 node_cast_adaptor< detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
Chris@16 1207 , slist_node_ptr, node_ptr >
Chris@16 1208 make_node_disposer(const Disposer &disposer) const
Chris@16 1209 {
Chris@16 1210 return node_cast_adaptor
Chris@16 1211 < detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
Chris@16 1212 , slist_node_ptr, node_ptr >
Chris@16 1213 (disposer, &this->priv_real_value_traits());
Chris@16 1214 }
Chris@16 1215
Chris@16 1216 /// @endcond
Chris@16 1217
Chris@16 1218 public:
Chris@16 1219 typedef detail::insert_commit_data_impl insert_commit_data;
Chris@16 1220
Chris@16 1221 typedef detail::transform_iterator
Chris@16 1222 < typename slist_impl::iterator
Chris@16 1223 , downcast_node_to_value_t
Chris@16 1224 < real_value_traits
Chris@16 1225 , false> > local_iterator;
Chris@16 1226
Chris@16 1227 typedef detail::transform_iterator
Chris@16 1228 < typename slist_impl::iterator
Chris@16 1229 , downcast_node_to_value_t
Chris@16 1230 < real_value_traits
Chris@16 1231 , true> > const_local_iterator;
Chris@16 1232
Chris@16 1233 public:
Chris@16 1234
Chris@16 1235 //! <b>Requires</b>: buckets must not be being used by any other resource.
Chris@16 1236 //!
Chris@16 1237 //! <b>Effects</b>: Constructs an empty unordered_set, storing a reference
Chris@16 1238 //! to the bucket array and copies of the key_hasher and equal_func functors.
Chris@16 1239 //!
Chris@16 1240 //! <b>Complexity</b>: Constant.
Chris@16 1241 //!
Chris@16 1242 //! <b>Throws</b>: If value_traits::node_traits::node
Chris@16 1243 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
Chris@16 1244 //! or the copy constructor or invocation of hash_func or equal_func throws.
Chris@16 1245 //!
Chris@16 1246 //! <b>Notes</b>: buckets array must be disposed only after
Chris@16 1247 //! *this is disposed.
Chris@16 1248 explicit hashtable_impl ( const bucket_traits &b_traits
Chris@16 1249 , const hasher & hash_func = hasher()
Chris@16 1250 , const key_equal &equal_func = key_equal()
Chris@16 1251 , const value_traits &v_traits = value_traits())
Chris@16 1252 : data_type(b_traits, hash_func, equal_func, v_traits)
Chris@16 1253 {
Chris@16 1254 this->priv_initialize_buckets();
Chris@16 1255 this->priv_size_traits().set_size(size_type(0));
Chris@16 1256 size_type bucket_sz = this->priv_bucket_count();
Chris@16 1257 BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
Chris@16 1258 //Check power of two bucket array if the option is activated
Chris@16 1259 BOOST_INTRUSIVE_INVARIANT_ASSERT
Chris@16 1260 (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1))));
Chris@16 1261 this->priv_split_traits().set_size(bucket_sz>>1);
Chris@16 1262 }
Chris@16 1263
Chris@16 1264 //! <b>Effects</b>: to-do
Chris@16 1265 //!
Chris@16 1266 hashtable_impl(BOOST_RV_REF(hashtable_impl) x)
Chris@16 1267 : data_type( ::boost::move(x.priv_bucket_traits())
Chris@16 1268 , ::boost::move(x.priv_hasher())
Chris@16 1269 , ::boost::move(x.priv_equal())
Chris@16 1270 , ::boost::move(x.priv_value_traits())
Chris@16 1271 )
Chris@16 1272 {
Chris@16 1273 this->priv_swap_cache(x);
Chris@16 1274 x.priv_initialize_cache();
Chris@16 1275 if(constant_time_size){
Chris@16 1276 this->priv_size_traits().set_size(size_type(0));
Chris@16 1277 this->priv_size_traits().set_size(x.priv_size_traits().get_size());
Chris@16 1278 x.priv_size_traits().set_size(size_type(0));
Chris@16 1279 }
Chris@16 1280 if(incremental){
Chris@16 1281 this->priv_split_traits().set_size(x.priv_split_traits().get_size());
Chris@16 1282 x.priv_split_traits().set_size(size_type(0));
Chris@16 1283 }
Chris@16 1284 }
Chris@16 1285
Chris@16 1286 //! <b>Effects</b>: to-do
Chris@16 1287 //!
Chris@16 1288 hashtable_impl& operator=(BOOST_RV_REF(hashtable_impl) x)
Chris@16 1289 { this->swap(x); return *this; }
Chris@16 1290
Chris@16 1291 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
Chris@16 1292 //! <b>Effects</b>: Detaches all elements from this. The objects in the unordered_set
Chris@16 1293 //! are not deleted (i.e. no destructors are called).
Chris@16 1294 //!
Chris@16 1295 //! <b>Complexity</b>: Linear to the number of elements in the unordered_set, if
Chris@16 1296 //! it's a safe-mode or auto-unlink value. Otherwise constant.
Chris@16 1297 //!
Chris@16 1298 //! <b>Throws</b>: Nothing.
Chris@16 1299 ~hashtable_impl()
Chris@16 1300 {}
Chris@16 1301 #endif
Chris@16 1302
Chris@16 1303 //! <b>Effects</b>: Returns an iterator pointing to the beginning of the unordered_set.
Chris@16 1304 //!
Chris@16 1305 //! <b>Complexity</b>: Amortized constant time.
Chris@16 1306 //! Worst case (empty unordered_set): O(this->bucket_count())
Chris@16 1307 //!
Chris@16 1308 //! <b>Throws</b>: Nothing.
Chris@16 1309 iterator begin()
Chris@16 1310 { return iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
Chris@16 1311
Chris@16 1312 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
Chris@16 1313 //! of the unordered_set.
Chris@16 1314 //!
Chris@16 1315 //! <b>Complexity</b>: Amortized constant time.
Chris@16 1316 //! Worst case (empty unordered_set): O(this->bucket_count())
Chris@16 1317 //!
Chris@16 1318 //! <b>Throws</b>: Nothing.
Chris@16 1319 const_iterator begin() const
Chris@16 1320 { return this->cbegin(); }
Chris@16 1321
Chris@16 1322 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
Chris@16 1323 //! of the unordered_set.
Chris@16 1324 //!
Chris@16 1325 //! <b>Complexity</b>: Amortized constant time.
Chris@16 1326 //! Worst case (empty unordered_set): O(this->bucket_count())
Chris@16 1327 //!
Chris@16 1328 //! <b>Throws</b>: Nothing.
Chris@16 1329 const_iterator cbegin() const
Chris@16 1330 { return const_iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
Chris@16 1331
Chris@16 1332 //! <b>Effects</b>: Returns an iterator pointing to the end of the unordered_set.
Chris@16 1333 //!
Chris@16 1334 //! <b>Complexity</b>: Constant.
Chris@16 1335 //!
Chris@16 1336 //! <b>Throws</b>: Nothing.
Chris@16 1337 iterator end()
Chris@16 1338 { return iterator(this->priv_invalid_local_it(), 0); }
Chris@16 1339
Chris@16 1340 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
Chris@16 1341 //!
Chris@16 1342 //! <b>Complexity</b>: Constant.
Chris@16 1343 //!
Chris@16 1344 //! <b>Throws</b>: Nothing.
Chris@16 1345 const_iterator end() const
Chris@16 1346 { return this->cend(); }
Chris@16 1347
Chris@16 1348 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
Chris@16 1349 //!
Chris@16 1350 //! <b>Complexity</b>: Constant.
Chris@16 1351 //!
Chris@16 1352 //! <b>Throws</b>: Nothing.
Chris@16 1353 const_iterator cend() const
Chris@16 1354 { return const_iterator(this->priv_invalid_local_it(), 0); }
Chris@16 1355
Chris@16 1356 //! <b>Effects</b>: Returns the hasher object used by the unordered_set.
Chris@16 1357 //!
Chris@16 1358 //! <b>Complexity</b>: Constant.
Chris@16 1359 //!
Chris@16 1360 //! <b>Throws</b>: If hasher copy-constructor throws.
Chris@16 1361 hasher hash_function() const
Chris@16 1362 { return this->priv_hasher(); }
Chris@16 1363
Chris@16 1364 //! <b>Effects</b>: Returns the key_equal object used by the unordered_set.
Chris@16 1365 //!
Chris@16 1366 //! <b>Complexity</b>: Constant.
Chris@16 1367 //!
Chris@16 1368 //! <b>Throws</b>: If key_equal copy-constructor throws.
Chris@16 1369 key_equal key_eq() const
Chris@16 1370 { return this->priv_equal(); }
Chris@16 1371
Chris@16 1372 //! <b>Effects</b>: Returns true if the container is empty.
Chris@16 1373 //!
Chris@16 1374 //! <b>Complexity</b>: if constant-time size and cache_begin options are disabled,
Chris@16 1375 //! average constant time (worst case, with empty() == true: O(this->bucket_count()).
Chris@16 1376 //! Otherwise constant.
Chris@16 1377 //!
Chris@16 1378 //! <b>Throws</b>: Nothing.
Chris@16 1379 bool empty() const
Chris@16 1380 {
Chris@16 1381 if(constant_time_size){
Chris@16 1382 return !this->size();
Chris@16 1383 }
Chris@16 1384 else if(cache_begin){
Chris@16 1385 return this->begin() == this->end();
Chris@16 1386 }
Chris@16 1387 else{
Chris@16 1388 size_type bucket_cnt = this->priv_bucket_count();
Chris@16 1389 const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer());
Chris@16 1390 for (size_type n = 0; n < bucket_cnt; ++n, ++b){
Chris@16 1391 if(!b->empty()){
Chris@16 1392 return false;
Chris@16 1393 }
Chris@16 1394 }
Chris@16 1395 return true;
Chris@16 1396 }
Chris@16 1397 }
Chris@16 1398
Chris@16 1399 //! <b>Effects</b>: Returns the number of elements stored in the unordered_set.
Chris@16 1400 //!
Chris@16 1401 //! <b>Complexity</b>: Linear to elements contained in *this if
Chris@16 1402 //! constant_time_size is false. Constant-time otherwise.
Chris@16 1403 //!
Chris@16 1404 //! <b>Throws</b>: Nothing.
Chris@16 1405 size_type size() const
Chris@16 1406 {
Chris@16 1407 if(constant_time_size)
Chris@16 1408 return this->priv_size_traits().get_size();
Chris@16 1409 else{
Chris@16 1410 size_type len = 0;
Chris@16 1411 size_type bucket_cnt = this->priv_bucket_count();
Chris@16 1412 const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer());
Chris@16 1413 for (size_type n = 0; n < bucket_cnt; ++n, ++b){
Chris@16 1414 len += b->size();
Chris@16 1415 }
Chris@16 1416 return len;
Chris@16 1417 }
Chris@16 1418 }
Chris@16 1419
Chris@16 1420 //! <b>Requires</b>: the hasher and the equality function unqualified swap
Chris@16 1421 //! call should not throw.
Chris@16 1422 //!
Chris@16 1423 //! <b>Effects</b>: Swaps the contents of two unordered_sets.
Chris@16 1424 //! Swaps also the contained bucket array and equality and hasher functors.
Chris@16 1425 //!
Chris@16 1426 //! <b>Complexity</b>: Constant.
Chris@16 1427 //!
Chris@16 1428 //! <b>Throws</b>: If the swap() call for the comparison or hash functors
Chris@16 1429 //! found using ADL throw. Basic guarantee.
Chris@16 1430 void swap(hashtable_impl& other)
Chris@16 1431 {
Chris@16 1432 using std::swap;
Chris@16 1433 //These can throw
Chris@16 1434 swap(this->priv_equal(), other.priv_equal());
Chris@16 1435 swap(this->priv_hasher(), other.priv_hasher());
Chris@16 1436 //These can't throw
Chris@16 1437 swap(this->priv_bucket_traits(), other.priv_bucket_traits());
Chris@16 1438 swap(this->priv_value_traits(), other.priv_value_traits());
Chris@16 1439 this->priv_swap_cache(other);
Chris@16 1440 if(constant_time_size){
Chris@16 1441 size_type backup = this->priv_size_traits().get_size();
Chris@16 1442 this->priv_size_traits().set_size(other.priv_size_traits().get_size());
Chris@16 1443 other.priv_size_traits().set_size(backup);
Chris@16 1444 }
Chris@16 1445 if(incremental){
Chris@16 1446 size_type backup = this->priv_split_traits().get_size();
Chris@16 1447 this->priv_split_traits().set_size(other.priv_split_traits().get_size());
Chris@16 1448 other.priv_split_traits().set_size(backup);
Chris@16 1449 }
Chris@16 1450 }
Chris@16 1451
Chris@16 1452 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
Chris@16 1453 //! Cloner should yield to nodes that compare equal and produce the same
Chris@16 1454 //! hash than the original node.
Chris@16 1455 //!
Chris@16 1456 //! <b>Effects</b>: Erases all the elements from *this
Chris@16 1457 //! calling Disposer::operator()(pointer), clones all the
Chris@16 1458 //! elements from src calling Cloner::operator()(const_reference )
Chris@16 1459 //! and inserts them on *this. The hash function and the equality
Chris@16 1460 //! predicate are copied from the source.
Chris@16 1461 //!
Chris@16 1462 //! If store_hash option is true, this method does not use the hash function.
Chris@16 1463 //!
Chris@16 1464 //! If any operation throws, all cloned elements are unlinked and disposed
Chris@16 1465 //! calling Disposer::operator()(pointer).
Chris@16 1466 //!
Chris@16 1467 //! <b>Complexity</b>: Linear to erased plus inserted elements.
Chris@16 1468 //!
Chris@16 1469 //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
Chris@16 1470 //! throws. Basic guarantee.
Chris@16 1471 template <class Cloner, class Disposer>
Chris@16 1472 void clone_from(const hashtable_impl &src, Cloner cloner, Disposer disposer)
Chris@16 1473 {
Chris@16 1474 this->clear_and_dispose(disposer);
Chris@16 1475 if(!constant_time_size || !src.empty()){
Chris@16 1476 const size_type src_bucket_count = src.bucket_count();
Chris@16 1477 const size_type dst_bucket_count = this->bucket_count();
Chris@16 1478 //Check power of two bucket array if the option is activated
Chris@16 1479 BOOST_INTRUSIVE_INVARIANT_ASSERT
Chris@16 1480 (!power_2_buckets || (0 == (src_bucket_count & (src_bucket_count-1))));
Chris@16 1481 BOOST_INTRUSIVE_INVARIANT_ASSERT
Chris@16 1482 (!power_2_buckets || (0 == (dst_bucket_count & (dst_bucket_count-1))));
Chris@16 1483
Chris@16 1484 //If src bucket count is bigger or equal, structural copy is possible
Chris@16 1485 if(!incremental && (src_bucket_count >= dst_bucket_count)){
Chris@16 1486 //First clone the first ones
Chris@16 1487 const bucket_ptr src_buckets = src.priv_bucket_pointer();
Chris@16 1488 const bucket_ptr dst_buckets = this->priv_bucket_pointer();
Chris@16 1489 size_type constructed;
Chris@16 1490
Chris@16 1491 typedef node_cast_adaptor< detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
Chris@16 1492 , slist_node_ptr, node_ptr > NodeDisposer;
Chris@16 1493 typedef node_cast_adaptor< detail::node_cloner <Cloner, real_value_traits, CircularSListAlgorithms>
Chris@16 1494 , slist_node_ptr, node_ptr > NodeCloner;
Chris@16 1495 NodeDisposer node_disp(disposer, &this->priv_real_value_traits());
Chris@16 1496
Chris@16 1497 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
Chris@16 1498 rollback(dst_buckets[0], node_disp, constructed);
Chris@16 1499 for( constructed = 0
Chris@16 1500 ; constructed < dst_bucket_count
Chris@16 1501 ; ++constructed){
Chris@16 1502 dst_buckets[constructed].clone_from
Chris@16 1503 ( src_buckets[constructed]
Chris@16 1504 , NodeCloner(cloner, &this->priv_real_value_traits()), node_disp);
Chris@16 1505 }
Chris@16 1506 if(src_bucket_count != dst_bucket_count){
Chris@16 1507 //Now insert the remaining ones using the modulo trick
Chris@16 1508 for(//"constructed" comes from the previous loop
Chris@16 1509 ; constructed < src_bucket_count
Chris@16 1510 ; ++constructed){
Chris@16 1511 bucket_type &dst_b =
Chris@16 1512 dst_buckets[detail::hash_to_bucket_split<power_2_buckets, incremental>(constructed, dst_bucket_count, dst_bucket_count)];
Chris@16 1513 bucket_type &src_b = src_buckets[constructed];
Chris@16 1514 for( siterator b(src_b.begin()), e(src_b.end())
Chris@16 1515 ; b != e
Chris@16 1516 ; ++b){
Chris@16 1517 dst_b.push_front(*(NodeCloner(cloner, &this->priv_real_value_traits())(*b.pointed_node())));
Chris@16 1518 }
Chris@16 1519 }
Chris@16 1520 }
Chris@16 1521 this->priv_hasher() = src.priv_hasher();
Chris@16 1522 this->priv_equal() = src.priv_equal();
Chris@16 1523 rollback.release();
Chris@16 1524 this->priv_size_traits().set_size(src.priv_size_traits().get_size());
Chris@16 1525 this->priv_split_traits().set_size(dst_bucket_count);
Chris@16 1526 this->priv_insertion_update_cache(0u);
Chris@16 1527 this->priv_erasure_update_cache();
Chris@16 1528 }
Chris@16 1529 else if(store_hash){
Chris@16 1530 //Unlike previous cloning algorithm, this can throw
Chris@16 1531 //if cloner, hasher or comparison functor throw
Chris@16 1532 const_iterator b(src.begin()), e(src.end());
Chris@16 1533 detail::exception_disposer<hashtable_impl, Disposer>
Chris@16 1534 rollback(*this, disposer);
Chris@16 1535 for(; b != e; ++b){
Chris@16 1536 std::size_t hash_value = this->priv_stored_or_compute_hash(*b, store_hash_t());;
Chris@16 1537 this->priv_insert_equal_with_hash(*cloner(*b), hash_value);
Chris@16 1538 }
Chris@16 1539 rollback.release();
Chris@16 1540 }
Chris@16 1541 else{
Chris@16 1542 //Unlike previous cloning algorithm, this can throw
Chris@16 1543 //if cloner, hasher or comparison functor throw
Chris@16 1544 const_iterator b(src.begin()), e(src.end());
Chris@16 1545 detail::exception_disposer<hashtable_impl, Disposer>
Chris@16 1546 rollback(*this, disposer);
Chris@16 1547 for(; b != e; ++b){
Chris@16 1548 this->insert_equal(*cloner(*b));
Chris@16 1549 }
Chris@16 1550 rollback.release();
Chris@16 1551 }
Chris@16 1552 }
Chris@16 1553 }
Chris@16 1554
Chris@16 1555 //! <b>Requires</b>: value must be an lvalue
Chris@16 1556 //!
Chris@16 1557 //! <b>Effects</b>: Inserts the value into the unordered_set.
Chris@16 1558 //!
Chris@16 1559 //! <b>Returns</b>: An iterator to the inserted value.
Chris@16 1560 //!
Chris@16 1561 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 1562 //!
Chris@16 1563 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
Chris@16 1564 //!
Chris@16 1565 //! <b>Note</b>: Does not affect the validity of iterators and references.
Chris@16 1566 //! No copy-constructors are called.
Chris@16 1567 iterator insert_equal(reference value)
Chris@16 1568 {
Chris@16 1569 size_type bucket_num;
Chris@16 1570 std::size_t hash_value;
Chris@16 1571 siterator prev;
Chris@16 1572 siterator it = this->priv_find
Chris@16 1573 (value, this->priv_hasher(), this->priv_equal(), bucket_num, hash_value, prev);
Chris@16 1574 return this->priv_insert_equal_find(value, bucket_num, hash_value, it);
Chris@16 1575 }
Chris@16 1576
Chris@16 1577 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
Chris@16 1578 //! of type value_type.
Chris@16 1579 //!
Chris@16 1580 //! <b>Effects</b>: Equivalent to this->insert_equal(t) for each element in [b, e).
Chris@16 1581 //!
Chris@16 1582 //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
Chris@16 1583 //! Worst case O(N*this->size()).
Chris@16 1584 //!
Chris@16 1585 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
Chris@16 1586 //!
Chris@16 1587 //! <b>Note</b>: Does not affect the validity of iterators and references.
Chris@16 1588 //! No copy-constructors are called.
Chris@16 1589 template<class Iterator>
Chris@16 1590 void insert_equal(Iterator b, Iterator e)
Chris@16 1591 {
Chris@16 1592 for (; b != e; ++b)
Chris@16 1593 this->insert_equal(*b);
Chris@16 1594 }
Chris@16 1595
Chris@16 1596 //! <b>Requires</b>: value must be an lvalue
Chris@16 1597 //!
Chris@16 1598 //! <b>Effects</b>: Tries to inserts value into the unordered_set.
Chris@16 1599 //!
Chris@16 1600 //! <b>Returns</b>: If the value
Chris@16 1601 //! is not already present inserts it and returns a pair containing the
Chris@16 1602 //! iterator to the new value and true. If there is an equivalent value
Chris@16 1603 //! returns a pair containing an iterator to the already present value
Chris@16 1604 //! and false.
Chris@16 1605 //!
Chris@16 1606 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 1607 //!
Chris@16 1608 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
Chris@16 1609 //!
Chris@16 1610 //! <b>Note</b>: Does not affect the validity of iterators and references.
Chris@16 1611 //! No copy-constructors are called.
Chris@16 1612 std::pair<iterator, bool> insert_unique(reference value)
Chris@16 1613 {
Chris@16 1614 insert_commit_data commit_data;
Chris@16 1615 std::pair<iterator, bool> ret = this->insert_unique_check
Chris@16 1616 (value, this->priv_hasher(), this->priv_equal(), commit_data);
Chris@16 1617 if(!ret.second)
Chris@16 1618 return ret;
Chris@16 1619 return std::pair<iterator, bool>
Chris@16 1620 (this->insert_unique_commit(value, commit_data), true);
Chris@16 1621 }
Chris@16 1622
Chris@16 1623 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
Chris@16 1624 //! of type value_type.
Chris@16 1625 //!
Chris@16 1626 //! <b>Effects</b>: Equivalent to this->insert_unique(t) for each element in [b, e).
Chris@16 1627 //!
Chris@16 1628 //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
Chris@16 1629 //! Worst case O(N*this->size()).
Chris@16 1630 //!
Chris@16 1631 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
Chris@16 1632 //!
Chris@16 1633 //! <b>Note</b>: Does not affect the validity of iterators and references.
Chris@16 1634 //! No copy-constructors are called.
Chris@16 1635 template<class Iterator>
Chris@16 1636 void insert_unique(Iterator b, Iterator e)
Chris@16 1637 {
Chris@16 1638 for (; b != e; ++b)
Chris@16 1639 this->insert_unique(*b);
Chris@16 1640 }
Chris@16 1641
Chris@16 1642 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 1643 //! the same hash values as the stored hasher. The difference is that
Chris@16 1644 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 1645 //!
Chris@16 1646 //! "equal_func" must be a equality function that induces
Chris@16 1647 //! the same equality as key_equal. The difference is that
Chris@16 1648 //! "equal_func" compares an arbitrary key with the contained values.
Chris@16 1649 //!
Chris@16 1650 //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
Chris@16 1651 //! a user provided key instead of the value itself.
Chris@16 1652 //!
Chris@16 1653 //! <b>Returns</b>: If there is an equivalent value
Chris@16 1654 //! returns a pair containing an iterator to the already present value
Chris@16 1655 //! and false. If the value can be inserted returns true in the returned
Chris@16 1656 //! pair boolean and fills "commit_data" that is meant to be used with
Chris@16 1657 //! the "insert_commit" function.
Chris@16 1658 //!
Chris@16 1659 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 1660 //!
Chris@16 1661 //! <b>Throws</b>: If hash_func or equal_func throw. Strong guarantee.
Chris@16 1662 //!
Chris@16 1663 //! <b>Notes</b>: This function is used to improve performance when constructing
Chris@16 1664 //! a value_type is expensive: if there is an equivalent value
Chris@16 1665 //! the constructed object must be discarded. Many times, the part of the
Chris@16 1666 //! node that is used to impose the hash or the equality is much cheaper to
Chris@16 1667 //! construct than the value_type and this function offers the possibility to
Chris@16 1668 //! use that the part to check if the insertion will be successful.
Chris@16 1669 //!
Chris@16 1670 //! If the check is successful, the user can construct the value_type and use
Chris@16 1671 //! "insert_commit" to insert the object in constant-time.
Chris@16 1672 //!
Chris@16 1673 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
Chris@16 1674 //! objects are inserted or erased from the unordered_set.
Chris@16 1675 //!
Chris@16 1676 //! After a successful rehashing insert_commit_data remains valid.
Chris@16 1677 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 1678 std::pair<iterator, bool> insert_unique_check
Chris@16 1679 ( const KeyType &key
Chris@16 1680 , KeyHasher hash_func
Chris@16 1681 , KeyValueEqual equal_func
Chris@16 1682 , insert_commit_data &commit_data)
Chris@16 1683 {
Chris@16 1684 size_type bucket_num;
Chris@16 1685 siterator prev;
Chris@16 1686 siterator prev_pos =
Chris@16 1687 this->priv_find(key, hash_func, equal_func, bucket_num, commit_data.hash, prev);
Chris@16 1688 bool success = prev_pos == this->priv_invalid_local_it();
Chris@16 1689 if(success){
Chris@16 1690 prev_pos = prev;
Chris@16 1691 }
Chris@16 1692 return std::pair<iterator, bool>(iterator(prev_pos, &this->get_bucket_value_traits()),success);
Chris@16 1693 }
Chris@16 1694
Chris@16 1695 //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
Chris@16 1696 //! must have been obtained from a previous call to "insert_check".
Chris@16 1697 //! No objects should have been inserted or erased from the unordered_set between
Chris@16 1698 //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
Chris@16 1699 //!
Chris@16 1700 //! <b>Effects</b>: Inserts the value in the unordered_set using the information obtained
Chris@16 1701 //! from the "commit_data" that a previous "insert_check" filled.
Chris@16 1702 //!
Chris@16 1703 //! <b>Returns</b>: An iterator to the newly inserted object.
Chris@16 1704 //!
Chris@16 1705 //! <b>Complexity</b>: Constant time.
Chris@16 1706 //!
Chris@16 1707 //! <b>Throws</b>: Nothing.
Chris@16 1708 //!
Chris@16 1709 //! <b>Notes</b>: This function has only sense if a "insert_check" has been
Chris@16 1710 //! previously executed to fill "commit_data". No value should be inserted or
Chris@16 1711 //! erased between the "insert_check" and "insert_commit" calls.
Chris@16 1712 //!
Chris@16 1713 //! After a successful rehashing insert_commit_data remains valid.
Chris@16 1714 iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
Chris@16 1715 {
Chris@16 1716 size_type bucket_num = this->priv_hash_to_bucket(commit_data.hash);
Chris@16 1717 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
Chris@16 1718 this->priv_size_traits().increment();
Chris@16 1719 node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
Chris@16 1720 node_functions_t::store_hash(n, commit_data.hash, store_hash_t());
Chris@16 1721 if(safemode_or_autounlink)
Chris@16 1722 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n));
Chris@16 1723 this->priv_insertion_update_cache(bucket_num);
Chris@16 1724 group_functions_t::insert_in_group(node_ptr(), n, optimize_multikey_t());
Chris@16 1725 return iterator(b.insert_after(b.before_begin(), *n), &this->get_bucket_value_traits());
Chris@16 1726 }
Chris@16 1727
Chris@16 1728 //! <b>Effects</b>: Erases the element pointed to by i.
Chris@16 1729 //!
Chris@16 1730 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 1731 //!
Chris@16 1732 //! <b>Throws</b>: Nothing.
Chris@16 1733 //!
Chris@16 1734 //! <b>Note</b>: Invalidates the iterators (but not the references)
Chris@16 1735 //! to the erased element. No destructors are called.
Chris@16 1736 void erase(const_iterator i)
Chris@16 1737 { this->erase_and_dispose(i, detail::null_disposer()); }
Chris@16 1738
Chris@16 1739 //! <b>Effects</b>: Erases the range pointed to by b end e.
Chris@16 1740 //!
Chris@16 1741 //! <b>Complexity</b>: Average case O(std::distance(b, e)),
Chris@16 1742 //! worst case O(this->size()).
Chris@16 1743 //!
Chris@16 1744 //! <b>Throws</b>: Nothing.
Chris@16 1745 //!
Chris@16 1746 //! <b>Note</b>: Invalidates the iterators (but not the references)
Chris@16 1747 //! to the erased elements. No destructors are called.
Chris@16 1748 void erase(const_iterator b, const_iterator e)
Chris@16 1749 { this->erase_and_dispose(b, e, detail::null_disposer()); }
Chris@16 1750
Chris@16 1751 //! <b>Effects</b>: Erases all the elements with the given value.
Chris@16 1752 //!
Chris@16 1753 //! <b>Returns</b>: The number of erased elements.
Chris@16 1754 //!
Chris@16 1755 //! <b>Complexity</b>: Average case O(this->count(value)).
Chris@16 1756 //! Worst case O(this->size()).
Chris@16 1757 //!
Chris@16 1758 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
Chris@16 1759 //! Basic guarantee.
Chris@16 1760 //!
Chris@16 1761 //! <b>Note</b>: Invalidates the iterators (but not the references)
Chris@16 1762 //! to the erased elements. No destructors are called.
Chris@16 1763 size_type erase(const_reference value)
Chris@16 1764 { return this->erase(value, this->priv_hasher(), this->priv_equal()); }
Chris@16 1765
Chris@16 1766 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 1767 //! the same hash values as the stored hasher. The difference is that
Chris@16 1768 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 1769 //!
Chris@16 1770 //! "equal_func" must be a equality function that induces
Chris@16 1771 //! the same equality as key_equal. The difference is that
Chris@16 1772 //! "equal_func" compares an arbitrary key with the contained values.
Chris@16 1773 //!
Chris@16 1774 //! <b>Effects</b>: Erases all the elements that have the same hash and
Chris@16 1775 //! compare equal with the given key.
Chris@16 1776 //!
Chris@16 1777 //! <b>Returns</b>: The number of erased elements.
Chris@16 1778 //!
Chris@16 1779 //! <b>Complexity</b>: Average case O(this->count(value)).
Chris@16 1780 //! Worst case O(this->size()).
Chris@16 1781 //!
Chris@16 1782 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
Chris@16 1783 //!
Chris@16 1784 //! <b>Note</b>: Invalidates the iterators (but not the references)
Chris@16 1785 //! to the erased elements. No destructors are called.
Chris@16 1786 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 1787 size_type erase(const KeyType& key, KeyHasher hash_func, KeyValueEqual equal_func)
Chris@16 1788 { return this->erase_and_dispose(key, hash_func, equal_func, detail::null_disposer()); }
Chris@16 1789
Chris@16 1790 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
Chris@16 1791 //!
Chris@16 1792 //! <b>Effects</b>: Erases the element pointed to by i.
Chris@16 1793 //! Disposer::operator()(pointer) is called for the removed element.
Chris@16 1794 //!
Chris@16 1795 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 1796 //!
Chris@16 1797 //! <b>Throws</b>: Nothing.
Chris@16 1798 //!
Chris@16 1799 //! <b>Note</b>: Invalidates the iterators
Chris@16 1800 //! to the erased elements.
Chris@16 1801 template<class Disposer>
Chris@16 1802 void erase_and_dispose(const_iterator i, Disposer disposer
Chris@16 1803 /// @cond
Chris@16 1804 , typename detail::enable_if_c<!detail::is_convertible<Disposer, const_iterator>::value >::type * = 0
Chris@16 1805 /// @endcond
Chris@16 1806 )
Chris@16 1807 {
Chris@16 1808 this->priv_erase(i, disposer, optimize_multikey_t());
Chris@16 1809 this->priv_size_traits().decrement();
Chris@16 1810 this->priv_erasure_update_cache();
Chris@16 1811 }
Chris@16 1812
Chris@16 1813 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
Chris@16 1814 //!
Chris@16 1815 //! <b>Effects</b>: Erases the range pointed to by b end e.
Chris@16 1816 //! Disposer::operator()(pointer) is called for the removed elements.
Chris@16 1817 //!
Chris@16 1818 //! <b>Complexity</b>: Average case O(std::distance(b, e)),
Chris@16 1819 //! worst case O(this->size()).
Chris@16 1820 //!
Chris@16 1821 //! <b>Throws</b>: Nothing.
Chris@16 1822 //!
Chris@16 1823 //! <b>Note</b>: Invalidates the iterators
Chris@16 1824 //! to the erased elements.
Chris@16 1825 template<class Disposer>
Chris@16 1826 void erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
Chris@16 1827 {
Chris@16 1828 if(b != e){
Chris@16 1829 //Get the bucket number and local iterator for both iterators
Chris@16 1830 siterator first_local_it(b.slist_it());
Chris@16 1831 size_type first_bucket_num = this->priv_get_bucket_num(first_local_it);
Chris@16 1832
Chris@16 1833 const bucket_ptr buck_ptr = this->priv_bucket_pointer();
Chris@16 1834 siterator before_first_local_it
Chris@16 1835 = this->priv_get_previous(buck_ptr[first_bucket_num], first_local_it);
Chris@16 1836 size_type last_bucket_num;
Chris@16 1837 siterator last_local_it;
Chris@16 1838
Chris@16 1839 //For the end iterator, we will assign the end iterator
Chris@16 1840 //of the last bucket
Chris@16 1841 if(e == this->end()){
Chris@16 1842 last_bucket_num = this->bucket_count() - 1;
Chris@16 1843 last_local_it = buck_ptr[last_bucket_num].end();
Chris@16 1844 }
Chris@16 1845 else{
Chris@16 1846 last_local_it = e.slist_it();
Chris@16 1847 last_bucket_num = this->priv_get_bucket_num(last_local_it);
Chris@16 1848 }
Chris@16 1849 this->priv_erase_range(before_first_local_it, first_bucket_num, last_local_it, last_bucket_num, disposer);
Chris@16 1850 this->priv_erasure_update_cache_range(first_bucket_num, last_bucket_num);
Chris@16 1851 }
Chris@16 1852 }
Chris@16 1853
Chris@16 1854 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
Chris@16 1855 //!
Chris@16 1856 //! <b>Effects</b>: Erases all the elements with the given value.
Chris@16 1857 //! Disposer::operator()(pointer) is called for the removed elements.
Chris@16 1858 //!
Chris@16 1859 //! <b>Returns</b>: The number of erased elements.
Chris@16 1860 //!
Chris@16 1861 //! <b>Complexity</b>: Average case O(this->count(value)).
Chris@16 1862 //! Worst case O(this->size()).
Chris@16 1863 //!
Chris@16 1864 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
Chris@16 1865 //! Basic guarantee.
Chris@16 1866 //!
Chris@16 1867 //! <b>Note</b>: Invalidates the iterators (but not the references)
Chris@16 1868 //! to the erased elements. No destructors are called.
Chris@16 1869 template<class Disposer>
Chris@16 1870 size_type erase_and_dispose(const_reference value, Disposer disposer)
Chris@16 1871 { return this->erase_and_dispose(value, this->priv_hasher(), this->priv_equal(), disposer); }
Chris@16 1872
Chris@16 1873 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
Chris@16 1874 //!
Chris@16 1875 //! <b>Effects</b>: Erases all the elements with the given key.
Chris@16 1876 //! according to the comparison functor "equal_func".
Chris@16 1877 //! Disposer::operator()(pointer) is called for the removed elements.
Chris@16 1878 //!
Chris@16 1879 //! <b>Returns</b>: The number of erased elements.
Chris@16 1880 //!
Chris@16 1881 //! <b>Complexity</b>: Average case O(this->count(value)).
Chris@16 1882 //! Worst case O(this->size()).
Chris@16 1883 //!
Chris@16 1884 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
Chris@16 1885 //!
Chris@16 1886 //! <b>Note</b>: Invalidates the iterators
Chris@16 1887 //! to the erased elements.
Chris@16 1888 template<class KeyType, class KeyHasher, class KeyValueEqual, class Disposer>
Chris@16 1889 size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func
Chris@16 1890 ,KeyValueEqual equal_func, Disposer disposer)
Chris@16 1891 {
Chris@16 1892 size_type bucket_num;
Chris@16 1893 std::size_t h;
Chris@16 1894 siterator prev;
Chris@16 1895 siterator it = this->priv_find(key, hash_func, equal_func, bucket_num, h, prev);
Chris@16 1896 bool success = it != this->priv_invalid_local_it();
Chris@16 1897 size_type cnt(0);
Chris@16 1898 if(!success){
Chris@16 1899 return 0;
Chris@16 1900 }
Chris@16 1901 else if(optimize_multikey){
Chris@16 1902 siterator last = bucket_type::s_iterator_to
Chris@16 1903 (*node_traits::get_next(group_functions_t::get_last_in_group
Chris@16 1904 (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t())));
Chris@16 1905 this->priv_erase_range_impl(bucket_num, prev, last, disposer, cnt);
Chris@16 1906 }
Chris@16 1907 else{
Chris@16 1908 //If found erase all equal values
Chris@16 1909 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
Chris@16 1910 for(siterator end_sit = b.end(); it != end_sit; ++cnt, ++it){
Chris@16 1911 slist_node_ptr n(it.pointed_node());
Chris@16 1912 const value_type &v = this->priv_value_from_slist_node(n);
Chris@16 1913 if(compare_hash){
Chris@16 1914 std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t());
Chris@16 1915 if(h != vh || !equal_func(key, v)){
Chris@16 1916 break;
Chris@16 1917 }
Chris@16 1918 }
Chris@16 1919 else if(!equal_func(key, v)){
Chris@16 1920 break;
Chris@16 1921 }
Chris@16 1922 this->priv_size_traits().decrement();
Chris@16 1923 }
Chris@16 1924 b.erase_after_and_dispose(prev, it, make_node_disposer(disposer));
Chris@16 1925 }
Chris@16 1926 this->priv_erasure_update_cache();
Chris@16 1927 return cnt;
Chris@16 1928 }
Chris@16 1929
Chris@16 1930 //! <b>Effects</b>: Erases all of the elements.
Chris@16 1931 //!
Chris@16 1932 //! <b>Complexity</b>: Linear to the number of elements on the container.
Chris@16 1933 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
Chris@16 1934 //!
Chris@16 1935 //! <b>Throws</b>: Nothing.
Chris@16 1936 //!
Chris@16 1937 //! <b>Note</b>: Invalidates the iterators (but not the references)
Chris@16 1938 //! to the erased elements. No destructors are called.
Chris@16 1939 void clear()
Chris@16 1940 {
Chris@16 1941 this->priv_clear_buckets();
Chris@16 1942 this->priv_size_traits().set_size(size_type(0));
Chris@16 1943 }
Chris@16 1944
Chris@16 1945 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
Chris@16 1946 //!
Chris@16 1947 //! <b>Effects</b>: Erases all of the elements.
Chris@16 1948 //!
Chris@16 1949 //! <b>Complexity</b>: Linear to the number of elements on the container.
Chris@16 1950 //! Disposer::operator()(pointer) is called for the removed elements.
Chris@16 1951 //!
Chris@16 1952 //! <b>Throws</b>: Nothing.
Chris@16 1953 //!
Chris@16 1954 //! <b>Note</b>: Invalidates the iterators (but not the references)
Chris@16 1955 //! to the erased elements. No destructors are called.
Chris@16 1956 template<class Disposer>
Chris@16 1957 void clear_and_dispose(Disposer disposer)
Chris@16 1958 {
Chris@16 1959 if(!constant_time_size || !this->empty()){
Chris@16 1960 size_type num_buckets = this->bucket_count();
Chris@16 1961 bucket_ptr b = this->priv_bucket_pointer();
Chris@16 1962 for(; num_buckets--; ++b){
Chris@16 1963 b->clear_and_dispose(make_node_disposer(disposer));
Chris@16 1964 }
Chris@16 1965 this->priv_size_traits().set_size(size_type(0));
Chris@16 1966 }
Chris@16 1967 this->priv_initialize_cache();
Chris@16 1968 }
Chris@16 1969
Chris@16 1970 //! <b>Effects</b>: Returns the number of contained elements with the given value
Chris@16 1971 //!
Chris@16 1972 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 1973 //!
Chris@16 1974 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
Chris@16 1975 size_type count(const_reference value) const
Chris@16 1976 { return this->count(value, this->priv_hasher(), this->priv_equal()); }
Chris@16 1977
Chris@16 1978 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 1979 //! the same hash values as the stored hasher. The difference is that
Chris@16 1980 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 1981 //!
Chris@16 1982 //! "equal_func" must be a equality function that induces
Chris@16 1983 //! the same equality as key_equal. The difference is that
Chris@16 1984 //! "equal_func" compares an arbitrary key with the contained values.
Chris@16 1985 //!
Chris@16 1986 //! <b>Effects</b>: Returns the number of contained elements with the given key
Chris@16 1987 //!
Chris@16 1988 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 1989 //!
Chris@16 1990 //! <b>Throws</b>: If hash_func or equal throw.
Chris@16 1991 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 1992 size_type count(const KeyType &key, const KeyHasher &hash_func, const KeyValueEqual &equal_func) const
Chris@16 1993 {
Chris@16 1994 size_type bucket_n1, bucket_n2, cnt;
Chris@16 1995 this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
Chris@16 1996 return cnt;
Chris@16 1997 }
Chris@16 1998
Chris@16 1999 //! <b>Effects</b>: Finds an iterator to the first element is equal to
Chris@16 2000 //! "value" or end() if that element does not exist.
Chris@16 2001 //!
Chris@16 2002 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 2003 //!
Chris@16 2004 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
Chris@16 2005 iterator find(const_reference value)
Chris@16 2006 { return this->find(value, this->priv_hasher(), this->priv_equal()); }
Chris@16 2007
Chris@16 2008 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 2009 //! the same hash values as the stored hasher. The difference is that
Chris@16 2010 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 2011 //!
Chris@16 2012 //! "equal_func" must be a equality function that induces
Chris@16 2013 //! the same equality as key_equal. The difference is that
Chris@16 2014 //! "equal_func" compares an arbitrary key with the contained values.
Chris@16 2015 //!
Chris@16 2016 //! <b>Effects</b>: Finds an iterator to the first element whose key is
Chris@16 2017 //! "key" according to the given hash and equality functor or end() if
Chris@16 2018 //! that element does not exist.
Chris@16 2019 //!
Chris@16 2020 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 2021 //!
Chris@16 2022 //! <b>Throws</b>: If hash_func or equal_func throw.
Chris@16 2023 //!
Chris@16 2024 //! <b>Note</b>: This function is used when constructing a value_type
Chris@16 2025 //! is expensive and the value_type can be compared with a cheaper
Chris@16 2026 //! key type. Usually this key is part of the value_type.
Chris@16 2027 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 2028 iterator find(const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func)
Chris@16 2029 {
Chris@16 2030 size_type bucket_n;
Chris@16 2031 std::size_t hash;
Chris@16 2032 siterator prev;
Chris@16 2033 siterator local_it = this->priv_find(key, hash_func, equal_func, bucket_n, hash, prev);
Chris@16 2034 return iterator(local_it, &this->get_bucket_value_traits());
Chris@16 2035 }
Chris@16 2036
Chris@16 2037 //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
Chris@16 2038 //! "key" or end() if that element does not exist.
Chris@16 2039 //!
Chris@16 2040 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 2041 //!
Chris@16 2042 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
Chris@16 2043 const_iterator find(const_reference value) const
Chris@16 2044 { return this->find(value, this->priv_hasher(), this->priv_equal()); }
Chris@16 2045
Chris@16 2046 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 2047 //! the same hash values as the stored hasher. The difference is that
Chris@16 2048 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 2049 //!
Chris@16 2050 //! "equal_func" must be a equality function that induces
Chris@16 2051 //! the same equality as key_equal. The difference is that
Chris@16 2052 //! "equal_func" compares an arbitrary key with the contained values.
Chris@16 2053 //!
Chris@16 2054 //! <b>Effects</b>: Finds an iterator to the first element whose key is
Chris@16 2055 //! "key" according to the given hasher and equality functor or end() if
Chris@16 2056 //! that element does not exist.
Chris@16 2057 //!
Chris@16 2058 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
Chris@16 2059 //!
Chris@16 2060 //! <b>Throws</b>: If hash_func or equal_func throw.
Chris@16 2061 //!
Chris@16 2062 //! <b>Note</b>: This function is used when constructing a value_type
Chris@16 2063 //! is expensive and the value_type can be compared with a cheaper
Chris@16 2064 //! key type. Usually this key is part of the value_type.
Chris@16 2065 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 2066 const_iterator find
Chris@16 2067 (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const
Chris@16 2068 {
Chris@16 2069 size_type bucket_n;
Chris@16 2070 std::size_t hash_value;
Chris@16 2071 siterator prev;
Chris@16 2072 siterator sit = this->priv_find(key, hash_func, equal_func, bucket_n, hash_value, prev);
Chris@16 2073 return const_iterator(sit, &this->get_bucket_value_traits());
Chris@16 2074 }
Chris@16 2075
Chris@16 2076 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
Chris@16 2077 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
Chris@16 2078 //! elements exist.
Chris@16 2079 //!
Chris@16 2080 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
Chris@16 2081 //!
Chris@16 2082 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
Chris@16 2083 std::pair<iterator,iterator> equal_range(const_reference value)
Chris@16 2084 { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); }
Chris@16 2085
Chris@16 2086 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 2087 //! the same hash values as the stored hasher. The difference is that
Chris@16 2088 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 2089 //!
Chris@16 2090 //! "equal_func" must be a equality function that induces
Chris@16 2091 //! the same equality as key_equal. The difference is that
Chris@16 2092 //! "equal_func" compares an arbitrary key with the contained values.
Chris@16 2093 //!
Chris@16 2094 //! <b>Effects</b>: Returns a range containing all elements with equivalent
Chris@16 2095 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
Chris@16 2096 //! elements exist.
Chris@16 2097 //!
Chris@16 2098 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
Chris@16 2099 //! Worst case O(this->size()).
Chris@16 2100 //!
Chris@16 2101 //! <b>Throws</b>: If hash_func or the equal_func throw.
Chris@16 2102 //!
Chris@16 2103 //! <b>Note</b>: This function is used when constructing a value_type
Chris@16 2104 //! is expensive and the value_type can be compared with a cheaper
Chris@16 2105 //! key type. Usually this key is part of the value_type.
Chris@16 2106 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 2107 std::pair<iterator,iterator> equal_range
Chris@16 2108 (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func)
Chris@16 2109 {
Chris@16 2110 size_type bucket_n1, bucket_n2, cnt;
Chris@16 2111 std::pair<siterator, siterator> ret = this->priv_equal_range
Chris@16 2112 (key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
Chris@16 2113 return std::pair<iterator, iterator>
Chris@16 2114 (iterator(ret.first, &this->get_bucket_value_traits()), iterator(ret.second, &this->get_bucket_value_traits()));
Chris@16 2115 }
Chris@16 2116
Chris@16 2117 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
Chris@16 2118 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
Chris@16 2119 //! elements exist.
Chris@16 2120 //!
Chris@16 2121 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
Chris@16 2122 //!
Chris@16 2123 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
Chris@16 2124 std::pair<const_iterator, const_iterator>
Chris@16 2125 equal_range(const_reference value) const
Chris@16 2126 { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); }
Chris@16 2127
Chris@16 2128 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 2129 //! the same hash values as the stored hasher. The difference is that
Chris@16 2130 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 2131 //!
Chris@16 2132 //! "equal_func" must be a equality function that induces
Chris@16 2133 //! the same equality as key_equal. The difference is that
Chris@16 2134 //! "equal_func" compares an arbitrary key with the contained values.
Chris@16 2135 //!
Chris@16 2136 //! <b>Effects</b>: Returns a range containing all elements with equivalent
Chris@16 2137 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
Chris@16 2138 //! elements exist.
Chris@16 2139 //!
Chris@16 2140 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
Chris@16 2141 //! Worst case O(this->size()).
Chris@16 2142 //!
Chris@16 2143 //! <b>Throws</b>: If the hasher or equal_func throw.
Chris@16 2144 //!
Chris@16 2145 //! <b>Note</b>: This function is used when constructing a value_type
Chris@16 2146 //! is expensive and the value_type can be compared with a cheaper
Chris@16 2147 //! key type. Usually this key is part of the value_type.
Chris@16 2148 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 2149 std::pair<const_iterator,const_iterator> equal_range
Chris@16 2150 (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const
Chris@16 2151 {
Chris@16 2152 size_type bucket_n1, bucket_n2, cnt;
Chris@16 2153 std::pair<siterator, siterator> ret =
Chris@16 2154 this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
Chris@16 2155 return std::pair<const_iterator, const_iterator>
Chris@16 2156 (const_iterator(ret.first, &this->get_bucket_value_traits()), const_iterator(ret.second, &this->get_bucket_value_traits()));
Chris@16 2157 }
Chris@16 2158
Chris@16 2159 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
Chris@16 2160 //! appropriate type. Otherwise the behavior is undefined.
Chris@16 2161 //!
Chris@16 2162 //! <b>Effects</b>: Returns: a valid iterator belonging to the unordered_set
Chris@16 2163 //! that points to the value
Chris@16 2164 //!
Chris@16 2165 //! <b>Complexity</b>: Constant.
Chris@16 2166 //!
Chris@16 2167 //! <b>Throws</b>: If the internal hash function throws.
Chris@16 2168 iterator iterator_to(reference value)
Chris@16 2169 {
Chris@16 2170 return iterator(bucket_type::s_iterator_to(this->priv_value_to_node(value)), &this->get_bucket_value_traits());
Chris@16 2171 }
Chris@16 2172
Chris@16 2173 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
Chris@16 2174 //! appropriate type. Otherwise the behavior is undefined.
Chris@16 2175 //!
Chris@16 2176 //! <b>Effects</b>: Returns: a valid const_iterator belonging to the
Chris@16 2177 //! unordered_set that points to the value
Chris@16 2178 //!
Chris@16 2179 //! <b>Complexity</b>: Constant.
Chris@16 2180 //!
Chris@16 2181 //! <b>Throws</b>: If the internal hash function throws.
Chris@16 2182 const_iterator iterator_to(const_reference value) const
Chris@16 2183 {
Chris@16 2184 siterator sit = bucket_type::s_iterator_to(const_cast<node &>(this->priv_value_to_node(value)));
Chris@16 2185 return const_iterator(sit, &this->get_bucket_value_traits());
Chris@16 2186 }
Chris@16 2187
Chris@16 2188 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
Chris@16 2189 //! appropriate type. Otherwise the behavior is undefined.
Chris@16 2190 //!
Chris@16 2191 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
Chris@16 2192 //! that points to the value
Chris@16 2193 //!
Chris@16 2194 //! <b>Complexity</b>: Constant.
Chris@16 2195 //!
Chris@16 2196 //! <b>Throws</b>: Nothing.
Chris@16 2197 //!
Chris@16 2198 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
Chris@16 2199 //! is stateless.
Chris@16 2200 static local_iterator s_local_iterator_to(reference value)
Chris@16 2201 {
Chris@16 2202 BOOST_STATIC_ASSERT((!stateful_value_traits));
Chris@16 2203 siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(value));
Chris@16 2204 return local_iterator(sit, const_real_value_traits_ptr());
Chris@16 2205 }
Chris@16 2206
Chris@16 2207 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
Chris@16 2208 //! appropriate type. Otherwise the behavior is undefined.
Chris@16 2209 //!
Chris@16 2210 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
Chris@16 2211 //! the unordered_set that points to the value
Chris@16 2212 //!
Chris@16 2213 //! <b>Complexity</b>: Constant.
Chris@16 2214 //!
Chris@16 2215 //! <b>Throws</b>: Nothing.
Chris@16 2216 //!
Chris@16 2217 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
Chris@16 2218 //! is stateless.
Chris@16 2219 static const_local_iterator s_local_iterator_to(const_reference value)
Chris@16 2220 {
Chris@16 2221 BOOST_STATIC_ASSERT((!stateful_value_traits));
Chris@16 2222 siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(const_cast<value_type&>(value)));
Chris@16 2223 return const_local_iterator(sit, const_real_value_traits_ptr());
Chris@16 2224 }
Chris@16 2225
Chris@16 2226 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
Chris@16 2227 //! appropriate type. Otherwise the behavior is undefined.
Chris@16 2228 //!
Chris@16 2229 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
Chris@16 2230 //! that points to the value
Chris@16 2231 //!
Chris@16 2232 //! <b>Complexity</b>: Constant.
Chris@16 2233 //!
Chris@16 2234 //! <b>Throws</b>: Nothing.
Chris@16 2235 local_iterator local_iterator_to(reference value)
Chris@16 2236 {
Chris@16 2237 siterator sit = bucket_type::s_iterator_to(this->priv_value_to_node(value));
Chris@16 2238 return local_iterator(sit, this->real_value_traits_ptr());
Chris@16 2239 }
Chris@16 2240
Chris@16 2241 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
Chris@16 2242 //! appropriate type. Otherwise the behavior is undefined.
Chris@16 2243 //!
Chris@16 2244 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
Chris@16 2245 //! the unordered_set that points to the value
Chris@16 2246 //!
Chris@16 2247 //! <b>Complexity</b>: Constant.
Chris@16 2248 //!
Chris@16 2249 //! <b>Throws</b>: Nothing.
Chris@16 2250 const_local_iterator local_iterator_to(const_reference value) const
Chris@16 2251 {
Chris@16 2252 siterator sit = bucket_type::s_iterator_to
Chris@16 2253 (const_cast<node &>(this->priv_value_to_node(value)));
Chris@16 2254 return const_local_iterator(sit, this->real_value_traits_ptr());
Chris@16 2255 }
Chris@16 2256
Chris@16 2257 //! <b>Effects</b>: Returns the number of buckets passed in the constructor
Chris@16 2258 //! or the last rehash function.
Chris@16 2259 //!
Chris@16 2260 //! <b>Complexity</b>: Constant.
Chris@16 2261 //!
Chris@16 2262 //! <b>Throws</b>: Nothing.
Chris@16 2263 size_type bucket_count() const
Chris@16 2264 { return this->priv_bucket_count(); }
Chris@16 2265
Chris@16 2266 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
Chris@16 2267 //!
Chris@16 2268 //! <b>Effects</b>: Returns the number of elements in the nth bucket.
Chris@16 2269 //!
Chris@16 2270 //! <b>Complexity</b>: Constant.
Chris@16 2271 //!
Chris@16 2272 //! <b>Throws</b>: Nothing.
Chris@16 2273 size_type bucket_size(size_type n) const
Chris@16 2274 { return this->priv_bucket_pointer()[n].size(); }
Chris@16 2275
Chris@16 2276 //! <b>Effects</b>: Returns the index of the bucket in which elements
Chris@16 2277 //! with keys equivalent to k would be found, if any such element existed.
Chris@16 2278 //!
Chris@16 2279 //! <b>Complexity</b>: Constant.
Chris@16 2280 //!
Chris@16 2281 //! <b>Throws</b>: If the hash functor throws.
Chris@16 2282 //!
Chris@16 2283 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
Chris@16 2284 size_type bucket(const key_type& k) const
Chris@16 2285 { return this->bucket(k, this->priv_hasher()); }
Chris@16 2286
Chris@16 2287 //! <b>Requires</b>: "hash_func" must be a hash function that induces
Chris@16 2288 //! the same hash values as the stored hasher. The difference is that
Chris@16 2289 //! "hash_func" hashes the given key instead of the value_type.
Chris@16 2290 //!
Chris@16 2291 //! <b>Effects</b>: Returns the index of the bucket in which elements
Chris@16 2292 //! with keys equivalent to k would be found, if any such element existed.
Chris@16 2293 //!
Chris@16 2294 //! <b>Complexity</b>: Constant.
Chris@16 2295 //!
Chris@16 2296 //! <b>Throws</b>: If hash_func throws.
Chris@16 2297 //!
Chris@16 2298 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
Chris@16 2299 template<class KeyType, class KeyHasher>
Chris@16 2300 size_type bucket(const KeyType& k, const KeyHasher &hash_func) const
Chris@16 2301 { return this->priv_hash_to_bucket(hash_func(k)); }
Chris@16 2302
Chris@16 2303 //! <b>Effects</b>: Returns the bucket array pointer passed in the constructor
Chris@16 2304 //! or the last rehash function.
Chris@16 2305 //!
Chris@16 2306 //! <b>Complexity</b>: Constant.
Chris@16 2307 //!
Chris@16 2308 //! <b>Throws</b>: Nothing.
Chris@16 2309 bucket_ptr bucket_pointer() const
Chris@16 2310 { return this->priv_bucket_pointer(); }
Chris@16 2311
Chris@16 2312 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
Chris@16 2313 //!
Chris@16 2314 //! <b>Effects</b>: Returns a local_iterator pointing to the beginning
Chris@16 2315 //! of the sequence stored in the bucket n.
Chris@16 2316 //!
Chris@16 2317 //! <b>Complexity</b>: Constant.
Chris@16 2318 //!
Chris@16 2319 //! <b>Throws</b>: Nothing.
Chris@16 2320 //!
Chris@16 2321 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
Chris@16 2322 //! containing all of the elements in the nth bucket.
Chris@16 2323 local_iterator begin(size_type n)
Chris@16 2324 { return local_iterator(this->priv_bucket_pointer()[n].begin(), this->real_value_traits_ptr()); }
Chris@16 2325
Chris@16 2326 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
Chris@16 2327 //!
Chris@16 2328 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
Chris@16 2329 //! of the sequence stored in the bucket n.
Chris@16 2330 //!
Chris@16 2331 //! <b>Complexity</b>: Constant.
Chris@16 2332 //!
Chris@16 2333 //! <b>Throws</b>: Nothing.
Chris@16 2334 //!
Chris@16 2335 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
Chris@16 2336 //! containing all of the elements in the nth bucket.
Chris@16 2337 const_local_iterator begin(size_type n) const
Chris@16 2338 { return this->cbegin(n); }
Chris@16 2339
Chris@16 2340 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
Chris@16 2341 //!
Chris@16 2342 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
Chris@16 2343 //! of the sequence stored in the bucket n.
Chris@16 2344 //!
Chris@16 2345 //! <b>Complexity</b>: Constant.
Chris@16 2346 //!
Chris@16 2347 //! <b>Throws</b>: Nothing.
Chris@16 2348 //!
Chris@16 2349 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
Chris@16 2350 //! containing all of the elements in the nth bucket.
Chris@16 2351 const_local_iterator cbegin(size_type n) const
Chris@16 2352 {
Chris@16 2353 siterator sit = const_cast<bucket_type&>(this->priv_bucket_pointer()[n]).begin();
Chris@16 2354 return const_local_iterator(sit, this->real_value_traits_ptr());
Chris@16 2355 }
Chris@16 2356
Chris@16 2357 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
Chris@16 2358 //!
Chris@16 2359 //! <b>Effects</b>: Returns a local_iterator pointing to the end
Chris@16 2360 //! of the sequence stored in the bucket n.
Chris@16 2361 //!
Chris@16 2362 //! <b>Complexity</b>: Constant.
Chris@16 2363 //!
Chris@16 2364 //! <b>Throws</b>: Nothing.
Chris@16 2365 //!
Chris@16 2366 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
Chris@16 2367 //! containing all of the elements in the nth bucket.
Chris@16 2368 local_iterator end(size_type n)
Chris@16 2369 { return local_iterator(this->priv_bucket_pointer()[n].end(), this->real_value_traits_ptr()); }
Chris@16 2370
Chris@16 2371 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
Chris@16 2372 //!
Chris@16 2373 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
Chris@16 2374 //! of the sequence stored in the bucket n.
Chris@16 2375 //!
Chris@16 2376 //! <b>Complexity</b>: Constant.
Chris@16 2377 //!
Chris@16 2378 //! <b>Throws</b>: Nothing.
Chris@16 2379 //!
Chris@16 2380 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
Chris@16 2381 //! containing all of the elements in the nth bucket.
Chris@16 2382 const_local_iterator end(size_type n) const
Chris@16 2383 { return this->cend(n); }
Chris@16 2384
Chris@16 2385 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
Chris@16 2386 //!
Chris@16 2387 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
Chris@16 2388 //! of the sequence stored in the bucket n.
Chris@16 2389 //!
Chris@16 2390 //! <b>Complexity</b>: Constant.
Chris@16 2391 //!
Chris@16 2392 //! <b>Throws</b>: Nothing.
Chris@16 2393 //!
Chris@16 2394 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
Chris@16 2395 //! containing all of the elements in the nth bucket.
Chris@16 2396 const_local_iterator cend(size_type n) const
Chris@16 2397 {
Chris@16 2398 return const_local_iterator ( const_cast<bucket_type&>(this->priv_bucket_pointer()[n]).end()
Chris@16 2399 , this->real_value_traits_ptr());
Chris@16 2400 }
Chris@16 2401
Chris@16 2402 //! <b>Requires</b>: new_bucket_traits can hold a pointer to a new bucket array
Chris@16 2403 //! or the same as the old bucket array with a different length. new_size is the length of the
Chris@16 2404 //! the array pointed by new_buckets. If new_bucket_traits.bucket_begin() == this->bucket_pointer()
Chris@16 2405 //! new_bucket_traits.bucket_count() can be bigger or smaller than this->bucket_count().
Chris@16 2406 //! 'new_bucket_traits' copy constructor should not throw.
Chris@16 2407 //!
Chris@16 2408 //! <b>Effects</b>: Updates the internal reference with the new bucket, erases
Chris@16 2409 //! the values from the old bucket and inserts then in the new one.
Chris@16 2410 //! Bucket traits hold by *this is assigned from new_bucket_traits.
Chris@16 2411 //! If the container is configured as incremental<>, the split bucket is set
Chris@16 2412 //! to the new bucket_count().
Chris@16 2413 //!
Chris@16 2414 //! If store_hash option is true, this method does not use the hash function.
Chris@16 2415 //!
Chris@16 2416 //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
Chris@16 2417 //!
Chris@16 2418 //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
Chris@16 2419 void rehash(const bucket_traits &new_bucket_traits)
Chris@16 2420 {
Chris@16 2421 const bucket_ptr new_buckets = new_bucket_traits.bucket_begin();
Chris@16 2422 size_type new_bucket_count = new_bucket_traits.bucket_count();
Chris@16 2423 const bucket_ptr old_buckets = this->priv_bucket_pointer();
Chris@16 2424 size_type old_bucket_count = this->priv_bucket_count();
Chris@16 2425
Chris@16 2426 //Check power of two bucket array if the option is activated
Chris@16 2427 BOOST_INTRUSIVE_INVARIANT_ASSERT
Chris@16 2428 (!power_2_buckets || (0 == (new_bucket_count & (new_bucket_count-1u))));
Chris@16 2429
Chris@16 2430 size_type n = this->priv_get_cache_bucket_num();
Chris@16 2431 const bool same_buffer = old_buckets == new_buckets;
Chris@16 2432 //If the new bucket length is a common factor
Chris@16 2433 //of the old one we can avoid hash calculations.
Chris@16 2434 const bool fast_shrink = (!incremental) && (old_bucket_count > new_bucket_count) &&
Chris@16 2435 (power_2_buckets ||(old_bucket_count % new_bucket_count) == 0);
Chris@16 2436 //If we are shrinking the same bucket array and it's
Chris@16 2437 //is a fast shrink, just rehash the last nodes
Chris@16 2438 size_type new_first_bucket_num = new_bucket_count;
Chris@16 2439 if(same_buffer && fast_shrink && (n < new_bucket_count)){
Chris@16 2440 n = new_bucket_count;
Chris@16 2441 new_first_bucket_num = this->priv_get_cache_bucket_num();
Chris@16 2442 }
Chris@16 2443
Chris@16 2444 //Anti-exception stuff: they destroy the elements if something goes wrong.
Chris@16 2445 //If the source and destination buckets are the same, the second rollback function
Chris@16 2446 //is harmless, because all elements have been already unlinked and destroyed
Chris@16 2447 typedef detail::init_disposer<node_algorithms> NodeDisposer;
Chris@16 2448 NodeDisposer node_disp;
Chris@16 2449 bucket_type & newbuck = new_buckets[0];
Chris@16 2450 bucket_type & oldbuck = old_buckets[0];
Chris@16 2451 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
Chris@16 2452 rollback1(newbuck, node_disp, new_bucket_count);
Chris@16 2453 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
Chris@16 2454 rollback2(oldbuck, node_disp, old_bucket_count);
Chris@16 2455
Chris@16 2456 //Put size in a safe value for rollback exception
Chris@16 2457 size_type size_backup = this->priv_size_traits().get_size();
Chris@16 2458 this->priv_size_traits().set_size(0);
Chris@16 2459 //Put cache to safe position
Chris@16 2460 this->priv_initialize_cache();
Chris@16 2461 this->priv_insertion_update_cache(size_type(0u));
Chris@16 2462
Chris@16 2463 //Iterate through nodes
Chris@16 2464 for(; n < old_bucket_count; ++n){
Chris@16 2465 bucket_type &old_bucket = old_buckets[n];
Chris@16 2466
Chris@16 2467 if(!fast_shrink){
Chris@16 2468 siterator before_i(old_bucket.before_begin());
Chris@16 2469 siterator end_sit(old_bucket.end());
Chris@16 2470 siterator i(old_bucket.begin());
Chris@16 2471 for(;i != end_sit; ++i){
Chris@16 2472 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
Chris@16 2473 const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
Chris@16 2474 const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>(hash_value, new_bucket_count, new_bucket_count);
Chris@16 2475 if(cache_begin && new_n < new_first_bucket_num)
Chris@16 2476 new_first_bucket_num = new_n;
Chris@16 2477 siterator last = bucket_type::s_iterator_to
Chris@16 2478 (*group_functions_t::get_last_in_group
Chris@16 2479 (detail::dcast_bucket_ptr<node>(i.pointed_node()), optimize_multikey_t()));
Chris@16 2480 if(same_buffer && new_n == n){
Chris@16 2481 before_i = last;
Chris@16 2482 }
Chris@16 2483 else{
Chris@16 2484 bucket_type &new_b = new_buckets[new_n];
Chris@16 2485 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
Chris@16 2486 }
Chris@16 2487 i = before_i;
Chris@16 2488 }
Chris@16 2489 }
Chris@16 2490 else{
Chris@16 2491 const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>(n, new_bucket_count, new_bucket_count);
Chris@16 2492 if(cache_begin && new_n < new_first_bucket_num)
Chris@16 2493 new_first_bucket_num = new_n;
Chris@16 2494 bucket_type &new_b = new_buckets[new_n];
Chris@16 2495 if(!old_bucket.empty()){
Chris@16 2496 new_b.splice_after( new_b.before_begin()
Chris@16 2497 , old_bucket
Chris@16 2498 , old_bucket.before_begin()
Chris@16 2499 , hashtable_impl::priv_get_last(old_bucket));
Chris@16 2500 }
Chris@16 2501 }
Chris@16 2502 }
Chris@16 2503
Chris@16 2504 this->priv_size_traits().set_size(size_backup);
Chris@16 2505 this->priv_split_traits().set_size(new_bucket_count);
Chris@16 2506 this->priv_real_bucket_traits() = new_bucket_traits;
Chris@16 2507 this->priv_initialize_cache();
Chris@16 2508 this->priv_insertion_update_cache(new_first_bucket_num);
Chris@16 2509 rollback1.release();
Chris@16 2510 rollback2.release();
Chris@16 2511 }
Chris@16 2512
Chris@16 2513 //! <b>Requires</b>:
Chris@16 2514 //!
Chris@16 2515 //! <b>Effects</b>:
Chris@16 2516 //!
Chris@16 2517 //! <b>Complexity</b>:
Chris@16 2518 //!
Chris@16 2519 //! <b>Throws</b>:
Chris@16 2520 //!
Chris@16 2521 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
Chris@16 2522 bool incremental_rehash(bool grow = true)
Chris@16 2523 {
Chris@16 2524 //This function is only available for containers with incremental hashing
Chris@16 2525 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
Chris@16 2526 const size_type split_idx = this->priv_split_traits().get_size();
Chris@16 2527 const size_type bucket_cnt = this->priv_bucket_count();
Chris@16 2528 const bucket_ptr buck_ptr = this->priv_bucket_pointer();
Chris@16 2529
Chris@16 2530 if(grow){
Chris@16 2531 //Test if the split variable can be changed
Chris@16 2532 if(split_idx >= bucket_cnt)
Chris@16 2533 return false;
Chris@16 2534
Chris@16 2535 const size_type bucket_to_rehash = split_idx - bucket_cnt/2;
Chris@16 2536 bucket_type &old_bucket = buck_ptr[bucket_to_rehash];
Chris@16 2537 siterator before_i(old_bucket.before_begin());
Chris@16 2538 const siterator end_sit(old_bucket.end());
Chris@16 2539 siterator i(old_bucket.begin());
Chris@16 2540 this->priv_split_traits().increment();
Chris@16 2541
Chris@16 2542 //Anti-exception stuff: if an exception is thrown while
Chris@16 2543 //moving elements from old_bucket to the target bucket, all moved
Chris@16 2544 //elements are moved back to the original one.
Chris@16 2545 detail::incremental_rehash_rollback<bucket_type, split_traits> rollback
Chris@16 2546 ( buck_ptr[split_idx], old_bucket, this->priv_split_traits());
Chris@16 2547 for(;i != end_sit; ++i){
Chris@16 2548 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
Chris@16 2549 const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
Chris@16 2550 const size_type new_n = this->priv_hash_to_bucket(hash_value);
Chris@16 2551 siterator last = bucket_type::s_iterator_to
Chris@16 2552 (*group_functions_t::get_last_in_group
Chris@16 2553 (detail::dcast_bucket_ptr<node>(i.pointed_node()), optimize_multikey_t()));
Chris@16 2554 if(new_n == bucket_to_rehash){
Chris@16 2555 before_i = last;
Chris@16 2556 }
Chris@16 2557 else{
Chris@16 2558 bucket_type &new_b = buck_ptr[new_n];
Chris@16 2559 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
Chris@16 2560 }
Chris@16 2561 i = before_i;
Chris@16 2562 }
Chris@16 2563 rollback.release();
Chris@16 2564 this->priv_erasure_update_cache();
Chris@16 2565 return true;
Chris@16 2566 }
Chris@16 2567 else{
Chris@16 2568 //Test if the split variable can be changed
Chris@16 2569 if(split_idx <= bucket_cnt/2)
Chris@16 2570 return false;
Chris@16 2571 const size_type target_bucket_num = split_idx - 1 - bucket_cnt/2;
Chris@16 2572 bucket_type &target_bucket = buck_ptr[target_bucket_num];
Chris@16 2573 bucket_type &source_bucket = buck_ptr[split_idx-1];
Chris@16 2574 target_bucket.splice_after(target_bucket.cbefore_begin(), source_bucket);
Chris@16 2575 this->priv_split_traits().decrement();
Chris@16 2576 this->priv_insertion_update_cache(target_bucket_num);
Chris@16 2577 return true;
Chris@16 2578 }
Chris@16 2579 }
Chris@16 2580
Chris@16 2581 //! <b>Effects</b>: If new_bucket_traits.bucket_count() is not
Chris@16 2582 //! this->bucket_count()/2 or this->bucket_count()*2, or
Chris@16 2583 //! this->split_bucket() != new_bucket_traits.bucket_count() returns false
Chris@16 2584 //! and does nothing.
Chris@16 2585 //!
Chris@16 2586 //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits
Chris@16 2587 //! and transfers all the objects from old buckets to the new ones.
Chris@16 2588 //!
Chris@16 2589 //! <b>Complexity</b>: Linear to size().
Chris@16 2590 //!
Chris@16 2591 //! <b>Throws</b>: Nothing
Chris@16 2592 //!
Chris@16 2593 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
Chris@16 2594 bool incremental_rehash(const bucket_traits &new_bucket_traits)
Chris@16 2595 {
Chris@16 2596 //This function is only available for containers with incremental hashing
Chris@16 2597 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
Chris@16 2598 size_type new_bucket_traits_size = new_bucket_traits.bucket_count();
Chris@16 2599 size_type cur_bucket_traits = this->priv_bucket_count();
Chris@16 2600 if(new_bucket_traits_size/2 != cur_bucket_traits && new_bucket_traits_size != cur_bucket_traits/2){
Chris@16 2601 return false;
Chris@16 2602 }
Chris@16 2603
Chris@16 2604 const size_type split_idx = this->split_count();
Chris@16 2605
Chris@16 2606 if(new_bucket_traits_size/2 == cur_bucket_traits){
Chris@16 2607 //Test if the split variable can be changed
Chris@16 2608 if(!(split_idx >= cur_bucket_traits))
Chris@16 2609 return false;
Chris@16 2610 }
Chris@16 2611 else{
Chris@16 2612 //Test if the split variable can be changed
Chris@16 2613 if(!(split_idx <= cur_bucket_traits/2))
Chris@16 2614 return false;
Chris@16 2615 }
Chris@16 2616
Chris@16 2617 const size_type ini_n = this->priv_get_cache_bucket_num();
Chris@16 2618 const bucket_ptr old_buckets = this->priv_bucket_pointer();
Chris@16 2619 this->priv_real_bucket_traits() = new_bucket_traits;
Chris@16 2620 if(new_bucket_traits.bucket_begin() != old_buckets){
Chris@16 2621 for(size_type n = ini_n; n < split_idx; ++n){
Chris@16 2622 bucket_type &new_bucket = new_bucket_traits.bucket_begin()[n];
Chris@16 2623 bucket_type &old_bucket = old_buckets[n];
Chris@16 2624 new_bucket.splice_after(new_bucket.cbefore_begin(), old_bucket);
Chris@16 2625 }
Chris@16 2626 //Put cache to safe position
Chris@16 2627 this->priv_initialize_cache();
Chris@16 2628 this->priv_insertion_update_cache(ini_n);
Chris@16 2629 }
Chris@16 2630 return true;
Chris@16 2631 }
Chris@16 2632
Chris@16 2633 //! <b>Requires</b>:
Chris@16 2634 //!
Chris@16 2635 //! <b>Effects</b>:
Chris@16 2636 //!
Chris@16 2637 //! <b>Complexity</b>:
Chris@16 2638 //!
Chris@16 2639 //! <b>Throws</b>:
Chris@16 2640 size_type split_count() const
Chris@16 2641 {
Chris@16 2642 //This function is only available if incremental hashing is activated
Chris@16 2643 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
Chris@16 2644 return this->priv_split_traits().get_size();
Chris@16 2645 }
Chris@16 2646
Chris@16 2647 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
Chris@16 2648 //! the container that is bigger or equal than n. This suggestion can be
Chris@16 2649 //! used to create bucket arrays with a size that will usually improve
Chris@16 2650 //! container's performance. If such value does not exist, the
Chris@16 2651 //! higher possible value is returned.
Chris@16 2652 //!
Chris@16 2653 //! <b>Complexity</b>: Amortized constant time.
Chris@16 2654 //!
Chris@16 2655 //! <b>Throws</b>: Nothing.
Chris@16 2656 static size_type suggested_upper_bucket_count(size_type n)
Chris@16 2657 {
Chris@16 2658 const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0];
Chris@16 2659 const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size;
Chris@16 2660 std::size_t const* bound = std::lower_bound(primes, primes_end, n);
Chris@16 2661 if(bound == primes_end)
Chris@16 2662 --bound;
Chris@16 2663 return size_type(*bound);
Chris@16 2664 }
Chris@16 2665
Chris@16 2666 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
Chris@16 2667 //! the container that is smaller or equal than n. This suggestion can be
Chris@16 2668 //! used to create bucket arrays with a size that will usually improve
Chris@16 2669 //! container's performance. If such value does not exist, the
Chris@16 2670 //! lowest possible value is returned.
Chris@16 2671 //!
Chris@16 2672 //! <b>Complexity</b>: Amortized constant time.
Chris@16 2673 //!
Chris@16 2674 //! <b>Throws</b>: Nothing.
Chris@16 2675 static size_type suggested_lower_bucket_count(size_type n)
Chris@16 2676 {
Chris@16 2677 const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0];
Chris@16 2678 const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size;
Chris@16 2679 size_type const* bound = std::upper_bound(primes, primes_end, n);
Chris@16 2680 if(bound != primes)
Chris@16 2681 --bound;
Chris@16 2682 return size_type(*bound);
Chris@16 2683 }
Chris@16 2684
Chris@16 2685 /// @cond
Chris@16 2686 private:
Chris@16 2687
Chris@16 2688 void priv_clear_buckets(bucket_ptr buckets_ptr, size_type bucket_cnt)
Chris@16 2689 {
Chris@16 2690 for(; bucket_cnt--; ++buckets_ptr){
Chris@16 2691 if(safemode_or_autounlink){
Chris@16 2692 bucket_plus_vtraits_t::priv_clear_group_nodes(*buckets_ptr, optimize_multikey_t());
Chris@16 2693 buckets_ptr->clear_and_dispose(detail::init_disposer<node_algorithms>());
Chris@16 2694 }
Chris@16 2695 else{
Chris@16 2696 buckets_ptr->clear();
Chris@16 2697 }
Chris@16 2698 }
Chris@16 2699 this->priv_initialize_cache();
Chris@16 2700 }
Chris@16 2701
Chris@16 2702 void priv_initialize_buckets()
Chris@16 2703 { this->priv_clear_buckets(this->priv_bucket_pointer(), this->priv_bucket_count()); }
Chris@16 2704
Chris@16 2705 void priv_clear_buckets()
Chris@16 2706 {
Chris@16 2707 this->priv_clear_buckets
Chris@16 2708 ( this->priv_get_cache()
Chris@16 2709 , this->priv_bucket_count() - (this->priv_get_cache() - this->priv_bucket_pointer()));
Chris@16 2710 }
Chris@16 2711
Chris@16 2712 std::size_t priv_hash_to_bucket(std::size_t hash_value) const
Chris@16 2713 {
Chris@16 2714 return detail::hash_to_bucket_split<power_2_buckets, incremental>
Chris@16 2715 (hash_value, this->priv_real_bucket_traits().bucket_count(), this->priv_split_traits().get_size());
Chris@16 2716 }
Chris@16 2717
Chris@16 2718 template<class Disposer>
Chris@16 2719 void priv_erase_range_impl
Chris@16 2720 (size_type bucket_num, siterator before_first_it, siterator end_sit, Disposer disposer, size_type &num_erased)
Chris@16 2721 {
Chris@16 2722 const bucket_ptr buckets = this->priv_bucket_pointer();
Chris@16 2723 bucket_type &b = buckets[bucket_num];
Chris@16 2724
Chris@16 2725 if(before_first_it == b.before_begin() && end_sit == b.end()){
Chris@16 2726 this->priv_erase_range_impl(bucket_num, 1, disposer, num_erased);
Chris@16 2727 }
Chris@16 2728 else{
Chris@16 2729 num_erased = 0;
Chris@16 2730 siterator to_erase(before_first_it);
Chris@16 2731 ++to_erase;
Chris@16 2732 slist_node_ptr end_ptr = end_sit.pointed_node();
Chris@16 2733 while(to_erase != end_sit){
Chris@16 2734 group_functions_t::erase_from_group(end_ptr, detail::dcast_bucket_ptr<node>(to_erase.pointed_node()), optimize_multikey_t());
Chris@16 2735 to_erase = b.erase_after_and_dispose(before_first_it, make_node_disposer(disposer));
Chris@16 2736 ++num_erased;
Chris@16 2737 }
Chris@16 2738 this->priv_size_traits().set_size(this->priv_size_traits().get_size()-num_erased);
Chris@16 2739 }
Chris@16 2740 }
Chris@16 2741
Chris@16 2742 template<class Disposer>
Chris@16 2743 void priv_erase_range_impl
Chris@16 2744 (size_type first_bucket_num, size_type num_buckets, Disposer disposer, size_type &num_erased)
Chris@16 2745 {
Chris@16 2746 //Now fully clear the intermediate buckets
Chris@16 2747 const bucket_ptr buckets = this->priv_bucket_pointer();
Chris@16 2748 num_erased = 0;
Chris@16 2749 for(size_type i = first_bucket_num; i < (num_buckets + first_bucket_num); ++i){
Chris@16 2750 bucket_type &b = buckets[i];
Chris@16 2751 siterator b_begin(b.before_begin());
Chris@16 2752 siterator nxt(b_begin);
Chris@16 2753 ++nxt;
Chris@16 2754 siterator end_sit(b.end());
Chris@16 2755 while(nxt != end_sit){
Chris@16 2756 group_functions_t::init_group(detail::dcast_bucket_ptr<node>(nxt.pointed_node()), optimize_multikey_t());
Chris@16 2757 nxt = b.erase_after_and_dispose
Chris@16 2758 (b_begin, make_node_disposer(disposer));
Chris@16 2759 this->priv_size_traits().decrement();
Chris@16 2760 ++num_erased;
Chris@16 2761 }
Chris@16 2762 }
Chris@16 2763 }
Chris@16 2764
Chris@16 2765 template<class Disposer>
Chris@16 2766 void priv_erase_range( siterator before_first_it, size_type first_bucket
Chris@16 2767 , siterator last_it, size_type last_bucket
Chris@16 2768 , Disposer disposer)
Chris@16 2769 {
Chris@16 2770 size_type num_erased;
Chris@16 2771 if (first_bucket == last_bucket){
Chris@16 2772 this->priv_erase_range_impl(first_bucket, before_first_it, last_it, disposer, num_erased);
Chris@16 2773 }
Chris@16 2774 else {
Chris@16 2775 bucket_type *b = (&this->priv_bucket_pointer()[0]);
Chris@16 2776 this->priv_erase_range_impl(first_bucket, before_first_it, b[first_bucket].end(), disposer, num_erased);
Chris@16 2777 if(size_type n = (last_bucket - first_bucket - 1))
Chris@16 2778 this->priv_erase_range_impl(first_bucket + 1, n, disposer, num_erased);
Chris@16 2779 this->priv_erase_range_impl(last_bucket, b[last_bucket].before_begin(), last_it, disposer, num_erased);
Chris@16 2780 }
Chris@16 2781 }
Chris@16 2782
Chris@16 2783 std::size_t priv_get_bucket_num(siterator it)
Chris@16 2784 { return this->priv_get_bucket_num_hash_dispatch(it, store_hash_t()); }
Chris@16 2785
Chris@16 2786 std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) //store_hash
Chris@16 2787 {
Chris@16 2788 return this->priv_hash_to_bucket
Chris@16 2789 (this->priv_stored_hash(it.pointed_node(), store_hash_t()));
Chris@16 2790 }
Chris@16 2791
Chris@16 2792 std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) //NO store_hash
Chris@16 2793 { return this->priv_get_bucket_num_no_hash_store(it, optimize_multikey_t()); }
Chris@16 2794
Chris@16 2795 static siterator priv_get_previous(bucket_type &b, siterator i)
Chris@16 2796 { return bucket_plus_vtraits_t::priv_get_previous(b, i, optimize_multikey_t()); }
Chris@16 2797
Chris@16 2798 static siterator priv_get_last(bucket_type &b)
Chris@16 2799 { return bucket_plus_vtraits_t::priv_get_last(b, optimize_multikey_t()); }
Chris@16 2800
Chris@16 2801 template<class Disposer>
Chris@16 2802 void priv_erase(const_iterator i, Disposer disposer, detail::true_)
Chris@16 2803 {
Chris@16 2804 slist_node_ptr elem(i.slist_it().pointed_node());
Chris@16 2805 slist_node_ptr f_bucket_end, l_bucket_end;
Chris@16 2806 if(store_hash){
Chris@16 2807 f_bucket_end = l_bucket_end =
Chris@16 2808 (this->priv_bucket_pointer()
Chris@16 2809 [this->priv_hash_to_bucket
Chris@16 2810 (this->priv_stored_hash(elem, store_hash_t()))
Chris@16 2811 ]).before_begin().pointed_node();
Chris@16 2812 }
Chris@16 2813 else{
Chris@16 2814 f_bucket_end = this->priv_bucket_pointer()->cend().pointed_node();
Chris@16 2815 l_bucket_end = f_bucket_end + this->priv_bucket_count() - 1;
Chris@16 2816 }
Chris@16 2817 node_ptr nxt_in_group;
Chris@16 2818 siterator prev = bucket_type::s_iterator_to
Chris@16 2819 (*group_functions_t::get_previous_and_next_in_group
Chris@16 2820 ( elem, nxt_in_group, f_bucket_end, l_bucket_end)
Chris@16 2821 );
Chris@16 2822 bucket_type::s_erase_after_and_dispose(prev, make_node_disposer(disposer));
Chris@16 2823 if(nxt_in_group)
Chris@16 2824 group_algorithms::unlink_after(nxt_in_group);
Chris@16 2825 if(safemode_or_autounlink)
Chris@16 2826 group_algorithms::init(detail::dcast_bucket_ptr<node>(elem));
Chris@16 2827 }
Chris@16 2828
Chris@16 2829 template <class Disposer>
Chris@16 2830 void priv_erase(const_iterator i, Disposer disposer, detail::false_)
Chris@16 2831 {
Chris@16 2832 siterator to_erase(i.slist_it());
Chris@16 2833 bucket_type &b = this->priv_bucket_pointer()[this->priv_get_bucket_num(to_erase)];
Chris@16 2834 siterator prev(this->priv_get_previous(b, to_erase));
Chris@16 2835 b.erase_after_and_dispose(prev, make_node_disposer(disposer));
Chris@16 2836 }
Chris@16 2837
Chris@16 2838 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 2839 siterator priv_find
Chris@16 2840 ( const KeyType &key, KeyHasher hash_func
Chris@16 2841 , KeyValueEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const
Chris@16 2842 {
Chris@16 2843 h = hash_func(key);
Chris@16 2844 return this->priv_find_with_hash(key, equal_func, bucket_number, h, previt);
Chris@16 2845 }
Chris@16 2846
Chris@16 2847 template<class KeyType, class KeyValueEqual>
Chris@16 2848 siterator priv_find_with_hash
Chris@16 2849 ( const KeyType &key, KeyValueEqual equal_func, size_type &bucket_number, const std::size_t h, siterator &previt) const
Chris@16 2850 {
Chris@16 2851 bucket_number = this->priv_hash_to_bucket(h);
Chris@16 2852 bucket_type &b = this->priv_bucket_pointer()[bucket_number];
Chris@16 2853 previt = b.before_begin();
Chris@16 2854 if(constant_time_size && this->empty()){
Chris@16 2855 return this->priv_invalid_local_it();
Chris@16 2856 }
Chris@16 2857
Chris@16 2858 siterator it = previt;
Chris@16 2859 ++it;
Chris@16 2860
Chris@16 2861 while(it != b.end()){
Chris@16 2862 const value_type &v = this->priv_value_from_slist_node(it.pointed_node());
Chris@16 2863 if(compare_hash){
Chris@16 2864 std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t());
Chris@16 2865 if(h == vh && equal_func(key, v)){
Chris@16 2866 return it;
Chris@16 2867 }
Chris@16 2868 }
Chris@16 2869 else if(equal_func(key, v)){
Chris@16 2870 return it;
Chris@16 2871 }
Chris@16 2872 if(optimize_multikey){
Chris@16 2873 previt = bucket_type::s_iterator_to
Chris@16 2874 (*group_functions_t::get_last_in_group
Chris@16 2875 (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t()));
Chris@16 2876 it = previt;
Chris@16 2877 }
Chris@16 2878 else{
Chris@16 2879 previt = it;
Chris@16 2880 }
Chris@16 2881 ++it;
Chris@16 2882 }
Chris@16 2883 previt = b.before_begin();
Chris@16 2884 return this->priv_invalid_local_it();
Chris@16 2885 }
Chris@16 2886
Chris@16 2887 iterator priv_insert_equal_with_hash(reference value, std::size_t hash_value)
Chris@16 2888 {
Chris@16 2889 size_type bucket_num;
Chris@16 2890 siterator prev;
Chris@16 2891 siterator it = this->priv_find_with_hash
Chris@16 2892 (value, this->priv_equal(), bucket_num, hash_value, prev);
Chris@16 2893 return this->priv_insert_equal_find(value, bucket_num, hash_value, it);
Chris@16 2894 }
Chris@16 2895
Chris@16 2896 iterator priv_insert_equal_find(reference value, size_type bucket_num, std::size_t hash_value, siterator it)
Chris@16 2897 {
Chris@16 2898 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
Chris@16 2899 bool found_equal = it != this->priv_invalid_local_it();
Chris@16 2900 if(!found_equal){
Chris@16 2901 it = b.before_begin();
Chris@16 2902 }
Chris@16 2903 //Now store hash if needed
Chris@16 2904 node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
Chris@16 2905 node_functions_t::store_hash(n, hash_value, store_hash_t());
Chris@16 2906 //Checks for some modes
Chris@16 2907 if(safemode_or_autounlink)
Chris@16 2908 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n));
Chris@16 2909 //Shortcut for optimize_multikey cases
Chris@16 2910 if(optimize_multikey){
Chris@16 2911 node_ptr first_in_group = found_equal ?
Chris@16 2912 detail::dcast_bucket_ptr<node>(it.pointed_node()) : node_ptr();
Chris@16 2913 group_functions_t::insert_in_group(first_in_group, n, optimize_multikey_t());
Chris@16 2914 }
Chris@16 2915 //Update cache and increment size if needed
Chris@16 2916 this->priv_insertion_update_cache(bucket_num);
Chris@16 2917 this->priv_size_traits().increment();
Chris@16 2918 //Insert the element in the bucket after it
Chris@16 2919 return iterator(b.insert_after(it, *n), &this->get_bucket_value_traits());
Chris@16 2920 }
Chris@16 2921
Chris@16 2922 template<class KeyType, class KeyHasher, class KeyValueEqual>
Chris@16 2923 std::pair<siterator, siterator> priv_equal_range
Chris@16 2924 ( const KeyType &key
Chris@16 2925 , KeyHasher hash_func
Chris@16 2926 , KeyValueEqual equal_func
Chris@16 2927 , size_type &bucket_number_first
Chris@16 2928 , size_type &bucket_number_second
Chris@16 2929 , size_type &cnt) const
Chris@16 2930 {
Chris@16 2931 std::size_t h;
Chris@16 2932 cnt = 0;
Chris@16 2933 siterator prev;
Chris@16 2934 //Let's see if the element is present
Chris@16 2935 std::pair<siterator, siterator> to_return
Chris@16 2936 ( this->priv_find(key, hash_func, equal_func, bucket_number_first, h, prev)
Chris@16 2937 , this->priv_invalid_local_it());
Chris@16 2938 if(to_return.first == to_return.second){
Chris@16 2939 bucket_number_second = bucket_number_first;
Chris@16 2940 return to_return;
Chris@16 2941 }
Chris@16 2942 {
Chris@16 2943 //If it's present, find the first that it's not equal in
Chris@16 2944 //the same bucket
Chris@16 2945 bucket_type &b = this->priv_bucket_pointer()[bucket_number_first];
Chris@16 2946 siterator it = to_return.first;
Chris@16 2947 if(optimize_multikey){
Chris@16 2948 to_return.second = bucket_type::s_iterator_to
Chris@16 2949 (*node_traits::get_next(group_functions_t::get_last_in_group
Chris@16 2950 (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t())));
Chris@16 2951 cnt = std::distance(it, to_return.second);
Chris@16 2952 if(to_return.second != b.end()){
Chris@16 2953 bucket_number_second = bucket_number_first;
Chris@16 2954 return to_return;
Chris@16 2955 }
Chris@16 2956 }
Chris@16 2957 else{
Chris@16 2958 ++cnt;
Chris@16 2959 ++it;
Chris@16 2960 while(it != b.end()){
Chris@16 2961 const value_type &v = this->priv_value_from_slist_node(it.pointed_node());
Chris@16 2962 if(compare_hash){
Chris@16 2963 std::size_t hv = this->priv_stored_or_compute_hash(v, store_hash_t());
Chris@16 2964 if(hv != h || !equal_func(key, v)){
Chris@16 2965 to_return.second = it;
Chris@16 2966 bucket_number_second = bucket_number_first;
Chris@16 2967 return to_return;
Chris@16 2968 }
Chris@16 2969 }
Chris@16 2970 else if(!equal_func(key, v)){
Chris@16 2971 to_return.second = it;
Chris@16 2972 bucket_number_second = bucket_number_first;
Chris@16 2973 return to_return;
Chris@16 2974 }
Chris@16 2975 ++it;
Chris@16 2976 ++cnt;
Chris@16 2977 }
Chris@16 2978 }
Chris@16 2979 }
Chris@16 2980
Chris@16 2981 //If we reached the end, find the first, non-empty bucket
Chris@16 2982 for(bucket_number_second = bucket_number_first+1
Chris@16 2983 ; bucket_number_second != this->priv_bucket_count()
Chris@16 2984 ; ++bucket_number_second){
Chris@16 2985 bucket_type &b = this->priv_bucket_pointer()[bucket_number_second];
Chris@16 2986 if(!b.empty()){
Chris@16 2987 to_return.second = b.begin();
Chris@16 2988 return to_return;
Chris@16 2989 }
Chris@16 2990 }
Chris@16 2991
Chris@16 2992 //Otherwise, return the end node
Chris@16 2993 to_return.second = this->priv_invalid_local_it();
Chris@16 2994 return to_return;
Chris@16 2995 }
Chris@16 2996 /// @endcond
Chris@16 2997 };
Chris@16 2998
Chris@16 2999 /// @cond
Chris@16 3000 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
Chris@16 3001 template < class T
Chris@16 3002 , bool UniqueKeys
Chris@16 3003 , class PackedOptions
Chris@16 3004 >
Chris@16 3005 #else
Chris@16 3006 template <class T, bool UniqueKeys, class ...Options>
Chris@16 3007 #endif
Chris@16 3008 struct make_real_bucket_traits
Chris@16 3009 {
Chris@16 3010 //Real value traits must be calculated from options
Chris@16 3011 typedef typename detail::get_value_traits
Chris@16 3012 <T, typename PackedOptions::proto_value_traits>::type value_traits;
Chris@16 3013 /*
Chris@16 3014 static const bool resizable_bucket_traits =
Chris@16 3015 detail::resizable_bool_is_true<bucket_traits_traits>::value;*/
Chris@16 3016 typedef typename detail::get_real_value_traits<value_traits>::type real_value_traits;
Chris@16 3017 typedef typename PackedOptions::bucket_traits specified_bucket_traits;
Chris@16 3018
Chris@16 3019 //Real bucket traits must be calculated from options and calculated value_traits
Chris@16 3020 typedef typename detail::get_slist_impl
Chris@16 3021 <typename detail::reduced_slist_node_traits
Chris@16 3022 <typename real_value_traits::node_traits>::type
Chris@16 3023 >::type slist_impl;
Chris@16 3024
Chris@16 3025 typedef typename
Chris@16 3026 detail::if_c< detail::is_same
Chris@16 3027 < specified_bucket_traits
Chris@16 3028 , default_bucket_traits
Chris@16 3029 >::value
Chris@16 3030 , detail::bucket_traits_impl<slist_impl>
Chris@16 3031 , specified_bucket_traits
Chris@16 3032 >::type type;
Chris@16 3033 };
Chris@16 3034 /// @endcond
Chris@16 3035
Chris@16 3036 //! Helper metafunction to define a \c hashtable that yields to the same type when the
Chris@16 3037 //! same options (either explicitly or implicitly) are used.
Chris@16 3038 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
Chris@16 3039 template<class T, class ...Options>
Chris@16 3040 #else
Chris@16 3041 template<class T, class O1 = void, class O2 = void
Chris@16 3042 , class O3 = void, class O4 = void
Chris@16 3043 , class O5 = void, class O6 = void
Chris@16 3044 , class O7 = void, class O8 = void
Chris@16 3045 , class O9 = void, class O10= void
Chris@16 3046 >
Chris@16 3047 #endif
Chris@16 3048 struct make_hashtable
Chris@16 3049 {
Chris@16 3050 /// @cond
Chris@16 3051 typedef typename pack_options
Chris@16 3052 < hashtable_defaults,
Chris@16 3053 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
Chris@16 3054 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
Chris@16 3055 #else
Chris@16 3056 Options...
Chris@16 3057 #endif
Chris@16 3058 >::type packed_options;
Chris@16 3059
Chris@16 3060 typedef typename detail::get_value_traits
Chris@16 3061 <T, typename packed_options::proto_value_traits>::type value_traits;
Chris@16 3062
Chris@16 3063 typedef typename make_real_bucket_traits
Chris@16 3064 <T, false, packed_options>::type real_bucket_traits;
Chris@16 3065
Chris@16 3066 typedef hashtable_impl
Chris@16 3067 < value_traits
Chris@16 3068 , typename packed_options::hash
Chris@16 3069 , typename packed_options::equal
Chris@16 3070 , typename packed_options::size_type
Chris@16 3071 , real_bucket_traits
Chris@16 3072 , (std::size_t(false)*hash_bool_flags::unique_keys_pos)
Chris@16 3073 | (std::size_t(packed_options::constant_time_size)*hash_bool_flags::constant_time_size_pos)
Chris@16 3074 | (std::size_t(packed_options::power_2_buckets)*hash_bool_flags::power_2_buckets_pos)
Chris@16 3075 | (std::size_t(packed_options::cache_begin)*hash_bool_flags::cache_begin_pos)
Chris@16 3076 | (std::size_t(packed_options::compare_hash)*hash_bool_flags::compare_hash_pos)
Chris@16 3077 | (std::size_t(packed_options::incremental)*hash_bool_flags::incremental_pos)
Chris@16 3078 > implementation_defined;
Chris@16 3079
Chris@16 3080 /// @endcond
Chris@16 3081 typedef implementation_defined type;
Chris@16 3082 };
Chris@16 3083
Chris@16 3084 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
Chris@16 3085
Chris@16 3086 #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
Chris@16 3087 template<class T, class ...Options>
Chris@16 3088 #else
Chris@16 3089 template<class T, class O1, class O2, class O3, class O4, class O5, class O6, class O7, class O8, class O9, class O10>
Chris@16 3090 #endif
Chris@16 3091 class hashtable
Chris@16 3092 : public make_hashtable<T,
Chris@16 3093 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
Chris@16 3094 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
Chris@16 3095 #else
Chris@16 3096 Options...
Chris@16 3097 #endif
Chris@16 3098 >::type
Chris@16 3099 {
Chris@16 3100 typedef typename make_hashtable<T,
Chris@16 3101 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
Chris@16 3102 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
Chris@16 3103 #else
Chris@16 3104 Options...
Chris@16 3105 #endif
Chris@16 3106 >::type Base;
Chris@16 3107 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable)
Chris@16 3108
Chris@16 3109 public:
Chris@16 3110 typedef typename Base::value_traits value_traits;
Chris@16 3111 typedef typename Base::real_value_traits real_value_traits;
Chris@16 3112 typedef typename Base::iterator iterator;
Chris@16 3113 typedef typename Base::const_iterator const_iterator;
Chris@16 3114 typedef typename Base::bucket_ptr bucket_ptr;
Chris@16 3115 typedef typename Base::size_type size_type;
Chris@16 3116 typedef typename Base::hasher hasher;
Chris@16 3117 typedef typename Base::bucket_traits bucket_traits;
Chris@16 3118 typedef typename Base::key_equal key_equal;
Chris@16 3119
Chris@16 3120 //Assert if passed value traits are compatible with the type
Chris@16 3121 BOOST_STATIC_ASSERT((detail::is_same<typename real_value_traits::value_type, T>::value));
Chris@16 3122
Chris@16 3123 explicit hashtable ( const bucket_traits &b_traits
Chris@16 3124 , const hasher & hash_func = hasher()
Chris@16 3125 , const key_equal &equal_func = key_equal()
Chris@16 3126 , const value_traits &v_traits = value_traits())
Chris@16 3127 : Base(b_traits, hash_func, equal_func, v_traits)
Chris@16 3128 {}
Chris@16 3129
Chris@16 3130 hashtable(BOOST_RV_REF(hashtable) x)
Chris@16 3131 : Base(::boost::move(static_cast<Base&>(x)))
Chris@16 3132 {}
Chris@16 3133
Chris@16 3134 hashtable& operator=(BOOST_RV_REF(hashtable) x)
Chris@16 3135 { return static_cast<hashtable&>(this->Base::operator=(::boost::move(static_cast<Base&>(x)))); }
Chris@16 3136 };
Chris@16 3137
Chris@16 3138 #endif
Chris@16 3139
Chris@16 3140 } //namespace intrusive
Chris@16 3141 } //namespace boost
Chris@16 3142
Chris@16 3143 #include <boost/intrusive/detail/config_end.hpp>
Chris@16 3144
Chris@16 3145 #endif //BOOST_INTRUSIVE_HASHTABLE_HPP