Chris@16
|
1 // (C) Copyright Gennadiy Rozental 2001-2008.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
3 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/test for the library home page.
|
Chris@16
|
7 //
|
Chris@16
|
8 // File : $RCSfile$
|
Chris@16
|
9 //
|
Chris@101
|
10 // Version : $Revision$
|
Chris@16
|
11 //
|
Chris@16
|
12 // Description : fixed sized mapping with specified invalid value
|
Chris@16
|
13 // ***************************************************************************
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_TEST_FIXED_MAPPING_HPP_071894GER
|
Chris@16
|
16 #define BOOST_TEST_FIXED_MAPPING_HPP_071894GER
|
Chris@16
|
17
|
Chris@16
|
18 // Boost
|
Chris@16
|
19 #include <boost/preprocessor/repetition/repeat.hpp>
|
Chris@16
|
20 #include <boost/preprocessor/arithmetic/add.hpp>
|
Chris@16
|
21 #include <boost/call_traits.hpp>
|
Chris@16
|
22 #include <boost/detail/binary_search.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 // STL
|
Chris@16
|
25 #include <vector>
|
Chris@16
|
26 #include <functional>
|
Chris@16
|
27 #include <algorithm>
|
Chris@16
|
28 #include <utility>
|
Chris@16
|
29
|
Chris@16
|
30 #include <boost/test/detail/suppress_warnings.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 //____________________________________________________________________________//
|
Chris@16
|
33
|
Chris@16
|
34 namespace boost {
|
Chris@16
|
35
|
Chris@16
|
36 namespace unit_test {
|
Chris@16
|
37
|
Chris@16
|
38 // configurable maximum fixed sized mapping size supported by this header.
|
Chris@16
|
39 // You can redefine it before inclusion of this file.
|
Chris@16
|
40 #ifndef MAX_MAP_SIZE
|
Chris@16
|
41 #define MAX_MAP_SIZE 20
|
Chris@16
|
42 #endif
|
Chris@16
|
43
|
Chris@16
|
44 #define CONSTR_DECL_MID( z, i, dummy1 ) key_param_type key##i, value_param_type v##i,
|
Chris@16
|
45 #define CONSTR_BODY_MID( z, i, dummy1 ) add_pair( key##i, v##i );
|
Chris@16
|
46
|
Chris@16
|
47 #define CONSTR_DECL( z, n, dummy1 ) \
|
Chris@16
|
48 fixed_mapping( BOOST_PP_REPEAT_ ## z( n, CONSTR_DECL_MID, "" ) \
|
Chris@16
|
49 value_param_type invalid_value ) \
|
Chris@16
|
50 : m_invalid_value( invalid_value ) \
|
Chris@16
|
51 { \
|
Chris@16
|
52 BOOST_PP_REPEAT_ ## z( n, CONSTR_BODY_MID, "" ) \
|
Chris@16
|
53 init(); \
|
Chris@16
|
54 } \
|
Chris@16
|
55 /**/
|
Chris@16
|
56
|
Chris@16
|
57 #define CONTRUCTORS( n ) BOOST_PP_REPEAT( n, CONSTR_DECL, "" )
|
Chris@16
|
58
|
Chris@16
|
59 template<typename Key, typename Value, typename Compare = std::less<Key> >
|
Chris@16
|
60 class fixed_mapping
|
Chris@16
|
61 {
|
Chris@16
|
62 typedef std::pair<Key,Value> elem_type;
|
Chris@16
|
63 typedef std::vector<elem_type> map_type;
|
Chris@16
|
64 typedef typename std::vector<elem_type>::const_iterator iterator;
|
Chris@16
|
65
|
Chris@16
|
66 typedef typename call_traits<Key>::param_type key_param_type;
|
Chris@16
|
67 typedef typename call_traits<Value>::param_type value_param_type;
|
Chris@16
|
68 typedef typename call_traits<Value>::const_reference value_ref_type;
|
Chris@16
|
69
|
Chris@16
|
70 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
|
Chris@16
|
71 struct p1; friend struct p1;
|
Chris@16
|
72 struct p2; friend struct p2;
|
Chris@16
|
73 #endif
|
Chris@16
|
74
|
Chris@16
|
75 // bind( Compare(), bind(select1st<elem_type>(), _1), bind(identity<Key>(), _2) )
|
Chris@16
|
76 struct p1 : public std::binary_function<elem_type,Key,bool>
|
Chris@16
|
77 {
|
Chris@16
|
78 bool operator()( elem_type const& x, Key const& y ) const { return Compare()( x.first, y ); }
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 // bind( Compare(), bind(select1st<elem_type>(), _1), bind(select1st<elem_type>(), _2) )
|
Chris@16
|
82 struct p2 : public std::binary_function<elem_type,elem_type,bool>
|
Chris@16
|
83 {
|
Chris@16
|
84 bool operator()( elem_type const& x, elem_type const& y ) const { return Compare()( x.first, y.first ); }
|
Chris@16
|
85 };
|
Chris@16
|
86
|
Chris@16
|
87 public:
|
Chris@16
|
88 // Constructors
|
Chris@16
|
89 CONTRUCTORS( BOOST_PP_ADD( MAX_MAP_SIZE, 1 ) )
|
Chris@16
|
90
|
Chris@16
|
91 // key -> value access
|
Chris@16
|
92 value_ref_type operator[]( key_param_type key ) const
|
Chris@16
|
93 {
|
Chris@16
|
94 iterator it = boost::detail::lower_bound( m_map.begin(), m_map.end(), key, p1() );
|
Chris@16
|
95
|
Chris@16
|
96 return (it == m_map.end() || Compare()( key, it->first ) ) ? m_invalid_value : it->second;
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 private:
|
Chris@16
|
100 // Implementation
|
Chris@16
|
101 void init() { std::sort( m_map.begin(), m_map.end(), p2() ); }
|
Chris@16
|
102 void add_pair( key_param_type key, value_param_type value ) { m_map.push_back( elem_type( key, value ) ); }
|
Chris@16
|
103
|
Chris@16
|
104 // Data members
|
Chris@16
|
105 Value m_invalid_value;
|
Chris@16
|
106 map_type m_map;
|
Chris@16
|
107 };
|
Chris@16
|
108
|
Chris@16
|
109 } // namespace unit_test
|
Chris@16
|
110
|
Chris@16
|
111 } // namespace boost
|
Chris@16
|
112
|
Chris@16
|
113 //____________________________________________________________________________//
|
Chris@16
|
114
|
Chris@16
|
115 #include <boost/test/detail/enable_warnings.hpp>
|
Chris@16
|
116
|
Chris@16
|
117 #undef MAX_MAP_SIZE
|
Chris@16
|
118 #undef CONSTR_DECL_MID
|
Chris@16
|
119 #undef CONSTR_BODY_MID
|
Chris@16
|
120 #undef CONSTR_DECL
|
Chris@16
|
121 #undef CONTRUCTORS
|
Chris@16
|
122
|
Chris@16
|
123 #endif // BOOST_TEST_FIXED_MAPPING_HPP_071894GER
|
Chris@16
|
124
|