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