forked from boostorg/intrusive
Changes introduced by the new intrusive version.
[SVN r39550]
This commit is contained in:
1 parent
cbb4812c00
commit
c4aa57001c
32 files changed
+2259
-1025
No files matched your search
@@ -10,7 +10,7 @@
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//[doc_advanced_value_traits_code
|
||||
#include <boost/intrusive/linking_policy.hpp>
|
||||
#include <boost/intrusive/link_mode.hpp>
|
||||
#include <boost/intrusive/list.hpp>
|
||||
#include <vector>
|
||||
|
||||
@@ -46,7 +46,7 @@ struct simple_node_traits
|
||||
|
||||
//A templatized value traits for value_1 and value_2
|
||||
template<class ValueType>
|
||||
struct value_traits
|
||||
struct simple_value_traits
|
||||
{
|
||||
typedef simple_node_traits node_traits;
|
||||
typedef node_traits::node_ptr node_ptr;
|
||||
@@ -54,7 +54,7 @@ struct value_traits
|
||||
typedef ValueType value_type;
|
||||
typedef ValueType * pointer;
|
||||
typedef const ValueType * const_pointer;
|
||||
enum { linking_policy = boost::intrusive::normal_link };
|
||||
static const boost::intrusive::link_mode_type link_mode = boost::intrusive::normal_link;
|
||||
static node_ptr to_node_ptr (value_type &value) { return node_ptr(&value); }
|
||||
static const_node_ptr to_node_ptr (const value_type &value) { return const_node_ptr(&value); }
|
||||
static pointer to_value_ptr(node_ptr n) { return static_cast<value_type*>(n); }
|
||||
@@ -65,8 +65,10 @@ struct value_traits
|
||||
//[doc_advanced_value_traits_containers
|
||||
//Now define two intrusive lists. Both lists will use the same algorithms:
|
||||
// circular_list_algorithms<simple_node_traits>
|
||||
typedef boost::intrusive::list <value_traits<value_1> > Value1List;
|
||||
typedef boost::intrusive::list <value_traits<value_2> > Value2List;
|
||||
|
||||
using namespace boost::intrusive;
|
||||
typedef list <value_1, value_traits<simple_value_traits<value_1> > > Value1List;
|
||||
typedef list <value_2, value_traits<simple_value_traits<value_2> > > Value2List;
|
||||
//]
|
||||
|
||||
//[doc_advanced_value_traits_test
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
// See http://www.boost.org/libs/intrusive for documentation.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
#include <boost/intrusive/linking_policy.hpp>
|
||||
#include <boost/intrusive/link_mode.hpp>
|
||||
#include <boost/intrusive/list.hpp>
|
||||
#include <boost/intrusive/member_value_traits.hpp>
|
||||
#include <vector>
|
||||
@@ -57,8 +57,8 @@ typedef member_value_traits
|
||||
|
||||
//Now define two intrusive lists. Both lists will use the same algorithms:
|
||||
// circular_list_algorithms<simple_node_traits>
|
||||
typedef boost::intrusive::list <ValueTraits1> Value1List;
|
||||
typedef boost::intrusive::list <ValueTraits2> Value2List;
|
||||
typedef list <value_1, value_traits<ValueTraits1> > Value1List;
|
||||
typedef list <value_2, value_traits<ValueTraits2> > Value2List;
|
||||
//]
|
||||
|
||||
//[doc_advanced_value_traits2_test
|
||||
|
||||
@@ -51,24 +51,21 @@ class Expensive : public set_base_hook<>, public unordered_set_base_hook<>
|
||||
};
|
||||
|
||||
// A set and unordered_set that store Expensive objects
|
||||
typedef set<set_base_hook<>::value_traits<Expensive> > Set;
|
||||
typedef unordered_set<unordered_set_base_hook<>::
|
||||
value_traits<Expensive> > UnorderedSet;
|
||||
typedef set<Expensive> Set;
|
||||
typedef unordered_set<Expensive> UnorderedSet;
|
||||
|
||||
// Search functions
|
||||
Expensive *get_from_set(const char* key, Set &set)
|
||||
Expensive *get_from_set(const char* key, Set &set_object)
|
||||
{
|
||||
Set::iterator it = set.find(Expensive(key));
|
||||
if( it == set.end() ) return 0;
|
||||
Set::iterator it = set_object.find(Expensive(key));
|
||||
if( it == set_object.end() ) return 0;
|
||||
return &*it;
|
||||
}
|
||||
|
||||
Expensive *get_from_unordered_set
|
||||
(const char* key, UnorderedSet &unordered_set)
|
||||
Expensive *get_from_uset(const char* key, UnorderedSet &uset_object)
|
||||
{
|
||||
UnorderedSet::iterator it =
|
||||
unordered_set.find(Expensive (key));
|
||||
if( it == unordered_set.end() ) return 0;
|
||||
UnorderedSet::iterator it = uset_object.find(Expensive (key));
|
||||
if( it == uset_object.end() ) return 0;
|
||||
return &*it;
|
||||
}
|
||||
//]
|
||||
@@ -94,38 +91,35 @@ struct StrExpEqual
|
||||
};
|
||||
|
||||
// Optimized search functions
|
||||
Expensive *get_from_set_optimized(const char* key, Set &set)
|
||||
Expensive *get_from_set_optimized(const char* key, Set &set_object)
|
||||
{
|
||||
Set::iterator it = set.find(key, StrExpComp());
|
||||
if( it == set.end() ) return 0;
|
||||
Set::iterator it = set_object.find(key, StrExpComp());
|
||||
if( it == set_object.end() ) return 0;
|
||||
return &*it;
|
||||
}
|
||||
|
||||
Expensive *get_from_unordered_set_optimized
|
||||
(const char* key, UnorderedSet &unordered_set)
|
||||
Expensive *get_from_uset_optimized(const char* key, UnorderedSet &uset_object)
|
||||
{
|
||||
UnorderedSet::iterator it =
|
||||
unordered_set.find(key, StrHasher(), StrExpEqual());
|
||||
if( it == unordered_set.end() ) return 0;
|
||||
UnorderedSet::iterator it = uset_object.find(key, StrHasher(), StrExpEqual());
|
||||
if( it == uset_object.end() ) return 0;
|
||||
return &*it;
|
||||
}
|
||||
//]
|
||||
|
||||
//[doc_assoc_optimized_code_normal_insert
|
||||
// Insertion functions
|
||||
bool insert_to_set(const char* key, Set &set)
|
||||
bool insert_to_set(const char* key, Set &set_object)
|
||||
{
|
||||
Expensive *pobject = new Expensive(key);
|
||||
bool success = set.insert(*pobject).second;
|
||||
bool success = set_object.insert(*pobject).second;
|
||||
if(!success) delete pobject;
|
||||
return success;
|
||||
}
|
||||
|
||||
bool insert_to_unordered_set
|
||||
(const char* key, UnorderedSet &unordered_set)
|
||||
bool insert_to_uset(const char* key, UnorderedSet &uset_object)
|
||||
{
|
||||
Expensive *pobject = new Expensive(key);
|
||||
bool success = unordered_set.insert(*pobject).second;
|
||||
bool success = uset_object.insert(*pobject).second;
|
||||
if(!success) delete pobject;
|
||||
return success;
|
||||
}
|
||||
@@ -133,24 +127,20 @@ bool insert_to_unordered_set
|
||||
|
||||
//[doc_assoc_optimized_code_optimized_insert
|
||||
// Optimized insertion functions
|
||||
bool insert_to_set_optimized(const char* key, Set &set)
|
||||
bool insert_to_set_optimized(const char* key, Set &set_object)
|
||||
{
|
||||
Set::insert_commit_data insert_data;
|
||||
bool success = set.insert_check
|
||||
(key, StrExpComp(), insert_data).second;
|
||||
if(success)
|
||||
set.insert_commit(*new Expensive(key), insert_data);
|
||||
bool success = set_object.insert_check(key, StrExpComp(), insert_data).second;
|
||||
if(success) set_object.insert_commit(*new Expensive(key), insert_data);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool insert_to_unordered_set_optimized
|
||||
(const char* key, UnorderedSet &unordered_set)
|
||||
bool insert_to_uset_optimized(const char* key, UnorderedSet &uset_object)
|
||||
{
|
||||
UnorderedSet::insert_commit_data insert_data;
|
||||
bool success = unordered_set.insert_check
|
||||
bool success = uset_object.insert_check
|
||||
(key, StrHasher(), StrExpEqual(), insert_data).second;
|
||||
if(success)
|
||||
unordered_set.insert_commit(*new Expensive(key), insert_data);
|
||||
if(success) uset_object.insert_commit(*new Expensive(key), insert_data);
|
||||
return success;
|
||||
}
|
||||
//]
|
||||
@@ -159,7 +149,7 @@ int main()
|
||||
{
|
||||
Set set;
|
||||
UnorderedSet::bucket_type buckets[10];
|
||||
UnorderedSet unordered_set(buckets, 10);
|
||||
UnorderedSet unordered_set(UnorderedSet::bucket_traits(buckets, 10));
|
||||
|
||||
const char * const expensive_key
|
||||
= "A long string that avoids small string optimization";
|
||||
@@ -170,7 +160,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(get_from_unordered_set(expensive_key, unordered_set)){
|
||||
if(get_from_uset(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -178,7 +168,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(get_from_unordered_set_optimized(expensive_key, unordered_set)){
|
||||
if(get_from_uset_optimized(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -189,7 +179,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!get_from_unordered_set(expensive_key, unordered_set)){
|
||||
if(!get_from_uset(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -197,7 +187,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!get_from_unordered_set_optimized(expensive_key, unordered_set)){
|
||||
if(!get_from_uset_optimized(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -208,7 +198,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!insert_to_unordered_set(expensive_key, unordered_set)){
|
||||
if(!insert_to_uset(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -228,7 +218,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!insert_to_unordered_set_optimized(expensive_key, unordered_set)){
|
||||
if(!insert_to_uset_optimized(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -251,7 +241,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(insert_to_unordered_set(expensive_key, unordered_set)){
|
||||
if(insert_to_uset(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -259,7 +249,7 @@ int main()
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(insert_to_unordered_set_optimized(expensive_key, unordered_set)){
|
||||
if(insert_to_uset_optimized(expensive_key, unordered_set)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+16
-17
@@ -15,39 +15,38 @@
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
class MyClass : public list_base_hook<tag, auto_unlink>
|
||||
typedef list_base_hook<link_mode<auto_unlink> > auto_unlink_hook;
|
||||
|
||||
class MyClass : public auto_unlink_hook
|
||||
//This hook removes the node in the destructor
|
||||
{
|
||||
int int_;
|
||||
|
||||
public:
|
||||
MyClass(int i = 0) : int_(i) {}
|
||||
void unlink() { list_base_hook<tag, auto_unlink>::unlink(); }
|
||||
bool is_linked() { return list_base_hook<tag, auto_unlink>::is_linked(); }
|
||||
int get() const { return int_; }
|
||||
void unlink() { auto_unlink_hook::unlink(); }
|
||||
bool is_linked() { return auto_unlink_hook::is_linked(); }
|
||||
};
|
||||
|
||||
//Define a list that will store MyClass
|
||||
//using the public base hook
|
||||
//Define a list that will store values using the base hook
|
||||
//The list can't have constant-time size!
|
||||
typedef list< list_base_hook<tag, auto_unlink>::
|
||||
value_traits<MyClass>, false > List;
|
||||
typedef list< MyClass, constant_time_size<false> > List;
|
||||
|
||||
int main()
|
||||
{
|
||||
//Create the list
|
||||
List list;
|
||||
List l;
|
||||
{
|
||||
//Create myclass and check it's linked
|
||||
MyClass myclass;
|
||||
assert(myclass.is_linked() == false);
|
||||
|
||||
//Insert the object
|
||||
list.push_back(myclass);
|
||||
l.push_back(myclass);
|
||||
|
||||
//Check that we have inserted the object
|
||||
assert(list.empty() == false);
|
||||
assert(&list.front() == &myclass);
|
||||
assert(l.empty() == false);
|
||||
assert(&l.front() == &myclass);
|
||||
assert(myclass.is_linked() == true);
|
||||
|
||||
//Now myclass' destructor will unlink it
|
||||
@@ -55,7 +54,7 @@ int main()
|
||||
}
|
||||
|
||||
//Check auto-unlink has been executed
|
||||
assert(list.empty() == true);
|
||||
assert(l.empty() == true);
|
||||
|
||||
{
|
||||
//Now test the unlink() function
|
||||
@@ -65,18 +64,18 @@ int main()
|
||||
assert(myclass.is_linked() == false);
|
||||
|
||||
//Insert the object
|
||||
list.push_back(myclass);
|
||||
l.push_back(myclass);
|
||||
|
||||
//Check that we have inserted the object
|
||||
assert(list.empty() == false);
|
||||
assert(&list.front() == &myclass);
|
||||
assert(l.empty() == false);
|
||||
assert(&l.front() == &myclass);
|
||||
assert(myclass.is_linked() == true);
|
||||
|
||||
//Now unlink the node
|
||||
myclass.unlink();
|
||||
|
||||
//Check auto-unlink has been executed
|
||||
assert(list.empty() == true);
|
||||
assert(l.empty() == true);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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_bucket_traits
|
||||
#include <boost/intrusive/unordered_set.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <vector>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//A class to be inserted in an unordered_set
|
||||
class MyClass : public unordered_set_base_hook<>
|
||||
{
|
||||
int int_;
|
||||
|
||||
public:
|
||||
MyClass(int i = 0) : int_(i)
|
||||
{}
|
||||
|
||||
friend bool operator==(const MyClass &l, const MyClass &r)
|
||||
{ return l.int_ == r.int_; }
|
||||
friend std::size_t hash_value(const MyClass &v)
|
||||
{ return boost::hash_value(v.int_); }
|
||||
};
|
||||
|
||||
//Define the base hook option
|
||||
typedef base_hook< unordered_set_base_hook<> > BaseHookOption;
|
||||
|
||||
//Obtain the types of the bucket and the bucket pointer
|
||||
typedef unordered_bucket<BaseHookOption>::type BucketType;
|
||||
typedef unordered_bucket_ptr<BaseHookOption>::type BucketPtr;
|
||||
|
||||
//The custom bucket traits.
|
||||
class custom_bucket_traits
|
||||
{
|
||||
public:
|
||||
static const int NumBuckets = 100;
|
||||
|
||||
custom_bucket_traits(BucketPtr buckets)
|
||||
: buckets_(buckets)
|
||||
{}
|
||||
|
||||
//Functions to be implemented by custom bucket traits
|
||||
BucketPtr bucket_begin() const { return buckets_; }
|
||||
std::size_t bucket_count() const { return NumBuckets;}
|
||||
|
||||
private:
|
||||
BucketPtr buckets_;
|
||||
};
|
||||
|
||||
//Define the container using the custom bucket traits
|
||||
typedef unordered_set<MyClass, bucket_traits<custom_bucket_traits> > BucketTraitsUset;
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<MyClass>::iterator VectIt;
|
||||
std::vector<MyClass> values;
|
||||
|
||||
//Fill values
|
||||
for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
|
||||
|
||||
//Now create the bucket array and the custom bucket traits object
|
||||
BucketType buckets[custom_bucket_traits::NumBuckets];
|
||||
custom_bucket_traits btraits(buckets);
|
||||
|
||||
//Now create the unordered set
|
||||
BucketTraitsUset uset(btraits);
|
||||
|
||||
//Insert the values in the unordered set
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
uset.insert(*it);
|
||||
|
||||
return 0;
|
||||
}
|
||||
//]
|
||||
+10
-14
@@ -14,9 +14,10 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//A class that can be inserted in an intrusive list
|
||||
class my_class
|
||||
: public boost::intrusive::list_base_hook<>
|
||||
class my_class : public list_base_hook<>
|
||||
{
|
||||
public:
|
||||
friend bool operator==(const my_class &a, const my_class &b)
|
||||
@@ -28,20 +29,18 @@ class my_class
|
||||
};
|
||||
|
||||
//Definition of the intrusive list
|
||||
typedef boost::intrusive::list< my_class::value_traits<my_class> > my_class_list;
|
||||
typedef list<my_class> my_class_list;
|
||||
|
||||
//Cloner object function
|
||||
class new_cloner
|
||||
struct new_cloner
|
||||
{
|
||||
public:
|
||||
my_class *operator()(const my_class &clone_this)
|
||||
{ return new my_class(clone_this); }
|
||||
};
|
||||
|
||||
//The disposer object function
|
||||
class delete_disposer
|
||||
struct delete_disposer
|
||||
{
|
||||
public:
|
||||
void operator()(my_class *delete_this)
|
||||
{ delete delete_this; }
|
||||
};
|
||||
@@ -54,9 +53,8 @@ int main()
|
||||
//Fill all the nodes and insert them in the list
|
||||
my_class_list list;
|
||||
|
||||
for(int i = 0; i < MaxElem; ++i){
|
||||
nodes[i].int_ = i;
|
||||
}
|
||||
for(int i = 0; i < MaxElem; ++i) nodes[i].int_ = i;
|
||||
|
||||
list.insert(list.end(), nodes.begin(), nodes.end());
|
||||
|
||||
//Now clone "list" using "new" and "delete" object functions
|
||||
@@ -64,12 +62,10 @@ int main()
|
||||
cloned_list.clone_from(list, new_cloner(), delete_disposer());
|
||||
|
||||
//Test that both are equal
|
||||
if(cloned_list != list){
|
||||
if(cloned_list != list)
|
||||
std::cout << "Both lists are different" << std::endl;
|
||||
}
|
||||
else{
|
||||
else
|
||||
std::cout << "Both lists are equal" << std::endl;
|
||||
}
|
||||
|
||||
//Don't forget to free the memory from the second list
|
||||
cloned_list.clear_and_dispose(delete_disposer());
|
||||
|
||||
+14
-13
@@ -12,9 +12,10 @@
|
||||
//[doc_entity_code
|
||||
#include <boost/intrusive/list.hpp>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//A class that can be inserted in an intrusive list
|
||||
class entity
|
||||
: public boost::intrusive::list_base_hook<>
|
||||
class entity : public list_base_hook<>
|
||||
{
|
||||
public:
|
||||
virtual ~entity();
|
||||
@@ -26,25 +27,25 @@ class some_entity : public entity
|
||||
{/**/};
|
||||
|
||||
//Definition of the intrusive list
|
||||
typedef boost::intrusive::list< entity::value_traits<entity> > entity_list;
|
||||
typedef list<entity> entity_list;
|
||||
|
||||
//A global list
|
||||
entity_list list;
|
||||
entity_list global_list;
|
||||
|
||||
//The destructor removes itself from the list
|
||||
//The destructor removes itself from the global list
|
||||
entity::~entity()
|
||||
{ list.erase(entity_list::iterator_to(*this)); }
|
||||
{ global_list.erase(entity_list::s_iterator_to(*this)); }
|
||||
|
||||
//Function to insert a new "some_entity" in the list
|
||||
//Function to insert a new "some_entity" in the global list
|
||||
void insert_some_entity()
|
||||
{ list.push_back (*new some_entity(/*...*/)); }
|
||||
{ global_list.push_back (*new some_entity(/*...*/)); }
|
||||
|
||||
//Function to clear an entity from the intrusive list
|
||||
//Function to clear an entity from the intrusive global list
|
||||
void clear_list ()
|
||||
{
|
||||
// entity's destructor removes itself from the list implicitly
|
||||
while (!list.empty())
|
||||
delete &list.front();
|
||||
// entity's destructor removes itself from the global list implicitly
|
||||
while (!global_list.empty())
|
||||
delete &global_list.front();
|
||||
}
|
||||
|
||||
int main()
|
||||
@@ -52,7 +53,7 @@ int main()
|
||||
//Insert some new entities
|
||||
insert_some_entity();
|
||||
insert_some_entity();
|
||||
//list's destructor will free objects
|
||||
//global_list's destructor will free objects
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,10 @@
|
||||
//[doc_erasing_and_disposing
|
||||
#include <boost/intrusive/list.hpp>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//A class that can be inserted in an intrusive list
|
||||
class my_class
|
||||
: public boost::intrusive::list_base_hook<>
|
||||
class my_class : public list_base_hook<>
|
||||
{
|
||||
public:
|
||||
my_class(int i)
|
||||
@@ -26,20 +27,18 @@ class my_class
|
||||
};
|
||||
|
||||
//Definition of the intrusive list
|
||||
typedef boost::intrusive::list< my_class::value_traits<my_class> > my_class_list;
|
||||
typedef list<my_class> my_class_list;
|
||||
|
||||
//The predicate function
|
||||
class is_even
|
||||
struct is_even
|
||||
{
|
||||
public:
|
||||
bool operator()(const my_class &c) const
|
||||
{ return 0 == (c.int_ % 2); }
|
||||
};
|
||||
|
||||
//The disposer object function
|
||||
class delete_disposer
|
||||
struct delete_disposer
|
||||
{
|
||||
public:
|
||||
void operator()(my_class *delete_this)
|
||||
{ delete delete_this; }
|
||||
};
|
||||
@@ -53,9 +52,7 @@ int main()
|
||||
|
||||
try{
|
||||
//Insert new objects in the container
|
||||
for(int i = 0; i < MaxElem; ++i){
|
||||
list.push_back(*new my_class(i));
|
||||
}
|
||||
for(int i = 0; i < MaxElem; ++i) list.push_back(*new my_class(i));
|
||||
|
||||
//Now use remove_and_dispose_if to erase and delete the objects
|
||||
list.remove_and_dispose_if(is_even(), delete_disposer());
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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_external_value_traits
|
||||
#include <boost/intrusive/list.hpp>
|
||||
#include <vector>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//This type is not modifiable so we can't store hooks or custom nodes
|
||||
typedef int identifier_t;
|
||||
|
||||
//This value traits will associate elements from an array of identifiers with
|
||||
//elements of an array of nodes. The element i of the value array will use the
|
||||
//node i of the node array:
|
||||
class external_traits
|
||||
{
|
||||
//Non-copyable
|
||||
external_traits(const external_traits &);
|
||||
external_traits& operator=(const external_traits &);
|
||||
|
||||
public:
|
||||
typedef list_node_traits<void*> node_traits;
|
||||
typedef node_traits::node node;
|
||||
typedef node * node_ptr;
|
||||
typedef const node * const_node_ptr;
|
||||
typedef identifier_t value_type;
|
||||
typedef identifier_t * pointer;
|
||||
typedef const identifier_t * const_pointer;
|
||||
static const link_mode_type link_mode = normal_link;
|
||||
|
||||
external_traits(pointer ids, std::size_t NumElements)
|
||||
: ids_(ids), nodes_(NumElements)
|
||||
{}
|
||||
|
||||
///Note: non static functions!
|
||||
node_ptr to_node_ptr (value_type &value)
|
||||
{ return &this->nodes_[0] + (&value - this->ids_); }
|
||||
const_node_ptr to_node_ptr (const value_type &value) const
|
||||
{ return &this->nodes_[0] + (&value - this->ids_); }
|
||||
pointer to_value_ptr(node_ptr n)
|
||||
{ return this->ids_ + (n - &this->nodes_[0]); }
|
||||
const_pointer to_value_ptr(const_node_ptr n) const
|
||||
{ return this->ids_ + (n - &this->nodes_[0]); }
|
||||
|
||||
private:
|
||||
pointer ids_;
|
||||
//This is an array of nodes that is necessary to form the linked list
|
||||
std::vector<list_node_traits<void*>::node> nodes_;
|
||||
};
|
||||
|
||||
//This is the value traits class that will be stored in the container
|
||||
//and that will lead to the external traits using the address
|
||||
//of the container.
|
||||
struct internal_traits
|
||||
{
|
||||
static const bool external_value_traits = true;
|
||||
typedef external_traits value_traits;
|
||||
|
||||
template<class Container>
|
||||
value_traits &get_value_traits(Container &cont);
|
||||
|
||||
template<class Container>
|
||||
const value_traits &get_value_traits(const Container &cont) const;
|
||||
};
|
||||
|
||||
//The intrusive list that will use external value traits
|
||||
typedef list<identifier_t, value_traits<internal_traits> > List;
|
||||
|
||||
class data_holder
|
||||
: public List
|
||||
{
|
||||
public:
|
||||
data_holder(identifier_t *ids, std::size_t NumElements)
|
||||
: List()
|
||||
, external_traits_(ids, NumElements)
|
||||
{}
|
||||
external_traits external_traits_;
|
||||
};
|
||||
|
||||
template<class Container>
|
||||
internal_traits::value_traits &internal_traits::get_value_traits(Container &cont)
|
||||
{ return static_cast<data_holder&>(cont).external_traits_; }
|
||||
|
||||
template<class Container>
|
||||
const internal_traits::value_traits &internal_traits::get_value_traits(const Container &cont) const
|
||||
{ return static_cast<const data_holder&>(cont).external_traits_; }
|
||||
|
||||
int main()
|
||||
{
|
||||
const int NumElements = 100;
|
||||
|
||||
//This is an array of ids that we want to "store"
|
||||
identifier_t ids [NumElements];
|
||||
|
||||
//Initialize id objects, each one with a different number
|
||||
for(int i = 0; i != NumElements; ++i) ids[i] = i;
|
||||
|
||||
//The data holding the list and the external traits
|
||||
data_holder data(ids, NumElements);
|
||||
|
||||
//This list will store ids without modifying identifier_t instances
|
||||
//Stateful value traits must be explicitly passed in the constructor.
|
||||
List &my_list = data;
|
||||
|
||||
//Insert ids in reverse order in the list
|
||||
for(identifier_t * it(&ids[0]), *itend(&ids[NumElements]); it != itend; ++it)
|
||||
my_list.push_front(*it);
|
||||
|
||||
//Now test lists
|
||||
List::const_iterator list_it (my_list.cbegin());
|
||||
identifier_t *it_val(&ids[NumElements]-1), *it_rbeg_val(&ids[0] -1);
|
||||
|
||||
//Test the objects inserted in the base hook list
|
||||
for(; it_val != it_rbeg_val; --it_val, ++list_it){
|
||||
if(&*list_it != &*it_val) return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
//]
|
||||
+17
-28
@@ -15,7 +15,7 @@
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
class MyClass : public list_base_hook<>
|
||||
class MyClass : public list_base_hook<>
|
||||
{
|
||||
int int_;
|
||||
|
||||
@@ -23,62 +23,51 @@ class MyClass : public list_base_hook<>
|
||||
list_member_hook<> member_hook_;
|
||||
|
||||
MyClass(int i) : int_(i) {}
|
||||
int get() const { return int_; }
|
||||
};
|
||||
|
||||
//Define a list that will store MyClass using the base hook
|
||||
typedef list< list_base_hook<>::value_traits<MyClass> >
|
||||
BaseList;
|
||||
typedef list<MyClass> BaseList;
|
||||
|
||||
//Define a list that will store MyClass using the member hook
|
||||
typedef list< list_member_hook<>::
|
||||
value_traits<MyClass, &MyClass::member_hook_>
|
||||
> MemberList;
|
||||
typedef member_hook
|
||||
< MyClass, list_member_hook<>, &MyClass::member_hook_> MemberOption;
|
||||
typedef list<MyClass, MemberOption> MemberList;
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<MyClass> Vect;
|
||||
typedef Vect::iterator VectIt;
|
||||
typedef Vect::reverse_iterator VectRit;
|
||||
typedef std::vector<MyClass>::iterator VectIt;
|
||||
typedef std::vector<MyClass>::reverse_iterator VectRit;
|
||||
|
||||
//Create several MyClass objects, each one
|
||||
//with a different internal number
|
||||
Vect myclassvector;
|
||||
for(int i = 0; i < 100; ++i)
|
||||
myclassvector.push_back(MyClass(i));
|
||||
//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));
|
||||
|
||||
BaseList baselist;
|
||||
MemberList memberlist;
|
||||
|
||||
//Now insert them in the reverse order
|
||||
//in the base hook intrusive list
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
//Now insert them in the reverse order in the base hook list
|
||||
for(VectIt it(values.begin()), itend(values.end())
|
||||
; it != itend ; ++it){
|
||||
baselist.push_front(*it);
|
||||
}
|
||||
|
||||
//Now insert them in the same order as in vector in the
|
||||
//member hook intrusive list
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
; it != itend; ++it){
|
||||
//Now insert them in the same order as in vector in the member hook list
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
memberlist.push_back(*it);
|
||||
}
|
||||
|
||||
//Now test lists
|
||||
{
|
||||
BaseList::reverse_iterator rbit(baselist.rbegin()), rbitend(baselist.rend());
|
||||
MemberList::iterator mit(memberlist.begin()), mitend(memberlist.end());
|
||||
VectIt it(myclassvector.begin()), itend(myclassvector.end());
|
||||
VectIt it(values.begin()), itend(values.end());
|
||||
|
||||
//Test the objects inserted in the base hook list
|
||||
for(; it != itend; ++it, ++rbit){
|
||||
for(; it != itend; ++it, ++rbit)
|
||||
if(&*rbit != &*it) return 1;
|
||||
}
|
||||
|
||||
//Test the objects inserted in the member hook list
|
||||
for(it = myclassvector.begin(); it != itend; ++it, ++mit){
|
||||
for(it = values.begin(); it != itend; ++it, ++mit)
|
||||
if(&*mit != &*it) return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -15,43 +15,44 @@
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <vector>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
class intrusive_data
|
||||
{
|
||||
int data_id_;
|
||||
public:
|
||||
|
||||
int get() const { return data_id_; }
|
||||
void set(int id) { data_id_ = id; }
|
||||
|
||||
//This class can be inserted in an intrusive list
|
||||
typedef boost::intrusive::
|
||||
list_member_hook<> list_member_hook_t;
|
||||
list_member_hook_t list_hook_;
|
||||
list_member_hook<> list_hook_;
|
||||
|
||||
//This class can be inserted in an intrusive unordered_set
|
||||
typedef boost::intrusive::
|
||||
unordered_set_member_hook<> unordered_set_member_hook_t;
|
||||
unordered_set_member_hook_t unordered_set_hook_;
|
||||
unordered_set_member_hook<> unordered_set_hook_;
|
||||
|
||||
//Comparison operators
|
||||
friend bool operator==(const intrusive_data &a, const intrusive_data &b)
|
||||
{ return a.get() == b.get(); }
|
||||
{ return a.data_id_ == b.data_id_; }
|
||||
|
||||
friend bool operator!=(const intrusive_data &a, const intrusive_data &b)
|
||||
{ return a.get() != b.get(); }
|
||||
{ return a.data_id_ != b.data_id_; }
|
||||
|
||||
//The hash function
|
||||
friend std::size_t hash_value(const intrusive_data &i)
|
||||
{ return boost::hash<int>()(i.data_id_); }
|
||||
};
|
||||
|
||||
//The hash function
|
||||
std::size_t hash_value(const intrusive_data &i)
|
||||
{ return boost::hash<int>()(i.get()); }
|
||||
|
||||
//Definition of the intrusive list that will hold intrusive_data
|
||||
typedef boost::intrusive::list< intrusive_data::list_member_hook_t::
|
||||
value_traits<intrusive_data, &intrusive_data::list_hook_> > list_t;
|
||||
typedef member_hook<intrusive_data, list_member_hook<>
|
||||
, &intrusive_data::list_hook_> MemberListOption;
|
||||
typedef list<intrusive_data, MemberListOption> list_t;
|
||||
|
||||
//Definition of the intrusive unordered_set that will hold intrusive_data
|
||||
typedef boost::intrusive::unordered_set< intrusive_data::unordered_set_member_hook_t::
|
||||
value_traits<intrusive_data, &intrusive_data::unordered_set_hook_> > unordered_set_t;
|
||||
typedef member_hook
|
||||
< intrusive_data, unordered_set_member_hook<>
|
||||
, &intrusive_data::unordered_set_hook_> MemberUsetOption;
|
||||
typedef boost::intrusive::unordered_set
|
||||
< intrusive_data, MemberUsetOption> unordered_set_t;
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -62,30 +63,32 @@ int main()
|
||||
//Declare the intrusive containers
|
||||
list_t list;
|
||||
unordered_set_t::bucket_type buckets[MaxElem];
|
||||
unordered_set_t unordered_set(buckets, MaxElem);
|
||||
unordered_set_t unordered_set
|
||||
(unordered_set_t::bucket_traits(buckets, MaxElem));
|
||||
|
||||
//Initialize all the nodes
|
||||
for(int i = 0; i < MaxElem; ++i)
|
||||
nodes[i].set(i);
|
||||
for(int i = 0; i < MaxElem; ++i) nodes[i].set(i);
|
||||
|
||||
//Now insert them in both intrusive containers
|
||||
list.insert(list.end(), nodes.begin(), nodes.end());
|
||||
unordered_set.insert(nodes.begin(), nodes.end());
|
||||
|
||||
//Now check list::iterator_to
|
||||
//which is an static member function
|
||||
//Now check the iterator_to function
|
||||
list_t::iterator list_it(list.begin());
|
||||
for(int i = 0; i < MaxElem; ++i, ++list_it)
|
||||
if(list_t::iterator_to(nodes[i]) != list_it)
|
||||
if(list.iterator_to(nodes[i]) != list_it ||
|
||||
list_t::s_iterator_to(nodes[i]) != list_it)
|
||||
return 1;
|
||||
|
||||
//Now check unordered_set::iterator_to (which is a member function)
|
||||
//and unordered_set::local_current (which is an static member function)
|
||||
//Now check unordered_set::s_iterator_to (which is a member function)
|
||||
//and unordered_set::s_local_iterator_to (which is an static member function)
|
||||
unordered_set_t::iterator unordered_set_it(unordered_set.begin());
|
||||
for(int i = 0; i < MaxElem; ++i, ++unordered_set_it){
|
||||
if(unordered_set.iterator_to(nodes[i]) != unordered_set.find(nodes[i]))
|
||||
for(int i = 0; i < MaxElem; ++i){
|
||||
unordered_set_it = unordered_set.find(nodes[i]);
|
||||
if(unordered_set.iterator_to(nodes[i]) != unordered_set_it)
|
||||
return 1;
|
||||
if(*unordered_set_t::local_iterator_to(nodes[i]) != *unordered_set.find(nodes[i]))
|
||||
if(*unordered_set.local_iterator_to(nodes[i]) != *unordered_set_it ||
|
||||
*unordered_set_t::s_local_iterator_to(nodes[i]) != *unordered_set_it )
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+17
-31
@@ -15,7 +15,7 @@
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
class MyClass : public list_base_hook<> //This is a derivation hook
|
||||
class MyClass : public list_base_hook<> //This is a derivation hook
|
||||
{
|
||||
int int_;
|
||||
|
||||
@@ -26,63 +26,49 @@ class MyClass : public list_base_hook<> //This is a derivation hook
|
||||
MyClass(int i)
|
||||
: int_(i)
|
||||
{}
|
||||
|
||||
int get() const
|
||||
{ return int_; }
|
||||
};
|
||||
|
||||
//Define a list that will store MyClass using the public base hook
|
||||
typedef list< list_base_hook<>::value_traits<MyClass> > BaseList;
|
||||
typedef list<MyClass> BaseList;
|
||||
|
||||
//Define a list that will store MyClass using the public member hook
|
||||
typedef list< list_member_hook<>::value_traits<MyClass, &MyClass::member_hook_> > MemberList;
|
||||
typedef list< MyClass
|
||||
, member_hook< MyClass, list_member_hook<>, &MyClass::member_hook_>
|
||||
> MemberList;
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<MyClass> Vect;
|
||||
typedef Vect::iterator VectIt;
|
||||
typedef Vect::reverse_iterator VectRit;
|
||||
typedef std::vector<MyClass>::iterator VectIt;
|
||||
typedef std::vector<MyClass>::reverse_iterator VectRit;
|
||||
|
||||
//Create several MyClass objects, each one
|
||||
//with a different internal number
|
||||
Vect myclassvector;
|
||||
for(int i = 0; i < 100; ++i)
|
||||
myclassvector.push_back(MyClass(i));
|
||||
//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));
|
||||
|
||||
BaseList baselist;
|
||||
MemberList memberlist;
|
||||
|
||||
//Now insert them in the reverse order
|
||||
//in the base hook intrusive list
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
; it != itend
|
||||
; ++it){
|
||||
//Now insert them in the reverse order in the base hook list
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
baselist.push_front(*it);
|
||||
}
|
||||
|
||||
//Now insert them in the same order as in vector in the
|
||||
//member hook intrusive list
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
; it != itend
|
||||
; ++it){
|
||||
//Now insert them in the same order as in vector in the member hook list
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
memberlist.push_back(*it);
|
||||
}
|
||||
|
||||
//Now test lists
|
||||
{
|
||||
BaseList::reverse_iterator rbit(baselist.rbegin()), rbitend(baselist.rend());
|
||||
MemberList::iterator mit(memberlist.begin()), mitend(memberlist.end());
|
||||
VectIt it(myclassvector.begin()), itend(myclassvector.end());
|
||||
VectIt it(values.begin()), itend(values.end());
|
||||
|
||||
//Test the objects inserted in the base hook list
|
||||
for(; it != itend; ++it, ++rbit){
|
||||
for(; it != itend; ++it, ++rbit)
|
||||
if(&*rbit != &*it) return 1;
|
||||
}
|
||||
|
||||
//Test the objects inserted in the member hook list
|
||||
for(it = myclassvector.begin(); it != itend; ++it, ++mit){
|
||||
for(it = values.begin(); it != itend; ++it, ++mit)
|
||||
if(&*mit != &*it) return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
+53
-42
@@ -9,35 +9,49 @@
|
||||
// See http://www.boost.org/libs/intrusive for documentation.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//This is needed to allow concurrent test execution in
|
||||
//several platforms. The shared memory must be unique
|
||||
//for each process...
|
||||
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
||||
#include <sstream>
|
||||
|
||||
const char *get_shared_memory_name()
|
||||
{
|
||||
std::stringstream s;
|
||||
s << "process_" << boost::interprocess::detail::get_current_process_id();
|
||||
static std::string str = s.str();
|
||||
return str.c_str();
|
||||
}
|
||||
|
||||
//[doc_offset_ptr_0
|
||||
#include <boost/intrusive/list.hpp>
|
||||
#include <boost/interprocess/offset_ptr.hpp>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
namespace ip = boost::interprocess;
|
||||
|
||||
class shared_memory_data
|
||||
//Declare the hook with an offset_ptr from Boost.Interprocess
|
||||
//to make this class compatible with shared memory
|
||||
: public list_base_hook< void_pointer< ip::offset_ptr<void> > >
|
||||
{
|
||||
int data_id_;
|
||||
public:
|
||||
|
||||
int get() const { return data_id_; }
|
||||
void set(int id) { data_id_ = id; }
|
||||
|
||||
//Declare the hook with an offset_ptr from Boost.Interprocess
|
||||
//to make this class compatible with shared memory
|
||||
typedef boost::intrusive::list_member_hook
|
||||
< boost::intrusive::safe_link
|
||||
, boost::interprocess::offset_ptr<void> > member_hook_t;
|
||||
member_hook_t list_hook_;
|
||||
};
|
||||
//]
|
||||
|
||||
//[doc_offset_ptr_1
|
||||
#include <boost/interprocess/managed_shared_memory.hpp>
|
||||
#include <boost/interprocess/containers/vector.hpp>
|
||||
#include <boost/interprocess/containers/list.hpp>
|
||||
#include <boost/interprocess/allocators/allocator.hpp>
|
||||
|
||||
//Definition of the shared memory friendly intrusive list
|
||||
typedef boost::intrusive::list< shared_memory_data::member_hook_t::
|
||||
value_traits<shared_memory_data, &shared_memory_data::list_hook_> > shm_list_t;
|
||||
typedef ip::list<shared_memory_data> shm_list_t;
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -45,45 +59,42 @@ int main()
|
||||
//nodes and the container itself must be created in shared memory
|
||||
const int MaxElem = 100;
|
||||
const int ShmSize = 50000;
|
||||
const char *ShmName = "SharedMemoryName";
|
||||
const char *ShmName = get_shared_memory_name();
|
||||
{
|
||||
//Erase all old shared memory
|
||||
ip::shared_memory_object::remove(ShmName);
|
||||
ip::managed_shared_memory shm(ip::create_only, ShmName, ShmSize);
|
||||
|
||||
using namespace boost::interprocess;
|
||||
//Create all nodes in shared memory using a shared memory vector
|
||||
//See Boost.Interprocess documentation for more information on this
|
||||
typedef ip::allocator
|
||||
< shared_memory_data, ip::managed_shared_memory::segment_manager>
|
||||
shm_allocator_t;
|
||||
typedef ip::vector<shared_memory_data, shm_allocator_t> shm_vector_t;
|
||||
shm_allocator_t shm_alloc(shm.get_segment_manager());
|
||||
shm_vector_t *pshm_vect =
|
||||
shm.construct<shm_vector_t>(ip::anonymous_instance)(shm_alloc);
|
||||
pshm_vect->resize(MaxElem);
|
||||
|
||||
//Erase all old shared memory
|
||||
shared_memory_object::remove(ShmName);
|
||||
managed_shared_memory shm(create_only, ShmName, ShmSize);
|
||||
//Initialize all the nodes
|
||||
for(int i = 0; i < MaxElem; ++i) (*pshm_vect)[i].set(i);
|
||||
|
||||
//Create all nodes in shared memory using a shared memory vector
|
||||
//See Boost.Interprocess documentation for more information on this
|
||||
typedef allocator< shared_memory_data
|
||||
, managed_shared_memory::segment_manager> shm_allocator_t;
|
||||
typedef vector<shared_memory_data, shm_allocator_t> shm_vector_t;
|
||||
shm_allocator_t shm_alloc(shm.get_segment_manager());
|
||||
shm_vector_t *pshm_vect = shm.construct<shm_vector_t>(anonymous_instance)(shm_alloc);
|
||||
pshm_vect->resize(MaxElem);
|
||||
//Now create the shared memory intrusive list
|
||||
shm_list_t *plist = shm.construct<shm_list_t>(ip::anonymous_instance)();
|
||||
plist->insert(plist->end(), pshm_vect->begin(), pshm_vect->end());
|
||||
|
||||
//Initialize all the nodes
|
||||
for(int i = 0; i < MaxElem; ++i){
|
||||
(*pshm_vect)[i].set(i);
|
||||
}
|
||||
|
||||
//Now create the shared memory intrusive list
|
||||
shm_list_t *plist = shm.construct<shm_list_t>(anonymous_instance)();
|
||||
plist->insert(plist->end(), pshm_vect->begin(), pshm_vect->end());
|
||||
|
||||
//Check all the inserted nodes
|
||||
int checker = 0;
|
||||
for( shm_list_t::const_iterator it = plist->begin(), itend(plist->end())
|
||||
; it != itend
|
||||
; ++it, ++checker){
|
||||
if(it->get() != checker){
|
||||
return false;
|
||||
//Check all the inserted nodes
|
||||
int checker = 0;
|
||||
for( shm_list_t::const_iterator it = plist->begin(), itend(plist->end())
|
||||
; it != itend; ++it, ++checker){
|
||||
if(it->get() != checker) return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Now delete the list and after that, the nodes
|
||||
shm.destroy_ptr(plist);
|
||||
shm.destroy_ptr(pshm_vect);
|
||||
//Now delete the list and after that, the nodes
|
||||
shm.destroy_ptr(plist);
|
||||
shm.destroy_ptr(pshm_vect);
|
||||
}
|
||||
ip::shared_memory_object::remove(ShmName);
|
||||
return 0;
|
||||
}
|
||||
//]
|
||||
+21
-36
@@ -17,7 +17,7 @@
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//This is a base hook
|
||||
class MyClass : public set_base_hook<>
|
||||
class MyClass : public set_base_hook<>
|
||||
{
|
||||
int int_;
|
||||
|
||||
@@ -28,69 +28,54 @@ class MyClass : public set_base_hook<>
|
||||
MyClass(int i)
|
||||
: int_(i)
|
||||
{}
|
||||
int get() const
|
||||
{ return int_; }
|
||||
friend bool operator< (const MyClass &a, const MyClass &b)
|
||||
{ return a.get() < b.get(); }
|
||||
{ return a.int_ < b.int_; }
|
||||
friend bool operator> (const MyClass &a, const MyClass &b)
|
||||
{ return a.get() > b.get(); }
|
||||
{ return a.int_ > b.int_; }
|
||||
friend bool operator== (const MyClass &a, const MyClass &b)
|
||||
{ return a.get() < b.get(); }
|
||||
{ return a.int_ < b.int_; }
|
||||
};
|
||||
|
||||
//Define an set that will store MyClass
|
||||
//in reverse order using the public base hook
|
||||
typedef set< set_base_hook<>::value_traits<MyClass>
|
||||
, std::greater<MyClass> > BaseSet;
|
||||
//Define an set using the base hook that will store values in reverse order
|
||||
typedef set< MyClass, compare<std::greater<MyClass> > > BaseSet;
|
||||
|
||||
//Define an multiset that will store MyClass
|
||||
//using the public member hook
|
||||
typedef multiset< set_member_hook<>::
|
||||
value_traits<MyClass, &MyClass::member_hook_>
|
||||
, std::less<MyClass> > MemberIMultiset;
|
||||
//Define an multiset using the member hook
|
||||
typedef member_hook<MyClass, set_member_hook<>, &MyClass::member_hook_> MemberOption;
|
||||
typedef multiset< MyClass, MemberOption> MemberIMultiset;
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<MyClass> Vect;
|
||||
typedef Vect::iterator VectIt;
|
||||
typedef Vect::reverse_iterator VectRit;
|
||||
typedef std::vector<MyClass>::iterator VectIt;
|
||||
typedef std::vector<MyClass>::reverse_iterator VectRit;
|
||||
|
||||
//Create several MyClass objects, each one
|
||||
//with a different internal number
|
||||
Vect myclassvector;
|
||||
for(int i = 0; i < 100; ++i)
|
||||
myclassvector.push_back(MyClass(i));
|
||||
//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;
|
||||
MemberIMultiset membermultiset;
|
||||
|
||||
//Now insert them in the reverse order
|
||||
//in the base hook intrusive set
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
; it != itend; ++it)
|
||||
//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 intrusive set
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
; it != itend; ++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)
|
||||
membermultiset.insert(*it);
|
||||
|
||||
//Now test sets
|
||||
{
|
||||
BaseSet::reverse_iterator rbit(baseset.rbegin()), rbitend(baseset.rend());
|
||||
MemberIMultiset::iterator mit(membermultiset.begin()), mitend(membermultiset.end());
|
||||
VectIt it(myclassvector.begin()), itend(myclassvector.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 = myclassvector.begin(); it != itend; ++it, ++mit){
|
||||
for(it = values.begin(); it != itend; ++it, ++mit)
|
||||
if(&*mit != &*it) return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
+17
-30
@@ -16,7 +16,7 @@
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//This is a base hook
|
||||
class MyClass : public slist_base_hook<>
|
||||
class MyClass : public slist_base_hook<>
|
||||
{
|
||||
int int_;
|
||||
|
||||
@@ -27,45 +27,34 @@ class MyClass : public slist_base_hook<>
|
||||
MyClass(int i)
|
||||
: int_(i)
|
||||
{}
|
||||
|
||||
int get() const
|
||||
{ return int_; }
|
||||
};
|
||||
|
||||
//Define an slist that will store MyClass using the public base hook
|
||||
typedef slist< slist_base_hook<>::value_traits<MyClass> > BaseList;
|
||||
typedef slist<MyClass> BaseList;
|
||||
|
||||
//Define an slist that will store MyClass using the public member hook
|
||||
typedef slist< slist_member_hook<>::value_traits<MyClass, &MyClass::member_hook_> > MemberList;
|
||||
typedef member_hook<MyClass, slist_member_hook<>, &MyClass::member_hook_> MemberOption;
|
||||
typedef slist<MyClass, MemberOption> MemberList;
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<MyClass> Vect;
|
||||
typedef Vect::iterator VectIt;
|
||||
typedef Vect::reverse_iterator VectRit;
|
||||
typedef std::vector<MyClass>::iterator VectIt;
|
||||
typedef std::vector<MyClass>::reverse_iterator VectRit;
|
||||
|
||||
//Create several MyClass objects, each one
|
||||
//with a different internal number
|
||||
Vect myclassvector;
|
||||
for(int i = 0; i < 100; ++i)
|
||||
myclassvector.push_back(MyClass(i));
|
||||
//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));
|
||||
|
||||
BaseList baselist;
|
||||
MemberList memberlist;
|
||||
|
||||
//Now insert them in the reverse order
|
||||
//in the base hook intrusive list
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
; it != itend
|
||||
; ++it){
|
||||
//Now insert them in the reverse order in the base hook list
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
baselist.push_front(*it);
|
||||
}
|
||||
|
||||
//Now insert them in the same order as in vector in the
|
||||
//member hook intrusive list
|
||||
//Now insert them in the same order as in vector in the member hook list
|
||||
for(BaseList::iterator it(baselist.begin()), itend(baselist.end())
|
||||
; it != itend
|
||||
; ++it){
|
||||
; it != itend; ++it){
|
||||
memberlist.push_front(*it);
|
||||
}
|
||||
|
||||
@@ -73,18 +62,16 @@ int main()
|
||||
{
|
||||
BaseList::iterator bit(baselist.begin()), bitend(baselist.end());
|
||||
MemberList::iterator mit(memberlist.begin()), mitend(memberlist.end());
|
||||
VectRit rit(myclassvector.rbegin()), ritend(myclassvector.rend());
|
||||
VectIt it(myclassvector.begin()), itend(myclassvector.end());
|
||||
VectRit rit(values.rbegin()), ritend(values.rend());
|
||||
VectIt it(values.begin()), itend(values.end());
|
||||
|
||||
//Test the objects inserted in the base hook list
|
||||
for(; rit != ritend; ++rit, ++bit){
|
||||
for(; rit != ritend; ++rit, ++bit)
|
||||
if(&*bit != &*rit) return 1;
|
||||
}
|
||||
|
||||
//Test the objects inserted in the member hook list
|
||||
for(; it != itend; ++it, ++mit){
|
||||
for(; it != itend; ++it, ++mit)
|
||||
if(&*mit != &*it) return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (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_stateful_value_traits
|
||||
#include <boost/intrusive/list.hpp>
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//This type is not modifiable so we can't store hooks or custom nodes
|
||||
typedef int identifier_t;
|
||||
|
||||
//This value traits will associate elements from an array of identifiers with
|
||||
//elements of an array of nodes. The element i of the value array will use the
|
||||
//node i of the node array:
|
||||
struct stateful_value_traits
|
||||
{
|
||||
typedef list_node_traits<void*> node_traits;
|
||||
typedef node_traits::node node;
|
||||
typedef node * node_ptr;
|
||||
typedef const node * const_node_ptr;
|
||||
typedef identifier_t value_type;
|
||||
typedef identifier_t * pointer;
|
||||
typedef const identifier_t * const_pointer;
|
||||
static const link_mode_type link_mode = normal_link;
|
||||
|
||||
stateful_value_traits(pointer ids, node_ptr node_array)
|
||||
: ids_(ids), nodes_(node_array)
|
||||
{}
|
||||
|
||||
///Note: non static functions!
|
||||
node_ptr to_node_ptr (value_type &value)
|
||||
{ return this->nodes_ + (&value - this->ids_); }
|
||||
const_node_ptr to_node_ptr (const value_type &value) const
|
||||
{ return this->nodes_ + (&value - this->ids_); }
|
||||
pointer to_value_ptr(node_ptr n)
|
||||
{ return this->ids_ + (n - this->nodes_); }
|
||||
const_pointer to_value_ptr(const_node_ptr n) const
|
||||
{ return this->ids_ + (n - this->nodes_); }
|
||||
|
||||
private:
|
||||
pointer ids_;
|
||||
node_ptr nodes_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
const int NumElements = 100;
|
||||
|
||||
//This is an array of ids that we want to "store"
|
||||
identifier_t ids [NumElements];
|
||||
|
||||
//This is an array of nodes that is necessary to form the linked list
|
||||
list_node_traits<void*>::node nodes [NumElements];
|
||||
|
||||
//Initialize id objects, each one with a different number
|
||||
for(int i = 0; i != NumElements; ++i) ids[i] = i;
|
||||
|
||||
//Define a list that will "link" identifiers using external nodes
|
||||
typedef list<identifier_t, value_traits<stateful_value_traits> > List;
|
||||
|
||||
//This list will store ids without modifying identifier_t instances
|
||||
//Stateful value traits must be explicitly passed in the constructor.
|
||||
List my_list (stateful_value_traits (ids, nodes));
|
||||
|
||||
//Insert ids in reverse order in the list
|
||||
for(identifier_t * it(&ids[0]), *itend(&ids[NumElements]); it != itend; ++it)
|
||||
my_list.push_front(*it);
|
||||
|
||||
//Now test lists
|
||||
List::const_iterator list_it (my_list.cbegin());
|
||||
identifier_t *it_val(&ids[NumElements-1]), *it_rbeg_val(&ids[0]-1);
|
||||
|
||||
//Test the objects inserted in the base hook list
|
||||
for(; it_val != it_rbeg_val; --it_val, ++list_it)
|
||||
if(&*list_it != &*it_val) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
//]
|
||||
@@ -17,56 +17,43 @@
|
||||
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//This is a derivation hook
|
||||
class MyClass : public unordered_set_base_hook<>
|
||||
{
|
||||
class MyClass : public unordered_set_base_hook<>
|
||||
{ //This is a derivation hook
|
||||
int int_;
|
||||
|
||||
public:
|
||||
//This is a member hook
|
||||
unordered_set_member_hook<> member_hook_;
|
||||
unordered_set_member_hook<> member_hook_; //This is a member hook
|
||||
|
||||
MyClass(int i)
|
||||
: int_(i)
|
||||
{}
|
||||
|
||||
int get() const
|
||||
{ return int_; }
|
||||
|
||||
friend bool operator== (const MyClass &a, const MyClass &b)
|
||||
{ return a.get() == b.get(); }
|
||||
{ return a.int_ == b.int_; }
|
||||
|
||||
friend bool operator> (const MyClass &a, const MyClass &b)
|
||||
{ return a.get() > b.get(); }
|
||||
friend std::size_t hash_value(const MyClass &value)
|
||||
{ return std::size_t(value.int_); }
|
||||
};
|
||||
|
||||
std::size_t hash_value(const MyClass &value)
|
||||
{ return std::size_t(value.get()); }
|
||||
//Define an unordered_set that will store MyClass objects using the base hook
|
||||
typedef unordered_set<MyClass> BaseSet;
|
||||
|
||||
//Define an unordered_set that will store MyClass
|
||||
//in reverse order using the public base hook
|
||||
typedef unordered_set< unordered_set_base_hook<>::
|
||||
value_traits<MyClass> > BaseSet;
|
||||
|
||||
//Define an unordered_multiset that will store MyClass
|
||||
//using the public member hook
|
||||
typedef unordered_multiset< unordered_set_member_hook<>::
|
||||
value_traits<MyClass, &MyClass::member_hook_> > MemberMultiSet;
|
||||
//Define an unordered_multiset that will store MyClass using the member hook
|
||||
typedef member_hook<MyClass, unordered_set_member_hook<>, &MyClass::member_hook_>
|
||||
MemberOption;
|
||||
typedef unordered_multiset< MyClass, MemberOption> MemberMultiSet;
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<MyClass> Vect;
|
||||
typedef Vect::iterator VectIt;
|
||||
typedef Vect::reverse_iterator VectRit;
|
||||
typedef std::vector<MyClass>::iterator VectIt;
|
||||
typedef std::vector<MyClass>::reverse_iterator VectRit;
|
||||
|
||||
//Create a vector with 100 different MyClass objects,
|
||||
//each one with a different internal number
|
||||
Vect myclassvector;
|
||||
for(int i = 0; i < 100; ++i)
|
||||
myclassvector.push_back(MyClass(i));
|
||||
//Create a vector with 100 different MyClass objects
|
||||
std::vector<MyClass> values;
|
||||
for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
|
||||
|
||||
//Create a copy of the vector
|
||||
Vect myclassvector2(myclassvector);
|
||||
std::vector<MyClass> values2(values);
|
||||
|
||||
//Create a bucket array for base_set
|
||||
BaseSet::bucket_type base_buckets[100];
|
||||
@@ -74,20 +61,18 @@ int main()
|
||||
//Create a bucket array for member_multi_set
|
||||
MemberMultiSet::bucket_type member_buckets[200];
|
||||
|
||||
//Create a the unordered_set and unordered_multiset,
|
||||
//taking buckets as arguments
|
||||
BaseSet base_set(base_buckets, 100);
|
||||
MemberMultiSet member_multi_set(member_buckets, 200);
|
||||
//Create unordered containers taking buckets as arguments
|
||||
BaseSet base_set(BaseSet::bucket_traits(base_buckets, 100));
|
||||
MemberMultiSet member_multi_set
|
||||
(MemberMultiSet::bucket_traits(member_buckets, 200));
|
||||
|
||||
//Now insert myclassvector's elements in the unordered_set
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end())
|
||||
; it != itend; ++it){
|
||||
//Now insert values's elements in the unordered_set
|
||||
for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
|
||||
base_set.insert(*it);
|
||||
}
|
||||
|
||||
//Now insert myclassvector's and myclassvector2's elements in the unordered_multiset
|
||||
for(VectIt it(myclassvector.begin()), itend(myclassvector.end()),
|
||||
it2(myclassvector2.begin()),itend2(myclassvector2.end())
|
||||
//Now insert values's and values2's elements in the unordered_multiset
|
||||
for(VectIt it(values.begin()), itend(values.end()),
|
||||
it2(values2.begin()),itend2(values2.end())
|
||||
; it != itend; ++it, ++it2){
|
||||
member_multi_set.insert(*it);
|
||||
member_multi_set.insert(*it2);
|
||||
@@ -95,7 +80,7 @@ int main()
|
||||
|
||||
//Now find every element
|
||||
{
|
||||
VectIt it(myclassvector.begin()), itend(myclassvector.end());
|
||||
VectIt it(values.begin()), itend(values.end());
|
||||
|
||||
for(; it != itend; ++it){
|
||||
//base_set should contain one element for each key
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//[doc_value_traits_code_legacy
|
||||
#include <boost/intrusive/linking_policy.hpp>
|
||||
#include <boost/intrusive/link_mode.hpp>
|
||||
#include <boost/intrusive/list.hpp>
|
||||
#include <boost/intrusive/slist.hpp>
|
||||
#include <vector>
|
||||
@@ -30,6 +30,9 @@ struct legacy_value
|
||||
//Define our own NodeTraits that will configure singly and doubly linked
|
||||
//list algorithms. Note that this node traits is compatible with
|
||||
//circular_slist_algorithms and circular_list_algorithms.
|
||||
|
||||
namespace bi = boost::intrusive;
|
||||
|
||||
struct legacy_node_traits
|
||||
{
|
||||
typedef legacy_value node;
|
||||
@@ -54,7 +57,7 @@ struct legacy_value_traits
|
||||
typedef legacy_value value_type;
|
||||
typedef legacy_value * pointer;
|
||||
typedef const legacy_value * const_pointer;
|
||||
enum { linking_policy = boost::intrusive::normal_link };
|
||||
static const bi::link_mode_type link_mode = bi::normal_link;
|
||||
static node_ptr to_node_ptr (value_type &value) { return node_ptr(&value); }
|
||||
static const_node_ptr to_node_ptr (const value_type &value) { return const_node_ptr(&value); }
|
||||
static pointer to_value_ptr(node_ptr n) { return pointer(n); }
|
||||
@@ -65,8 +68,9 @@ struct legacy_value_traits
|
||||
|
||||
//[doc_value_traits_test
|
||||
//Now define an intrusive list and slist that will store legacy_value objects
|
||||
typedef boost::intrusive::list <legacy_value_traits> LegacyAbiList;
|
||||
typedef boost::intrusive::slist<legacy_value_traits> LegacyAbiSlist;
|
||||
typedef bi::value_traits<legacy_value_traits> ValueTraitsOption;
|
||||
typedef bi::list<legacy_value, ValueTraitsOption> LegacyAbiList;
|
||||
typedef bi::slist<legacy_value, ValueTraitsOption> LegacyAbiSlist;
|
||||
|
||||
template<class List>
|
||||
bool test_list()
|
||||
@@ -87,9 +91,8 @@ bool test_list()
|
||||
typename Vect::const_iterator it(legacy_vector.begin()), itend(legacy_vector.end());
|
||||
|
||||
//Test the objects inserted in our list
|
||||
for(; it != itend; ++it, ++bit){
|
||||
for(; it != itend; ++it, ++bit)
|
||||
if(&*bit != &*it) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
using namespace boost::intrusive;
|
||||
|
||||
//An abstract class that can be inserted in an intrusive list
|
||||
class Window : public list_base_hook<>
|
||||
class Window : public list_base_hook<>
|
||||
{
|
||||
public:
|
||||
//This is a container those value is an abstract class: you can't do this with std::list.
|
||||
typedef list< list_base_hook<>::value_traits<Window> > win_list;
|
||||
typedef list<Window> win_list;
|
||||
|
||||
//An static intrusive list declaration
|
||||
static win_list all_windows;
|
||||
@@ -27,7 +27,7 @@ class Window : public list_base_hook<>
|
||||
//Constructor. Includes this window in the list
|
||||
Window() { all_windows.push_back(*this); }
|
||||
//Destructor. Removes this node from the list
|
||||
virtual ~Window() { all_windows.erase(win_list::iterator_to(*this)); }
|
||||
virtual ~Window() { all_windows.erase(win_list::s_iterator_to(*this)); }
|
||||
//Pure virtual function to be implemented by derived classes
|
||||
virtual void Paint() = 0;
|
||||
};
|
||||
@@ -71,15 +71,13 @@ class MainWindow : public Window
|
||||
//Main function
|
||||
int main()
|
||||
{
|
||||
//When each Window class is created, is
|
||||
//automatically registered in the global list
|
||||
//When a Window class is created, is automatically registered in the global list
|
||||
MainWindow window;
|
||||
|
||||
//Paint all the windows, sub-windows and so on
|
||||
paint_all_windows();
|
||||
|
||||
//All the windows are automatically unregistered
|
||||
//in their destructors.
|
||||
//All the windows are automatically unregistered in their destructors.
|
||||
return 0;
|
||||
}
|
||||
//]
|
||||
Reference in new issue
Block a user