PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 178 lines | 151 code | 22 blank | 5 comment | 3 complexity | a0af0d103089b948f174ac909a8116ff MD5 | raw file
  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 <boost/mpl/if.hpp>
 17#include <boost/mpl/eval_if.hpp>
 18#include <boost/mpl/identity.hpp>
 19#include <boost/type_traits/is_same.hpp>
 20#include "../objects/fwd.hpp"
 21#include "./metafunctions.hpp"
 22#include "./helpers.hpp"
 23#include "./equivalent.hpp"
 24#include "./list.hpp"
 25
 26namespace test
 27{
 28    template <class X>
 29    struct equals_to_compare2
 30        : public boost::mpl::identity<
 31            std::less<BOOST_DEDUCED_TYPENAME X::first_argument_type> >
 32    {
 33    };
 34
 35    template <class X>
 36    struct equals_to_compare
 37        : public boost::mpl::eval_if<
 38            boost::is_same<X, test::equal_to>,
 39            boost::mpl::identity<test::less>,
 40            equals_to_compare2<X>
 41        >
 42    {
 43    };
 44
 45    template <class X1, class X2>
 46    void compare_range(X1 const& x1, X2 const& x2)
 47    {
 48        typedef test::list<BOOST_DEDUCED_TYPENAME X1::value_type> value_list;
 49        value_list values1(x1.begin(), x1.end());
 50        value_list values2(x2.begin(), x2.end());
 51        values1.sort();
 52        values2.sort();
 53        BOOST_TEST(values1.size() == values2.size() &&
 54                test::equal(values1.begin(), values1.end(), values2.begin(),
 55                    test::equivalent));
 56    }
 57
 58    template <class X1, class X2, class T>
 59    void compare_pairs(X1 const& x1, X2 const& x2, T*)
 60    {
 61        test::list<T> values1(x1.first, x1.second);
 62        test::list<T> values2(x2.first, x2.second);
 63        values1.sort();
 64        values2.sort();
 65        BOOST_TEST(values1.size() == values2.size() &&
 66                test::equal(values1.begin(), values1.end(),
 67                    values2.begin(), test::equivalent));
 68    }
 69
 70    template <class X>
 71    struct ordered_set : public
 72        boost::mpl::if_<
 73            test::has_unique_keys<X>,
 74            std::set<
 75                BOOST_DEDUCED_TYPENAME X::value_type,
 76                BOOST_DEDUCED_TYPENAME equals_to_compare<
 77                    BOOST_DEDUCED_TYPENAME X::key_equal
 78                >::type
 79            >,
 80            std::multiset<
 81                BOOST_DEDUCED_TYPENAME X::value_type,
 82                BOOST_DEDUCED_TYPENAME equals_to_compare<
 83                    BOOST_DEDUCED_TYPENAME X::key_equal
 84                >::type
 85            >
 86        > {};
 87
 88    template <class X>
 89    struct ordered_map : public
 90        boost::mpl::if_<
 91            test::has_unique_keys<X>,
 92            std::map<
 93                BOOST_DEDUCED_TYPENAME X::key_type,
 94                BOOST_DEDUCED_TYPENAME X::mapped_type,
 95                BOOST_DEDUCED_TYPENAME equals_to_compare<
 96                    BOOST_DEDUCED_TYPENAME X::key_equal
 97                >::type
 98            >,
 99            std::multimap<
100                BOOST_DEDUCED_TYPENAME X::key_type,
101                BOOST_DEDUCED_TYPENAME X::mapped_type,
102                BOOST_DEDUCED_TYPENAME equals_to_compare<
103                    BOOST_DEDUCED_TYPENAME X::key_equal
104                >::type
105            >
106        > {};
107
108    template <class X>
109    struct ordered_base : public
110        boost::mpl::eval_if<
111            test::is_set<X>,
112            test::ordered_set<X>,
113            test::ordered_map<X>
114        > {};
115
116    template <class X>
117    class ordered : public ordered_base<X>::type
118    {
119        typedef BOOST_DEDUCED_TYPENAME ordered_base<X>::type base;
120    public:
121        typedef BOOST_DEDUCED_TYPENAME base::key_compare key_compare;
122
123        ordered()
124            : base()
125        {}
126
127        explicit ordered(key_compare const& compare)
128            : base(compare)
129        {}
130
131        void compare(X const& x)
132        {
133            compare_range(x, *this);
134        }
135
136        void compare_key(X const& x,
137            BOOST_DEDUCED_TYPENAME X::value_type const& val)
138        {
139            compare_pairs(
140                x.equal_range(get_key<X>(val)),
141                this->equal_range(get_key<X>(val)),
142                (BOOST_DEDUCED_TYPENAME X::value_type*) 0);
143        }
144
145        template <class It>
146        void insert_range(It begin, It end) {
147            while(begin != end) {
148                this->insert(*begin);
149                ++begin;
150            }
151        }
152    };
153
154    template <class Equals>
155    BOOST_DEDUCED_TYPENAME
156        equals_to_compare<Equals>::type create_compare(Equals const&)
157    {
158        BOOST_DEDUCED_TYPENAME equals_to_compare<Equals>::type x;
159        return x;
160    }
161
162    template <class X>
163    ordered<X> create_ordered(X const& container)
164    {
165        return ordered<X>(create_compare(container.key_eq()));
166    }
167
168    template <class X1, class X2>
169    void check_container(X1 const& container, X2 const& values)
170    {
171        ordered<X1> tracker = create_ordered(container);
172        tracker.insert_range(values.begin(), values.end());
173        tracker.compare(container);
174    }
175}
176
177#endif
178