bm_traits.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright (c) Marshall Clow 2010-2012.
  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. For more information, see http://www.boost.org
  6. */
  7. #ifndef BOOST_ALGORITHM_SEARCH_DETAIL_BM_TRAITS_HPP
  8. #define BOOST_ALGORITHM_SEARCH_DETAIL_BM_TRAITS_HPP
  9. #include <climits> // for CHAR_BIT
  10. #include <vector>
  11. #include <iterator> // for std::iterator_traits
  12. #include <boost/type_traits/make_unsigned.hpp>
  13. #include <boost/type_traits/is_integral.hpp>
  14. #include <boost/type_traits/remove_pointer.hpp>
  15. #include <boost/type_traits/remove_const.hpp>
  16. #include <boost/array.hpp>
  17. #include <boost/tr1/tr1/unordered_map>
  18. #include <boost/algorithm/searching/detail/debugging.hpp>
  19. namespace boost { namespace algorithm { namespace detail {
  20. //
  21. // Default implementations of the skip tables for B-M and B-M-H
  22. //
  23. template<typename key_type, typename value_type, bool /*useArray*/> class skip_table;
  24. // General case for data searching other than bytes; use a map
  25. template<typename key_type, typename value_type>
  26. class skip_table<key_type, value_type, false> {
  27. private:
  28. typedef std::tr1::unordered_map<key_type, value_type> skip_map;
  29. const value_type k_default_value;
  30. skip_map skip_;
  31. public:
  32. skip_table ( std::size_t patSize, value_type default_value )
  33. : k_default_value ( default_value ), skip_ ( patSize ) {}
  34. void insert ( key_type key, value_type val ) {
  35. skip_ [ key ] = val; // Would skip_.insert (val) be better here?
  36. }
  37. value_type operator [] ( key_type key ) const {
  38. typename skip_map::const_iterator it = skip_.find ( key );
  39. return it == skip_.end () ? k_default_value : it->second;
  40. }
  41. void PrintSkipTable () const {
  42. std::cout << "BM(H) Skip Table <unordered_map>:" << std::endl;
  43. for ( typename skip_map::const_iterator it = skip_.begin (); it != skip_.end (); ++it )
  44. if ( it->second != k_default_value )
  45. std::cout << " " << it->first << ": " << it->second << std::endl;
  46. std::cout << std::endl;
  47. }
  48. };
  49. // Special case small numeric values; use an array
  50. template<typename key_type, typename value_type>
  51. class skip_table<key_type, value_type, true> {
  52. private:
  53. typedef typename boost::make_unsigned<key_type>::type unsigned_key_type;
  54. typedef boost::array<value_type, 1U << (CHAR_BIT * sizeof(key_type))> skip_map;
  55. skip_map skip_;
  56. const value_type k_default_value;
  57. public:
  58. skip_table ( std::size_t patSize, value_type default_value ) : k_default_value ( default_value ) {
  59. std::fill_n ( skip_.begin(), skip_.size(), default_value );
  60. }
  61. void insert ( key_type key, value_type val ) {
  62. skip_ [ static_cast<unsigned_key_type> ( key ) ] = val;
  63. }
  64. value_type operator [] ( key_type key ) const {
  65. return skip_ [ static_cast<unsigned_key_type> ( key ) ];
  66. }
  67. void PrintSkipTable () const {
  68. std::cout << "BM(H) Skip Table <boost:array>:" << std::endl;
  69. for ( typename skip_map::const_iterator it = skip_.begin (); it != skip_.end (); ++it )
  70. if ( *it != k_default_value )
  71. std::cout << " " << std::distance (skip_.begin (), it) << ": " << *it << std::endl;
  72. std::cout << std::endl;
  73. }
  74. };
  75. template<typename Iterator>
  76. struct BM_traits {
  77. typedef typename std::iterator_traits<Iterator>::difference_type value_type;
  78. typedef typename std::iterator_traits<Iterator>::value_type key_type;
  79. typedef boost::algorithm::detail::skip_table<key_type, value_type,
  80. boost::is_integral<key_type>::value && (sizeof(key_type)==1)> skip_table_t;
  81. };
  82. }}} // namespaces
  83. #endif // BOOST_ALGORITHM_SEARCH_DETAIL_BM_TRAITS_HPP