/* Copyright (c) Marshall Clow 2010-2012. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) For more information, see http://www.boost.org */ #ifndef BOOST_ALGORITHM_SEARCH_DETAIL_BM_TRAITS_HPP #define BOOST_ALGORITHM_SEARCH_DETAIL_BM_TRAITS_HPP #include // for CHAR_BIT #include #include // for std::iterator_traits #include #include #include #include #include #ifdef BOOST_NO_CXX11_HDR_UNORDERED_MAP #include #else #include #endif #include namespace boost { namespace algorithm { namespace detail { // // Default implementations of the skip tables for B-M and B-M-H // template class skip_table; // General case for data searching other than bytes; use a map template class skip_table { private: #ifdef BOOST_NO_CXX11_HDR_UNORDERED_MAP typedef std::tr1::unordered_map skip_map; #else typedef std::unordered_map skip_map; #endif const value_type k_default_value; skip_map skip_; public: skip_table ( std::size_t patSize, value_type default_value ) : k_default_value ( default_value ), skip_ ( patSize ) {} void insert ( key_type key, value_type val ) { skip_ [ key ] = val; // Would skip_.insert (val) be better here? } value_type operator [] ( key_type key ) const { typename skip_map::const_iterator it = skip_.find ( key ); return it == skip_.end () ? k_default_value : it->second; } void PrintSkipTable () const { std::cout << "BM(H) Skip Table :" << std::endl; for ( typename skip_map::const_iterator it = skip_.begin (); it != skip_.end (); ++it ) if ( it->second != k_default_value ) std::cout << " " << it->first << ": " << it->second << std::endl; std::cout << std::endl; } }; // Special case small numeric values; use an array template class skip_table { private: typedef typename boost::make_unsigned::type unsigned_key_type; typedef boost::array skip_map; skip_map skip_; const value_type k_default_value; public: skip_table ( std::size_t patSize, value_type default_value ) : k_default_value ( default_value ) { std::fill_n ( skip_.begin(), skip_.size(), default_value ); } void insert ( key_type key, value_type val ) { skip_ [ static_cast ( key ) ] = val; } value_type operator [] ( key_type key ) const { return skip_ [ static_cast ( key ) ]; } void PrintSkipTable () const { std::cout << "BM(H) Skip Table :" << std::endl; for ( typename skip_map::const_iterator it = skip_.begin (); it != skip_.end (); ++it ) if ( *it != k_default_value ) std::cout << " " << std::distance (skip_.begin (), it) << ": " << *it << std::endl; std::cout << std::endl; } }; template struct BM_traits { typedef typename std::iterator_traits::difference_type value_type; typedef typename std::iterator_traits::value_type key_type; typedef boost::algorithm::detail::skip_table::value && (sizeof(key_type)==1)> skip_table_t; }; }}} // namespaces #endif // BOOST_ALGORITHM_SEARCH_DETAIL_BM_TRAITS_HPP