Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: base/containers/flat_set.h

Issue 2552343003: First implementation of flat sets (Closed)
Patch Set: Review, round 1. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 - we would like to research a possibility of using
39 // a small buffer.
40 // * allocator support was not tested and we might to decide to use underlying
41 // type customization instead.
42 //
43 // Tests:
dyaroshev 2016/12/15 16:10:59 This section does not make sense, since we have on
dyaroshev 2016/12/18 22:27:23 Done.
44 // flast_set_libcpp_unittest.cc - partially adapted unit tests for std::set from
45 // libcpp.
46 // flat_set_unittest.cc - our extra tests (libcpp's one do not cover
47 // everything).
48 //
49 // Benchmarks:
50 // (Please, if you add a benchmark that is heavily affected by the performance
51 // of flat_sets, list it below.)
52 // (soon) components_perftests.HQPPerfTestOnePopularURL*
53 //
54 // Notes:
55 // Current implementation is not particularly tweaked and based on
56 // boost::containers::flat_set, eastl::vector_set and folly::sorted_vector.
57 // All of these implementations do insert(first, last) as insertion one by one
58 // (some implementations with hints and/or reserve). Boost documentation claims
59 // this algorithm to be O(n*log2(n)) but it seems to just be a good quadratic
60 // algorithm. We will refer to it is as O(n^2).
61 //
62
63 template <class Key,
64 class Compare = std::less<Key>, // instead of std::default_order_t
65 class Allocator = std::allocator<Key>>
66 // Meets the requirements of Container, AllocatorAwareContainer,
67 // AssociativeContainer, ReversibleContainer.
68 // Requires:
69 // Key is Movable
70 // Compare defines strict weak ordering on Key
71 class flat_set {
72 // private types much easier to declare before everything else in templates.
Peter Kasting 2016/12/16 08:09:07 Nit: Give an explicit "private:" header.
dyaroshev 2016/12/18 22:27:22 Done.
73 using underlying_type = std::vector<Key, Allocator>;
74
75 public:
76 // -------------------------------------------------------------------------
77 // Types:
Peter Kasting 2016/12/16 08:09:07 Nit: Some of the section headers use a trailing co
dyaroshev 2016/12/18 22:27:22 Done.
78 //
79 // Some of these typedefs in the proposal are defined based on allocator but
80 // we define them in terms of underlying_type to avoid any subtleties.
81 using key_type = Key;
82 using key_compare = Compare;
83 using value_type = Key;
84 using value_compare = Compare;
85 using allocator_type = typename underlying_type::allocator_type;
86
87 using pointer = typename underlying_type::pointer;
88 using const_pointer = typename underlying_type::const_pointer;
89 using reference = typename underlying_type::reference;
90 using const_reference = typename underlying_type::const_reference;
91 using size_type = typename underlying_type::size_type;
92 using difference_type = typename underlying_type::difference_type;
93 using iterator = typename underlying_type::iterator;
94 using const_iterator = typename underlying_type::const_iterator;
95 using reverse_iterator = typename underlying_type::reverse_iterator;
96 using const_reverse_iterator =
97 typename underlying_type::const_reverse_iterator;
98
99 // -------------------------------------------------------------------------
100 // Lifetime.
101 //
102 // Constructors that take range guarantee O(n) complexity if the input range
103 // is sorted, otherwise - O(n^2).
104 //
105 // Assume that move constructors invalidate iterators and references.
106
107 flat_set();
108 explicit flat_set(const Compare& comp, const Allocator& alloc = Allocator());
109 explicit flat_set(const Allocator& alloc);
110
111 template <class InputIterator>
112 flat_set(InputIterator first,
113 InputIterator last,
114 const Compare& comp = Compare(),
115 const Allocator& alloc = Allocator());
116
117 template <class InputIterator>
118 flat_set(InputIterator first, InputIterator last, const Allocator& alloc);
119
120 flat_set(const flat_set& x);
121 flat_set(const flat_set& x, const Allocator& alloc);
122
123 flat_set(flat_set&& x);
124 flat_set(flat_set&& x, const Allocator& alloc);
125
126 flat_set(std::initializer_list<value_type> il,
127 const Compare& comp = Compare(),
128 const Allocator& alloc = Allocator());
129 flat_set(std::initializer_list<value_type> il, const Allocator& a);
130
131 ~flat_set();
132
133 // --------------------------------------------------------------------------
134 // Assignments.
135 //
136 // Assume that move assignment invalidates iterators and references.
137
138 flat_set& operator=(const flat_set& x);
139 flat_set& operator=(flat_set&& x);
140 flat_set& operator=(std::initializer_list<value_type> il);
141
142 // --------------------------------------------------------------------------
143 // Memory management.
144 //
145 // Generally be cautious about using reserve. There is a potential to do
146 // insert more efficient if we insert into new memory. We do not take
147 // advantage of this possibility in current version but we might in the
148 // future.
149 //
150 // reserve, capacity and shrink_to_fit invalidate iterators and references.
Peter Kasting 2016/12/16 08:09:07 Why would capacity() invalidate anything? Nit: Yo
dyaroshev 2016/12/18 22:27:23 Done.
151
152 allocator_type get_allocator() const;
153
154 void reserve(size_type size);
155 size_type capacity() const;
156 void shrink_to_fit();
157
158 // --------------------------------------------------------------------------
159 // Size management.
160 //
161 // clear() leaves the capacity() of the flat_set unchanged.
162
163 void clear();
164
165 size_type size() const;
166 size_type max_size() const;
167 bool empty() const;
168
169 // --------------------------------------------------------------------------
170 // Iterators.
171
172 iterator begin();
173 const_iterator begin() const;
174 const_iterator cbegin() const;
175
176 iterator end();
177 const_iterator end() const;
178 const_iterator cend() const;
179
180 reverse_iterator rbegin();
181 const_reverse_iterator rbegin() const;
182 const_reverse_iterator crbegin() const;
183
184 reverse_iterator rend();
185 const_reverse_iterator rend() const;
186 const_reverse_iterator crend() const;
187
188 // --------------------------------------------------------------------------
189 // Insert/Erase operations.
190 //
191 // Assume that every operation invalidates iterators and references.
192 // Insertion of one element can take O(size). Range insertion currently
193 // forwards to insertion by one element but we plan on optimizing it.
194 // Prefer erase(remove) idiom for deleting multiple elements.
Peter Kasting 2016/12/16 08:09:07 Nit: I might separate the future plans into a TODO
dyaroshev 2016/12/18 22:27:22 Done.
195 //
Peter Kasting 2016/12/16 08:09:07 Nit: Remove
dyaroshev 2016/12/18 22:27:23 Done.
196
197 std::pair<iterator, bool> insert(const value_type& x);
198 std::pair<iterator, bool> insert(value_type&& x);
199
200 iterator insert(const_iterator position, const value_type& x);
201 iterator insert(const_iterator position, value_type&& x);
202
203 template <class InputIterator>
204 void insert(InputIterator first, InputIterator last);
205
206 void insert(std::initializer_list<value_type> il);
207
208 template <class... Args>
209 std::pair<iterator, bool> emplace(Args&&... args);
210
211 template <class... Args>
212 iterator emplace_hint(const_iterator position, Args&&... args);
213
214 iterator erase(const_iterator position);
215
216 iterator erase(const_iterator first, const_iterator last);
217
218 size_type erase(const key_type& x);
219
220 // --------------------------------------------------------------------------
221 // Comparators.
222
223 key_compare key_comp() const;
224
225 value_compare value_comp() const;
226
227 // --------------------------------------------------------------------------
228 // Binary search operations.
229
230 size_type count(const key_type& x) const;
231
232 iterator find(const key_type& x);
233 const_iterator find(const key_type& x) const;
234
235 std::pair<iterator, iterator> equal_range(const key_type& x);
236 std::pair<const_iterator, const_iterator> equal_range(
237 const key_type& x) const;
238
239 iterator lower_bound(const key_type& x);
240 const_iterator lower_bound(const key_type& x) const;
241
242 iterator upper_bound(const key_type& x);
243 const_iterator upper_bound(const key_type& x) const;
244
245 // --------------------------------------------------------------------------
246 // Regular type operations
247 //
248 // Assume that swap invalidates iterators and references.
249 //
250 // Comparisons are done using operations on value_type, not value_compare
251 // (same as std::set). This allows, for example, to keep flat_sets with
252 // equivalent elements in sets or maps.
Peter Kasting 2016/12/16 08:09:07 I don't really understand this comment, sorry :(
dyaroshev 2016/12/18 22:27:22 I've tried to make it cleaner. Done.
253
254 void swap(flat_set&);
255
256 friend bool operator==(const flat_set& x, const flat_set& y) {
Peter Kasting 2016/12/16 08:09:07 Wow, I never even knew you could define non-member
dyaroshev 2016/12/18 22:27:23 Because I'm writing stl like container, I've tried
Peter Kasting 2016/12/18 22:47:43 Yeah, I'm fine with it, it was just new to me.
257 return x.impl_.body_ == y.impl_.body_;
258 }
259
260 friend bool operator!=(const flat_set& x, const flat_set& y) {
261 return !operator==(x, y);
262 }
263
264 friend bool operator<(const flat_set& x, const flat_set& y) {
265 return x.impl_.body_ < y.impl_.body_;
266 }
267
268 friend bool operator>(const flat_set& x, const flat_set& y) { return y < x; }
269
270 friend bool operator>=(const flat_set& x, const flat_set& y) {
271 return !(x < y);
272 }
273
274 friend bool operator<=(const flat_set& x, const flat_set& y) {
275 return !(y < x);
276 }
277
278 friend void swap(flat_set& lhs, flat_set& rhs) { lhs.swap(rhs); }
279
280 private:
281 const flat_set& as_const() { return *this; }
282
283 iterator const_cast_it(const_iterator c_it) {
284 auto distance = std::distance(cbegin(), c_it);
285 return std::next(begin(), distance);
286 }
287
288 struct Impl : Compare { // empty base class optimization
Peter Kasting 2016/12/16 08:09:07 Nit: I suggest being less opaque here, e.g.: So t
dyaroshev 2016/12/18 22:27:22 Motivation is not 100% correct, made comment more
289
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/Erase operations.
498 // Currently we use hint the same way as eastl or boost:
499 // https://github.com/electronicarts/EASTL/blob/master/include/EASTL/vector_set. h#L493
500 //
501 // We duplicate code between copy and move version so that we can avoid
502 // creating a temporary value.
503
504 template <class Key, class Compare, class Allocator>
505 auto flat_set<Key, Compare, Allocator>::insert(const value_type& x)
506 -> std::pair<iterator, bool> {
507 auto position = lower_bound(x);
508
509 if (position == end() || value_comp()(x, *position))
510 return {impl_.body_.insert(position, x), true};
511
512 return {position, false};
513 }
514
515 template <class Key, class Compare, class Allocator>
516 auto flat_set<Key, Compare, Allocator>::insert(value_type&& x)
517 -> std::pair<iterator, bool> {
518 auto position = lower_bound(x);
519
520 if (position == end() || value_comp()(x, *position))
521 return {impl_.body_.insert(position, std::move(x)), true};
522
523 return {position, false};
524 }
525
526 template <class Key, class Compare, class Allocator>
527 auto flat_set<Key, Compare, Allocator>::insert(const_iterator position,
528 const value_type& x)
529 -> iterator {
530 if (position == end() || value_comp()(x, *position)) {
531 if (position == begin() || value_comp()(*(position - 1), x))
532 return impl_.body_.insert(position, x);
533 }
534 return insert(x).first;
535 }
536
537 template <class Key, class Compare, class Allocator>
538 auto flat_set<Key, Compare, Allocator>::insert(const_iterator position,
539 value_type&& x) -> iterator {
540 if (position == end() || value_comp()(x, *position)) {
541 if (position == begin() || value_comp()(*(position - 1), x))
542 return impl_.body_.insert(position, std::move(x));
543 }
544 return insert(std::move(x)).first;
545 }
546
547 template <class Key, class Compare, class Allocator>
548 template <class InputIterator>
549 void flat_set<Key, Compare, Allocator>::insert(InputIterator first,
550 InputIterator last) {
551 std::copy(first, last, std::inserter(*this, end()));
552 }
553
554 template <class Key, class Compare, class Allocator>
555 void flat_set<Key, Compare, Allocator>::insert(
556 std::initializer_list<value_type> il) {
557 insert(il.begin(), il.end());
558 }
559
560 template <class Key, class Compare, class Allocator>
561 template <class... Args>
562 auto flat_set<Key, Compare, Allocator>::emplace(Args&&... args)
563 -> std::pair<iterator, bool> {
564 return insert(value_type(std::forward<Args>(args)...));
565 }
566
567 template <class Key, class Compare, class Allocator>
568 template <class... Args>
569 auto flat_set<Key, Compare, Allocator>::emplace_hint(const_iterator position,
570 Args&&... args)
571 -> iterator {
572 return insert(position, value_type(std::forward<Args>(args)...));
573 }
574
575 template <class Key, class Compare, class Allocator>
576 auto flat_set<Key, Compare, Allocator>::erase(const_iterator position)
577 -> iterator {
578 return impl_.body_.erase(position);
579 }
580
581 template <class Key, class Compare, class Allocator>
582 auto flat_set<Key, Compare, Allocator>::erase(const key_type& x) -> size_type {
583 auto eq_range = equal_range(x);
584 auto res = std::distance(eq_range.first, eq_range.second);
585 erase(eq_range.first, eq_range.second);
586 return res;
587 }
588
589 template <class Key, class Compare, class Allocator>
590 auto flat_set<Key, Compare, Allocator>::erase(const_iterator first,
591 const_iterator last) -> iterator {
592 return impl_.body_.erase(first, last);
593 }
594
595 // ----------------------------------------------------------------------------
596 // Comparators.
597
598 template <class Key, class Compare, class Allocator>
599 auto flat_set<Key, Compare, Allocator>::key_comp() const -> key_compare {
600 return impl_;
601 }
602
603 template <class Key, class Compare, class Allocator>
604 auto flat_set<Key, Compare, Allocator>::value_comp() const -> value_compare {
605 return impl_;
606 }
607
608 // ----------------------------------------------------------------------------
609 // Binary search operations.
610
611 template <class Key, class Compare, class Allocator>
612 auto flat_set<Key, Compare, Allocator>::count(const key_type& x) const
613 -> size_type {
614 auto eq_range = equal_range(x);
615 return std::distance(eq_range.first, eq_range.second);
616 }
617
618 template <class Key, class Compare, class Allocator>
619 auto flat_set<Key, Compare, Allocator>::find(const key_type& x) -> iterator {
620 return const_cast_it(as_const().find(x));
621 }
622
623 template <class Key, class Compare, class Allocator>
624 auto flat_set<Key, Compare, Allocator>::find(const key_type& x) const
625 -> const_iterator {
626 auto eq_range = equal_range(x);
627 return (eq_range.first == eq_range.second) ? end() : eq_range.first;
628 }
629
630 template <class Key, class Compare, class Allocator>
631 auto flat_set<Key, Compare, Allocator>::equal_range(const key_type& x)
632 -> std::pair<iterator, iterator> {
633 auto res = as_const().equal_range(x);
634 return {const_cast_it(res.first), const_cast_it(res.second)};
635 }
636
637 template <class Key, class Compare, class Allocator>
638 auto flat_set<Key, Compare, Allocator>::equal_range(const key_type& x) const
639 -> std::pair<const_iterator, const_iterator> {
640 auto lower = lower_bound(x);
641
642 if (lower == end() || key_comp()(x, *lower))
Peter Kasting 2016/12/16 08:09:07 When would lower != end(), but key_comp() succeed?
dyaroshev 2016/12/18 22:27:23 Lower bound returns the smallest position, where y
Peter Kasting 2016/12/18 22:47:43 Oh, key_comp() is basically equivalent to "<"? I
643 return {lower, lower};
644
645 return {lower, std::next(lower)};
646 }
647
648 template <class Key, class Compare, class Allocator>
649 auto flat_set<Key, Compare, Allocator>::lower_bound(const key_type& x)
650 -> iterator {
651 return const_cast_it(as_const().lower_bound(x));
652 }
653
654 template <class Key, class Compare, class Allocator>
655 auto flat_set<Key, Compare, Allocator>::lower_bound(const key_type& x) const
656 -> const_iterator {
657 return std::lower_bound(begin(), end(), x, key_comp());
658 }
659
660 template <class Key, class Compare, class Allocator>
661 auto flat_set<Key, Compare, Allocator>::upper_bound(const key_type& x)
662 -> iterator {
663 return const_cast_it(as_const().upper_bound(x));
664 }
665
666 template <class Key, class Compare, class Allocator>
667 auto flat_set<Key, Compare, Allocator>::upper_bound(const key_type& x) const
668 -> const_iterator {
669 return std::upper_bound(begin(), end(), x, key_comp());
670 }
671
672 // ----------------------------------------------------------------------------
673 // Regular type operations
674
675 template <class Key, class Compare, class Allocator>
676 void flat_set<Key, Compare, Allocator>::swap(flat_set& rhs) {
677 std::swap(impl_, rhs.impl_);
678 }
679
680 } // namespace base
681
682 #endif // BASE_CONTAINERS_FLAT_SET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698