Skip to content

Commit f20adae

Browse files
committed
Rename the equal_range wrapper and add a lookup
1 parent 90f0d47 commit f20adae

File tree

8 files changed

+61
-61
lines changed

8 files changed

+61
-61
lines changed

include/graphqlservice/internal/SortedMap.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <algorithm>
1010
#include <initializer_list>
11+
#include <optional>
1112
#include <stdexcept>
1213
#include <utility>
1314
#include <vector>
@@ -32,9 +33,9 @@ struct sorted_map_key
3233
const K& key;
3334
};
3435

35-
template <class Compare, class Iterator, class K = decltype(std::declval<Iterator>()->first),
36+
template <class Compare, class Iterator, class K,
3637
class V = decltype(std::declval<Iterator>()->second)>
37-
constexpr std::pair<Iterator, Iterator> find_sorted_map_key(
38+
constexpr std::pair<Iterator, Iterator> sorted_map_equal_range(
3839
Iterator itrBegin, Iterator itrEnd, const K& key) noexcept
3940
{
4041
return std::equal_range(itrBegin,
@@ -45,6 +46,15 @@ constexpr std::pair<Iterator, Iterator> find_sorted_map_key(
4546
});
4647
}
4748

49+
template <class Compare, class Container, class K>
50+
constexpr auto sorted_map_lookup(const Container& container, const K& key) noexcept
51+
{
52+
const auto [itr, itrEnd] =
53+
sorted_map_equal_range<Compare>(container.begin(), container.end(), key);
54+
55+
return itr == itrEnd ? std::nullopt : std::make_optional(itr->second);
56+
}
57+
4858
template <class K, class V, class Compare = std::less<K>>
4959
class sorted_map
5060
{
@@ -124,7 +134,7 @@ class sorted_map
124134

125135
constexpr const_iterator find(const K& key) const noexcept
126136
{
127-
const auto [itr, itrEnd] = find_sorted_map_key<Compare>(_data.begin(), _data.end(), key);
137+
const auto [itr, itrEnd] = sorted_map_equal_range<Compare>(_data.begin(), _data.end(), key);
128138

129139
return itr == itrEnd ? _data.cend() : itr;
130140
}
@@ -141,7 +151,7 @@ class sorted_map
141151
std::pair<const_iterator, bool> emplace(KeyArg&& keyArg, ValueArgs&&... args) noexcept
142152
{
143153
K key { std::forward<KeyArg>(keyArg) };
144-
const auto [itr, itrEnd] = find_sorted_map_key<Compare>(_data.begin(), _data.end(), key);
154+
const auto [itr, itrEnd] = sorted_map_equal_range<Compare>(_data.begin(), _data.end(), key);
145155

146156
if (itr != itrEnd)
147157
{
@@ -154,7 +164,7 @@ class sorted_map
154164

155165
const_iterator erase(const K& key) noexcept
156166
{
157-
const auto [itr, itrEnd] = find_sorted_map_key<Compare>(_data.begin(), _data.end(), key);
167+
const auto [itr, itrEnd] = sorted_map_equal_range<Compare>(_data.begin(), _data.end(), key);
158168

159169
if (itr == itrEnd)
160170
{
@@ -181,7 +191,7 @@ class sorted_map
181191
V& operator[](KeyArg&& keyArg) noexcept
182192
{
183193
K key { std::forward<KeyArg>(keyArg) };
184-
const auto [itr, itrEnd] = find_sorted_map_key<Compare>(_data.begin(), _data.end(), key);
194+
const auto [itr, itrEnd] = sorted_map_equal_range<Compare>(_data.begin(), _data.end(), key);
185195

186196
if (itr != itrEnd)
187197
{
@@ -195,7 +205,7 @@ class sorted_map
195205
V& at(KeyArg&& keyArg)
196206
{
197207
const K key { std::forward<KeyArg>(keyArg) };
198-
const auto [itr, itrEnd] = find_sorted_map_key<Compare>(_data.begin(), _data.end(), key);
208+
const auto [itr, itrEnd] = sorted_map_equal_range<Compare>(_data.begin(), _data.end(), key);
199209

200210
if (itr == itrEnd)
201211
{

samples/learn/schema/StarWarsSchema.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,16 @@ learn::Episode ModifiedArgument<learn::Episode>::convert(const response::Value&
3535
throw service::schema_exception { { R"ex(not a valid Episode value)ex" } };
3636
}
3737

38-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
39-
s_valuesEpisode.begin(),
40-
s_valuesEpisode.end(),
38+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
39+
s_valuesEpisode,
4140
std::string_view { value.get<std::string>() });
4241

43-
if (itr == itrEnd)
42+
if (!result)
4443
{
4544
throw service::schema_exception { { R"ex(not a valid Episode value)ex" } };
4645
}
4746

48-
return itr->second;
47+
return *result;
4948
}
5049

5150
template <>
@@ -70,7 +69,7 @@ void ModifiedResult<learn::Episode>::validateScalar(const response::Value& value
7069
throw service::schema_exception { { R"ex(not a valid Episode value)ex" } };
7170
}
7271

73-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
72+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
7473
s_valuesEpisode.begin(),
7574
s_valuesEpisode.end(),
7675
std::string_view { value.get<std::string>() });

samples/today/nointrospection/TodaySchema.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ today::TaskState ModifiedArgument<today::TaskState>::convert(const response::Val
3636
throw service::schema_exception { { R"ex(not a valid TaskState value)ex" } };
3737
}
3838

39-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
40-
s_valuesTaskState.begin(),
41-
s_valuesTaskState.end(),
39+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
40+
s_valuesTaskState,
4241
std::string_view { value.get<std::string>() });
4342

44-
if (itr == itrEnd)
43+
if (!result)
4544
{
4645
throw service::schema_exception { { R"ex(not a valid TaskState value)ex" } };
4746
}
4847

49-
return itr->second;
48+
return *result;
5049
}
5150

5251
template <>
@@ -71,7 +70,7 @@ void ModifiedResult<today::TaskState>::validateScalar(const response::Value& val
7170
throw service::schema_exception { { R"ex(not a valid TaskState value)ex" } };
7271
}
7372

74-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
73+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
7574
s_valuesTaskState.begin(),
7675
s_valuesTaskState.end(),
7776
std::string_view { value.get<std::string>() });

samples/today/schema/TodaySchema.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ today::TaskState ModifiedArgument<today::TaskState>::convert(const response::Val
3636
throw service::schema_exception { { R"ex(not a valid TaskState value)ex" } };
3737
}
3838

39-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
40-
s_valuesTaskState.begin(),
41-
s_valuesTaskState.end(),
39+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
40+
s_valuesTaskState,
4241
std::string_view { value.get<std::string>() });
4342

44-
if (itr == itrEnd)
43+
if (!result)
4544
{
4645
throw service::schema_exception { { R"ex(not a valid TaskState value)ex" } };
4746
}
4847

49-
return itr->second;
48+
return *result;
5049
}
5150

5251
template <>
@@ -71,7 +70,7 @@ void ModifiedResult<today::TaskState>::validateScalar(const response::Value& val
7170
throw service::schema_exception { { R"ex(not a valid TaskState value)ex" } };
7271
}
7372

74-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
73+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
7574
s_valuesTaskState.begin(),
7675
s_valuesTaskState.end(),
7776
std::string_view { value.get<std::string>() });

samples/validation/schema/ValidationSchema.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ validation::DogCommand ModifiedArgument<validation::DogCommand>::convert(const r
3636
throw service::schema_exception { { R"ex(not a valid DogCommand value)ex" } };
3737
}
3838

39-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
40-
s_valuesDogCommand.begin(),
41-
s_valuesDogCommand.end(),
39+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
40+
s_valuesDogCommand,
4241
std::string_view { value.get<std::string>() });
4342

44-
if (itr == itrEnd)
43+
if (!result)
4544
{
4645
throw service::schema_exception { { R"ex(not a valid DogCommand value)ex" } };
4746
}
4847

49-
return itr->second;
48+
return *result;
5049
}
5150

5251
template <>
@@ -71,7 +70,7 @@ void ModifiedResult<validation::DogCommand>::validateScalar(const response::Valu
7170
throw service::schema_exception { { R"ex(not a valid DogCommand value)ex" } };
7271
}
7372

74-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
73+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
7574
s_valuesDogCommand.begin(),
7675
s_valuesDogCommand.end(),
7776
std::string_view { value.get<std::string>() });
@@ -93,17 +92,16 @@ validation::CatCommand ModifiedArgument<validation::CatCommand>::convert(const r
9392
throw service::schema_exception { { R"ex(not a valid CatCommand value)ex" } };
9493
}
9594

96-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
97-
s_valuesCatCommand.begin(),
98-
s_valuesCatCommand.end(),
95+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
96+
s_valuesCatCommand,
9997
std::string_view { value.get<std::string>() });
10098

101-
if (itr == itrEnd)
99+
if (!result)
102100
{
103101
throw service::schema_exception { { R"ex(not a valid CatCommand value)ex" } };
104102
}
105103

106-
return itr->second;
104+
return *result;
107105
}
108106

109107
template <>
@@ -128,7 +126,7 @@ void ModifiedResult<validation::CatCommand>::validateScalar(const response::Valu
128126
throw service::schema_exception { { R"ex(not a valid CatCommand value)ex" } };
129127
}
130128

131-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
129+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
132130
s_valuesCatCommand.begin(),
133131
s_valuesCatCommand.end(),
134132
std::string_view { value.get<std::string>() });

src/SchemaGenerator.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,20 +1244,18 @@ template <>
12441244
<< enumType.type << R"cpp( value)ex" } };
12451245
}
12461246
1247-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
1247+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
12481248
s_values)cpp" << enumType.cppType
1249-
<< R"cpp(.begin(),
1250-
s_values)cpp" << enumType.cppType
1251-
<< R"cpp(.end(),
1249+
<< R"cpp(,
12521250
std::string_view { value.get<std::string>() });
12531251
1254-
if (itr == itrEnd)
1252+
if (!result)
12551253
{
12561254
throw service::schema_exception { { R"ex(not a valid )cpp"
12571255
<< enumType.type << R"cpp( value)ex" } };
12581256
}
12591257
1260-
return itr->second;
1258+
return *result;
12611259
}
12621260
12631261
template <>
@@ -1291,7 +1289,7 @@ void ModifiedResult<)cpp"
12911289
<< enumType.type << R"cpp( value)ex" } };
12921290
}
12931291
1294-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
1292+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
12951293
s_values)cpp" << enumType.cppType
12961294
<< R"cpp(.begin(),
12971295
s_values)cpp" << enumType.cppType

src/introspection/IntrospectionSchema.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ introspection::TypeKind ModifiedArgument<introspection::TypeKind>::convert(const
3030
throw service::schema_exception { { R"ex(not a valid __TypeKind value)ex" } };
3131
}
3232

33-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
34-
s_valuesTypeKind.begin(),
35-
s_valuesTypeKind.end(),
33+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
34+
s_valuesTypeKind,
3635
std::string_view { value.get<std::string>() });
3736

38-
if (itr == itrEnd)
37+
if (!result)
3938
{
4039
throw service::schema_exception { { R"ex(not a valid __TypeKind value)ex" } };
4140
}
4241

43-
return itr->second;
42+
return *result;
4443
}
4544

4645
template <>
@@ -65,7 +64,7 @@ void ModifiedResult<introspection::TypeKind>::validateScalar(const response::Val
6564
throw service::schema_exception { { R"ex(not a valid __TypeKind value)ex" } };
6665
}
6766

68-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
67+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
6968
s_valuesTypeKind.begin(),
7069
s_valuesTypeKind.end(),
7170
std::string_view { value.get<std::string>() });
@@ -87,17 +86,16 @@ introspection::DirectiveLocation ModifiedArgument<introspection::DirectiveLocati
8786
throw service::schema_exception { { R"ex(not a valid __DirectiveLocation value)ex" } };
8887
}
8988

90-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
91-
s_valuesDirectiveLocation.begin(),
92-
s_valuesDirectiveLocation.end(),
89+
const auto result = internal::sorted_map_lookup<internal::shorter_or_less>(
90+
s_valuesDirectiveLocation,
9391
std::string_view { value.get<std::string>() });
9492

95-
if (itr == itrEnd)
93+
if (!result)
9694
{
9795
throw service::schema_exception { { R"ex(not a valid __DirectiveLocation value)ex" } };
9896
}
9997

100-
return itr->second;
98+
return *result;
10199
}
102100

103101
template <>
@@ -122,7 +120,7 @@ void ModifiedResult<introspection::DirectiveLocation>::validateScalar(const resp
122120
throw service::schema_exception { { R"ex(not a valid __DirectiveLocation value)ex" } };
123121
}
124122

125-
const auto [itr, itrEnd] = internal::find_sorted_map_key<internal::shorter_or_less>(
123+
const auto [itr, itrEnd] = internal::sorted_map_equal_range<internal::shorter_or_less>(
126124
s_valuesDirectiveLocation.begin(),
127125
s_valuesDirectiveLocation.end(),
128126
std::string_view { value.get<std::string>() });

test/ArgumentTests.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,12 @@ TEST(ArgumentsCase, TaskStateEnumConstexpr)
214214
using namespace std::literals;
215215

216216
const auto values = today::getTaskStateValues();
217-
const auto [itr, itrEnd] =
218-
internal::find_sorted_map_key<internal::shorter_or_less>(values.begin(),
219-
values.end(),
217+
constexpr auto actual =
218+
internal::sorted_map_lookup<internal::shorter_or_less>(today::getTaskStateValues(),
220219
"Started"sv);
221220

222-
ASSERT_TRUE(itr != itrEnd) << "should find a value";
223-
EXPECT_TRUE(today::TaskState::Started == itr->second) << "should parse the enum";
221+
ASSERT_TRUE(actual) << "should find a value";
222+
EXPECT_TRUE(today::TaskState::Started == *actual) << "should parse the enum";
224223
}
225224

226225
TEST(ArgumentsCase, ScalarArgumentMap)

0 commit comments

Comments
 (0)