doc_map.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015
  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_map_code
  13. #include <boost/static_assert.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/intrusive/set.hpp>
  16. #include <boost/intrusive/unordered_set.hpp>
  17. #include <vector>
  18. #include <cassert>
  19. using namespace boost::intrusive;
  20. class MyClass : public set_base_hook<>
  21. , public unordered_set_base_hook<>
  22. {
  23. public:
  24. int first;
  25. explicit MyClass(int i) : first(i){}
  26. };
  27. //key_of_value function object, must:
  28. //- be default constructible if the container constructor requires it
  29. //- define the key type using "type"
  30. //- define an operator() taking "const value_type&" and
  31. // returning "type" or "const type &"
  32. struct first_int_is_key
  33. {
  34. typedef int type;
  35. const type & operator()(const MyClass& v) const
  36. { return v.first; }
  37. };
  38. //Define omap like ordered and unordered classes
  39. typedef set< MyClass, key_of_value<first_int_is_key> > OrderedMap;
  40. typedef unordered_set< MyClass, key_of_value<first_int_is_key> > UnorderedMap;
  41. int main()
  42. {
  43. BOOST_STATIC_ASSERT((boost::is_same< OrderedMap::key_type, int>::value));
  44. BOOST_STATIC_ASSERT((boost::is_same<UnorderedMap::key_type, int>::value));
  45. //Create several MyClass objects, each one with a different value
  46. //and insert them into the omap
  47. std::vector<MyClass> values;
  48. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
  49. //Create ordered/unordered maps and insert values
  50. OrderedMap omap(values.begin(), values.end());
  51. UnorderedMap::bucket_type buckets[100];
  52. UnorderedMap umap(values.begin(), values.end(), UnorderedMap::bucket_traits(buckets, 100));
  53. //Test each element using the key_type (int)
  54. for(int i = 0; i != 100; ++i){
  55. assert(omap.find(i) != omap.end());
  56. assert(umap.find(i) != umap.end());
  57. assert(omap.lower_bound(i) != omap.end());
  58. assert(++omap.lower_bound(i) == omap.upper_bound(i));
  59. assert(omap.equal_range(i).first != omap.equal_range(i).second);
  60. assert(umap.equal_range(i).first != umap.equal_range(i).second);
  61. }
  62. //Count and erase by key
  63. for(int i = 0; i != 100; ++i){
  64. assert(1 == omap.count(i));
  65. assert(1 == umap.count(i));
  66. assert(1 == omap.erase(i));
  67. assert(1 == umap.erase(i));
  68. }
  69. assert(omap.empty());
  70. assert(umap.empty());
  71. return 0;
  72. }
  73. //]