From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/intrusive/example/doc_set.cpp | 85 ++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/boost/libs/intrusive/example/doc_set.cpp (limited to 'src/boost/libs/intrusive/example/doc_set.cpp') diff --git a/src/boost/libs/intrusive/example/doc_set.cpp b/src/boost/libs/intrusive/example/doc_set.cpp new file mode 100644 index 00000000..dae81abc --- /dev/null +++ b/src/boost/libs/intrusive/example/doc_set.cpp @@ -0,0 +1,85 @@ +///////////////////////////////////////////////////////////////////////////// +// +// (C) Copyright Ion Gaztanaga 2006-2013 +// +// 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_set_code +#include +#include +#include +#include + +using namespace boost::intrusive; + + //This is a base hook optimized for size +class MyClass : public set_base_hook > +{ + int int_; + + public: + //This is a member hook + 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 a set using the base hook that will store values in reverse order +typedef set< MyClass, compare > > BaseSet; + +//Define an multiset using the member hook +typedef member_hook, &MyClass::member_hook_> MemberOption; +typedef multiset< MyClass, MemberOption> MemberMultiset; + +int main() +{ + typedef std::vector::iterator VectIt; + + //Create several MyClass objects, each one with a different value + std::vector 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(set_base_hook >) == 3*sizeof(void*)); + //Check that size optimization is deactivated in the member hook + assert(sizeof(set_member_hook<>) > 3*sizeof(void*)); + + //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); + membermultiset.insert(*it); + } + + //Now test sets + { + BaseSet::reverse_iterator rbit(baseset.rbegin()); + MemberMultiset::iterator mit(membermultiset.begin()); + VectIt it(values.begin()), itend(values.end()); + + //Test the objects inserted in the base hook set + 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; + } + return 0; +} +//] -- cgit v1.2.3