forked from boostorg/intrusive
Added avl trees
[SVN r40598]
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2006-2007
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//[doc_avl_set_code
|
||||
#include <boost/intrusive/avl_set.hpp>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//This is a base hook optimized for size
|
||||
class MyClass : public avl_set_base_hook<optimize_size<true> >
|
||||
{
|
||||
int int_;
|
||||
|
||||
public:
|
||||
//This is a member hook
|
||||
avl_set_member_hook<> member_hook_;
|
||||
|
||||
MyClass(int i)
|
||||
: int_(i)
|
||||
{}
|
||||
friend bool operator< (const MyClass &a, const MyClass &b)
|
||||
{ return a.int_ < b.int_; }
|
||||
friend bool operator> (const MyClass &a, const MyClass &b)
|
||||
{ return a.int_ > b.int_; }
|
||||
friend bool operator== (const MyClass &a, const MyClass &b)
|
||||
{ return a.int_ < b.int_; }
|
||||
};
|
||||
|
||||
//Define an avl_set using the base hook that will store values in reverse order
|
||||
typedef avl_set< MyClass, compare<std::greater<MyClass> > > BaseSet;
|
||||
|
||||
//Define an multiset using the member hook
|
||||
typedef member_hook<MyClass, avl_set_member_hook<>, &MyClass::member_hook_> MemberOption;
|
||||
typedef avl_multiset< MyClass, MemberOption> MemberMultiset;
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<MyClass>::iterator VectIt;
|
||||
typedef std::vector<MyClass>::reverse_iterator VectRit;
|
||||
|
||||
//Create several MyClass objects, each one with a different value
|
||||
std::vector<MyClass> values;
|
||||
for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
|
||||
|
||||
BaseSet baseset;
|
||||
MemberMultiset membermultiset;
|
||||
|
||||
//Check that size optimization is activated in the base hook
|
||||
assert(sizeof(avl_set_base_hook<optimize_size<true> >) == 3*sizeof(void*));
|
||||
//Check that size optimization is deactivated in the member hook
|
||||
assert(sizeof(avl_set_member_hook<>) > 3*sizeof(void*));
|
||||
|
||||
//Now insert them in the reverse order in the base hook avl_set
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
baseset.insert(*it);
|
||||
|
||||
//Now insert them in the same order as in vector in the member hook avl_set
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
membermultiset.insert(*it);
|
||||
|
||||
//Now test avl_sets
|
||||
{
|
||||
BaseSet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend());
|
||||
MemberMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end());
|
||||
VectIt it(values.begin()), itend(values.end());
|
||||
|
||||
//Test the objects inserted in the base hook avl_set
|
||||
for(; it != itend; ++it, ++rbit)
|
||||
if(&*rbit != &*it) return 1;
|
||||
|
||||
//Test the objects inserted in the member hook avl_set
|
||||
for(it = values.begin(); it != itend; ++it, ++mit)
|
||||
if(&*mit != &*it) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//]
|
||||
@@ -0,0 +1,85 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2007
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//[doc_avltree_algorithms_code
|
||||
#include <boost/intrusive/avltree_algorithms.hpp>
|
||||
#include <cassert>
|
||||
|
||||
struct my_node
|
||||
{
|
||||
my_node(int i = 0)
|
||||
: int_(i)
|
||||
{}
|
||||
my_node *parent_, *left_, *right_;
|
||||
int balance_;
|
||||
//other members
|
||||
int int_;
|
||||
};
|
||||
|
||||
//Define our own avltree_node_traits
|
||||
struct my_avltree_node_traits
|
||||
{
|
||||
typedef my_node node;
|
||||
typedef my_node * node_ptr;
|
||||
typedef const my_node * const_node_ptr;
|
||||
typedef int balance;
|
||||
|
||||
static node_ptr get_parent(const_node_ptr n) { return n->parent_; }
|
||||
static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; }
|
||||
static node_ptr get_left(const_node_ptr n) { return n->left_; }
|
||||
static void set_left(node_ptr n, node_ptr left) { n->left_ = left; }
|
||||
static node_ptr get_right(const_node_ptr n) { return n->right_; }
|
||||
static void set_right(node_ptr n, node_ptr right) { n->right_ = right; }
|
||||
static balance get_balance(const_node_ptr n) { return n->balance_; }
|
||||
static void set_balance(node_ptr n, balance b) { n->balance_ = b; }
|
||||
static balance negative() { return -1; }
|
||||
static balance zero() { return 0; }
|
||||
static balance positive() { return 1; }
|
||||
};
|
||||
|
||||
struct node_ptr_compare
|
||||
{
|
||||
bool operator()(my_node *a, my_node *b)
|
||||
{ return a->int_ < b->int_; }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef boost::intrusive::avltree_algorithms<my_avltree_node_traits> algo;
|
||||
my_node header, two(2), three(3);
|
||||
|
||||
//Create an empty avltree container:
|
||||
//"header" will be the header node of the tree
|
||||
algo::init_header(&header);
|
||||
|
||||
//Now insert node "two" in the tree using the sorting functor
|
||||
algo::insert_equal_upper_bound(&header, &two, node_ptr_compare());
|
||||
|
||||
//Now insert node "three" in the tree using the sorting functor
|
||||
algo::insert_equal_lower_bound(&header, &three, node_ptr_compare());
|
||||
|
||||
//Now take the first node (the left node of the header)
|
||||
my_node *n = header.left_;
|
||||
assert(n == &two);
|
||||
|
||||
//Now go to the next node
|
||||
n = algo::next_node(n);
|
||||
assert(n == &three);
|
||||
|
||||
//Erase a node just using a pointer to it
|
||||
algo::unlink(&two);
|
||||
|
||||
//Erase a node using also the header (faster)
|
||||
algo::erase(&header, &three);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//]
|
||||
@@ -0,0 +1,78 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2007
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//[doc_splaytree_algorithms_code
|
||||
#include <boost/intrusive/splaytree_algorithms.hpp>
|
||||
#include <cassert>
|
||||
|
||||
struct my_node
|
||||
{
|
||||
my_node(int i = 0)
|
||||
: int_(i)
|
||||
{}
|
||||
my_node *parent_, *left_, *right_;
|
||||
//other members
|
||||
int int_;
|
||||
};
|
||||
|
||||
//Define our own splaytree_node_traits
|
||||
struct my_splaytree_node_traits
|
||||
{
|
||||
typedef my_node node;
|
||||
typedef my_node * node_ptr;
|
||||
typedef const my_node * const_node_ptr;
|
||||
|
||||
static node_ptr get_parent(const_node_ptr n) { return n->parent_; }
|
||||
static void set_parent(node_ptr n, node_ptr parent){ n->parent_ = parent; }
|
||||
static node_ptr get_left(const_node_ptr n) { return n->left_; }
|
||||
static void set_left(node_ptr n, node_ptr left) { n->left_ = left; }
|
||||
static node_ptr get_right(const_node_ptr n) { return n->right_; }
|
||||
static void set_right(node_ptr n, node_ptr right) { n->right_ = right; }
|
||||
};
|
||||
|
||||
struct node_ptr_compare
|
||||
{
|
||||
bool operator()(my_node *a, my_node *b)
|
||||
{ return a->int_ < b->int_; }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef boost::intrusive::splaytree_algorithms<my_splaytree_node_traits> algo;
|
||||
my_node header, two(2), three(3);
|
||||
|
||||
//Create an empty splaytree container:
|
||||
//"header" will be the header node of the tree
|
||||
algo::init_header(&header);
|
||||
|
||||
//Now insert node "two" in the tree using the sorting functor
|
||||
algo::insert_equal_upper_bound(&header, &two, node_ptr_compare());
|
||||
|
||||
//Now insert node "three" in the tree using the sorting functor
|
||||
algo::insert_equal_lower_bound(&header, &three, node_ptr_compare());
|
||||
|
||||
//Now take the first node (the left node of the header)
|
||||
my_node *n = header.left_;
|
||||
assert(n == &two);
|
||||
|
||||
//Now go to the next node
|
||||
n = algo::next_node(n);
|
||||
assert(n == &three);
|
||||
|
||||
//Erase a node just using a pointer to it
|
||||
algo::unlink(&two);
|
||||
|
||||
//Erase a node using also the header (faster)
|
||||
algo::erase(&header, &three);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//]
|
||||
Reference in New Issue
Block a user