fixed_mapping.hpp 4.2 KB

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