doc_treap_set.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_treap_set_code
  13. #include <boost/intrusive/treap_set.hpp>
  14. #include <vector>
  15. #include <functional>
  16. #include <cassert>
  17. using namespace boost::intrusive;
  18. class MyClass : public bs_set_base_hook<> //This is a base hook
  19. {
  20. int int_;
  21. unsigned int prio_;
  22. public:
  23. //This is a member hook
  24. bs_set_member_hook<> member_hook_;
  25. MyClass(int i, unsigned int prio) : int_(i), prio_(prio)
  26. {}
  27. unsigned int get_priority() const
  28. { return this->prio_; }
  29. //Less and greater operators
  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. //Default priority compare
  35. friend bool priority_order (const MyClass &a, const MyClass &b)
  36. { return a.prio_ < b.prio_; } //Lower value means higher priority
  37. //Inverse priority compare
  38. friend bool priority_inverse_order (const MyClass &a, const MyClass &b)
  39. { return a.prio_ > b.prio_; } //Higher value means higher priority
  40. };
  41. struct inverse_priority
  42. {
  43. bool operator()(const MyClass &a, const MyClass &b) const
  44. { return priority_inverse_order(a, b); }
  45. };
  46. //Define an treap_set using the base hook that will store values in reverse order
  47. typedef treap_set< MyClass, compare<std::greater<MyClass> > > BaseSet;
  48. //Define an multiset using the member hook that will store
  49. typedef member_hook<MyClass, bs_set_member_hook<>, &MyClass::member_hook_> MemberOption;
  50. typedef treap_multiset
  51. < MyClass, MemberOption, priority<inverse_priority> > MemberMultiset;
  52. int main()
  53. {
  54. typedef std::vector<MyClass>::iterator VectIt;
  55. //Create several MyClass objects, each one with a different value
  56. std::vector<MyClass> values;
  57. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i, (i % 10)));
  58. BaseSet baseset;
  59. MemberMultiset membermultiset;
  60. //Now insert them in the sets
  61. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
  62. baseset.insert(*it);
  63. membermultiset.insert(*it);
  64. }
  65. //Now test treap_sets
  66. {
  67. BaseSet::reverse_iterator rbit(baseset.rbegin());
  68. MemberMultiset::iterator mit(membermultiset.begin());
  69. VectIt it(values.begin()), itend(values.end());
  70. //Test the objects inserted in the base hook treap_set
  71. for(; it != itend; ++it, ++rbit)
  72. if(&*rbit != &*it) return 1;
  73. //Test the objects inserted in the member hook treap_set
  74. for(it = values.begin(); it != itend; ++it, ++mit)
  75. if(&*mit != &*it) return 1;
  76. //Test priority order
  77. for(int i = 0; i < 100; ++i){
  78. if(baseset.top()->get_priority() != static_cast<unsigned int>(i/10))
  79. return 1;
  80. if(membermultiset.top()->get_priority() != 9u - static_cast<unsigned int>(i/10))
  81. return 1;
  82. baseset.erase(baseset.top());
  83. membermultiset.erase(membermultiset.top());
  84. }
  85. }
  86. return 0;
  87. }
  88. //]