Files
boost_intrusive/include/boost/intrusive/detail/rbtree_node.hpp

301 lines
8.8 KiB
C++
Raw Normal View History

2007-05-04 21:22:02 +00:00
/////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Olaf Krzikalla 2004-2006.
// (C) Copyright Ion Gaztanaga 2006-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_RBTREE_NODE_HPP
#define BOOST_INTRUSIVE_RBTREE_NODE_HPP
#include <boost/intrusive/detail/config_begin.hpp>
#include <iterator>
#include <boost/intrusive/detail/pointer_to_other.hpp>
#include <boost/intrusive/rbtree_algorithms.hpp>
#include <boost/intrusive/pointer_plus_bit.hpp>
2007-06-23 13:01:38 +00:00
#include <boost/intrusive/detail/mpl.hpp>
2007-05-04 21:22:02 +00:00
namespace boost {
namespace intrusive {
/////////////////////////////////////////////////////////////////////////////
// //
// Generic node_traits for any pointer type //
// //
/////////////////////////////////////////////////////////////////////////////
//This is the compact representation: 3 pointers
template<class VoidPointer>
struct compact_rbtree_node
{
typedef typename pointer_to_other
<VoidPointer
,compact_rbtree_node<VoidPointer> >::type node_ptr;
enum color { red_t, black_t };
node_ptr parent_, left_, right_;
};
//This is the normal representation: 3 pointers + enum
template<class VoidPointer>
struct rbtree_node
{
typedef typename pointer_to_other
<VoidPointer
,rbtree_node<VoidPointer> >::type node_ptr;
enum color { red_t, black_t };
node_ptr parent_, left_, right_;
color color_;
};
//This is the default node traits implementation
//using a node with 3 generic pointers plus an enum
template<class VoidPointer>
struct default_rbtree_node_traits_impl
{
typedef rbtree_node<VoidPointer> node;
typedef typename boost::pointer_to_other
<VoidPointer, node>::type node_ptr;
typedef typename boost::pointer_to_other
<VoidPointer, const node>::type const_node_ptr;
typedef typename node::color color;
static node_ptr get_parent(const_node_ptr n)
{ return n->parent_; }
static void set_parent(node_ptr n, node_ptr p)
{ n->parent_ = p; }
static node_ptr get_left(const_node_ptr n)
{ return n->left_; }
static void set_left(node_ptr n, node_ptr l)
{ n->left_ = l; }
static node_ptr get_right(const_node_ptr n)
{ return n->right_; }
static void set_right(node_ptr n, node_ptr r)
{ n->right_ = r; }
static color get_color(const_node_ptr n)
{ return n->color_; }
static void set_color(node_ptr n, color c)
{ n->color_ = c; }
static color black()
{ return node::black_t; }
static color red()
{ return node::red_t; }
};
//This is the compact node traits implementation
//using a node with 3 generic pointers
template<class VoidPointer>
struct compact_rbtree_node_traits_impl
{
typedef compact_rbtree_node<VoidPointer> node;
typedef typename boost::pointer_to_other
<VoidPointer, node>::type node_ptr;
typedef typename boost::pointer_to_other
<VoidPointer, const node>::type const_node_ptr;
typedef pointer_plus_bit<node_ptr> ptr_bit;
typedef typename node::color color;
static node_ptr get_parent(const_node_ptr n)
{ return ptr_bit::get_pointer(n->parent_); }
static void set_parent(node_ptr n, node_ptr p)
{ ptr_bit::set_pointer(n->parent_, p); }
static node_ptr get_left(const_node_ptr n)
{ return n->left_; }
static void set_left(node_ptr n, node_ptr l)
{ n->left_ = l; }
static node_ptr get_right(const_node_ptr n)
{ return n->right_; }
static void set_right(node_ptr n, node_ptr r)
{ n->right_ = r; }
static color get_color(const_node_ptr n)
{ return (color)ptr_bit::get_bit(n->parent_); }
static void set_color(node_ptr n, color c)
{ ptr_bit::set_bit(n->parent_, c != 0); }
static color black()
{ return node::black_t; }
static color red()
{ return node::red_t; }
};
//Dispatches the implementation based on the boolean
template<class VoidPointer, bool compact>
struct rbtree_node_traits_dispatch
: public default_rbtree_node_traits_impl<VoidPointer>
{};
template<class VoidPointer>
struct rbtree_node_traits_dispatch<VoidPointer, true>
: public compact_rbtree_node_traits_impl<VoidPointer>
{};
//Inherit from the detail::link_dispatch depending on the embedding capabilities
2007-05-04 21:22:02 +00:00
template<class VoidPointer>
struct rbtree_node_traits
: public rbtree_node_traits_dispatch
< VoidPointer
, has_pointer_plus_bit
< VoidPointer
, detail::alignment_of<compact_rbtree_node<VoidPointer> >::value
2007-05-04 21:22:02 +00:00
>::value
>
{};
/////////////////////////////////////////////////////////////////////////////
// //
// Implementation of the rbtree iterator //
// //
/////////////////////////////////////////////////////////////////////////////
// rbtree_iterator provides some basic functions for a
// node oriented bidirectional iterator:
template<class Container, bool IsConst>
class rbtree_iterator
: public std::iterator
< std::bidirectional_iterator_tag
, typename detail::add_const_if_c
<typename Container::value_type, IsConst>::type
>
{
protected:
typedef typename Container::real_value_traits real_value_traits;
typedef typename real_value_traits::node_traits node_traits;
typedef typename node_traits::node node;
typedef typename node_traits::node_ptr node_ptr;
typedef rbtree_algorithms<node_traits> node_algorithms;
typedef typename boost::pointer_to_other
<node_ptr, void>::type void_pointer;
static const bool store_container_ptr =
detail::store_cont_ptr_on_it<Container>::value;
public:
public:
typedef typename detail::add_const_if_c
<typename Container::value_type, IsConst>
::type value_type;
typedef value_type & reference;
typedef value_type * pointer;
rbtree_iterator()
: members_ (0, 0)
{}
explicit rbtree_iterator(node_ptr node, const Container *cont_ptr)
: members_ (node, cont_ptr)
{}
rbtree_iterator(rbtree_iterator<Container, false> const& other)
: members_(other.pointed_node(), other.get_container())
{}
const node_ptr &pointed_node() const
{ return members_.nodeptr_; }
rbtree_iterator &operator=(const node_ptr &node)
{ members_.nodeptr_ = node; return static_cast<rbtree_iterator&>(*this); }
public:
rbtree_iterator& operator++()
{
members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
return static_cast<rbtree_iterator&> (*this);
}
rbtree_iterator operator++(int)
{
rbtree_iterator result (*this);
members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
return result;
}
rbtree_iterator& operator--()
{
members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
return static_cast<rbtree_iterator&> (*this);
}
rbtree_iterator operator--(int)
{
rbtree_iterator result (*this);
members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
return result;
}
bool operator== (const rbtree_iterator& i) const
{ return members_.nodeptr_ == i.pointed_node(); }
bool operator!= (const rbtree_iterator& i) const
{ return !operator== (i); }
value_type& operator*() const
{ return *operator->(); }
pointer operator->() const
{ return detail::get_pointer(this->get_real_value_traits()->to_value_ptr(members_.nodeptr_)); }
const Container *get_container() const
{
if(store_container_ptr)
return static_cast<const Container*>(members_.get_ptr());
else
return 0;
}
const real_value_traits *get_real_value_traits() const
{
if(store_container_ptr)
return &this->get_container()->get_real_value_traits();
else
return 0;
}
private:
struct members
: public detail::select_constptr
<void_pointer, store_container_ptr>::type
{
typedef typename detail::select_constptr
<void_pointer, store_container_ptr>::type Base;
members(const node_ptr &n_ptr, const void *cont)
: Base(cont), nodeptr_(n_ptr)
{}
node_ptr nodeptr_;
} members_;
};
2007-05-04 21:22:02 +00:00
} //namespace intrusive
} //namespace boost
#include <boost/intrusive/detail/config_end.hpp>
#endif //BOOST_INTRUSIVE_RBTREE_NODE_HPP