• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2006-2009 Daniel James.
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 
6 // This file implements a locale aware case insenstive equality predicate and
7 // hash function. Unfortunately it still falls short of full
8 // internationalization as it only deals with a single character at a time
9 // (some languages have tricky cases where the characters in an upper case
10 // string don't have a one-to-one correspondence with the lower case version of
11 // the text, eg. )
12 
13 #if !defined(BOOST_HASH_EXAMPLES_CASE_INSENSITIVE_HEADER)
14 #define BOOST_HASH_EXAMPLES_CASE_INSENSITIVE_HEADER
15 
16 #include <boost/algorithm/string/predicate.hpp>
17 #include <boost/functional/hash.hpp>
18 
19 namespace hash_examples
20 {
21     struct iequal_to
22     {
iequal_tohash_examples::iequal_to23         iequal_to() {}
iequal_tohash_examples::iequal_to24         explicit iequal_to(std::locale const& l) : locale_(l) {}
25 
26         template <typename String1, typename String2>
operator ()hash_examples::iequal_to27         bool operator()(String1 const& x1, String2 const& x2) const
28         {
29             return boost::algorithm::iequals(x1, x2, locale_);
30         }
31     private:
32         std::locale locale_;
33     };
34 
35     struct ihash
36     {
ihashhash_examples::ihash37         ihash() {}
ihashhash_examples::ihash38         explicit ihash(std::locale const& l) : locale_(l) {}
39 
40         template <typename String>
operator ()hash_examples::ihash41         std::size_t operator()(String const& x) const
42         {
43             std::size_t seed = 0;
44 
45             for(typename String::const_iterator it = x.begin();
46                 it != x.end(); ++it)
47             {
48                 boost::hash_combine(seed, std::toupper(*it, locale_));
49             }
50 
51             return seed;
52         }
53     private:
54         std::locale locale_;
55     };
56 }
57 
58 #endif
59