/Src/Dependencies/Boost/libs/unordered/test/helpers/tracker.hpp

http://hadesmem.googlecode.com/ · C++ Header · 178 lines · 151 code · 22 blank · 5 comment · 4 complexity · a0af0d103089b948f174ac909a8116ff MD5 · raw file

  1. // Copyright 2006-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. // This header contains metafunctions/functions to get the equivalent
  5. // associative container for an unordered container, and compare the contents.
  6. #if !defined(BOOST_UNORDERED_TEST_HELPERS_TRACKER_HEADER)
  7. #define BOOST_UNORDERED_TEST_HELPERS_TRACKER_HEADER
  8. #include <set>
  9. #include <map>
  10. #include <iterator>
  11. #include <algorithm>
  12. #include <boost/mpl/if.hpp>
  13. #include <boost/mpl/eval_if.hpp>
  14. #include <boost/mpl/identity.hpp>
  15. #include <boost/type_traits/is_same.hpp>
  16. #include "../objects/fwd.hpp"
  17. #include "./metafunctions.hpp"
  18. #include "./helpers.hpp"
  19. #include "./equivalent.hpp"
  20. #include "./list.hpp"
  21. namespace test
  22. {
  23. template <class X>
  24. struct equals_to_compare2
  25. : public boost::mpl::identity<
  26. std::less<BOOST_DEDUCED_TYPENAME X::first_argument_type> >
  27. {
  28. };
  29. template <class X>
  30. struct equals_to_compare
  31. : public boost::mpl::eval_if<
  32. boost::is_same<X, test::equal_to>,
  33. boost::mpl::identity<test::less>,
  34. equals_to_compare2<X>
  35. >
  36. {
  37. };
  38. template <class X1, class X2>
  39. void compare_range(X1 const& x1, X2 const& x2)
  40. {
  41. typedef test::list<BOOST_DEDUCED_TYPENAME X1::value_type> value_list;
  42. value_list values1(x1.begin(), x1.end());
  43. value_list values2(x2.begin(), x2.end());
  44. values1.sort();
  45. values2.sort();
  46. BOOST_TEST(values1.size() == values2.size() &&
  47. test::equal(values1.begin(), values1.end(), values2.begin(),
  48. test::equivalent));
  49. }
  50. template <class X1, class X2, class T>
  51. void compare_pairs(X1 const& x1, X2 const& x2, T*)
  52. {
  53. test::list<T> values1(x1.first, x1.second);
  54. test::list<T> values2(x2.first, x2.second);
  55. values1.sort();
  56. values2.sort();
  57. BOOST_TEST(values1.size() == values2.size() &&
  58. test::equal(values1.begin(), values1.end(),
  59. values2.begin(), test::equivalent));
  60. }
  61. template <class X>
  62. struct ordered_set : public
  63. boost::mpl::if_<
  64. test::has_unique_keys<X>,
  65. std::set<
  66. BOOST_DEDUCED_TYPENAME X::value_type,
  67. BOOST_DEDUCED_TYPENAME equals_to_compare<
  68. BOOST_DEDUCED_TYPENAME X::key_equal
  69. >::type
  70. >,
  71. std::multiset<
  72. BOOST_DEDUCED_TYPENAME X::value_type,
  73. BOOST_DEDUCED_TYPENAME equals_to_compare<
  74. BOOST_DEDUCED_TYPENAME X::key_equal
  75. >::type
  76. >
  77. > {};
  78. template <class X>
  79. struct ordered_map : public
  80. boost::mpl::if_<
  81. test::has_unique_keys<X>,
  82. std::map<
  83. BOOST_DEDUCED_TYPENAME X::key_type,
  84. BOOST_DEDUCED_TYPENAME X::mapped_type,
  85. BOOST_DEDUCED_TYPENAME equals_to_compare<
  86. BOOST_DEDUCED_TYPENAME X::key_equal
  87. >::type
  88. >,
  89. std::multimap<
  90. BOOST_DEDUCED_TYPENAME X::key_type,
  91. BOOST_DEDUCED_TYPENAME X::mapped_type,
  92. BOOST_DEDUCED_TYPENAME equals_to_compare<
  93. BOOST_DEDUCED_TYPENAME X::key_equal
  94. >::type
  95. >
  96. > {};
  97. template <class X>
  98. struct ordered_base : public
  99. boost::mpl::eval_if<
  100. test::is_set<X>,
  101. test::ordered_set<X>,
  102. test::ordered_map<X>
  103. > {};
  104. template <class X>
  105. class ordered : public ordered_base<X>::type
  106. {
  107. typedef BOOST_DEDUCED_TYPENAME ordered_base<X>::type base;
  108. public:
  109. typedef BOOST_DEDUCED_TYPENAME base::key_compare key_compare;
  110. ordered()
  111. : base()
  112. {}
  113. explicit ordered(key_compare const& compare)
  114. : base(compare)
  115. {}
  116. void compare(X const& x)
  117. {
  118. compare_range(x, *this);
  119. }
  120. void compare_key(X const& x,
  121. BOOST_DEDUCED_TYPENAME X::value_type const& val)
  122. {
  123. compare_pairs(
  124. x.equal_range(get_key<X>(val)),
  125. this->equal_range(get_key<X>(val)),
  126. (BOOST_DEDUCED_TYPENAME X::value_type*) 0);
  127. }
  128. template <class It>
  129. void insert_range(It begin, It end) {
  130. while(begin != end) {
  131. this->insert(*begin);
  132. ++begin;
  133. }
  134. }
  135. };
  136. template <class Equals>
  137. BOOST_DEDUCED_TYPENAME
  138. equals_to_compare<Equals>::type create_compare(Equals const&)
  139. {
  140. BOOST_DEDUCED_TYPENAME equals_to_compare<Equals>::type x;
  141. return x;
  142. }
  143. template <class X>
  144. ordered<X> create_ordered(X const& container)
  145. {
  146. return ordered<X>(create_compare(container.key_eq()));
  147. }
  148. template <class X1, class X2>
  149. void check_container(X1 const& container, X2 const& values)
  150. {
  151. ordered<X1> tracker = create_ordered(container);
  152. tracker.insert_range(values.begin(), values.end());
  153. tracker.compare(container);
  154. }
  155. }
  156. #endif