2007-05-04 21:22:02 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2007-05-12 12:54:15 +00:00
|
|
|
// (C) Copyright Ion Gaztanaga 2007
|
2007-05-04 21:22:02 +00:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
// See http://www.boost.org/libs/intrusive for documentation.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef BOOST_INTRUSIVE_HASHTABLE_NODE_HPP
|
|
|
|
#define BOOST_INTRUSIVE_HASHTABLE_NODE_HPP
|
|
|
|
|
|
|
|
#include <boost/intrusive/detail/config_begin.hpp>
|
|
|
|
#include <iterator>
|
2007-06-23 13:01:38 +00:00
|
|
|
#include <boost/intrusive/detail/assert.hpp>
|
2007-05-04 21:22:02 +00:00
|
|
|
#include <boost/intrusive/detail/pointer_to_other.hpp>
|
|
|
|
#include <boost/intrusive/circular_list_algorithms.hpp>
|
2007-06-23 13:01:38 +00:00
|
|
|
#include <boost/intrusive/detail/mpl.hpp>
|
2007-09-26 15:26:35 +00:00
|
|
|
#include <boost/intrusive/detail/utilities.hpp>
|
|
|
|
#include <boost/intrusive/detail/slist_node.hpp> //remove-me
|
2007-05-04 21:22:02 +00:00
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
namespace intrusive {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<int Dummy = 0>
|
|
|
|
struct prime_list_holder
|
|
|
|
{
|
|
|
|
static const std::size_t prime_list[];
|
|
|
|
static const std::size_t prime_list_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<int Dummy>
|
|
|
|
const std::size_t prime_list_holder<Dummy>::prime_list[] = {
|
|
|
|
53ul, 97ul, 193ul, 389ul, 769ul,
|
|
|
|
1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
|
|
|
|
49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
|
|
|
|
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
|
|
|
|
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
|
|
|
|
1610612741ul, 3221225473ul, 4294967291ul };
|
|
|
|
|
|
|
|
template<int Dummy>
|
|
|
|
const std::size_t prime_list_holder<Dummy>::prime_list_size
|
|
|
|
= sizeof(prime_list)/sizeof(std::size_t);
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
template <class Slist>
|
|
|
|
struct bucket_impl : public Slist
|
2007-05-04 21:22:02 +00:00
|
|
|
{
|
2007-09-26 15:26:35 +00:00
|
|
|
bucket_impl()
|
2007-05-04 21:22:02 +00:00
|
|
|
{}
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
bucket_impl(const bucket_impl &)
|
2007-05-04 21:22:02 +00:00
|
|
|
{}
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
~bucket_impl()
|
|
|
|
{
|
|
|
|
//This bucket is still being used!
|
|
|
|
BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket_impl &operator=(const bucket_impl&)
|
|
|
|
{
|
|
|
|
//This bucket is still in use!
|
|
|
|
BOOST_INTRUSIVE_INVARIANT_ASSERT(Slist::empty());
|
|
|
|
//Slist::clear();
|
|
|
|
}
|
2007-05-04 21:22:02 +00:00
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
static typename Slist::difference_type get_bucket_num
|
|
|
|
( typename Slist::const_iterator it
|
|
|
|
, const bucket_impl<Slist> &first_bucket
|
|
|
|
, const bucket_impl<Slist> &last_bucket)
|
2007-05-04 21:22:02 +00:00
|
|
|
{
|
2007-09-26 15:26:35 +00:00
|
|
|
typename Slist::const_iterator
|
2007-05-04 21:22:02 +00:00
|
|
|
first(first_bucket.cend()), last(last_bucket.cend());
|
|
|
|
|
|
|
|
//The end node is embedded in the singly linked list:
|
|
|
|
//iterate until we reach it.
|
|
|
|
while(!(first.pointed_node() <= it.pointed_node() &&
|
|
|
|
it.pointed_node() <= last.pointed_node())){
|
|
|
|
++it;
|
|
|
|
}
|
2007-09-26 15:26:35 +00:00
|
|
|
//Now get the bucket_impl from the iterator
|
|
|
|
const bucket_impl &b = static_cast<const bucket_impl&>
|
|
|
|
(Slist::container_from_end_iterator(it));
|
2007-05-04 21:22:02 +00:00
|
|
|
|
|
|
|
//Now just calculate the index b has in the bucket array
|
|
|
|
return &b - &first_bucket;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
template<class Slist>
|
|
|
|
struct bucket_traits_impl
|
2007-05-04 21:22:02 +00:00
|
|
|
{
|
2007-09-26 15:26:35 +00:00
|
|
|
/// @cond
|
2007-05-04 21:22:02 +00:00
|
|
|
typedef typename boost::pointer_to_other
|
2007-09-26 15:26:35 +00:00
|
|
|
< typename Slist::pointer, bucket_impl<Slist> >::type bucket_ptr;
|
|
|
|
typedef typename Slist::size_type size_type;
|
|
|
|
/// @endcond
|
2007-05-04 21:22:02 +00:00
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
bucket_traits_impl(bucket_ptr buckets, size_type len)
|
|
|
|
: buckets_(buckets), buckets_len_(len)
|
2007-05-23 16:07:03 +00:00
|
|
|
{}
|
2007-05-04 21:22:02 +00:00
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
bucket_ptr bucket_begin() const
|
|
|
|
{ return buckets_; }
|
2007-05-04 21:22:02 +00:00
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
size_type bucket_count() const
|
|
|
|
{ return buckets_len_; }
|
2007-05-04 21:22:02 +00:00
|
|
|
|
|
|
|
private:
|
2007-09-26 15:26:35 +00:00
|
|
|
bucket_ptr buckets_;
|
|
|
|
size_type buckets_len_;
|
2007-05-04 21:22:02 +00:00
|
|
|
};
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
template<class Container, bool IsConst>
|
2007-05-23 16:07:03 +00:00
|
|
|
class hashtable_iterator
|
2007-09-26 15:26:35 +00:00
|
|
|
: public std::iterator
|
|
|
|
< std::forward_iterator_tag
|
|
|
|
, typename detail::add_const_if_c
|
|
|
|
<typename Container::value_type, IsConst>::type
|
|
|
|
>
|
2007-05-23 16:07:03 +00:00
|
|
|
{
|
2007-09-26 15:26:35 +00:00
|
|
|
typedef typename Container::real_value_traits real_value_traits;
|
|
|
|
typedef typename Container::siterator siterator;
|
|
|
|
typedef typename Container::const_siterator const_siterator;
|
|
|
|
typedef typename Container::bucket_type bucket_type;
|
2007-05-23 16:07:03 +00:00
|
|
|
typedef typename boost::pointer_to_other
|
2007-09-26 15:26:35 +00:00
|
|
|
< typename Container::pointer, const Container>::type const_cont_ptr;
|
|
|
|
typedef typename Container::size_type size_type;
|
2007-05-23 16:07:03 +00:00
|
|
|
|
|
|
|
public:
|
2007-09-26 15:26:35 +00:00
|
|
|
typedef typename detail::add_const_if_c
|
|
|
|
<typename Container::value_type, IsConst>::type value_type;
|
2007-05-23 16:07:03 +00:00
|
|
|
|
|
|
|
hashtable_iterator ()
|
|
|
|
{}
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
explicit hashtable_iterator(siterator ptr, const Container *cont)
|
|
|
|
: slist_it_ (ptr), cont_ (cont)
|
2007-05-23 16:07:03 +00:00
|
|
|
{}
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
hashtable_iterator(const hashtable_iterator<Container, false> &other)
|
|
|
|
: slist_it_(other.slist_it()), cont_(other.get_container())
|
2007-05-23 16:07:03 +00:00
|
|
|
{}
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
const siterator &slist_it() const
|
|
|
|
{ return slist_it_; }
|
2007-05-23 16:07:03 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
hashtable_iterator& operator++()
|
2007-09-26 15:26:35 +00:00
|
|
|
{ this->increment(); return *this; }
|
2007-05-23 16:07:03 +00:00
|
|
|
|
|
|
|
hashtable_iterator operator++(int)
|
|
|
|
{
|
|
|
|
hashtable_iterator result (*this);
|
2007-09-26 15:26:35 +00:00
|
|
|
this->increment();
|
2007-05-23 16:07:03 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator== (const hashtable_iterator& i, const hashtable_iterator& i2)
|
2007-09-26 15:26:35 +00:00
|
|
|
{ return i.slist_it_ == i2.slist_it_; }
|
2007-05-23 16:07:03 +00:00
|
|
|
|
|
|
|
friend bool operator!= (const hashtable_iterator& i, const hashtable_iterator& i2)
|
|
|
|
{ return !(i == i2); }
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
value_type& operator*() const
|
|
|
|
{ return *this->operator ->(); }
|
2007-05-23 16:07:03 +00:00
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
value_type* operator->() const
|
|
|
|
{ return detail::get_pointer(this->get_real_value_traits()->to_value_ptr(slist_it_.pointed_node())); }
|
|
|
|
|
|
|
|
const Container *get_container() const
|
|
|
|
{ return detail::get_pointer(cont_); }
|
|
|
|
|
|
|
|
const real_value_traits *get_real_value_traits() const
|
|
|
|
{ return &this->get_container()->get_real_value_traits(); }
|
2007-05-23 16:07:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void increment()
|
|
|
|
{
|
2007-09-26 15:26:35 +00:00
|
|
|
const Container *cont = detail::get_pointer(cont_);
|
|
|
|
bucket_type* buckets = detail::get_pointer(cont->bucket_pointer());
|
|
|
|
size_type buckets_len = cont->bucket_count();
|
|
|
|
const_siterator first(buckets[0].cend());
|
|
|
|
const_siterator last (buckets[buckets_len].cend());
|
|
|
|
|
|
|
|
++slist_it_;
|
|
|
|
if(first.pointed_node() <= slist_it_.pointed_node() &&
|
|
|
|
slist_it_.pointed_node()<= last.pointed_node() ){
|
2007-05-23 16:07:03 +00:00
|
|
|
size_type n_bucket = (size_type)
|
2007-09-26 15:26:35 +00:00
|
|
|
bucket_type::get_bucket_num(slist_it_, buckets[0], buckets[buckets_len]);
|
2007-05-23 16:07:03 +00:00
|
|
|
do{
|
|
|
|
if (++n_bucket == buckets_len){
|
2007-09-26 15:26:35 +00:00
|
|
|
slist_it_ = buckets->end();
|
2007-05-23 16:07:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-09-26 15:26:35 +00:00
|
|
|
slist_it_ = buckets[n_bucket].begin();
|
2007-05-23 16:07:03 +00:00
|
|
|
}
|
2007-09-26 15:26:35 +00:00
|
|
|
while (slist_it_ == buckets[n_bucket].end());
|
2007-05-23 16:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-26 15:26:35 +00:00
|
|
|
siterator slist_it_;
|
|
|
|
const_cont_ptr cont_;
|
2007-05-23 16:07:03 +00:00
|
|
|
};
|
|
|
|
|
2007-05-04 21:22:02 +00:00
|
|
|
} //namespace detail {
|
|
|
|
} //namespace intrusive {
|
|
|
|
} //namespace boost {
|
|
|
|
|
|
|
|
#endif
|