doc_avl_set.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //[doc_avl_set_code
  13. #include <boost/intrusive/avl_set.hpp>
  14. #include <vector>
  15. #include <functional>
  16. #include <cassert>
  17. using namespace boost::intrusive;
  18. //This is a base hook optimized for size
  19. class MyClass : public avl_set_base_hook<optimize_size<true> >
  20. {
  21. int int_;
  22. public:
  23. //This is a member hook
  24. avl_set_member_hook<> member_hook_;
  25. MyClass(int i)
  26. : int_(i)
  27. {}
  28. friend bool operator< (const MyClass &a, const MyClass &b)
  29. { return a.int_ < b.int_; }
  30. friend bool operator> (const MyClass &a, const MyClass &b)
  31. { return a.int_ > b.int_; }
  32. friend bool operator== (const MyClass &a, const MyClass &b)
  33. { return a.int_ == b.int_; }
  34. };
  35. //Define an avl_set using the base hook that will store values in reverse order
  36. typedef avl_set< MyClass, compare<std::greater<MyClass> > > BaseSet;
  37. //Define an multiset using the member hook
  38. typedef member_hook<MyClass, avl_set_member_hook<>, &MyClass::member_hook_> MemberOption;
  39. typedef avl_multiset< MyClass, MemberOption> MemberMultiset;
  40. int main()
  41. {
  42. typedef std::vector<MyClass>::iterator VectIt;
  43. //Create several MyClass objects, each one with a different value
  44. std::vector<MyClass> values;
  45. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
  46. BaseSet baseset;
  47. MemberMultiset membermultiset;
  48. //Check that size optimization is activated in the base hook
  49. assert(sizeof(avl_set_base_hook<optimize_size<true> >) == 3*sizeof(void*));
  50. //Check that size optimization is deactivated in the member hook
  51. assert(sizeof(avl_set_member_hook<>) > 3*sizeof(void*));
  52. //Now insert them in the sets
  53. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
  54. baseset.insert(*it);
  55. membermultiset.insert(*it);
  56. }
  57. //Now test avl_sets
  58. {
  59. BaseSet::reverse_iterator rbit(baseset.rbegin());
  60. MemberMultiset::iterator mit(membermultiset.begin());
  61. VectIt it(values.begin()), itend(values.end());
  62. //Test the objects inserted in the base hook avl_set
  63. for(; it != itend; ++it, ++rbit)
  64. if(&*rbit != &*it) return 1;
  65. //Test the objects inserted in the member hook avl_set
  66. for(it = values.begin(); it != itend; ++it, ++mit)
  67. if(&*mit != &*it) return 1;
  68. }
  69. return 0;
  70. }
  71. //]