/Src/Dependencies/Boost/libs/unordered/doc/src_code/dictionary.cpp

http://hadesmem.googlecode.com/ · C++ · 103 lines · 74 code · 20 blank · 9 comment · 31 complexity · 4ed9d12622e789712a6cf33711b88a33 MD5 · raw file

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