Chromium Code Reviews| 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 reserve(), capacity() and shrink_to_fit() from std::vector. | |
| 35 // * iteration invalidation rules differ: | |
| 36 // - all cases of std::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. | |
|
vmpstr
2017/01/11 21:01:42
File a bug and reference it here please.
dyaroshev
2017/01/16 06:58:02
I would like to wait for a reply from a base/Owner
| |
| 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 | |
|
vmpstr
2017/01/11 21:01:42
File a bug and reference it here please.
dyaroshev
2017/01/16 06:58:02
I don't think this one can be done in a bug. The i
| |
| 44 // of flat_sets. | |
| 45 // | |
| 46 // Notes: | |
| 47 // Current implementation is based on boost::containers::flat_set, | |
| 48 // eastl::vector_set and folly::sorted_vector. All of these implementations do | |
| 49 // insert(first, last) as insertion one by one (some implementations with hints | |
| 50 // and/or reserve). Boost documentation claims this algorithm to be O(n*log2(n)) | |
| 51 // but it seems to just be a good quadratic algorithm. We will refer to it is as | |
| 52 // O(n^2). | |
| 53 // TODO(dyaroshev): research better algorithm for range insertion. | |
|
vmpstr
2017/01/11 21:01:41
File a bug and reference it here please.
dyaroshev
2017/01/16 06:58:02
I'd postpone this to after review from base/Owners
danakj
2017/01/16 15:21:43
I asked vmpstr to review this as he's an STL exper
| |
| 54 | |
| 55 template <class Key, | |
| 56 class Compare = std::less<Key>, // instead of std::default_order_t | |
|
vmpstr
2017/01/11 21:01:42
I don't think std::default_order_t is in the stand
dyaroshev
2017/01/16 06:58:02
Thanks, it looks like default_order_t is going to
| |
| 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 // Beware that shrink_to_fit() simply forward to the underlying_type and | |
| 144 // according to c++ standard shrink_to_fit() is non-binding. | |
| 145 // | |
| 146 // reserve() and shrink_to_fit() invalidate iterators and references. | |
| 147 | |
| 148 allocator_type get_allocator() const; | |
| 149 | |
| 150 void reserve(size_type size); | |
| 151 size_type capacity() const; | |
| 152 void shrink_to_fit(); | |
| 153 | |
| 154 // -------------------------------------------------------------------------- | |
| 155 // Size management. | |
| 156 // | |
| 157 // clear() leaves the capacity() of the flat_set unchanged. | |
|
vmpstr
2017/01/11 21:01:42
doesn't this depend on the underlying_type (it's t
dyaroshev
2017/01/16 06:58:02
I am a bit on edge here: we currently don't expose
| |
| 158 | |
| 159 void clear(); | |
| 160 | |
| 161 size_type size() const; | |
| 162 size_type max_size() const; | |
| 163 bool empty() const; | |
| 164 | |
| 165 // -------------------------------------------------------------------------- | |
| 166 // Iterators. | |
| 167 | |
| 168 iterator begin(); | |
| 169 const_iterator begin() const; | |
| 170 const_iterator cbegin() const; | |
| 171 | |
| 172 iterator end(); | |
| 173 const_iterator end() const; | |
| 174 const_iterator cend() const; | |
| 175 | |
| 176 reverse_iterator rbegin(); | |
| 177 const_reverse_iterator rbegin() const; | |
| 178 const_reverse_iterator crbegin() const; | |
| 179 | |
| 180 reverse_iterator rend(); | |
| 181 const_reverse_iterator rend() const; | |
| 182 const_reverse_iterator crend() const; | |
| 183 | |
| 184 // -------------------------------------------------------------------------- | |
| 185 // Insert operations. | |
| 186 // | |
| 187 // Assume that every operation invalidates iterators and references. | |
| 188 // Insertion of one element can take O(size). See the Notes section in the | |
| 189 // class comments for more on range insertion. | |
| 190 | |
| 191 std::pair<iterator, bool> insert(const value_type& x); | |
| 192 std::pair<iterator, bool> insert(value_type&& x); | |
| 193 | |
| 194 iterator insert(const_iterator position, const value_type& x); | |
| 195 iterator insert(const_iterator position, value_type&& x); | |
| 196 | |
| 197 template <class InputIterator> | |
| 198 void insert(InputIterator first, InputIterator last); | |
| 199 | |
| 200 void insert(std::initializer_list<value_type> il); | |
| 201 | |
| 202 template <class... Args> | |
| 203 std::pair<iterator, bool> emplace(Args&&... args); | |
| 204 | |
| 205 template <class... Args> | |
| 206 iterator emplace_hint(const_iterator position, Args&&... args); | |
| 207 | |
| 208 // -------------------------------------------------------------------------- | |
| 209 // Erase operations. | |
| 210 // | |
| 211 // Assume that every operation invalidates iterators and references. Removal | |
| 212 // of one element can take O(size). Prefer the erase(std::remove(), end()) | |
| 213 // idiom for deleting multiple elements. | |
| 214 | |
| 215 iterator erase(const_iterator position); | |
| 216 | |
| 217 iterator erase(const_iterator first, const_iterator last); | |
| 218 | |
| 219 size_type erase(const key_type& x); | |
| 220 | |
| 221 // -------------------------------------------------------------------------- | |
| 222 // Comparators. | |
| 223 | |
| 224 key_compare key_comp() const; | |
| 225 | |
| 226 value_compare value_comp() const; | |
| 227 | |
| 228 // -------------------------------------------------------------------------- | |
| 229 // Binary search operations. | |
| 230 | |
| 231 size_type count(const key_type& x) const; | |
| 232 | |
| 233 iterator find(const key_type& x); | |
| 234 const_iterator find(const key_type& x) const; | |
| 235 | |
| 236 std::pair<iterator, iterator> equal_range(const key_type& x); | |
| 237 std::pair<const_iterator, const_iterator> equal_range( | |
| 238 const key_type& x) const; | |
| 239 | |
| 240 iterator lower_bound(const key_type& x); | |
| 241 const_iterator lower_bound(const key_type& x) const; | |
| 242 | |
| 243 iterator upper_bound(const key_type& x); | |
| 244 const_iterator upper_bound(const key_type& x) const; | |
| 245 | |
| 246 // -------------------------------------------------------------------------- | |
| 247 // General operations. | |
| 248 // | |
| 249 // Assume that swap invalidates iterators and references. | |
| 250 // | |
| 251 // As with std::set, equality and ordering operations for the whole flat_set | |
| 252 // are equivalent to using equal() and lexicographical_compare() on the key | |
| 253 // types, rather than using element-wise key_comp() as e.g. lower_bound() | |
| 254 // does. Implementation note: currently we use operator==() and operator<() on | |
| 255 // std::vector, because they have the same contract we need, so we use them | |
| 256 // directly for brevity and in case it is more optimal than calling equal() | |
| 257 // and lexicograhpical_compare(). If the underlying container type is changed, | |
| 258 // this code may need to be modified. | |
| 259 | |
| 260 void swap(flat_set& x); | |
| 261 | |
| 262 friend bool operator==(const flat_set& x, const flat_set& y) { | |
| 263 return x.impl_.body_ == y.impl_.body_; | |
| 264 } | |
| 265 | |
| 266 friend bool operator!=(const flat_set& x, const flat_set& y) { | |
| 267 return !operator==(x, y); | |
| 268 } | |
| 269 | |
| 270 friend bool operator<(const flat_set& x, const flat_set& y) { | |
| 271 return x.impl_.body_ < y.impl_.body_; | |
| 272 } | |
| 273 | |
| 274 friend bool operator>(const flat_set& x, const flat_set& y) { return y < x; } | |
| 275 | |
| 276 friend bool operator>=(const flat_set& x, const flat_set& y) { | |
| 277 return !(x < y); | |
| 278 } | |
| 279 | |
| 280 friend bool operator<=(const flat_set& x, const flat_set& y) { | |
| 281 return !(y < x); | |
| 282 } | |
| 283 | |
| 284 friend void swap(flat_set& x, flat_set& y) { x.swap(y); } | |
| 285 | |
| 286 private: | |
| 287 const flat_set& as_const() { return *this; } | |
| 288 | |
| 289 iterator const_cast_it(const_iterator c_it) { | |
| 290 auto distance = std::distance(cbegin(), c_it); | |
| 291 return std::next(begin(), distance); | |
| 292 } | |
| 293 | |
| 294 // To support comparators that may not be possible to default-construct, we | |
| 295 // have to store an instance of Compare. Using inheritance here lets us take | |
| 296 // advantage of the empty base class optimization to avoid extra space in the | |
| 297 // common case when Compare has no state. | |
| 298 struct Impl : Compare { | |
|
vmpstr
2017/01/11 21:01:42
According to https://google.github.io/styleguide/c
dyaroshev
2017/01/16 06:58:02
I would say that Google's style guide is trying to
| |
| 299 // forwarding constructor. | |
| 300 template <class Cmp, class... Body> | |
| 301 explicit Impl(Cmp&& compare_arg, Body&&... underlying_type_args) | |
| 302 : Compare(std::forward<Cmp>(compare_arg)), | |
| 303 body_(std::forward<Body>(underlying_type_args)...) {} | |
| 304 | |
| 305 underlying_type body_; | |
| 306 } impl_; | |
| 307 }; | |
| 308 | |
| 309 // ---------------------------------------------------------------------------- | |
| 310 // Lifetime. | |
| 311 | |
| 312 template <class Key, class Compare, class Allocator> | |
| 313 flat_set<Key, Compare, Allocator>::flat_set() : flat_set(Compare()) {} | |
| 314 | |
| 315 template <class Key, class Compare, class Allocator> | |
| 316 flat_set<Key, Compare, Allocator>::flat_set(const Compare& comp, | |
| 317 const Allocator& alloc) | |
| 318 : impl_(comp, alloc) {} | |
| 319 | |
| 320 template <class Key, class Compare, class Allocator> | |
| 321 flat_set<Key, Compare, Allocator>::flat_set(const Allocator& alloc) | |
| 322 : flat_set(Compare(), alloc) {} | |
| 323 | |
| 324 template <class Key, class Compare, class Allocator> | |
| 325 template <class InputIterator> | |
| 326 flat_set<Key, Compare, Allocator>::flat_set(InputIterator first, | |
| 327 InputIterator last, | |
| 328 const Compare& comp, | |
| 329 const Allocator& alloc) | |
| 330 : impl_(comp, alloc) { | |
| 331 insert(first, last); | |
| 332 } | |
| 333 | |
| 334 template <class Key, class Compare, class Allocator> | |
| 335 template <class InputIterator> | |
| 336 flat_set<Key, Compare, Allocator>::flat_set(InputIterator first, | |
| 337 InputIterator last, | |
| 338 const Allocator& alloc) | |
| 339 : flat_set(first, last, Compare(), alloc) {} | |
| 340 | |
| 341 template <class Key, class Compare, class Allocator> | |
| 342 flat_set<Key, Compare, Allocator>::flat_set(const flat_set& x) = default; | |
| 343 | |
| 344 template <class Key, class Compare, class Allocator> | |
| 345 flat_set<Key, Compare, Allocator>::flat_set(const flat_set& x, | |
| 346 const Allocator& alloc) | |
| 347 : impl_(x.key_comp(), x.impl_.body_, alloc) {} | |
| 348 | |
| 349 template <class Key, class Compare, class Allocator> | |
| 350 flat_set<Key, Compare, Allocator>::flat_set(flat_set&& x) = default; | |
| 351 | |
| 352 template <class Key, class Compare, class Allocator> | |
| 353 flat_set<Key, Compare, Allocator>::flat_set(flat_set&& x, | |
| 354 const Allocator& alloc) | |
| 355 : impl_(x.key_comp(), std::move(x.impl_.body_), alloc) {} | |
| 356 | |
| 357 template <class Key, class Compare, class Allocator> | |
| 358 flat_set<Key, Compare, Allocator>::flat_set( | |
| 359 std::initializer_list<value_type> il, | |
| 360 const Compare& comp, | |
| 361 const Allocator& alloc) | |
| 362 : flat_set(std::begin(il), std::end(il), comp, alloc) {} | |
| 363 | |
| 364 template <class Key, class Compare, class Allocator> | |
| 365 flat_set<Key, Compare, Allocator>::flat_set( | |
| 366 std::initializer_list<value_type> il, | |
| 367 const Allocator& a) | |
| 368 : flat_set(il, Compare(), a) {} | |
| 369 | |
| 370 template <class Key, class Compare, class Allocator> | |
| 371 flat_set<Key, Compare, Allocator>::~flat_set() = default; | |
| 372 | |
| 373 // ---------------------------------------------------------------------------- | |
| 374 // Assignments. | |
| 375 | |
| 376 template <class Key, class Compare, class Allocator> | |
| 377 auto flat_set<Key, Compare, Allocator>::operator=(const flat_set& x) | |
| 378 -> flat_set& = default; | |
| 379 | |
| 380 template <class Key, class Compare, class Allocator> | |
| 381 auto flat_set<Key, Compare, Allocator>::operator=(flat_set&& x) | |
| 382 -> flat_set& = default; | |
| 383 | |
| 384 template <class Key, class Compare, class Allocator> | |
| 385 auto flat_set<Key, Compare, Allocator>::operator=( | |
| 386 std::initializer_list<value_type> il) -> flat_set& { | |
| 387 clear(); | |
| 388 insert(il); | |
| 389 return *this; | |
| 390 } | |
| 391 | |
| 392 // ---------------------------------------------------------------------------- | |
| 393 // Memory management. | |
| 394 | |
| 395 template <class Key, class Compare, class Allocator> | |
| 396 auto flat_set<Key, Compare, Allocator>::get_allocator() const | |
| 397 -> allocator_type { | |
| 398 return impl_.body_.get_allocator(); | |
| 399 } | |
| 400 | |
| 401 template <class Key, class Compare, class Allocator> | |
| 402 void flat_set<Key, Compare, Allocator>::reserve(size_type size) { | |
| 403 impl_.body_.reserve(size); | |
| 404 } | |
| 405 | |
| 406 template <class Key, class Compare, class Allocator> | |
| 407 auto flat_set<Key, Compare, Allocator>::capacity() const -> size_type { | |
| 408 return impl_.body_.capacity(); | |
| 409 } | |
| 410 | |
| 411 template <class Key, class Compare, class Allocator> | |
| 412 void flat_set<Key, Compare, Allocator>::shrink_to_fit() { | |
| 413 impl_.body_.shrink_to_fit(); | |
| 414 } | |
| 415 | |
| 416 // ---------------------------------------------------------------------------- | |
| 417 // Size management. | |
| 418 | |
| 419 template <class Key, class Compare, class Allocator> | |
| 420 void flat_set<Key, Compare, Allocator>::clear() { | |
| 421 impl_.body_.clear(); | |
| 422 } | |
| 423 | |
| 424 template <class Key, class Compare, class Allocator> | |
| 425 auto flat_set<Key, Compare, Allocator>::size() const -> size_type { | |
| 426 return impl_.body_.size(); | |
| 427 } | |
| 428 | |
| 429 template <class Key, class Compare, class Allocator> | |
| 430 auto flat_set<Key, Compare, Allocator>::max_size() const -> size_type { | |
| 431 return impl_.body_.max_size(); | |
| 432 } | |
| 433 | |
| 434 template <class Key, class Compare, class Allocator> | |
| 435 bool flat_set<Key, Compare, Allocator>::empty() const { | |
| 436 return impl_.body_.empty(); | |
| 437 } | |
| 438 | |
| 439 // ---------------------------------------------------------------------------- | |
| 440 // Iterators. | |
| 441 | |
| 442 template <class Key, class Compare, class Allocator> | |
| 443 auto flat_set<Key, Compare, Allocator>::begin() -> iterator { | |
| 444 return impl_.body_.begin(); | |
| 445 } | |
| 446 | |
| 447 template <class Key, class Compare, class Allocator> | |
| 448 auto flat_set<Key, Compare, Allocator>::begin() const -> const_iterator { | |
| 449 return impl_.body_.begin(); | |
| 450 } | |
| 451 | |
| 452 template <class Key, class Compare, class Allocator> | |
| 453 auto flat_set<Key, Compare, Allocator>::cbegin() const -> const_iterator { | |
| 454 return impl_.body_.cbegin(); | |
| 455 } | |
| 456 | |
| 457 template <class Key, class Compare, class Allocator> | |
| 458 auto flat_set<Key, Compare, Allocator>::end() -> iterator { | |
| 459 return impl_.body_.end(); | |
| 460 } | |
| 461 | |
| 462 template <class Key, class Compare, class Allocator> | |
| 463 auto flat_set<Key, Compare, Allocator>::end() const -> const_iterator { | |
| 464 return impl_.body_.end(); | |
| 465 } | |
| 466 | |
| 467 template <class Key, class Compare, class Allocator> | |
| 468 auto flat_set<Key, Compare, Allocator>::cend() const -> const_iterator { | |
| 469 return impl_.body_.cend(); | |
| 470 } | |
| 471 | |
| 472 template <class Key, class Compare, class Allocator> | |
| 473 auto flat_set<Key, Compare, Allocator>::rbegin() -> reverse_iterator { | |
| 474 return impl_.body_.rbegin(); | |
| 475 } | |
| 476 | |
| 477 template <class Key, class Compare, class Allocator> | |
| 478 auto flat_set<Key, Compare, Allocator>::rbegin() const | |
| 479 -> const_reverse_iterator { | |
| 480 return impl_.body_.rbegin(); | |
| 481 } | |
| 482 | |
| 483 template <class Key, class Compare, class Allocator> | |
| 484 auto flat_set<Key, Compare, Allocator>::crbegin() const | |
| 485 -> const_reverse_iterator { | |
| 486 return impl_.body_.crbegin(); | |
| 487 } | |
| 488 | |
| 489 template <class Key, class Compare, class Allocator> | |
| 490 auto flat_set<Key, Compare, Allocator>::rend() -> reverse_iterator { | |
| 491 return impl_.body_.rend(); | |
| 492 } | |
| 493 | |
| 494 template <class Key, class Compare, class Allocator> | |
| 495 auto flat_set<Key, Compare, Allocator>::rend() const -> const_reverse_iterator { | |
| 496 return impl_.body_.rend(); | |
| 497 } | |
| 498 | |
| 499 template <class Key, class Compare, class Allocator> | |
| 500 auto flat_set<Key, Compare, Allocator>::crend() const | |
| 501 -> const_reverse_iterator { | |
| 502 return impl_.body_.crend(); | |
| 503 } | |
| 504 | |
| 505 // ---------------------------------------------------------------------------- | |
| 506 // Insert operations. | |
| 507 // | |
| 508 // Currently we use hint the same way as eastl or boost: | |
| 509 // https://github.com/electronicarts/EASTL/blob/master/include/EASTL/vector_set. h#L493 | |
| 510 // | |
| 511 // We duplicate code between copy and move version so that we can avoid | |
| 512 // creating a temporary value. | |
| 513 | |
| 514 template <class Key, class Compare, class Allocator> | |
| 515 auto flat_set<Key, Compare, Allocator>::insert(const value_type& x) | |
| 516 -> std::pair<iterator, bool> { | |
| 517 auto position = lower_bound(x); | |
| 518 | |
| 519 if (position == end() || value_comp()(x, *position)) | |
| 520 return {impl_.body_.insert(position, x), true}; | |
| 521 | |
| 522 return {position, false}; | |
| 523 } | |
| 524 | |
| 525 template <class Key, class Compare, class Allocator> | |
| 526 auto flat_set<Key, Compare, Allocator>::insert(value_type&& x) | |
| 527 -> std::pair<iterator, bool> { | |
| 528 auto position = lower_bound(x); | |
| 529 | |
| 530 if (position == end() || value_comp()(x, *position)) | |
| 531 return {impl_.body_.insert(position, std::move(x)), true}; | |
| 532 | |
| 533 return {position, false}; | |
| 534 } | |
| 535 | |
| 536 template <class Key, class Compare, class Allocator> | |
| 537 auto flat_set<Key, Compare, Allocator>::insert(const_iterator position, | |
| 538 const value_type& x) | |
| 539 -> iterator { | |
| 540 if (position == end() || value_comp()(x, *position)) { | |
| 541 if (position == begin() || value_comp()(*(position - 1), x)) | |
| 542 // crbug.com/677044 | |
|
vmpstr
2017/01/11 21:01:42
Leave a more verbose comment please.
dyaroshev
2017/01/16 06:58:02
Done.
| |
| 543 return impl_.body_.insert(const_cast_it(position), x); | |
| 544 } | |
| 545 return insert(x).first; | |
| 546 } | |
| 547 | |
| 548 template <class Key, class Compare, class Allocator> | |
| 549 auto flat_set<Key, Compare, Allocator>::insert(const_iterator position, | |
| 550 value_type&& x) -> iterator { | |
| 551 if (position == end() || value_comp()(x, *position)) { | |
| 552 if (position == begin() || value_comp()(*(position - 1), x)) | |
| 553 // crbug.com/677044 | |
|
vmpstr
2017/01/11 21:01:42
Leave a more verbose comment please.
dyaroshev
2017/01/16 06:58:02
Done.
| |
| 554 return impl_.body_.insert(const_cast_it(position), std::move(x)); | |
| 555 } | |
| 556 return insert(std::move(x)).first; | |
| 557 } | |
| 558 | |
| 559 template <class Key, class Compare, class Allocator> | |
| 560 template <class InputIterator> | |
| 561 void flat_set<Key, Compare, Allocator>::insert(InputIterator first, | |
| 562 InputIterator last) { | |
| 563 std::copy(first, last, std::inserter(*this, end())); | |
| 564 } | |
| 565 | |
| 566 template <class Key, class Compare, class Allocator> | |
| 567 void flat_set<Key, Compare, Allocator>::insert( | |
| 568 std::initializer_list<value_type> il) { | |
| 569 insert(il.begin(), il.end()); | |
| 570 } | |
| 571 | |
| 572 template <class Key, class Compare, class Allocator> | |
| 573 template <class... Args> | |
| 574 auto flat_set<Key, Compare, Allocator>::emplace(Args&&... args) | |
| 575 -> std::pair<iterator, bool> { | |
| 576 return insert(value_type(std::forward<Args>(args)...)); | |
| 577 } | |
| 578 | |
| 579 template <class Key, class Compare, class Allocator> | |
| 580 template <class... Args> | |
| 581 auto flat_set<Key, Compare, Allocator>::emplace_hint(const_iterator position, | |
| 582 Args&&... args) | |
| 583 -> iterator { | |
| 584 return insert(position, value_type(std::forward<Args>(args)...)); | |
| 585 } | |
| 586 | |
| 587 // ---------------------------------------------------------------------------- | |
| 588 // Erase operations. | |
| 589 | |
| 590 template <class Key, class Compare, class Allocator> | |
| 591 auto flat_set<Key, Compare, Allocator>::erase(const_iterator position) | |
| 592 -> iterator { | |
| 593 // crbug.com/677044 | |
| 594 return impl_.body_.erase(const_cast_it(position)); | |
| 595 } | |
| 596 | |
| 597 template <class Key, class Compare, class Allocator> | |
| 598 auto flat_set<Key, Compare, Allocator>::erase(const key_type& x) -> size_type { | |
| 599 auto eq_range = equal_range(x); | |
| 600 auto res = std::distance(eq_range.first, eq_range.second); | |
| 601 // crbug.com/677044 | |
| 602 erase(const_cast_it(eq_range.first), const_cast_it(eq_range.second)); | |
| 603 return res; | |
| 604 } | |
| 605 | |
| 606 template <class Key, class Compare, class Allocator> | |
| 607 auto flat_set<Key, Compare, Allocator>::erase(const_iterator first, | |
| 608 const_iterator last) -> iterator { | |
| 609 // crbug.com/677044 | |
| 610 return impl_.body_.erase(const_cast_it(first), const_cast_it(last)); | |
| 611 } | |
| 612 | |
| 613 // ---------------------------------------------------------------------------- | |
| 614 // Comparators. | |
| 615 | |
| 616 template <class Key, class Compare, class Allocator> | |
| 617 auto flat_set<Key, Compare, Allocator>::key_comp() const -> key_compare { | |
| 618 return impl_; | |
| 619 } | |
| 620 | |
| 621 template <class Key, class Compare, class Allocator> | |
| 622 auto flat_set<Key, Compare, Allocator>::value_comp() const -> value_compare { | |
| 623 return impl_; | |
| 624 } | |
| 625 | |
| 626 // ---------------------------------------------------------------------------- | |
| 627 // Binary search operations. | |
| 628 | |
| 629 template <class Key, class Compare, class Allocator> | |
| 630 auto flat_set<Key, Compare, Allocator>::count(const key_type& x) const | |
| 631 -> size_type { | |
| 632 auto eq_range = equal_range(x); | |
| 633 return std::distance(eq_range.first, eq_range.second); | |
| 634 } | |
| 635 | |
| 636 template <class Key, class Compare, class Allocator> | |
| 637 auto flat_set<Key, Compare, Allocator>::find(const key_type& x) -> iterator { | |
| 638 return const_cast_it(as_const().find(x)); | |
| 639 } | |
| 640 | |
| 641 template <class Key, class Compare, class Allocator> | |
| 642 auto flat_set<Key, Compare, Allocator>::find(const key_type& x) const | |
| 643 -> const_iterator { | |
| 644 auto eq_range = equal_range(x); | |
| 645 return (eq_range.first == eq_range.second) ? end() : eq_range.first; | |
| 646 } | |
| 647 | |
| 648 template <class Key, class Compare, class Allocator> | |
| 649 auto flat_set<Key, Compare, Allocator>::equal_range(const key_type& x) | |
| 650 -> std::pair<iterator, iterator> { | |
| 651 auto res = as_const().equal_range(x); | |
| 652 return {const_cast_it(res.first), const_cast_it(res.second)}; | |
| 653 } | |
| 654 | |
| 655 template <class Key, class Compare, class Allocator> | |
| 656 auto flat_set<Key, Compare, Allocator>::equal_range(const key_type& x) const | |
| 657 -> std::pair<const_iterator, const_iterator> { | |
| 658 auto lower = lower_bound(x); | |
| 659 | |
| 660 if (lower == end() || key_comp()(x, *lower)) | |
| 661 return {lower, lower}; | |
| 662 | |
| 663 return {lower, std::next(lower)}; | |
| 664 } | |
| 665 | |
| 666 template <class Key, class Compare, class Allocator> | |
| 667 auto flat_set<Key, Compare, Allocator>::lower_bound(const key_type& x) | |
| 668 -> iterator { | |
| 669 return const_cast_it(as_const().lower_bound(x)); | |
| 670 } | |
| 671 | |
| 672 template <class Key, class Compare, class Allocator> | |
| 673 auto flat_set<Key, Compare, Allocator>::lower_bound(const key_type& x) const | |
| 674 -> const_iterator { | |
| 675 return std::lower_bound(begin(), end(), x, key_comp()); | |
| 676 } | |
| 677 | |
| 678 template <class Key, class Compare, class Allocator> | |
| 679 auto flat_set<Key, Compare, Allocator>::upper_bound(const key_type& x) | |
| 680 -> iterator { | |
| 681 return const_cast_it(as_const().upper_bound(x)); | |
| 682 } | |
| 683 | |
| 684 template <class Key, class Compare, class Allocator> | |
| 685 auto flat_set<Key, Compare, Allocator>::upper_bound(const key_type& x) const | |
| 686 -> const_iterator { | |
| 687 return std::upper_bound(begin(), end(), x, key_comp()); | |
| 688 } | |
| 689 | |
| 690 // ---------------------------------------------------------------------------- | |
| 691 // General operations. | |
| 692 | |
| 693 template <class Key, class Compare, class Allocator> | |
| 694 void flat_set<Key, Compare, Allocator>::swap(flat_set& rhs) { | |
| 695 std::swap(impl_, rhs.impl_); | |
| 696 } | |
| 697 | |
| 698 } // namespace base | |
| 699 | |
| 700 #endif // BASE_CONTAINERS_FLAT_SET_H_ | |
| OLD | NEW |