From 50650fbd345665e7ceeb2a2d1e8782744bdbec6c Mon Sep 17 00:00:00 2001 From: Daniel James Date: Fri, 16 Nov 2007 00:35:34 +0000 Subject: [PATCH] Fix the return values of the case insensitive hash examples. [SVN r41126] --- doc/src_code/insensitive.cpp | 78 +++++++++++++++++++++++++++++++++++ examples/case_insensitive.hpp | 4 +- 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 doc/src_code/insensitive.cpp diff --git a/doc/src_code/insensitive.cpp b/doc/src_code/insensitive.cpp new file mode 100644 index 00000000..2575d72c --- /dev/null +++ b/doc/src_code/insensitive.cpp @@ -0,0 +1,78 @@ + +// Copyright 2006-2007 Daniel James. +// 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) + +#include +#include +#include + +//[case_insensitive_functions + struct iequal_to + : std::binary_function + { + bool operator()(std::string const& x, + std::string const& y) const + { + return boost::algorithm::iequals(x, y); + } + }; + + struct ihash + : std::unary_function + { + std::size_t operator()(std::string const& x) const + { + std::size_t seed = 0; + + for(std::string::const_iterator it = x.begin(); + it != x.end(); ++it) + { + boost::hash_combine(seed, std::toupper(*it)); + } + + return seed; + } + }; + + struct word_info; +//] + + struct word_info { + int tag; + explicit word_info(int t = 0) : tag(t) {} + }; + +int main() { +//[case_insensitive_dictionary + boost::unordered_map + idictionary; +//] + + BOOST_TEST(idictionary.empty()); + + idictionary["one"] = word_info(1); + BOOST_TEST(idictionary.size() == 1); + BOOST_TEST(idictionary.find("ONE") != idictionary.end() && + idictionary.find("ONE") == idictionary.find("one")); + + idictionary.insert(std::make_pair("ONE", word_info(2))); + BOOST_TEST(idictionary.size() == 1); + BOOST_TEST(idictionary.find("ONE") != idictionary.end() && + idictionary.find("ONE")->first == "one" && + idictionary.find("ONE")->second.tag == 1); + + idictionary["One"] = word_info(3); + BOOST_TEST(idictionary.size() == 1); + BOOST_TEST(idictionary.find("ONE") != idictionary.end() && + idictionary.find("ONE")->first == "one" && + idictionary.find("ONE")->second.tag == 3); + + idictionary["two"] = word_info(4); + BOOST_TEST(idictionary.size() == 2); + BOOST_TEST(idictionary.find("two") != idictionary.end() && + idictionary.find("TWO")->first == "two" && + idictionary.find("Two")->second.tag == 4); + + return boost::report_errors(); +} diff --git a/examples/case_insensitive.hpp b/examples/case_insensitive.hpp index 02c25330..fba5b941 100644 --- a/examples/case_insensitive.hpp +++ b/examples/case_insensitive.hpp @@ -34,13 +34,13 @@ namespace hash_examples }; struct ihash - : std::unary_function + : std::unary_function { ihash() {} explicit ihash(std::locale const& l) : locale_(l) {} template - bool operator()(String const& x) const + std::size_t operator()(String const& x) const { std::size_t seed = 0;