dictionary.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2006-2007 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include <boost/unordered_map.hpp>
  5. #include <boost/core/lightweight_test.hpp>
  6. #include <boost/algorithm/string/predicate.hpp>
  7. #include "../../examples/fnv1.hpp"
  8. //[case_insensitive_functions
  9. struct iequal_to
  10. {
  11. bool operator()(std::string const& x,
  12. std::string const& y) const
  13. {
  14. return boost::algorithm::iequals(x, y, std::locale());
  15. }
  16. };
  17. struct ihash
  18. {
  19. std::size_t operator()(std::string const& x) const
  20. {
  21. std::size_t seed = 0;
  22. std::locale locale;
  23. for(std::string::const_iterator it = x.begin();
  24. it != x.end(); ++it)
  25. {
  26. boost::hash_combine(seed, std::toupper(*it, locale));
  27. }
  28. return seed;
  29. }
  30. };
  31. //]
  32. int main() {
  33. //[case_sensitive_dictionary_fnv
  34. boost::unordered_map<std::string, int, hash::fnv_1>
  35. dictionary;
  36. //]
  37. BOOST_TEST(dictionary.empty());
  38. dictionary["one"] = 1;
  39. BOOST_TEST(dictionary.size() == 1);
  40. BOOST_TEST(dictionary.find("ONE") == dictionary.end());
  41. dictionary.insert(std::make_pair("ONE", 2));
  42. BOOST_TEST(dictionary.size() == 2);
  43. BOOST_TEST(dictionary.find("ONE") != dictionary.end() &&
  44. dictionary.find("ONE")->first == "ONE" &&
  45. dictionary.find("ONE")->second == 2);
  46. dictionary["One"] = 3;
  47. BOOST_TEST(dictionary.size() == 3);
  48. BOOST_TEST(dictionary.find("One") != dictionary.end() &&
  49. dictionary.find("One")->first == "One" &&
  50. dictionary.find("One")->second == 3);
  51. dictionary["two"] = 4;
  52. BOOST_TEST(dictionary.size() == 4);
  53. BOOST_TEST(dictionary.find("Two") == dictionary.end() &&
  54. dictionary.find("two") != dictionary.end() &&
  55. dictionary.find("two")->second == 4);
  56. //[case_insensitive_dictionary
  57. boost::unordered_map<std::string, int, ihash, iequal_to>
  58. idictionary;
  59. //]
  60. BOOST_TEST(idictionary.empty());
  61. idictionary["one"] = 1;
  62. BOOST_TEST(idictionary.size() == 1);
  63. BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
  64. idictionary.find("ONE") == idictionary.find("one"));
  65. idictionary.insert(std::make_pair("ONE", 2));
  66. BOOST_TEST(idictionary.size() == 1);
  67. BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
  68. idictionary.find("ONE")->first == "one" &&
  69. idictionary.find("ONE")->second == 1);
  70. idictionary["One"] = 3;
  71. BOOST_TEST(idictionary.size() == 1);
  72. BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
  73. idictionary.find("ONE")->first == "one" &&
  74. idictionary.find("ONE")->second == 3);
  75. idictionary["two"] = 4;
  76. BOOST_TEST(idictionary.size() == 2);
  77. BOOST_TEST(idictionary.find("two") != idictionary.end() &&
  78. idictionary.find("TWO")->first == "two" &&
  79. idictionary.find("Two")->second == 4);
  80. return boost::report_errors();
  81. }