77#define SORTEDMAP_H
88
99#include < algorithm>
10+ #include < functional>
1011#include < initializer_list>
1112#include < stdexcept>
1213#include < tuple>
1314#include < vector>
1415
1516namespace graphql ::internal {
1617
17- template <class K , class V >
18+ template <class K , class V , class Compare = std::less<K> >
1819class sorted_map
1920{
2021public:
@@ -29,7 +30,7 @@ class sorted_map
2930 : _data { init }
3031 {
3132 std::sort (_data.begin (), _data.end (), [](const auto & lhs, const auto & rhs) noexcept {
32- return lhs.first < rhs.first ;
33+ return Compare {}( lhs.first , rhs.first ) ;
3334 });
3435 }
3536
@@ -89,7 +90,7 @@ class sorted_map
8990 end (),
9091 key,
9192 [](sorted_map_key lhs, sorted_map_key rhs) noexcept {
92- return lhs.key < rhs.key ;
93+ return Compare {}( lhs.key , rhs.key ) ;
9394 });
9495
9596 return itr == itrEnd ? _data.end () : itr;
@@ -111,17 +112,15 @@ class sorted_map
111112 _data.end (),
112113 key,
113114 [](sorted_map_key lhs, sorted_map_key rhs) noexcept {
114- return lhs.key < rhs.key ;
115+ return Compare {}( lhs.key , rhs.key ) ;
115116 });
116117
117118 if (itr != itrEnd)
118119 {
119120 return { itr, false };
120121 }
121122
122- return { _data.emplace (itrEnd,
123- std::move (key),
124- V { std::forward<ValueArgs>(args)... }),
123+ return { _data.emplace (itrEnd, std::move (key), V { std::forward<ValueArgs>(args)... }),
125124 true };
126125 }
127126
@@ -131,7 +130,7 @@ class sorted_map
131130 _data.end (),
132131 key,
133132 [](sorted_map_key lhs, sorted_map_key rhs) noexcept {
134- return lhs.key < rhs.key ;
133+ return Compare {}( lhs.key , rhs.key ) ;
135134 });
136135
137136 if (itr == itrEnd)
@@ -163,7 +162,7 @@ class sorted_map
163162 _data.end (),
164163 key,
165164 [](sorted_map_key lhs, sorted_map_key rhs) noexcept {
166- return lhs.key < rhs.key ;
165+ return Compare {}( lhs.key , rhs.key ) ;
167166 });
168167
169168 if (itr != itrEnd)
@@ -182,7 +181,7 @@ class sorted_map
182181 _data.end (),
183182 key,
184183 [](sorted_map_key lhs, sorted_map_key rhs) noexcept {
185- return lhs.key < rhs.key ;
184+ return Compare {}( lhs.key , rhs.key ) ;
186185 });
187186
188187 if (itr == itrEnd)
@@ -212,7 +211,7 @@ class sorted_map
212211 vector_type _data;
213212};
214213
215- template <class K >
214+ template <class K , class Compare = std::less<K> >
216215class sorted_set
217216{
218217public:
@@ -225,8 +224,8 @@ class sorted_set
225224 sorted_set (std::initializer_list<K> init)
226225 : _data { init }
227226 {
228- std::sort (_data.begin (), _data.end (), [](const auto & lhs, const auto & rhs) noexcept {
229- return lhs < rhs;
227+ std::sort (_data.begin (), _data.end (), [](const K & lhs, const K & rhs) noexcept {
228+ return Compare {}( lhs, rhs) ;
230229 });
231230 }
232231
@@ -283,8 +282,8 @@ class sorted_set
283282 const_iterator find (const K& key) const noexcept
284283 {
285284 const auto [itr, itrEnd] =
286- std::equal_range (begin (), end (), key, [](const auto & lhs, const auto & rhs) noexcept {
287- return lhs < rhs;
285+ std::equal_range (begin (), end (), key, [](const K & lhs, const K & rhs) noexcept {
286+ return Compare {}( lhs, rhs) ;
288287 });
289288
290289 return itr == itrEnd ? _data.end () : itr;
@@ -305,7 +304,7 @@ class sorted_set
305304 _data.end (),
306305 key,
307306 [](const auto & lhs, const auto & rhs) noexcept {
308- return lhs < rhs;
307+ return Compare {}( lhs, rhs) ;
309308 });
310309
311310 if (itr != itrEnd)
@@ -321,8 +320,8 @@ class sorted_set
321320 const auto [itr, itrEnd] = std::equal_range (_data.begin (),
322321 _data.end (),
323322 key,
324- [](const auto & lhs, const auto & rhs) noexcept {
325- return lhs < rhs;
323+ [](const K & lhs, const K & rhs) noexcept {
324+ return Compare {}( lhs, rhs) ;
326325 });
327326
328327 if (itr == itrEnd)
@@ -350,6 +349,18 @@ class sorted_set
350349 vector_type _data;
351350};
352351
352+ struct shorter_or_less
353+ {
354+ constexpr bool operator ()(const std::string_view& lhs, const std::string_view& rhs) const
355+ {
356+ return lhs.size () == rhs.size () ? lhs < rhs : lhs.size () < rhs.size ();
357+ }
358+ };
359+
360+ template <class V >
361+ using string_view_map = sorted_map<std::string_view, V, shorter_or_less>;
362+ using string_view_set = sorted_set<std::string_view, shorter_or_less>;
363+
353364} // namespace graphql::internal
354365
355366#endif // SORTEDMAP_H
0 commit comments