forked from boostorg/intrusive
Added scapegoat trees and an option to store the hash value in the hook for unordered containers
[SVN r41196]
This commit is contained in:
+24
-13
@@ -11,13 +11,16 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//[doc_splay_set_code
|
||||
#include <boost/intrusive/splay_set.hpp>
|
||||
#include <boost/intrusive/bs_set_hook.hpp>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//This is a base hook
|
||||
class MyClass : public splay_set_base_hook<>
|
||||
class MyClass
|
||||
: public splay_set_base_hook<> //This is an splay tree base hook
|
||||
, public bs_set_base_hook<> //This is a binary search tree base hook
|
||||
|
||||
{
|
||||
int int_;
|
||||
|
||||
@@ -36,9 +39,12 @@ class MyClass : public splay_set_base_hook<>
|
||||
{ return a.int_ < b.int_; }
|
||||
};
|
||||
|
||||
//Define an set using the base hook that will store values in reverse order
|
||||
//Define a set using the base hook that will store values in reverse order
|
||||
typedef splay_set< MyClass, compare<std::greater<MyClass> > > BaseSplaySet;
|
||||
|
||||
//Define a set using the binary search tree hook
|
||||
typedef splay_set< MyClass, base_hook<bs_set_base_hook<> > > BaseBsSplaySet;
|
||||
|
||||
//Define an multiset using the member hook
|
||||
typedef member_hook<MyClass, splay_set_member_hook<>, &MyClass::member_hook_> MemberOption;
|
||||
typedef splay_multiset< MyClass, MemberOption> MemberSplayMultiset;
|
||||
@@ -52,30 +58,35 @@ int main()
|
||||
std::vector<MyClass> values;
|
||||
for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
|
||||
|
||||
BaseSplaySet baseset;
|
||||
BaseSplaySet baseset;
|
||||
BaseBsSplaySet bsbaseset;
|
||||
MemberSplayMultiset membermultiset;
|
||||
|
||||
//Now insert them in the reverse order in the base hook 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 set
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
//Insert values in the container
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
|
||||
baseset.insert(*it);
|
||||
bsbaseset.insert(*it);
|
||||
membermultiset.insert(*it);
|
||||
}
|
||||
|
||||
//Now test sets
|
||||
{
|
||||
BaseSplaySet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend());
|
||||
BaseBsSplaySet::iterator bsit(bsbaseset.begin()), bsitend(bsbaseset.end());
|
||||
MemberSplayMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end());
|
||||
VectIt it(values.begin()), itend(values.end());
|
||||
|
||||
//Test the objects inserted in the base hook set
|
||||
for(; it != itend; ++it, ++rbit)
|
||||
for(; it != itend; ++it, ++rbit){
|
||||
if(&*rbit != &*it) return 1;
|
||||
}
|
||||
|
||||
//Test the objects inserted in the member hook set
|
||||
for(it = values.begin(); it != itend; ++it, ++mit)
|
||||
if(&*mit != &*it) return 1;
|
||||
//Test the objects inserted in member and binary search hook sets
|
||||
for(it = values.begin(); it != itend; ++it, ++bsit, ++mit){
|
||||
if(&*bsit != &*it) return 1;
|
||||
if(&*mit != &*it) return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user