Skip to content

Commit 91c6afb

Browse files
author
dgolear
committed
YT: Allow heterogeneous lookup for TCompactFlatMap
b7397bff929c40b73c27256bbf9be5a5594ebafa
1 parent 68122e8 commit 91c6afb

File tree

4 files changed

+139
-122
lines changed

4 files changed

+139
-122
lines changed

library/cpp/yt/small_containers/compact_flat_map-inl.h

Lines changed: 89 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,137 +8,140 @@ namespace NYT {
88

99
///////////////////////////////////////////////////////////////////////////////
1010

11-
template <class K, class V, size_t N>
11+
template <class TKey, class TValue, size_t N, class TKeyCompare>
1212
template <class TInputIterator>
13-
TCompactFlatMap<K, V, N>::TCompactFlatMap(TInputIterator begin, TInputIterator end)
13+
TCompactFlatMap<TKey, TValue, N, TKeyCompare>::TCompactFlatMap(TInputIterator begin, TInputIterator end)
1414
{
1515
insert(begin, end);
1616
}
1717

18-
template <class K, class V, size_t N>
19-
TCompactFlatMap<K, V, N>::TCompactFlatMap(std::initializer_list<value_type> values)
20-
: TCompactFlatMap<K, V, N>(values.begin(), values.end())
18+
template <class TKey, class TValue, size_t N, class TKeyCompare>
19+
TCompactFlatMap<TKey, TValue, N, TKeyCompare>::TCompactFlatMap(std::initializer_list<value_type> values)
20+
: TCompactFlatMap<TKey, TValue, N, TKeyCompare>(values.begin(), values.end())
2121
{ }
2222

23-
template <class K, class V, size_t N>
24-
bool TCompactFlatMap<K, V, N>::operator==(const TCompactFlatMap& rhs) const
23+
template <class TKey, class TValue, size_t N, class TKeyCompare>
24+
bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator==(const TCompactFlatMap& rhs) const
2525
{
2626
return Storage_ == rhs.Storage_;
2727
}
2828

29-
template <class K, class V, size_t N>
30-
bool TCompactFlatMap<K, V, N>::operator!=(const TCompactFlatMap& rhs) const
29+
template <class TKey, class TValue, size_t N, class TKeyCompare>
30+
bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator!=(const TCompactFlatMap& rhs) const
3131
{
3232
return !(*this == rhs);
3333
}
3434

35-
template <class K, class V, size_t N>
36-
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::begin()
35+
template <class TKey, class TValue, size_t N, class TKeyCompare>
36+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::begin()
3737
{
3838
return Storage_.begin();
3939
}
4040

41-
template <class K, class V, size_t N>
42-
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::begin() const
41+
template <class TKey, class TValue, size_t N, class TKeyCompare>
42+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::begin() const
4343
{
4444
return Storage_.begin();
4545
}
4646

47-
template <class K, class V, size_t N>
48-
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::cbegin() const
47+
template <class TKey, class TValue, size_t N, class TKeyCompare>
48+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::cbegin() const
4949
{
5050
return Storage_.begin();
5151
}
5252

53-
template <class K, class V, size_t N>
54-
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::end()
53+
template <class TKey, class TValue, size_t N, class TKeyCompare>
54+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::end()
5555
{
5656
return Storage_.end();
5757
}
5858

59-
template <class K, class V, size_t N>
60-
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::end() const
59+
template <class TKey, class TValue, size_t N, class TKeyCompare>
60+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::end() const
6161
{
6262
return Storage_.end();
6363
}
6464

65-
template <class K, class V, size_t N>
66-
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::cend() const
65+
template <class TKey, class TValue, size_t N, class TKeyCompare>
66+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::cend() const
6767
{
6868
return Storage_.end();
6969
}
7070

71-
template <class K, class V, size_t N>
72-
void TCompactFlatMap<K, V, N>::reserve(size_type n)
71+
template <class TKey, class TValue, size_t N, class TKeyCompare>
72+
void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::reserve(size_type n)
7373
{
7474
Storage_.reserve(n);
7575
}
7676

77-
template <class K, class V, size_t N>
78-
typename TCompactFlatMap<K, V, N>::size_type TCompactFlatMap<K, V, N>::size() const
77+
template <class TKey, class TValue, size_t N, class TKeyCompare>
78+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::size_type TCompactFlatMap<TKey, TValue, N, TKeyCompare>::size() const
7979
{
8080
return Storage_.size();
8181
}
8282

83-
template <class K, class V, size_t N>
84-
int TCompactFlatMap<K, V, N>::ssize() const
83+
template <class TKey, class TValue, size_t N, class TKeyCompare>
84+
int TCompactFlatMap<TKey, TValue, N, TKeyCompare>::ssize() const
8585
{
8686
return static_cast<int>(Storage_.size());
8787
}
8888

89-
template <class K, class V, size_t N>
90-
bool TCompactFlatMap<K, V, N>::empty() const
89+
template <class TKey, class TValue, size_t N, class TKeyCompare>
90+
bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::empty() const
9191
{
9292
return Storage_.empty();
9393
}
9494

95-
template <class K, class V, size_t N>
96-
void TCompactFlatMap<K, V, N>::clear()
95+
template <class TKey, class TValue, size_t N, class TKeyCompare>
96+
void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::clear()
9797
{
9898
Storage_.clear();
9999
}
100100

101-
template <class K, class V, size_t N>
102-
void TCompactFlatMap<K, V, N>::shrink_to_small()
101+
template <class TKey, class TValue, size_t N, class TKeyCompare>
102+
void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::shrink_to_small()
103103
{
104104
Storage_.shrink_to_small();
105105
}
106106

107-
template <class K, class V, size_t N>
108-
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::find(const K& k)
107+
template <class TKey, class TValue, size_t N, class TKeyCompare>
108+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
109+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::find(const TOtherKey& k)
109110
{
110111
auto [rangeBegin, rangeEnd] = equal_range(k);
111112
return rangeBegin == rangeEnd ? end() : rangeBegin;
112113
}
113114

114-
template <class K, class V, size_t N>
115-
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::find(const K& k) const
115+
template <class TKey, class TValue, size_t N, class TKeyCompare>
116+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
117+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::find(const TOtherKey& k) const
116118
{
117119
auto [rangeBegin, rangeEnd] = equal_range(k);
118120
return rangeBegin == rangeEnd ? end() : rangeBegin;
119121
}
120122

121-
template <class K, class V, size_t N>
122-
bool TCompactFlatMap<K, V, N>::contains(const K& k) const
123+
template <class TKey, class TValue, size_t N, class TKeyCompare>
124+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
125+
bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::contains(const TOtherKey& k) const
123126
{
124127
return find(k) != end();
125128
}
126129

127-
template <class K, class V, size_t N>
128-
auto TCompactFlatMap<K, V, N>::insert(const value_type& value) -> std::pair<iterator, bool>
130+
template <class TKey, class TValue, size_t N, class TKeyCompare>
131+
auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(const value_type& value) -> std::pair<iterator, bool>
129132
{
130133
return do_insert(value);
131134
}
132135

133-
template <class K, class V, size_t N>
134-
auto TCompactFlatMap<K, V, N>::insert(value_type&& value) -> std::pair<iterator, bool>
136+
template <class TKey, class TValue, size_t N, class TKeyCompare>
137+
auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(value_type&& value) -> std::pair<iterator, bool>
135138
{
136139
return do_insert(std::move(value));
137140
}
138141

139-
template <class K, class V, size_t N>
142+
template <class TKey, class TValue, size_t N, class TKeyCompare>
140143
template <class TArg>
141-
auto TCompactFlatMap<K, V, N>::do_insert(TArg&& value) -> std::pair<iterator, bool>
144+
auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::do_insert(TArg&& value) -> std::pair<iterator, bool>
142145
{
143146
auto [rangeBegin, rangeEnd] = equal_range(value.first);
144147
if (rangeBegin != rangeEnd) {
@@ -149,94 +152,100 @@ auto TCompactFlatMap<K, V, N>::do_insert(TArg&& value) -> std::pair<iterator, bo
149152
}
150153
}
151154

152-
template <class K, class V, size_t N>
155+
template <class TKey, class TValue, size_t N, class TKeyCompare>
153156
template <class TInputIterator>
154-
void TCompactFlatMap<K, V, N>::insert(TInputIterator begin, TInputIterator end)
157+
void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(TInputIterator begin, TInputIterator end)
155158
{
156159
for (auto it = begin; it != end; ++it) {
157160
insert(*it);
158161
}
159162
}
160163

161-
template <class K, class V, size_t N>
164+
template <class TKey, class TValue, size_t N, class TKeyCompare>
162165
template <class... TArgs>
163-
auto TCompactFlatMap<K, V, N>::emplace(TArgs&&... args) -> std::pair<iterator, bool>
166+
auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::emplace(TArgs&&... args) -> std::pair<iterator, bool>
164167
{
165168
return insert(value_type(std::forward<TArgs>(args)...));
166169
}
167170

168-
template <class K, class V, size_t N>
169-
V& TCompactFlatMap<K, V, N>::operator[](const K& k)
171+
template <class TKey, class TValue, size_t N, class TKeyCompare>
172+
TValue& TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator[](const TKey& k)
170173
{
171-
auto [it, inserted] = insert({k, V()});
174+
auto [it, inserted] = insert({k, TValue()});
172175
return it->second;
173176
}
174177

175-
template <class K, class V, size_t N>
176-
void TCompactFlatMap<K, V, N>::erase(const K& k)
178+
template <class TKey, class TValue, size_t N, class TKeyCompare>
179+
void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::erase(const TKey& k)
177180
{
178181
auto [rangeBegin, rangeEnd] = equal_range(k);
179182
erase(rangeBegin, rangeEnd);
180183
}
181184

182-
template <class K, class V, size_t N>
183-
void TCompactFlatMap<K, V, N>::erase(iterator pos)
185+
template <class TKey, class TValue, size_t N, class TKeyCompare>
186+
void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::erase(iterator pos)
184187
{
185188
Storage_.erase(pos);
186189

187190
// Try to keep the storage inline. This is why erase doesn't return an iterator.
188191
Storage_.shrink_to_small();
189192
}
190193

191-
template <class K, class V, size_t N>
192-
void TCompactFlatMap<K, V, N>::erase(iterator b, iterator e)
194+
template <class TKey, class TValue, size_t N, class TKeyCompare>
195+
void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::erase(iterator b, iterator e)
193196
{
194197
Storage_.erase(b, e);
195198

196199
// Try to keep the storage inline. This is why erase doesn't return an iterator.
197200
Storage_.shrink_to_small();
198201
}
199202

200-
template <class K, class V, size_t N>
201-
std::pair<typename TCompactFlatMap<K, V, N>::iterator, typename TCompactFlatMap<K, V, N>::iterator>
202-
TCompactFlatMap<K, V, N>::equal_range(const K& k)
203+
template <class TKey, class TValue, size_t N, class TKeyCompare>
204+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
205+
std::pair<typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator, typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator>
206+
TCompactFlatMap<TKey, TValue, N, TKeyCompare>::equal_range(const TOtherKey& k)
203207
{
204-
auto result = std::equal_range(Storage_.begin(), Storage_.end(), k, TKeyComparer());
205-
YT_ASSERT(std::distance(result.first, result.second) <= 1);
208+
auto result = std::ranges::equal_range(Storage_, k, {}, &value_type::first);
209+
YT_ASSERT(result.size() <= 1);
206210
return result;
207211
}
208212

209-
template <class K, class V, size_t N>
210-
std::pair<typename TCompactFlatMap<K, V, N>::const_iterator, typename TCompactFlatMap<K, V, N>::const_iterator>
211-
TCompactFlatMap<K, V, N>::equal_range(const K& k) const
213+
template <class TKey, class TValue, size_t N, class TKeyCompare>
214+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
215+
std::pair<typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator, typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator>
216+
TCompactFlatMap<TKey, TValue, N, TKeyCompare>::equal_range(const TOtherKey& k) const
212217
{
213-
auto result = std::equal_range(Storage_.begin(), Storage_.end(), k, TKeyComparer());
214-
YT_ASSERT(std::distance(result.first, result.second) <= 1);
218+
auto result = std::ranges::equal_range(Storage_, k, {}, &value_type::first);
219+
YT_ASSERT(result.size() <= 1);
215220
return result;
216221
}
217222

218-
template <class K, class V, size_t N>
219-
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::lower_bound(const K& k) const
223+
template <class TKey, class TValue, size_t N, class TKeyCompare>
224+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
225+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::lower_bound(const TOtherKey& k) const
220226
{
221-
return std::lower_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
227+
return std::ranges::lower_bound(Storage_, k, {}, &value_type::first);
222228
}
223229

224-
template <class K, class V, size_t N>
225-
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::lower_bound(const K& k)
230+
template <class TKey, class TValue, size_t N, class TKeyCompare>
231+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
232+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::lower_bound(const TOtherKey& k)
226233
{
227-
return std::lower_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
234+
return std::ranges::lower_bound(Storage_, k, {}, &value_type::first);
228235
}
229236

230-
template <class K, class V, size_t N>
231-
typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::upper_bound(const K& k) const
237+
template <class TKey, class TValue, size_t N, class TKeyCompare>
238+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
239+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::upper_bound(const TOtherKey& k) const
232240
{
233-
return std::upper_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
241+
return std::ranges::upper_bound(Storage_, k, {}, &value_type::first);
234242
}
235243

236-
template <class K, class V, size_t N>
237-
typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::upper_bound(const K& k)
244+
template <class TKey, class TValue, size_t N, class TKeyCompare>
245+
template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
246+
typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::upper_bound(const TOtherKey& k)
238247
{
239-
return std::upper_bound(Storage_.begin(), Storage_.end(), k, TKeyComparer());
248+
return std::ranges::upper_bound(Storage_, k, {}, &value_type::first);
240249
}
241250

242251
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)