File: sorted_string_map.cpp

package info (click to toggle)
mdds 3.1.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,064 kB
  • sloc: cpp: 20,809; sh: 1,369; makefile: 624; python: 603
file content (185 lines) | stat: -rw-r--r-- 5,791 bytes parent folder | download | duplicates (2)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * Copyright (c) 2024 Kohei Yoshida
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 ************************************************************************/

//!code-start: header
#include <mdds/sorted_string_map.hpp>
#include <iostream>
//!code-end: header

//!code-start: enum
enum class us_state_t
{
    unknown,
    alabama,
    alaska,
    arizona,
    arkansas,
    california,
    colorado,
    connecticut,
    delaware,
    florida,
    georgia,
    hawaii,
    idaho,
    illinois,
    indiana,
    iowa,
    kansas,
    kentucky,
    louisiana,
    maine,
    maryland,
    massachusetts,
    michigan,
    minnesota,
    mississippi,
    missouri,
    montana,
    nebraska,
    nevada,
    new_hampshire,
    new_jersey,
    new_mexico,
    new_york,
    north_carolina,
    north_dakota,
    ohio,
    oklahoma,
    oregon,
    pennsylvania,
    rhode_island,
    south_carolina,
    south_dakota,
    tennessee,
    texas,
    utah,
    vermont,
    virginia,
    washington,
    west_virginia,
    wisconsin,
    wyoming,
};
//!code-end: enum

//!code-start: type
using us_state_map_type = mdds::sorted_string_map<us_state_t>;
//!code-end: type

//!code-start: entries
// Keys must be sorted in ascending order.
constexpr us_state_map_type::entry_type us_state_entries[] = {
    { "Alabama", us_state_t::alabama },
    { "Alaska", us_state_t::alaska },
    { "Arizona", us_state_t::arizona },
    { "Arkansas", us_state_t::arkansas },
    { "California", us_state_t::california },
    { "Colorado", us_state_t::colorado },
    { "Connecticut", us_state_t::connecticut },
    { "Delaware", us_state_t::delaware },
    { "Florida", us_state_t::florida },
    { "Georgia", us_state_t::georgia },
    { "Hawaii", us_state_t::hawaii },
    { "Idaho", us_state_t::idaho },
    { "Illinois", us_state_t::illinois },
    { "Indiana", us_state_t::indiana },
    { "Iowa", us_state_t::iowa },
    { "Kansas", us_state_t::kansas },
    { "Kentucky", us_state_t::kentucky },
    { "Louisiana", us_state_t::louisiana },
    { "Maine", us_state_t::maine },
    { "Maryland", us_state_t::maryland },
    { "Massachusetts", us_state_t::massachusetts },
    { "Michigan", us_state_t::michigan },
    { "Minnesota", us_state_t::minnesota },
    { "Mississippi", us_state_t::mississippi },
    { "Missouri", us_state_t::missouri },
    { "Montana", us_state_t::montana },
    { "Nebraska", us_state_t::nebraska },
    { "Nevada", us_state_t::nevada },
    { "New Hampshire", us_state_t::new_hampshire },
    { "New Jersey", us_state_t::new_jersey },
    { "New Mexico", us_state_t::new_mexico },
    { "New York", us_state_t::new_york },
    { "North Carolina", us_state_t::north_carolina },
    { "North Dakota", us_state_t::north_dakota },
    { "Ohio", us_state_t::ohio },
    { "Oklahoma", us_state_t::oklahoma },
    { "Oregon", us_state_t::oregon },
    { "Pennsylvania", us_state_t::pennsylvania },
    { "Rhode Island", us_state_t::rhode_island },
    { "South Carolina", us_state_t::south_carolina },
    { "South Dakota", us_state_t::south_dakota },
    { "Tennessee", us_state_t::tennessee },
    { "Texas", us_state_t::texas },
    { "Utah", us_state_t::utah },
    { "Vermont", us_state_t::vermont },
    { "Virginia", us_state_t::virginia },
    { "Washington", us_state_t::washington },
    { "West Virginia", us_state_t::west_virginia },
    { "Wisconsin", us_state_t::wisconsin },
    { "Wyoming", us_state_t::wyoming },
};
//!code-end: entries

int main() try
{
    auto zero = us_state_t{};
    std::cout << "unknown? " << std::boolalpha << (zero == us_state_t::unknown) << std::endl;

    //!code-start: init
    const us_state_map_type state_map(us_state_entries, std::size(us_state_entries), us_state_t::unknown);
    //!code-end: init

    //!code-start: find
    auto state = state_map.find("Virginia");
    std::cout << "virginia? " << std::boolalpha << (state == us_state_t::virginia) << std::endl;
    state = state_map.find("North Carolina");
    std::cout << "north_carolina? " << std::boolalpha << (state == us_state_t::north_carolina) << std::endl;
    //!code-end: find

    //!code-start: find-null
    state = state_map.find("Alberta");
    std::cout << "unknown? " << std::boolalpha << (state == us_state_t::unknown) << std::endl;
    //!code-end: find-null

    //!code-start: find-key
    auto key = state_map.find_key(us_state_t::rhode_island);
    std::cout << "key for rhode_island: " << key << std::endl;
    //!code-end: find-key

    return EXIT_SUCCESS;
}
catch (...)
{
    return EXIT_FAILURE;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */