23#ifndef FROZEN_LETITGO_MAP_H
24#define FROZEN_LETITGO_MAP_H
39template <
class Comparator>
class CompareKey :
private Comparator {
41 constexpr Comparator
const&
key_comp() const noexcept {
42 return static_cast<Comparator const&
>(*this);
46 : Comparator(comparator) {}
48 template <
class Key1,
class Key2,
class Value>
49 constexpr auto operator()(std::pair<Key1, Value>
const &self,
50 std::pair<Key2, Value>
const &other)
const ->
decltype(
key_comp()(std::get<0>(self), std::get<0>(other))) {
51 return key_comp()(std::get<0>(self), std::get<0>(other));
54 template <
class Key1,
class Key2,
class Value>
56 std::pair<Key2, Value>
const &other)
const ->
decltype(
key_comp()(self_key, std::get<0>(other))) {
57 return key_comp()(self_key, std::get<0>(other));
60 template <
class Key1,
class Key2,
class Value>
61 constexpr auto operator()(std::pair<Key1, Value>
const &self,
62 Key2
const &other_key)
const ->
decltype(
key_comp()(std::get<0>(self), other_key)) {
63 return key_comp()(std::get<0>(self), other_key);
66 template <
class Key1,
class Key2>
67 constexpr auto operator()(Key1
const &self_key, Key2
const &other_key)
const ->
decltype(
key_comp()(self_key, other_key)) {
68 return key_comp()(self_key, other_key);
74template <
class Key,
class Value, std::
size_t N,
class Compare = std::less<Key>>
77 container_type items_;
98 constexpr map(container_type items, Compare
const &compare)
102 explicit constexpr map(container_type items)
103 :
map{items, Compare{}} {}
105 constexpr map(std::initializer_list<value_type> items, Compare
const &compare)
106 :
map{container_type {items}, compare} {
109 constexpr map(std::initializer_list<value_type> items)
110 :
map{items, Compare{}} {}
113 constexpr Value
const&
at(Key
const &key)
const {
114 return at_impl(*
this, key);
116 constexpr Value&
at(Key
const &key) {
117 return at_impl(*
this, key);
136 constexpr bool empty()
const {
return !N; }
142 template <
class KeyType>
143 constexpr std::size_t
count(KeyType
const &key)
const {
147 template <
class KeyType>
149 return map::find_impl(*
this, key);
151 template <
class KeyType>
153 return map::find_impl(*
this, key);
156 template <
class KeyType>
157 constexpr bool contains(KeyType
const &key)
const {
158 return this->
find(key) != this->
end();
161 template <
class KeyType>
162 constexpr std::pair<const_iterator, const_iterator>
164 return equal_range_impl(*
this, key);
166 template <
class KeyType>
167 constexpr std::pair<iterator, iterator>
equal_range(KeyType
const &key) {
168 return equal_range_impl(*
this, key);
171 template <
class KeyType>
173 return lower_bound_impl(*
this, key);
175 template <
class KeyType>
177 return lower_bound_impl(*
this, key);
180 template <
class KeyType>
182 return upper_bound_impl(*
this, key);
184 template <
class KeyType>
186 return upper_bound_impl(*
this, key);
194 template <
class This,
class KeyType>
195 static inline constexpr auto& at_impl(This&& self, KeyType
const &key) {
196 auto where = self.find(key);
197 if (where != self.end())
198 return where->second;
203 template <
class This,
class KeyType>
204 static inline constexpr auto find_impl(This&& self, KeyType
const &key) {
205 auto where = self.lower_bound(key);
206 if (where != self.end() && !self.value_comp()(key, *where))
212 template <
class This,
class KeyType>
213 static inline constexpr auto equal_range_impl(This&& self, KeyType
const &key) {
214 auto lower = self.lower_bound(key);
215 using lower_t =
decltype(lower);
216 if (lower != self.end() && !self.value_comp()(key, *lower))
217 return std::pair<lower_t, lower_t>{lower, lower + 1};
219 return std::pair<lower_t, lower_t>{lower, lower};
222 template <
class This,
class KeyType>
223 static inline constexpr auto lower_bound_impl(This&& self, KeyType
const &key) ->
decltype(self.end()) {
227 template <
class This,
class KeyType>
228 static inline constexpr auto upper_bound_impl(This&& self, KeyType
const &key) {
229 auto lower = self.lower_bound(key);
230 if (lower != self.end() && !self.value_comp()(key, *lower))
237template <
class Key,
class Value,
class Compare>
260 constexpr map(
const map &other) =
default;
261 constexpr map(std::initializer_list<value_type>, Compare
const &compare)
263 constexpr map(std::initializer_list<value_type> items)
264 :
map{items, Compare{}} {}
267 template <
class KeyType>
271 template <
class KeyType>
292 constexpr bool empty()
const {
return true; }
298 template <
class KeyType>
299 constexpr std::size_t
count(KeyType
const &)
const {
return 0; }
301 template <
class KeyType>
303 template <
class KeyType>
306 template <
class KeyType>
307 constexpr std::pair<const_iterator, const_iterator>
309 template <
class KeyType>
310 constexpr std::pair<iterator, iterator>
313 template <
class KeyType>
315 template <
class KeyType>
318 template <
class KeyType>
320 template <
class KeyType>
328template <
typename T,
typename U,
typename Compare = std::less<T>>
330 return map<T, U, 0, Compare>{};
333template <
typename T,
typename U, std::
size_t N>
334constexpr auto make_map(std::pair<T, U>
const (&items)[N]) {
338template <
typename T,
typename U, std::
size_t N>
339constexpr auto make_map(std::array<std::pair<T, U>, N>
const &items) {
343template <
typename T,
typename U,
typename Compare, std::
size_t N>
344constexpr auto make_map(std::pair<T, U>
const (&items)[N], Compare
const& compare = Compare{}) {
345 return map<T, U, N, Compare>{items, compare};
348template <
typename T,
typename U,
typename Compare, std::
size_t N>
349constexpr auto make_map(std::array<std::pair<T, U>, N>
const &items, Compare
const& compare = Compare{}) {
350 return map<T, U, N, Compare>{items, compare};
Definition basic_types.h:90
const value_type & const_reference
Definition basic_types.h:109
value_type * pointer
Definition basic_types.h:110
std::ptrdiff_t difference_type
Definition basic_types.h:115
const_pointer const_iterator
Definition basic_types.h:113
pointer iterator
Definition basic_types.h:112
const value_type * const_pointer
Definition basic_types.h:111
std::size_t size_type
Definition basic_types.h:114
std::pair< const Key, Value > value_type
Definition basic_types.h:107
value_type & reference
Definition basic_types.h:108
constexpr CompareKey(Comparator const &comparator)
Definition map.h:45
constexpr auto operator()(std::pair< Key1, Value > const &self, Key2 const &other_key) const -> decltype(key_comp()(std::get< 0 >(self), other_key))
Definition map.h:61
constexpr auto operator()(std::pair< Key1, Value > const &self, std::pair< Key2, Value > const &other) const -> decltype(key_comp()(std::get< 0 >(self), std::get< 0 >(other)))
Definition map.h:49
constexpr Comparator const & key_comp() const noexcept
Definition map.h:41
constexpr auto operator()(Key1 const &self_key, std::pair< Key2, Value > const &other) const -> decltype(key_comp()(self_key, std::get< 0 >(other)))
Definition map.h:55
constexpr auto operator()(Key1 const &self_key, Key2 const &other_key) const -> decltype(key_comp()(self_key, other_key))
Definition map.h:67
constexpr map(std::initializer_list< value_type >, Compare const &compare)
Definition map.h:261
constexpr const_iterator cend() const
Definition map.h:282
constexpr value_compare const & value_comp() const
Definition map.h:325
constexpr mapped_type at(KeyType const &)
Definition map.h:272
constexpr map(std::initializer_list< value_type > items)
Definition map.h:263
constexpr std::size_t count(KeyType const &) const
Definition map.h:299
Value mapped_type
Definition map.h:243
constexpr const_reverse_iterator crend() const
Definition map.h:289
constexpr reverse_iterator rbegin()
Definition map.h:284
constexpr const_reverse_iterator crbegin() const
Definition map.h:286
constexpr iterator find(KeyType const &)
Definition map.h:304
constexpr std::pair< iterator, iterator > equal_range(KeyType const &)
Definition map.h:311
constexpr map(const map &other)=default
constexpr const_iterator begin() const
Definition map.h:278
constexpr iterator end()
Definition map.h:280
constexpr iterator lower_bound(KeyType const &)
Definition map.h:316
constexpr const_iterator find(KeyType const &) const
Definition map.h:302
typename container_type::size_type size_type
Definition map.h:245
pointer iterator
Definition map.h:253
typename container_type::const_pointer const_pointer
Definition map.h:252
constexpr bool empty() const
Definition map.h:292
const_pointer const_iterator
Definition map.h:254
Key key_type
Definition map.h:242
constexpr iterator upper_bound(KeyType const &)
Definition map.h:321
constexpr reverse_iterator rend()
Definition map.h:287
constexpr std::pair< const_iterator, const_iterator > equal_range(KeyType const &) const
Definition map.h:308
typename container_type::difference_type difference_type
Definition map.h:246
constexpr const_reverse_iterator rbegin() const
Definition map.h:285
constexpr key_compare const & key_comp() const
Definition map.h:324
constexpr const_iterator end() const
Definition map.h:281
const_pointer const_reverse_iterator
Definition map.h:256
constexpr mapped_type at(KeyType const &) const
Definition map.h:268
typename container_type::value_type value_type
Definition map.h:244
constexpr size_type size() const
Definition map.h:293
typename container_type::reference reference
Definition map.h:249
constexpr const_iterator cbegin() const
Definition map.h:279
Compare key_compare
Definition map.h:247
constexpr const_iterator upper_bound(KeyType const &) const
Definition map.h:319
constexpr const_reverse_iterator rend() const
Definition map.h:288
impl::CompareKey< Compare > value_compare
Definition map.h:248
constexpr iterator begin()
Definition map.h:277
typename container_type::pointer pointer
Definition map.h:251
constexpr size_type max_size() const
Definition map.h:294
constexpr const_iterator lower_bound(KeyType const &) const
Definition map.h:314
pointer reverse_iterator
Definition map.h:255
typename container_type::const_reference const_reference
Definition map.h:250
constexpr iterator end()
Definition map.h:124
constexpr size_type max_size() const
Definition map.h:138
constexpr const_iterator find(KeyType const &key) const
Definition map.h:148
constexpr reverse_iterator rend()
Definition map.h:131
constexpr const_reverse_iterator crbegin() const
Definition map.h:130
constexpr iterator find(KeyType const &key)
Definition map.h:152
constexpr const_iterator lower_bound(KeyType const &key) const
Definition map.h:172
constexpr const_iterator begin() const
Definition map.h:122
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition map.h:94
constexpr map(container_type items)
Definition map.h:102
constexpr const_iterator upper_bound(KeyType const &key) const
Definition map.h:181
typename container_type::const_pointer const_pointer
Definition map.h:90
typename container_type::size_type size_type
Definition map.h:83
constexpr const key_compare & key_comp() const
Definition map.h:190
constexpr const_iterator cbegin() const
Definition map.h:123
constexpr const_reverse_iterator rbegin() const
Definition map.h:129
constexpr map(std::initializer_list< value_type > items)
Definition map.h:109
constexpr iterator upper_bound(KeyType const &key)
Definition map.h:185
constexpr const_iterator end() const
Definition map.h:125
constexpr const_iterator cend() const
Definition map.h:126
constexpr const_reverse_iterator crend() const
Definition map.h:133
constexpr bool empty() const
Definition map.h:136
Key key_type
Definition map.h:80
typename container_type::const_reference const_reference
Definition map.h:88
constexpr Value & at(Key const &key)
Definition map.h:116
constexpr map(container_type items, Compare const &compare)
Definition map.h:98
typename container_type::const_iterator const_iterator
Definition map.h:92
Compare key_compare
Definition map.h:85
constexpr map(std::initializer_list< value_type > items, Compare const &compare)
Definition map.h:105
constexpr size_type size() const
Definition map.h:137
typename container_type::difference_type difference_type
Definition map.h:84
constexpr iterator begin()
Definition map.h:121
constexpr iterator lower_bound(KeyType const &key)
Definition map.h:176
typename container_type::pointer pointer
Definition map.h:89
Value mapped_type
Definition map.h:81
typename container_type::reference reference
Definition map.h:87
constexpr reverse_iterator rbegin()
Definition map.h:128
constexpr std::pair< const_iterator, const_iterator > equal_range(KeyType const &key) const
Definition map.h:163
constexpr bool contains(KeyType const &key) const
Definition map.h:157
constexpr std::size_t count(KeyType const &key) const
Definition map.h:143
typename container_type::iterator iterator
Definition map.h:91
typename container_type::value_type value_type
Definition map.h:82
std::reverse_iterator< iterator > reverse_iterator
Definition map.h:93
constexpr std::pair< iterator, iterator > equal_range(KeyType const &key)
Definition map.h:167
constexpr const value_compare & value_comp() const
Definition map.h:191
impl::CompareKey< Compare > value_compare
Definition map.h:86
constexpr const_reverse_iterator rend() const
Definition map.h:132
constexpr Value const & at(Key const &key) const
Definition map.h:113
#define FROZEN_THROW_OR_ABORT(_)
Definition exceptions.h:29
constexpr ForwardIt lower_bound(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:197
typename remove_cv< T >::type remove_cv_t
Definition mpl.h:50
constexpr void quicksort(Iterator left, Iterator right, Compare const &compare)
Definition algorithms.h:134
constexpr bool binary_search(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:202
Definition algorithm.h:30
constexpr auto make_map(bits::ignored_arg={})
Definition map.h:329
Definition basic_types.h:39