OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_CONTAINERS_FLAT_SET_H_ | |
6 #define BASE_CONTAINERS_FLAT_SET_H_ | |
7 | |
8 #include <algorithm> | |
9 #include <functional> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 namespace base { | |
14 | |
15 // Overview: | |
16 // This file implements flat_set container. It is an alternative to standard | |
17 // sorted containers that stores it's elements in contiguous memory (current | |
18 // version uses sorted std::vector). | |
19 // Discussion that preceded introduction of this container can be found here: | |
20 // https://groups.google.com/a/chromium.org/forum/#!searchin/chromium-dev/vector $20based/chromium-dev/4uQMma9vj9w/HaQ-WvMOAwAJ | |
21 // | |
22 // Motivation: | |
23 // Contiguous memory is very beneficial to iteration and copy speed at the cost | |
24 // of worse algorithmic complexity of insertion/erasure operations. They can | |
25 // be very fast for set operations and for small number of elements. | |
26 // | |
27 // Waring: | |
28 // Current version of flat sets is advised to be used only if you have good | |
29 // benchmarks and tests for your code: beware of bugs and potential performance | |
30 // pitfalls. | |
31 // | |
32 // Important usability aspects: | |
33 // * flat_set implements std::set interface from C++11 where possible. It | |
34 // also has capacity and shrink_to_fit from vector. | |
35 // * iteration invalidation rules differ: | |
36 // - all cases of vector::iterator invalidation also apply | |
37 // - we ask (for now) not to rely on the fact that move operations do not | |
38 // invalidate iterators. | |
39 // TODO(dyaroshev): Research the possibility of using a small buffer. | |
40 // * allocator support was not tested and we might to decide to use underlying | |
41 // type customization instead. | |
42 // | |
43 // TODO(dyaroshev): Add list of benchmarks heavily affected by performance | |
44 // of flat_sets. | |
45 // | |
46 // Notes: | |
47 // Current implementation is not particularly tweaked and based on | |
Peter Kasting
2016/12/20 02:33:50
Nit: Is this referring to the current implementati
dyaroshev
2016/12/20 21:59:55
Done.
| |
48 // boost::containers::flat_set, eastl::vector_set and folly::sorted_vector. | |
49 // All of these implementations do insert(first, last) as insertion one by one | |
50 // (some implementations with hints and/or reserve). Boost documentation claims | |
51 // this algorithm to be O(n*log2(n)) but it seems to just be a good quadratic | |
52 // algorithm. We will refer to it is as O(n^2). | |
53 // TODO(dyaroshev): research better algorithm for range insertion. | |
54 | |
55 template <class Key, | |
56 class Compare = std::less<Key>, // instead of std::default_order_t | |
57 class Allocator = std::allocator<Key>> | |
58 // Meets the requirements of Container, AllocatorAwareContainer, | |
59 // AssociativeContainer, ReversibleContainer. | |
60 // Requires: | |
61 // Key is Movable | |
62 // Compare defines strict weak ordering on Key | |
63 class flat_set { | |
64 private: | |
65 // Private typedefs have to be declared higher than used. | |
66 using underlying_type = std::vector<Key, Allocator>; | |
67 | |
68 public: | |
69 // -------------------------------------------------------------------------- | |
70 // Types. | |
71 // | |
72 // Some of these typedefs in the proposal are defined based on allocator but | |
73 // we define them in terms of underlying_type to avoid any subtleties. | |
74 using key_type = Key; | |
75 using key_compare = Compare; | |
76 using value_type = Key; | |
77 using value_compare = Compare; | |
78 using allocator_type = typename underlying_type::allocator_type; | |
79 | |
80 using pointer = typename underlying_type::pointer; | |
81 using const_pointer = typename underlying_type::const_pointer; | |
82 using reference = typename underlying_type::reference; | |
83 using const_reference = typename underlying_type::const_reference; | |
84 using size_type = typename underlying_type::size_type; | |
85 using difference_type = typename underlying_type::difference_type; | |
86 using iterator = typename underlying_type::iterator; | |
87 using const_iterator = typename underlying_type::const_iterator; | |
88 using reverse_iterator = typename underlying_type::reverse_iterator; | |
89 using const_reverse_iterator = | |
90 typename underlying_type::const_reverse_iterator; | |
91 | |
92 // -------------------------------------------------------------------------- | |
93 // Lifetime. | |
94 // | |
95 // Constructors that take range guarantee O(n) complexity if the input range | |
96 // is sorted, otherwise - O(n^2). | |
97 // | |
98 // Assume that move constructors invalidate iterators and references. | |
99 | |
100 flat_set(); | |
101 explicit flat_set(const Compare& comp, const Allocator& alloc = Allocator()); | |
102 explicit flat_set(const Allocator& alloc); | |
103 | |
104 template <class InputIterator> | |
105 flat_set(InputIterator first, | |
106 InputIterator last, | |
107 const Compare& comp = Compare(), | |
108 const Allocator& alloc = Allocator()); | |
109 | |
110 template <class InputIterator> | |
111 flat_set(InputIterator first, InputIterator last, const Allocator& alloc); | |
112 | |
113 flat_set(const flat_set& x); | |
114 flat_set(const flat_set& x, const Allocator& alloc); | |
115 | |
116 flat_set(flat_set&& x); | |
117 flat_set(flat_set&& x, const Allocator& alloc); | |
118 | |
119 flat_set(std::initializer_list<value_type> il, | |
120 const Compare& comp = Compare(), | |
121 const Allocator& alloc = Allocator()); | |
122 flat_set(std::initializer_list<value_type> il, const Allocator& a); | |
123 | |
124 ~flat_set(); | |
125 | |
126 // -------------------------------------------------------------------------- | |
127 // Assignments. | |
128 // | |
129 // Assume that move assignment invalidates iterators and references. | |
130 | |
131 flat_set& operator=(const flat_set& x); | |
132 flat_set& operator=(flat_set&& x); | |
133 flat_set& operator=(std::initializer_list<value_type> il); | |
134 | |
135 // -------------------------------------------------------------------------- | |
136 // Memory management. | |
137 // | |
138 // Generally be cautious about using reserve. There is a potential to do | |
139 // insert more efficient if we insert into new memory. We do not take | |
140 // advantage of this possibility in current version but we might in the | |
141 // future. | |
142 // | |
143 // reserve() and shrink_to_fit() invalidate iterators and references. | |
144 | |
145 allocator_type get_allocator() const; | |
146 | |
147 void reserve(size_type size); | |
148 size_type capacity() const; | |
149 void shrink_to_fit(); | |
dyaroshev
2016/12/18 23:07:14
Add a comment that shrink_to_fit can be a no-op.
dyaroshev
2016/12/20 21:59:55
Done.
| |
150 | |
151 // -------------------------------------------------------------------------- | |
152 // Size management. | |
153 // | |
154 // clear() leaves the capacity() of the flat_set unchanged. | |
155 | |
156 void clear(); | |
157 | |
158 size_type size() const; | |
159 size_type max_size() const; | |
160 bool empty() const; | |
161 | |
162 // -------------------------------------------------------------------------- | |
163 // Iterators. | |
164 | |
165 iterator begin(); | |
166 const_iterator begin() const; | |
167 const_iterator cbegin() const; | |
168 | |
169 iterator end(); | |
170 const_iterator end() const; | |
171 const_iterator cend() const; | |
172 | |
173 reverse_iterator rbegin(); | |
174 const_reverse_iterator rbegin() const; | |
175 const_reverse_iterator crbegin() const; | |
176 | |
177 reverse_iterator rend(); | |
178 const_reverse_iterator rend() const; | |
179 const_reverse_iterator crend() const; | |
180 | |
181 // -------------------------------------------------------------------------- | |
182 // Insert operations. | |
183 // | |
184 // Assume that every operation invalidates iterators and references. | |
185 // Insertion of one element can take O(size). See the Notes section about | |
186 // range insertion before class declaration. | |
Peter Kasting
2016/12/20 02:33:50
Nit: "before class declaration" here appears to be
dyaroshev
2016/12/20 21:59:54
Done.
| |
187 | |
188 std::pair<iterator, bool> insert(const value_type& x); | |
189 std::pair<iterator, bool> insert(value_type&& x); | |
190 | |
191 iterator insert(const_iterator position, const value_type& x); | |
192 iterator insert(const_iterator position, value_type&& x); | |
193 | |
194 template <class InputIterator> | |
195 void insert(InputIterator first, InputIterator last); | |
196 | |
197 void insert(std::initializer_list<value_type> il); | |
198 | |
199 template <class... Args> | |
200 std::pair<iterator, bool> emplace(Args&&... args); | |
201 | |
202 template <class... Args> | |
203 iterator emplace_hint(const_iterator position, Args&&... args); | |
204 | |
205 // -------------------------------------------------------------------------- | |
206 // Erase operations. | |
207 // | |
208 // Assume that every operation invalidates iterators and references. Removal | |
209 // of one element can take O(size). Prefer the erase(std::remove(), end()) | |
210 // idiom for deleting multiple elements. | |
211 | |
212 iterator erase(const_iterator position); | |
213 | |
214 iterator erase(const_iterator first, const_iterator last); | |
215 | |
216 size_type erase(const key_type& x); | |
217 | |
218 // -------------------------------------------------------------------------- | |
219 // Comparators. | |
220 | |
221 key_compare key_comp() const; | |
222 | |
223 value_compare value_comp() const; | |
224 | |
225 // -------------------------------------------------------------------------- | |
226 // Binary search operations. | |
227 | |
228 size_type count(const key_type& x) const; | |
229 | |
230 iterator find(const key_type& x); | |
231 const_iterator find(const key_type& x) const; | |
232 | |
233 std::pair<iterator, iterator> equal_range(const key_type& x); | |
234 std::pair<const_iterator, const_iterator> equal_range( | |
235 const key_type& x) const; | |
236 | |
237 iterator lower_bound(const key_type& x); | |
238 const_iterator lower_bound(const key_type& x) const; | |
239 | |
240 iterator upper_bound(const key_type& x); | |
241 const_iterator upper_bound(const key_type& x) const; | |
242 | |
243 // -------------------------------------------------------------------------- | |
244 // General operations. | |
245 // | |
246 // Assume that swap invalidates iterators and references. | |
247 // | |
248 // Flat sets are compared using operators defined for key type and not | |
Peter Kasting
2016/12/20 02:33:50
Shouldn't "key type" here be |underlying_type|? T
dyaroshev
2016/12/20 21:59:55
I think it should work like this regardless of the
Peter Kasting
2016/12/20 22:06:38
I'm just saying, the actual operator==() and opera
| |
249 // value_comp(). This matches the behavior of std::sets. | |
250 | |
251 void swap(flat_set& x); | |
252 | |
253 friend bool operator==(const flat_set& x, const flat_set& y) { | |
254 return x.impl_.body_ == y.impl_.body_; | |
255 } | |
256 | |
257 friend bool operator!=(const flat_set& x, const flat_set& y) { | |
258 return !operator==(x, y); | |
259 } | |
260 | |
261 friend bool operator<(const flat_set& x, const flat_set& y) { | |
262 return x.impl_.body_ < y.impl_.body_; | |
263 } | |
264 | |
265 friend bool operator>(const flat_set& x, const flat_set& y) { return y < x; } | |
266 | |
267 friend bool operator>=(const flat_set& x, const flat_set& y) { | |
268 return !(x < y); | |
269 } | |
270 | |
271 friend bool operator<=(const flat_set& x, const flat_set& y) { | |
272 return !(y < x); | |
273 } | |
274 | |
275 friend void swap(flat_set& x, flat_set& y) { x.swap(y); } | |
276 | |
277 private: | |
278 const flat_set& as_const() { return *this; } | |
279 | |
280 iterator const_cast_it(const_iterator c_it) { | |
281 auto distance = std::distance(cbegin(), c_it); | |
282 return std::next(begin(), distance); | |
283 } | |
284 | |
285 // We have to support comparators that are not default constructible or maybe | |
286 // are not cheep to default construct - therefor we have to store Compare. | |
287 // Most of the time Compare does not have any state, so we take the advantage | |
288 // of an empty base class optimization. | |
Peter Kasting
2016/12/20 02:33:50
Nit: OK, how about this. This is basically your c
dyaroshev
2016/12/20 21:59:55
Done, except I removed not cheap part. If we would
| |
289 struct Impl : Compare { | |
290 // forwarding constructor. | |
291 template <class Cmp, class... Body> | |
292 explicit Impl(Cmp&& compare_arg, Body&&... underlying_type_args) | |
293 : Compare(std::forward<Cmp>(compare_arg)), | |
294 body_(std::forward<Body>(underlying_type_args)...) {} | |
295 | |
296 underlying_type body_; | |
297 } impl_; | |
298 }; | |
299 | |
300 // ---------------------------------------------------------------------------- | |
301 // Lifetime. | |
302 | |
303 template <class Key, class Compare, class Allocator> | |
304 flat_set<Key, Compare, Allocator>::flat_set() : flat_set(Compare()) {} | |
305 | |
306 template <class Key, class Compare, class Allocator> | |
307 flat_set<Key, Compare, Allocator>::flat_set(const Compare& comp, | |
308 const Allocator& alloc) | |
309 : impl_(comp, alloc) {} | |
310 | |
311 template <class Key, class Compare, class Allocator> | |
312 flat_set<Key, Compare, Allocator>::flat_set(const Allocator& alloc) | |
313 : flat_set(Compare(), alloc) {} | |
314 | |
315 template <class Key, class Compare, class Allocator> | |
316 template <class InputIterator> | |
317 flat_set<Key, Compare, Allocator>::flat_set(InputIterator first, | |
318 InputIterator last, | |
319 const Compare& comp, | |
320 const Allocator& alloc) | |
321 : impl_(comp, alloc) { | |
322 insert(first, last); | |
323 } | |
324 | |
325 template <class Key, class Compare, class Allocator> | |
326 template <class InputIterator> | |
327 flat_set<Key, Compare, Allocator>::flat_set(InputIterator first, | |
328 InputIterator last, | |
329 const Allocator& alloc) | |
330 : flat_set(first, last, Compare(), alloc) {} | |
331 | |
332 template <class Key, class Compare, class Allocator> | |
333 flat_set<Key, Compare, Allocator>::flat_set(const flat_set& x) = default; | |
334 | |
335 template <class Key, class Compare, class Allocator> | |
336 flat_set<Key, Compare, Allocator>::flat_set(const flat_set& x, | |
337 const Allocator& alloc) | |
338 : impl_(x.key_comp(), x.impl_.body_, alloc) {} | |
339 | |
340 template <class Key, class Compare, class Allocator> | |
341 flat_set<Key, Compare, Allocator>::flat_set(flat_set&& x) = default; | |
342 | |
343 template <class Key, class Compare, class Allocator> | |
344 flat_set<Key, Compare, Allocator>::flat_set(flat_set&& x, | |
345 const Allocator& alloc) | |
346 : impl_(x.key_comp(), std::move(x.impl_.body_), alloc) {} | |
347 | |
348 template <class Key, class Compare, class Allocator> | |
349 flat_set<Key, Compare, Allocator>::flat_set( | |
350 std::initializer_list<value_type> il, | |
351 const Compare& comp, | |
352 const Allocator& alloc) | |
353 : flat_set(std::begin(il), std::end(il), comp, alloc) {} | |
354 | |
355 template <class Key, class Compare, class Allocator> | |
356 flat_set<Key, Compare, Allocator>::flat_set( | |
357 std::initializer_list<value_type> il, | |
358 const Allocator& a) | |
359 : flat_set(il, Compare(), a) {} | |
360 | |
361 template <class Key, class Compare, class Allocator> | |
362 flat_set<Key, Compare, Allocator>::~flat_set() = default; | |
363 | |
364 // ---------------------------------------------------------------------------- | |
365 // Assignments. | |
366 | |
367 template <class Key, class Compare, class Allocator> | |
368 auto flat_set<Key, Compare, Allocator>::operator=(const flat_set& x) | |
369 -> flat_set& = default; | |
370 | |
371 template <class Key, class Compare, class Allocator> | |
372 auto flat_set<Key, Compare, Allocator>::operator=(flat_set&& x) | |
373 -> flat_set& = default; | |
374 | |
375 template <class Key, class Compare, class Allocator> | |
376 auto flat_set<Key, Compare, Allocator>::operator=( | |
377 std::initializer_list<value_type> il) -> flat_set& { | |
378 clear(); | |
379 insert(il); | |
380 return *this; | |
381 } | |
382 | |
383 // ---------------------------------------------------------------------------- | |
384 // Memory management. | |
385 | |
386 template <class Key, class Compare, class Allocator> | |
387 auto flat_set<Key, Compare, Allocator>::get_allocator() const | |
388 -> allocator_type { | |
389 return impl_.body_.get_allocator(); | |
390 } | |
391 | |
392 template <class Key, class Compare, class Allocator> | |
393 void flat_set<Key, Compare, Allocator>::reserve(size_type size) { | |
394 impl_.body_.reserve(size); | |
395 } | |
396 | |
397 template <class Key, class Compare, class Allocator> | |
398 auto flat_set<Key, Compare, Allocator>::capacity() const -> size_type { | |
399 return impl_.body_.capacity(); | |
400 } | |
401 | |
402 template <class Key, class Compare, class Allocator> | |
403 void flat_set<Key, Compare, Allocator>::shrink_to_fit() { | |
404 impl_.body_.shrink_to_fit(); | |
405 } | |
406 | |
407 // ---------------------------------------------------------------------------- | |
408 // Size management. | |
409 | |
410 template <class Key, class Compare, class Allocator> | |
411 void flat_set<Key, Compare, Allocator>::clear() { | |
412 impl_.body_.clear(); | |
413 } | |
414 | |
415 template <class Key, class Compare, class Allocator> | |
416 auto flat_set<Key, Compare, Allocator>::size() const -> size_type { | |
417 return impl_.body_.size(); | |
418 } | |
419 | |
420 template <class Key, class Compare, class Allocator> | |
421 auto flat_set<Key, Compare, Allocator>::max_size() const -> size_type { | |
422 return impl_.body_.max_size(); | |
423 } | |
424 | |
425 template <class Key, class Compare, class Allocator> | |
426 bool flat_set<Key, Compare, Allocator>::empty() const { | |
427 return impl_.body_.empty(); | |
428 } | |
429 | |
430 // ---------------------------------------------------------------------------- | |
431 // Iterators. | |
432 | |
433 template <class Key, class Compare, class Allocator> | |
434 auto flat_set<Key, Compare, Allocator>::begin() -> iterator { | |
435 return impl_.body_.begin(); | |
436 } | |
437 | |
438 template <class Key, class Compare, class Allocator> | |
439 auto flat_set<Key, Compare, Allocator>::begin() const -> const_iterator { | |
440 return impl_.body_.begin(); | |
441 } | |
442 | |
443 template <class Key, class Compare, class Allocator> | |
444 auto flat_set<Key, Compare, Allocator>::cbegin() const -> const_iterator { | |
445 return impl_.body_.cbegin(); | |
446 } | |
447 | |
448 template <class Key, class Compare, class Allocator> | |
449 auto flat_set<Key, Compare, Allocator>::end() -> iterator { | |
450 return impl_.body_.end(); | |
451 } | |
452 | |
453 template <class Key, class Compare, class Allocator> | |
454 auto flat_set<Key, Compare, Allocator>::end() const -> const_iterator { | |
455 return impl_.body_.end(); | |
456 } | |
457 | |
458 template <class Key, class Compare, class Allocator> | |
459 auto flat_set<Key, Compare, Allocator>::cend() const -> const_iterator { | |
460 return impl_.body_.cend(); | |
461 } | |
462 | |
463 template <class Key, class Compare, class Allocator> | |
464 auto flat_set<Key, Compare, Allocator>::rbegin() -> reverse_iterator { | |
465 return impl_.body_.rbegin(); | |
466 } | |
467 | |
468 template <class Key, class Compare, class Allocator> | |
469 auto flat_set<Key, Compare, Allocator>::rbegin() const | |
470 -> const_reverse_iterator { | |
471 return impl_.body_.rbegin(); | |
472 } | |
473 | |
474 template <class Key, class Compare, class Allocator> | |
475 auto flat_set<Key, Compare, Allocator>::crbegin() const | |
476 -> const_reverse_iterator { | |
477 return impl_.body_.crbegin(); | |
478 } | |
479 | |
480 template <class Key, class Compare, class Allocator> | |
481 auto flat_set<Key, Compare, Allocator>::rend() -> reverse_iterator { | |
482 return impl_.body_.rend(); | |
483 } | |
484 | |
485 template <class Key, class Compare, class Allocator> | |
486 auto flat_set<Key, Compare, Allocator>::rend() const -> const_reverse_iterator { | |
487 return impl_.body_.rend(); | |
488 } | |
489 | |
490 template <class Key, class Compare, class Allocator> | |
491 auto flat_set<Key, Compare, Allocator>::crend() const | |
492 -> const_reverse_iterator { | |
493 return impl_.body_.crend(); | |
494 } | |
495 | |
496 // ---------------------------------------------------------------------------- | |
497 // Insert operations. | |
498 // | |
499 // Currently we use hint the same way as eastl or boost: | |
500 // https://github.com/electronicarts/EASTL/blob/master/include/EASTL/vector_set. h#L493 | |
501 // | |
502 // We duplicate code between copy and move version so that we can avoid | |
503 // creating a temporary value. | |
504 | |
505 template <class Key, class Compare, class Allocator> | |
506 auto flat_set<Key, Compare, Allocator>::insert(const value_type& x) | |
507 -> std::pair<iterator, bool> { | |
508 auto position = lower_bound(x); | |
509 | |
510 if (position == end() || value_comp()(x, *position)) | |
511 return {impl_.body_.insert(position, x), true}; | |
512 | |
513 return {position, false}; | |
514 } | |
515 | |
516 template <class Key, class Compare, class Allocator> | |
517 auto flat_set<Key, Compare, Allocator>::insert(value_type&& x) | |
518 -> std::pair<iterator, bool> { | |
519 auto position = lower_bound(x); | |
520 | |
521 if (position == end() || value_comp()(x, *position)) | |
522 return {impl_.body_.insert(position, std::move(x)), true}; | |
523 | |
524 return {position, false}; | |
525 } | |
526 | |
527 template <class Key, class Compare, class Allocator> | |
528 auto flat_set<Key, Compare, Allocator>::insert(const_iterator position, | |
529 const value_type& x) | |
530 -> iterator { | |
531 if (position == end() || value_comp()(x, *position)) { | |
532 if (position == begin() || value_comp()(*(position - 1), x)) | |
533 return impl_.body_.insert(position, x); | |
534 } | |
535 return insert(x).first; | |
536 } | |
537 | |
538 template <class Key, class Compare, class Allocator> | |
539 auto flat_set<Key, Compare, Allocator>::insert(const_iterator position, | |
540 value_type&& x) -> iterator { | |
541 if (position == end() || value_comp()(x, *position)) { | |
542 if (position == begin() || value_comp()(*(position - 1), x)) | |
543 return impl_.body_.insert(position, std::move(x)); | |
544 } | |
545 return insert(std::move(x)).first; | |
546 } | |
547 | |
548 template <class Key, class Compare, class Allocator> | |
549 template <class InputIterator> | |
550 void flat_set<Key, Compare, Allocator>::insert(InputIterator first, | |
551 InputIterator last) { | |
552 std::copy(first, last, std::inserter(*this, end())); | |
553 } | |
554 | |
555 template <class Key, class Compare, class Allocator> | |
556 void flat_set<Key, Compare, Allocator>::insert( | |
557 std::initializer_list<value_type> il) { | |
558 insert(il.begin(), il.end()); | |
559 } | |
560 | |
561 template <class Key, class Compare, class Allocator> | |
562 template <class... Args> | |
563 auto flat_set<Key, Compare, Allocator>::emplace(Args&&... args) | |
564 -> std::pair<iterator, bool> { | |
565 return insert(value_type(std::forward<Args>(args)...)); | |
566 } | |
567 | |
568 template <class Key, class Compare, class Allocator> | |
569 template <class... Args> | |
570 auto flat_set<Key, Compare, Allocator>::emplace_hint(const_iterator position, | |
571 Args&&... args) | |
572 -> iterator { | |
573 return insert(position, value_type(std::forward<Args>(args)...)); | |
574 } | |
575 | |
576 // ---------------------------------------------------------------------------- | |
577 // Erase operations. | |
578 | |
579 template <class Key, class Compare, class Allocator> | |
580 auto flat_set<Key, Compare, Allocator>::erase(const_iterator position) | |
581 -> iterator { | |
582 return impl_.body_.erase(position); | |
583 } | |
584 | |
585 template <class Key, class Compare, class Allocator> | |
586 auto flat_set<Key, Compare, Allocator>::erase(const key_type& x) -> size_type { | |
587 auto eq_range = equal_range(x); | |
588 auto res = std::distance(eq_range.first, eq_range.second); | |
589 erase(eq_range.first, eq_range.second); | |
590 return res; | |
591 } | |
592 | |
593 template <class Key, class Compare, class Allocator> | |
594 auto flat_set<Key, Compare, Allocator>::erase(const_iterator first, | |
595 const_iterator last) -> iterator { | |
596 return impl_.body_.erase(first, last); | |
597 } | |
598 | |
599 // ---------------------------------------------------------------------------- | |
600 // Comparators. | |
601 | |
602 template <class Key, class Compare, class Allocator> | |
603 auto flat_set<Key, Compare, Allocator>::key_comp() const -> key_compare { | |
604 return impl_; | |
605 } | |
606 | |
607 template <class Key, class Compare, class Allocator> | |
608 auto flat_set<Key, Compare, Allocator>::value_comp() const -> value_compare { | |
609 return impl_; | |
610 } | |
611 | |
612 // ---------------------------------------------------------------------------- | |
613 // Binary search operations. | |
614 | |
615 template <class Key, class Compare, class Allocator> | |
616 auto flat_set<Key, Compare, Allocator>::count(const key_type& x) const | |
617 -> size_type { | |
618 auto eq_range = equal_range(x); | |
619 return std::distance(eq_range.first, eq_range.second); | |
620 } | |
621 | |
622 template <class Key, class Compare, class Allocator> | |
623 auto flat_set<Key, Compare, Allocator>::find(const key_type& x) -> iterator { | |
624 return const_cast_it(as_const().find(x)); | |
625 } | |
626 | |
627 template <class Key, class Compare, class Allocator> | |
628 auto flat_set<Key, Compare, Allocator>::find(const key_type& x) const | |
629 -> const_iterator { | |
630 auto eq_range = equal_range(x); | |
631 return (eq_range.first == eq_range.second) ? end() : eq_range.first; | |
632 } | |
633 | |
634 template <class Key, class Compare, class Allocator> | |
635 auto flat_set<Key, Compare, Allocator>::equal_range(const key_type& x) | |
636 -> std::pair<iterator, iterator> { | |
637 auto res = as_const().equal_range(x); | |
638 return {const_cast_it(res.first), const_cast_it(res.second)}; | |
639 } | |
640 | |
641 template <class Key, class Compare, class Allocator> | |
642 auto flat_set<Key, Compare, Allocator>::equal_range(const key_type& x) const | |
643 -> std::pair<const_iterator, const_iterator> { | |
644 auto lower = lower_bound(x); | |
645 | |
646 if (lower == end() || key_comp()(x, *lower)) | |
647 return {lower, lower}; | |
648 | |
649 return {lower, std::next(lower)}; | |
650 } | |
651 | |
652 template <class Key, class Compare, class Allocator> | |
653 auto flat_set<Key, Compare, Allocator>::lower_bound(const key_type& x) | |
654 -> iterator { | |
655 return const_cast_it(as_const().lower_bound(x)); | |
656 } | |
657 | |
658 template <class Key, class Compare, class Allocator> | |
659 auto flat_set<Key, Compare, Allocator>::lower_bound(const key_type& x) const | |
660 -> const_iterator { | |
661 return std::lower_bound(begin(), end(), x, key_comp()); | |
662 } | |
663 | |
664 template <class Key, class Compare, class Allocator> | |
665 auto flat_set<Key, Compare, Allocator>::upper_bound(const key_type& x) | |
666 -> iterator { | |
667 return const_cast_it(as_const().upper_bound(x)); | |
668 } | |
669 | |
670 template <class Key, class Compare, class Allocator> | |
671 auto flat_set<Key, Compare, Allocator>::upper_bound(const key_type& x) const | |
672 -> const_iterator { | |
673 return std::upper_bound(begin(), end(), x, key_comp()); | |
674 } | |
675 | |
676 // ---------------------------------------------------------------------------- | |
677 // General operations. | |
678 | |
679 template <class Key, class Compare, class Allocator> | |
680 void flat_set<Key, Compare, Allocator>::swap(flat_set& rhs) { | |
681 std::swap(impl_, rhs.impl_); | |
682 } | |
683 | |
684 } // namespace base | |
685 | |
686 #endif // BASE_CONTAINERS_FLAT_SET_H_ | |
OLD | NEW |