File: dictionary.cpp

package info (click to toggle)
boost1.74 1.74.0%2Bds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 463,588 kB
  • sloc: cpp: 3,338,117; xml: 131,293; python: 33,088; ansic: 14,292; asm: 4,038; sh: 3,353; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (101 lines) | stat: -rw-r--r-- 3,138 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

// 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 <boost/unordered_map.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "../../examples/fnv1.hpp"

//[case_insensitive_functions
    struct iequal_to
    {
        bool operator()(std::string const& x,
            std::string const& y) const
        {
            return boost::algorithm::iequals(x, y, std::locale());
        }
    };

    struct ihash
    {
        std::size_t operator()(std::string const& x) const
        {
            std::size_t seed = 0;
            std::locale locale;

            for(std::string::const_iterator it = x.begin();
                it != x.end(); ++it)
            {
                boost::hash_combine(seed, std::toupper(*it, locale));
            }

            return seed;
        }
    };
//]

int main() {
//[case_sensitive_dictionary_fnv
    boost::unordered_map<std::string, int, hash::fnv_1>
        dictionary;
//]

    BOOST_TEST(dictionary.empty());

    dictionary["one"] = 1;
    BOOST_TEST(dictionary.size() == 1);
    BOOST_TEST(dictionary.find("ONE") == dictionary.end());

    dictionary.insert(std::make_pair("ONE", 2));
    BOOST_TEST(dictionary.size() == 2);
    BOOST_TEST(dictionary.find("ONE") != dictionary.end() &&
            dictionary.find("ONE")->first == "ONE" &&
            dictionary.find("ONE")->second == 2);

    dictionary["One"] = 3;
    BOOST_TEST(dictionary.size() == 3);
    BOOST_TEST(dictionary.find("One") != dictionary.end() &&
            dictionary.find("One")->first == "One" &&
            dictionary.find("One")->second == 3);

    dictionary["two"] = 4;
    BOOST_TEST(dictionary.size() == 4);
    BOOST_TEST(dictionary.find("Two") == dictionary.end() &&
            dictionary.find("two") != dictionary.end() &&
            dictionary.find("two")->second == 4);


//[case_insensitive_dictionary
    boost::unordered_map<std::string, int, ihash, iequal_to>
        idictionary;
//]

    BOOST_TEST(idictionary.empty());

    idictionary["one"] = 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", 2));
    BOOST_TEST(idictionary.size() == 1);
    BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
            idictionary.find("ONE")->first == "one" &&
            idictionary.find("ONE")->second == 1);

    idictionary["One"] = 3;
    BOOST_TEST(idictionary.size() == 1);
    BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
            idictionary.find("ONE")->first == "one" &&
            idictionary.find("ONE")->second == 3);

    idictionary["two"] = 4;
    BOOST_TEST(idictionary.size() == 2);
    BOOST_TEST(idictionary.find("two") != idictionary.end() &&
            idictionary.find("TWO")->first == "two" &&
            idictionary.find("Two")->second == 4);

    return boost::report_errors();
}