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