| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // ----------------------------------------------------------------------------- |
| 16 | // File: btree_map.h |
| 17 | // ----------------------------------------------------------------------------- |
| 18 | // |
| 19 | // This header file defines B-tree maps: sorted associative containers mapping |
| 20 | // keys to values. |
| 21 | // |
| 22 | // * `absl::btree_map<>` |
| 23 | // * `absl::btree_multimap<>` |
| 24 | // |
| 25 | // These B-tree types are similar to the corresponding types in the STL |
| 26 | // (`std::map` and `std::multimap`) and generally conform to the STL interfaces |
| 27 | // of those types. However, because they are implemented using B-trees, they |
| 28 | // are more efficient in most situations. |
| 29 | // |
| 30 | // Unlike `std::map` and `std::multimap`, which are commonly implemented using |
| 31 | // red-black tree nodes, B-tree maps use more generic B-tree nodes able to hold |
| 32 | // multiple values per node. Holding multiple values per node often makes |
| 33 | // B-tree maps perform better than their `std::map` counterparts, because |
| 34 | // multiple entries can be checked within the same cache hit. |
| 35 | // |
| 36 | // However, these types should not be considered drop-in replacements for |
| 37 | // `std::map` and `std::multimap` as there are some API differences, which are |
| 38 | // noted in this header file. The most consequential differences with respect to |
| 39 | // migrating to b-tree from the STL types are listed in the next paragraph. |
| 40 | // Other API differences are minor. |
| 41 | // |
| 42 | // Importantly, insertions and deletions may invalidate outstanding iterators, |
| 43 | // pointers, and references to elements. Such invalidations are typically only |
| 44 | // an issue if insertion and deletion operations are interleaved with the use of |
| 45 | // more than one iterator, pointer, or reference simultaneously. For this |
| 46 | // reason, `insert()`, `erase()`, and `extract_and_get_next()` return a valid |
| 47 | // iterator at the current position. Another important difference is that |
| 48 | // key-types must be copy-constructible. |
| 49 | // |
| 50 | // Another API difference is that btree iterators can be subtracted, and this |
| 51 | // is faster than using std::distance. |
| 52 | // |
| 53 | // B-tree maps are not exception-safe. |
| 54 |
| 55 | #ifndef ABSL_CONTAINER_BTREE_MAP_H_ |
| 56 | #define ABSL_CONTAINER_BTREE_MAP_H_ |
| 57 |
| 58 | #include "absl/base/attributes.h" |
| 59 | #include "absl/container/internal/btree.h" // IWYU pragma: export |
| 60 | #include "absl/container/internal/btree_container.h" // IWYU pragma: export |
| 61 |
| 62 | namespace absl { |
| 63 | ABSL_NAMESPACE_BEGIN |
| 64 |
| 65 | namespace container_internal { |
| 66 |
| 67 | template <typename Key, typename Data, typename Compare, typename Alloc, |
| 68 | int TargetNodeSize, bool IsMulti> |
| 69 | struct map_params; |
| 70 |
| 71 | } // namespace container_internal |
| 72 |
| 73 | // absl::btree_map<> |
| 74 | // |
| 75 | // An `absl::btree_map<K, V>` is an ordered associative container of |
| 76 | // unique keys and associated values designed to be a more efficient replacement |
| 77 | // for `std::map` (in most cases). |
| 78 | // |
| 79 | // Keys are sorted using an (optional) comparison function, which defaults to |
| 80 | // `std::less<K>`. |
| 81 | // |
| 82 | // An `absl::btree_map<K, V>` uses a default allocator of |
| 83 | // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate) |
| 84 | // nodes, and construct and destruct values within those nodes. You may |
| 85 | // instead specify a custom allocator `A` (which in turn requires specifying a |
| 86 | // custom comparator `C`) as in `absl::btree_map<K, V, C, A>`. |
| 87 | // |
| 88 | template <typename Key, typename Value, typename Compare = std::less<Key>, |
| 89 | typename Alloc = std::allocator<std::pair<const Key, Value>>> |
| 90 | class ABSL_ATTRIBUTE_OWNER btree_map |
| 91 | : public container_internal::btree_map_container< |
| 92 | container_internal::btree<container_internal::map_params< |
| 93 | Key, Value, Compare, Alloc, /*TargetNodeSize=*/256, |
| 94 | /*IsMulti=*/false>>> { |
| 95 | using Base = typename btree_map::btree_map_container; |
| 96 |
| 97 | public: |
| 98 | // Constructors and Assignment Operators |
| 99 | // |
| 100 | // A `btree_map` supports the same overload set as `std::map` |
| 101 | // for construction and assignment: |
| 102 | // |
| 103 | // * Default constructor |
| 104 | // |
| 105 | // absl::btree_map<int, std::string> map1; |
| 106 | // |
| 107 | // * Initializer List constructor |
| 108 | // |
| 109 | // absl::btree_map<int, std::string> map2 = |
| 110 | // {{1, "huey"}, {2, "dewey"}, {3, "louie"},}; |
| 111 | // |
| 112 | // * Copy constructor |
| 113 | // |
| 114 | // absl::btree_map<int, std::string> map3(map2); |
| 115 | // |
| 116 | // * Copy assignment operator |
| 117 | // |
| 118 | // absl::btree_map<int, std::string> map4; |
| 119 | // map4 = map3; |
| 120 | // |
| 121 | // * Move constructor |
| 122 | // |
| 123 | // // Move is guaranteed efficient |
| 124 | // absl::btree_map<int, std::string> map5(std::move(map4)); |
| 125 | // |
| 126 | // * Move assignment operator |
| 127 | // |
| 128 | // // May be efficient if allocators are compatible |
| 129 | // absl::btree_map<int, std::string> map6; |
| 130 | // map6 = std::move(map5); |
| 131 | // |
| 132 | // * Range constructor |
| 133 | // |
| 134 | // std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}}; |
| 135 | // absl::btree_map<int, std::string> map7(v.begin(), v.end()); |
| 136 | btree_map() {} |
| 137 | using Base::Base; |
| 138 |
| 139 | // btree_map::begin() |
| 140 | // |
| 141 | // Returns an iterator to the beginning of the `btree_map`. |
| 142 | using Base::begin; |
| 143 |
| 144 | // btree_map::cbegin() |
| 145 | // |
| 146 | // Returns a const iterator to the beginning of the `btree_map`. |
| 147 | using Base::cbegin; |
| 148 |
| 149 | // btree_map::end() |
| 150 | // |
| 151 | // Returns an iterator to the end of the `btree_map`. |
| 152 | using Base::end; |
| 153 |
| 154 | // btree_map::cend() |
| 155 | // |
| 156 | // Returns a const iterator to the end of the `btree_map`. |
| 157 | using Base::cend; |
| 158 |
| 159 | // btree_map::empty() |
| 160 | // |
| 161 | // Returns whether or not the `btree_map` is empty. |
| 162 | using Base::empty; |
| 163 |
| 164 | // btree_map::max_size() |
| 165 | // |
| 166 | // Returns the largest theoretical possible number of elements within a |
| 167 | // `btree_map` under current memory constraints. This value can be thought |
| 168 | // of as the largest value of `std::distance(begin(), end())` for a |
| 169 | // `btree_map<Key, T>`. |
| 170 | using Base::max_size; |
| 171 |
| 172 | // btree_map::size() |
| 173 | // |
| 174 | // Returns the number of elements currently within the `btree_map`. |
| 175 | using Base::size; |
| 176 |
| 177 | // btree_map::clear() |
| 178 | // |
| 179 | // Removes all elements from the `btree_map`. Invalidates any references, |
| 180 | // pointers, or iterators referring to contained elements. |
| 181 | using Base::clear; |
| 182 |
| 183 | // btree_map::erase() |
| 184 | // |
| 185 | // Erases elements within the `btree_map`. If an erase occurs, any references, |
| 186 | // pointers, or iterators are invalidated. |
| 187 | // Overloads are listed below. |
| 188 | // |
| 189 | // iterator erase(iterator position): |
| 190 | // iterator erase(const_iterator position): |
| 191 | // |
| 192 | // Erases the element at `position` of the `btree_map`, returning |
| 193 | // the iterator pointing to the element after the one that was erased |
| 194 | // (or end() if none exists). |
| 195 | // |
| 196 | // iterator erase(const_iterator first, const_iterator last): |
| 197 | // |
| 198 | // Erases the elements in the open interval [`first`, `last`), returning |
| 199 | // the iterator pointing to the element after the interval that was erased |
| 200 | // (or end() if none exists). |
| 201 | // |
| 202 | // template <typename K> size_type erase(const K& key): |
| 203 | // |
| 204 | // Erases the element with the matching key, if it exists, returning the |
| 205 | // number of elements erased (0 or 1). |
| 206 | using Base::erase; |
| 207 |
| 208 | // btree_map::insert() |
| 209 | // |
| 210 | // Inserts an element of the specified value into the `btree_map`, |
| 211 | // returning an iterator pointing to the newly inserted element, provided that |
| 212 | // an element with the given key does not already exist. If an insertion |
| 213 | // occurs, any references, pointers, or iterators are invalidated. |
| 214 | // Overloads are listed below. |
| 215 | // |
| 216 | // std::pair<iterator,bool> insert(const value_type& value): |
| 217 | // |
| 218 | // Inserts a value into the `btree_map`. Returns a pair consisting of an |
| 219 | // iterator to the inserted element (or to the element that prevented the |
| 220 | // insertion) and a bool denoting whether the insertion took place. |
| 221 | // |
| 222 | // std::pair<iterator,bool> insert(value_type&& value): |
| 223 | // |
| 224 | // Inserts a moveable value into the `btree_map`. Returns a pair |
| 225 | // consisting of an iterator to the inserted element (or to the element that |
| 226 | // prevented the insertion) and a bool denoting whether the insertion took |
| 227 | // place. |
| 228 | // |
| 229 | // iterator insert(const_iterator hint, const value_type& value): |
| 230 | // iterator insert(const_iterator hint, value_type&& value): |
| 231 | // |
| 232 | // Inserts a value, using the position of `hint` as a non-binding suggestion |
| 233 | // for where to begin the insertion search. Returns an iterator to the |
| 234 | // inserted element, or to the existing element that prevented the |
| 235 | // insertion. |
| 236 | // |
| 237 | // void insert(InputIterator first, InputIterator last): |
| 238 | // |
| 239 | // Inserts a range of values [`first`, `last`). |
| 240 | // |
| 241 | // void insert(std::initializer_list<init_type> ilist): |
| 242 | // |
| 243 | // Inserts the elements within the initializer list `ilist`. |
| 244 | using Base::insert; |
| 245 |
| 246 | // btree_map::insert_or_assign() |
| 247 | // |
| 248 | // Inserts an element of the specified value into the `btree_map` provided |
| 249 | // that a value with the given key does not already exist, or replaces the |
| 250 | // corresponding mapped type with the forwarded `obj` argument if a key for |
| 251 | // that value already exists, returning an iterator pointing to the newly |
| 252 | // inserted element. Overloads are listed below. |
| 253 | // |
| 254 | // pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj): |
| 255 | // pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj): |
| 256 | // |
| 257 | // Inserts/Assigns (or moves) the element of the specified key into the |
| 258 | // `btree_map`. If the returned bool is true, insertion took place, and if |
| 259 | // it's false, assignment took place. |
| 260 | // |
| 261 | // iterator insert_or_assign(const_iterator hint, |
| 262 | // const key_type& k, M&& obj): |
| 263 | // iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj): |
| 264 | // |
| 265 | // Inserts/Assigns (or moves) the element of the specified key into the |
| 266 | // `btree_map` using the position of `hint` as a non-binding suggestion |
| 267 | // for where to begin the insertion search. |
| 268 | using Base::insert_or_assign; |
| 269 |
| 270 | // btree_map::emplace() |
| 271 | // |
| 272 | // Inserts an element of the specified value by constructing it in-place |
| 273 | // within the `btree_map`, provided that no element with the given key |
| 274 | // already exists. |
| 275 | // |
| 276 | // The element may be constructed even if there already is an element with the |
| 277 | // key in the container, in which case the newly constructed element will be |
| 278 | // destroyed immediately. Prefer `try_emplace()` unless your key is not |
| 279 | // copyable or moveable. |
| 280 | // |
| 281 | // If an insertion occurs, any references, pointers, or iterators are |
| 282 | // invalidated. |
| 283 | using Base::emplace; |
| 284 |
| 285 | // btree_map::emplace_hint() |
| 286 | // |
| 287 | // Inserts an element of the specified value by constructing it in-place |
| 288 | // within the `btree_map`, using the position of `hint` as a non-binding |
| 289 | // suggestion for where to begin the insertion search, and only inserts |
| 290 | // provided that no element with the given key already exists. |
| 291 | // |
| 292 | // The element may be constructed even if there already is an element with the |
| 293 | // key in the container, in which case the newly constructed element will be |
| 294 | // destroyed immediately. Prefer `try_emplace()` unless your key is not |
| 295 | // copyable or moveable. |
| 296 | // |
| 297 | // If an insertion occurs, any references, pointers, or iterators are |
| 298 | // invalidated. |
| 299 | using Base::emplace_hint; |
| 300 |
| 301 | // btree_map::try_emplace() |
| 302 | // |
| 303 | // Inserts an element of the specified value by constructing it in-place |
| 304 | // within the `btree_map`, provided that no element with the given key |
| 305 | // already exists. Unlike `emplace()`, if an element with the given key |
| 306 | // already exists, we guarantee that no element is constructed. |
| 307 | // |
| 308 | // If an insertion occurs, any references, pointers, or iterators are |
| 309 | // invalidated. |
| 310 | // |
| 311 | // Overloads are listed below. |
| 312 | // |
| 313 | // std::pair<iterator, bool> try_emplace(const key_type& k, Args&&... args): |
| 314 | // std::pair<iterator, bool> try_emplace(key_type&& k, Args&&... args): |
| 315 | // |
| 316 | // Inserts (via copy or move) the element of the specified key into the |
| 317 | // `btree_map`. |
| 318 | // |
| 319 | // iterator try_emplace(const_iterator hint, |
| 320 | // const key_type& k, Args&&... args): |
| 321 | // iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args): |
| 322 | // |
| 323 | // Inserts (via copy or move) the element of the specified key into the |
| 324 | // `btree_map` using the position of `hint` as a non-binding suggestion |
| 325 | // for where to begin the insertion search. |
| 326 | using Base::try_emplace; |
| 327 |
| 328 | // btree_map::extract() |
| 329 | // |
| 330 | // Extracts the indicated element, erasing it in the process, and returns it |
| 331 | // as a C++17-compatible node handle. Any references, pointers, or iterators |
| 332 | // are invalidated. Overloads are listed below. |
| 333 | // |
| 334 | // node_type extract(const_iterator position): |
| 335 | // |
| 336 | // Extracts the element at the indicated position and returns a node handle |
| 337 | // owning that extracted data. |
| 338 | // |
| 339 | // template <typename K> node_type extract(const K& k): |
| 340 | // |
| 341 | // Extracts the element with the key matching the passed key value and |
| 342 | // returns a node handle owning that extracted data. If the `btree_map` |
| 343 | // does not contain an element with a matching key, this function returns an |
| 344 | // empty node handle. |
| 345 | // |
| 346 | // NOTE: when compiled in an earlier version of C++ than C++17, |
| 347 | // `node_type::key()` returns a const reference to the key instead of a |
| 348 | // mutable reference. We cannot safely return a mutable reference without |
| 349 | // std::launder (which is not available before C++17). |
| 350 | // |
| 351 | // NOTE: In this context, `node_type` refers to the C++17 concept of a |
| 352 | // move-only type that owns and provides access to the elements in associative |
| 353 | // containers (https://en.cppreference.com/w/cpp/container/node_handle). |
| 354 | // It does NOT refer to the data layout of the underlying btree. |
| 355 | using Base::extract; |
| 356 |
| 357 | // btree_map::extract_and_get_next() |
| 358 | // |
| 359 | // Extracts the indicated element, erasing it in the process, and returns it |
| 360 | // as a C++17-compatible node handle along with an iterator to the next |
| 361 | // element. |
| 362 | // |
| 363 | // extract_and_get_next_return_type extract_and_get_next( |
| 364 | // const_iterator position): |
| 365 | // |
| 366 | // Extracts the element at the indicated position, returns a struct |
| 367 | // containing a member named `node`: a node handle owning that extracted |
| 368 | // data and a member named `next`: an iterator pointing to the next element |
| 369 | // in the btree. |
| 370 | using Base::extract_and_get_next; |
| 371 |
| 372 | // btree_map::merge() |
| 373 | // |
| 374 | // Extracts elements from a given `source` btree_map into this |
| 375 | // `btree_map`. If the destination `btree_map` already contains an |
| 376 | // element with an equivalent key, that element is not extracted. |
| 377 | using Base::merge; |
| 378 |
| 379 | // btree_map::swap(btree_map& other) |
| 380 | // |
| 381 | // Exchanges the contents of this `btree_map` with those of the `other` |
| 382 | // btree_map, avoiding invocation of any move, copy, or swap operations on |
| 383 | // individual elements. |
| 384 | // |
| 385 | // All iterators and references on the `btree_map` remain valid, excepting |
| 386 | // for the past-the-end iterator, which is invalidated. |
| 387 | using Base::swap; |
| 388 |
| 389 | // btree_map::at() |
| 390 | // |
| 391 | // Returns a reference to the mapped value of the element with key equivalent |
| 392 | // to the passed key. |
| 393 | using Base::at; |
| 394 |
| 395 | // btree_map::contains() |
| 396 | // |
| 397 | // template <typename K> bool contains(const K& key) const: |
| 398 | // |
| 399 | // Determines whether an element comparing equal to the given `key` exists |
| 400 | // within the `btree_map`, returning `true` if so or `false` otherwise. |
| 401 | // |
| 402 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 403 | // heterogeneous comparator. |
| 404 | using Base::contains; |
| 405 |
| 406 | // btree_map::count() |
| 407 | // |
| 408 | // template <typename K> size_type count(const K& key) const: |
| 409 | // |
| 410 | // Returns the number of elements comparing equal to the given `key` within |
| 411 | // the `btree_map`. Note that this function will return either `1` or `0` |
| 412 | // since duplicate elements are not allowed within a `btree_map`. |
| 413 | // |
| 414 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 415 | // heterogeneous comparator. |
| 416 | using Base::count; |
| 417 |
| 418 | // btree_map::equal_range() |
| 419 | // |
| 420 | // Returns a half-open range [first, last), defined by a `std::pair` of two |
| 421 | // iterators, containing all elements with the passed key in the `btree_map`. |
| 422 | using Base::equal_range; |
| 423 |
| 424 | // btree_map::find() |
| 425 | // |
| 426 | // template <typename K> iterator find(const K& key): |
| 427 | // template <typename K> const_iterator find(const K& key) const: |
| 428 | // |
| 429 | // Finds an element with the passed `key` within the `btree_map`. |
| 430 | // |
| 431 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 432 | // heterogeneous comparator. |
| 433 | using Base::find; |
| 434 |
| 435 | // btree_map::lower_bound() |
| 436 | // |
| 437 | // template <typename K> iterator lower_bound(const K& key): |
| 438 | // template <typename K> const_iterator lower_bound(const K& key) const: |
| 439 | // |
| 440 | // Finds the first element with a key that is not less than `key` within the |
| 441 | // `btree_map`. |
| 442 | // |
| 443 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 444 | // heterogeneous comparator. |
| 445 | using Base::lower_bound; |
| 446 |
| 447 | // btree_map::upper_bound() |
| 448 | // |
| 449 | // template <typename K> iterator upper_bound(const K& key): |
| 450 | // template <typename K> const_iterator upper_bound(const K& key) const: |
| 451 | // |
| 452 | // Finds the first element with a key that is greater than `key` within the |
| 453 | // `btree_map`. |
| 454 | // |
| 455 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 456 | // heterogeneous comparator. |
| 457 | using Base::upper_bound; |
| 458 |
| 459 | // btree_map::operator[]() |
| 460 | // |
| 461 | // Returns a reference to the value mapped to the passed key within the |
| 462 | // `btree_map`, performing an `insert()` if the key does not already |
| 463 | // exist. |
| 464 | // |
| 465 | // If an insertion occurs, any references, pointers, or iterators are |
| 466 | // invalidated. Otherwise iterators are not affected and references are not |
| 467 | // invalidated. Overloads are listed below. |
| 468 | // |
| 469 | // T& operator[](key_type&& key): |
| 470 | // T& operator[](const key_type& key): |
| 471 | // |
| 472 | // Inserts a value_type object constructed in-place if the element with the |
| 473 | // given key does not exist. |
| 474 | using Base::operator[]; |
| 475 |
| 476 | // btree_map::get_allocator() |
| 477 | // |
| 478 | // Returns the allocator function associated with this `btree_map`. |
| 479 | using Base::get_allocator; |
| 480 |
| 481 | // btree_map::key_comp(); |
| 482 | // |
| 483 | // Returns the key comparator associated with this `btree_map`. |
| 484 | using Base::key_comp; |
| 485 |
| 486 | // btree_map::value_comp(); |
| 487 | // |
| 488 | // Returns the value comparator associated with this `btree_map`. |
| 489 | using Base::value_comp; |
| 490 | }; |
| 491 |
| 492 | // absl::swap(absl::btree_map<>, absl::btree_map<>) |
| 493 | // |
| 494 | // Swaps the contents of two `absl::btree_map` containers. |
| 495 | template <typename K, typename V, typename C, typename A> |
| 496 | void swap(btree_map<K, V, C, A> &x, btree_map<K, V, C, A> &y) { |
| 497 | return x.swap(y); |
| 498 | } |
| 499 |
| 500 | // absl::erase_if(absl::btree_map<>, Pred) |
| 501 | // |
| 502 | // Erases all elements that satisfy the predicate pred from the container. |
| 503 | // Returns the number of erased elements. |
| 504 | template <typename K, typename V, typename C, typename A, typename Pred> |
| 505 | typename btree_map<K, V, C, A>::size_type erase_if( |
| 506 | btree_map<K, V, C, A> &map, Pred pred) { |
| 507 | return container_internal::btree_access::erase_if(map, std::move(pred)); |
| 508 | } |
| 509 |
| 510 | // absl::btree_multimap |
| 511 | // |
| 512 | // An `absl::btree_multimap<K, V>` is an ordered associative container of |
| 513 | // keys and associated values designed to be a more efficient replacement for |
| 514 | // `std::multimap` (in most cases). Unlike `absl::btree_map`, a B-tree multimap |
| 515 | // allows multiple elements with equivalent keys. |
| 516 | // |
| 517 | // Keys are sorted using an (optional) comparison function, which defaults to |
| 518 | // `std::less<K>`. |
| 519 | // |
| 520 | // An `absl::btree_multimap<K, V>` uses a default allocator of |
| 521 | // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate) |
| 522 | // nodes, and construct and destruct values within those nodes. You may |
| 523 | // instead specify a custom allocator `A` (which in turn requires specifying a |
| 524 | // custom comparator `C`) as in `absl::btree_multimap<K, V, C, A>`. |
| 525 | // |
| 526 | template <typename Key, typename Value, typename Compare = std::less<Key>, |
| 527 | typename Alloc = std::allocator<std::pair<const Key, Value>>> |
| 528 | class ABSL_ATTRIBUTE_OWNER btree_multimap |
| 529 | : public container_internal::btree_multimap_container< |
| 530 | container_internal::btree<container_internal::map_params< |
| 531 | Key, Value, Compare, Alloc, /*TargetNodeSize=*/256, |
| 532 | /*IsMulti=*/true>>> { |
| 533 | using Base = typename btree_multimap::btree_multimap_container; |
| 534 |
| 535 | public: |
| 536 | // Constructors and Assignment Operators |
| 537 | // |
| 538 | // A `btree_multimap` supports the same overload set as `std::multimap` |
| 539 | // for construction and assignment: |
| 540 | // |
| 541 | // * Default constructor |
| 542 | // |
| 543 | // absl::btree_multimap<int, std::string> map1; |
| 544 | // |
| 545 | // * Initializer List constructor |
| 546 | // |
| 547 | // absl::btree_multimap<int, std::string> map2 = |
| 548 | // {{1, "huey"}, {2, "dewey"}, {3, "louie"},}; |
| 549 | // |
| 550 | // * Copy constructor |
| 551 | // |
| 552 | // absl::btree_multimap<int, std::string> map3(map2); |
| 553 | // |
| 554 | // * Copy assignment operator |
| 555 | // |
| 556 | // absl::btree_multimap<int, std::string> map4; |
| 557 | // map4 = map3; |
| 558 | // |
| 559 | // * Move constructor |
| 560 | // |
| 561 | // // Move is guaranteed efficient |
| 562 | // absl::btree_multimap<int, std::string> map5(std::move(map4)); |
| 563 | // |
| 564 | // * Move assignment operator |
| 565 | // |
| 566 | // // May be efficient if allocators are compatible |
| 567 | // absl::btree_multimap<int, std::string> map6; |
| 568 | // map6 = std::move(map5); |
| 569 | // |
| 570 | // * Range constructor |
| 571 | // |
| 572 | // std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}}; |
| 573 | // absl::btree_multimap<int, std::string> map7(v.begin(), v.end()); |
| 574 | btree_multimap() {} |
| 575 | using Base::Base; |
| 576 |
| 577 | // btree_multimap::begin() |
| 578 | // |
| 579 | // Returns an iterator to the beginning of the `btree_multimap`. |
| 580 | using Base::begin; |
| 581 |
| 582 | // btree_multimap::cbegin() |
| 583 | // |
| 584 | // Returns a const iterator to the beginning of the `btree_multimap`. |
| 585 | using Base::cbegin; |
| 586 |
| 587 | // btree_multimap::end() |
| 588 | // |
| 589 | // Returns an iterator to the end of the `btree_multimap`. |
| 590 | using Base::end; |
| 591 |
| 592 | // btree_multimap::cend() |
| 593 | // |
| 594 | // Returns a const iterator to the end of the `btree_multimap`. |
| 595 | using Base::cend; |
| 596 |
| 597 | // btree_multimap::empty() |
| 598 | // |
| 599 | // Returns whether or not the `btree_multimap` is empty. |
| 600 | using Base::empty; |
| 601 |
| 602 | // btree_multimap::max_size() |
| 603 | // |
| 604 | // Returns the largest theoretical possible number of elements within a |
| 605 | // `btree_multimap` under current memory constraints. This value can be |
| 606 | // thought of as the largest value of `std::distance(begin(), end())` for a |
| 607 | // `btree_multimap<Key, T>`. |
| 608 | using Base::max_size; |
| 609 |
| 610 | // btree_multimap::size() |
| 611 | // |
| 612 | // Returns the number of elements currently within the `btree_multimap`. |
| 613 | using Base::size; |
| 614 |
| 615 | // btree_multimap::clear() |
| 616 | // |
| 617 | // Removes all elements from the `btree_multimap`. Invalidates any references, |
| 618 | // pointers, or iterators referring to contained elements. |
| 619 | using Base::clear; |
| 620 |
| 621 | // btree_multimap::erase() |
| 622 | // |
| 623 | // Erases elements within the `btree_multimap`. If an erase occurs, any |
| 624 | // references, pointers, or iterators are invalidated. |
| 625 | // Overloads are listed below. |
| 626 | // |
| 627 | // iterator erase(iterator position): |
| 628 | // iterator erase(const_iterator position): |
| 629 | // |
| 630 | // Erases the element at `position` of the `btree_multimap`, returning |
| 631 | // the iterator pointing to the element after the one that was erased |
| 632 | // (or end() if none exists). |
| 633 | // |
| 634 | // iterator erase(const_iterator first, const_iterator last): |
| 635 | // |
| 636 | // Erases the elements in the open interval [`first`, `last`), returning |
| 637 | // the iterator pointing to the element after the interval that was erased |
| 638 | // (or end() if none exists). |
| 639 | // |
| 640 | // template <typename K> size_type erase(const K& key): |
| 641 | // |
| 642 | // Erases the elements matching the key, if any exist, returning the |
| 643 | // number of elements erased. |
| 644 | using Base::erase; |
| 645 |
| 646 | // btree_multimap::insert() |
| 647 | // |
| 648 | // Inserts an element of the specified value into the `btree_multimap`, |
| 649 | // returning an iterator pointing to the newly inserted element. |
| 650 | // Any references, pointers, or iterators are invalidated. Overloads are |
| 651 | // listed below. |
| 652 | // |
| 653 | // iterator insert(const value_type& value): |
| 654 | // |
| 655 | // Inserts a value into the `btree_multimap`, returning an iterator to the |
| 656 | // inserted element. |
| 657 | // |
| 658 | // iterator insert(value_type&& value): |
| 659 | // |
| 660 | // Inserts a moveable value into the `btree_multimap`, returning an iterator |
| 661 | // to the inserted element. |
| 662 | // |
| 663 | // iterator insert(const_iterator hint, const value_type& value): |
| 664 | // iterator insert(const_iterator hint, value_type&& value): |
| 665 | // |
| 666 | // Inserts a value, using the position of `hint` as a non-binding suggestion |
| 667 | // for where to begin the insertion search. Returns an iterator to the |
| 668 | // inserted element. |
| 669 | // |
| 670 | // void insert(InputIterator first, InputIterator last): |
| 671 | // |
| 672 | // Inserts a range of values [`first`, `last`). |
| 673 | // |
| 674 | // void insert(std::initializer_list<init_type> ilist): |
| 675 | // |
| 676 | // Inserts the elements within the initializer list `ilist`. |
| 677 | using Base::insert; |
| 678 |
| 679 | // btree_multimap::emplace() |
| 680 | // |
| 681 | // Inserts an element of the specified value by constructing it in-place |
| 682 | // within the `btree_multimap`. Any references, pointers, or iterators are |
| 683 | // invalidated. |
| 684 | using Base::emplace; |
| 685 |
| 686 | // btree_multimap::emplace_hint() |
| 687 | // |
| 688 | // Inserts an element of the specified value by constructing it in-place |
| 689 | // within the `btree_multimap`, using the position of `hint` as a non-binding |
| 690 | // suggestion for where to begin the insertion search. |
| 691 | // |
| 692 | // Any references, pointers, or iterators are invalidated. |
| 693 | using Base::emplace_hint; |
| 694 |
| 695 | // btree_multimap::extract() |
| 696 | // |
| 697 | // Extracts the indicated element, erasing it in the process, and returns it |
| 698 | // as a C++17-compatible node handle. Overloads are listed below. |
| 699 | // |
| 700 | // node_type extract(const_iterator position): |
| 701 | // |
| 702 | // Extracts the element at the indicated position and returns a node handle |
| 703 | // owning that extracted data. |
| 704 | // |
| 705 | // template <typename K> node_type extract(const K& k): |
| 706 | // |
| 707 | // Extracts the element with the key matching the passed key value and |
| 708 | // returns a node handle owning that extracted data. If the `btree_multimap` |
| 709 | // does not contain an element with a matching key, this function returns an |
| 710 | // empty node handle. |
| 711 | // |
| 712 | // NOTE: when compiled in an earlier version of C++ than C++17, |
| 713 | // `node_type::key()` returns a const reference to the key instead of a |
| 714 | // mutable reference. We cannot safely return a mutable reference without |
| 715 | // std::launder (which is not available before C++17). |
| 716 | // |
| 717 | // NOTE: In this context, `node_type` refers to the C++17 concept of a |
| 718 | // move-only type that owns and provides access to the elements in associative |
| 719 | // containers (https://en.cppreference.com/w/cpp/container/node_handle). |
| 720 | // It does NOT refer to the data layout of the underlying btree. |
| 721 | using Base::extract; |
| 722 |
| 723 | // btree_multimap::extract_and_get_next() |
| 724 | // |
| 725 | // Extracts the indicated element, erasing it in the process, and returns it |
| 726 | // as a C++17-compatible node handle along with an iterator to the next |
| 727 | // element. |
| 728 | // |
| 729 | // extract_and_get_next_return_type extract_and_get_next( |
| 730 | // const_iterator position): |
| 731 | // |
| 732 | // Extracts the element at the indicated position, returns a struct |
| 733 | // containing a member named `node`: a node handle owning that extracted |
| 734 | // data and a member named `next`: an iterator pointing to the next element |
| 735 | // in the btree. |
| 736 | using Base::extract_and_get_next; |
| 737 |
| 738 | // btree_multimap::merge() |
| 739 | // |
| 740 | // Extracts all elements from a given `source` btree_multimap into this |
| 741 | // `btree_multimap`. |
| 742 | using Base::merge; |
| 743 |
| 744 | // btree_multimap::swap(btree_multimap& other) |
| 745 | // |
| 746 | // Exchanges the contents of this `btree_multimap` with those of the `other` |
| 747 | // btree_multimap, avoiding invocation of any move, copy, or swap operations |
| 748 | // on individual elements. |
| 749 | // |
| 750 | // All iterators and references on the `btree_multimap` remain valid, |
| 751 | // excepting for the past-the-end iterator, which is invalidated. |
| 752 | using Base::swap; |
| 753 |
| 754 | // btree_multimap::contains() |
| 755 | // |
| 756 | // template <typename K> bool contains(const K& key) const: |
| 757 | // |
| 758 | // Determines whether an element comparing equal to the given `key` exists |
| 759 | // within the `btree_multimap`, returning `true` if so or `false` otherwise. |
| 760 | // |
| 761 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 762 | // heterogeneous comparator. |
| 763 | using Base::contains; |
| 764 |
| 765 | // btree_multimap::count() |
| 766 | // |
| 767 | // template <typename K> size_type count(const K& key) const: |
| 768 | // |
| 769 | // Returns the number of elements comparing equal to the given `key` within |
| 770 | // the `btree_multimap`. |
| 771 | // |
| 772 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 773 | // heterogeneous comparator. |
| 774 | using Base::count; |
| 775 |
| 776 | // btree_multimap::equal_range() |
| 777 | // |
| 778 | // Returns a half-open range [first, last), defined by a `std::pair` of two |
| 779 | // iterators, containing all elements with the passed key in the |
| 780 | // `btree_multimap`. |
| 781 | using Base::equal_range; |
| 782 |
| 783 | // btree_multimap::find() |
| 784 | // |
| 785 | // template <typename K> iterator find(const K& key): |
| 786 | // template <typename K> const_iterator find(const K& key) const: |
| 787 | // |
| 788 | // Finds an element with the passed `key` within the `btree_multimap`. |
| 789 | // |
| 790 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 791 | // heterogeneous comparator. |
| 792 | using Base::find; |
| 793 |
| 794 | // btree_multimap::lower_bound() |
| 795 | // |
| 796 | // template <typename K> iterator lower_bound(const K& key): |
| 797 | // template <typename K> const_iterator lower_bound(const K& key) const: |
| 798 | // |
| 799 | // Finds the first element with a key that is not less than `key` within the |
| 800 | // `btree_multimap`. |
| 801 | // |
| 802 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 803 | // heterogeneous comparator. |
| 804 | using Base::lower_bound; |
| 805 |
| 806 | // btree_multimap::upper_bound() |
| 807 | // |
| 808 | // template <typename K> iterator upper_bound(const K& key): |
| 809 | // template <typename K> const_iterator upper_bound(const K& key) const: |
| 810 | // |
| 811 | // Finds the first element with a key that is greater than `key` within the |
| 812 | // `btree_multimap`. |
| 813 | // |
| 814 | // Supports heterogeneous lookup, provided that the map has a compatible |
| 815 | // heterogeneous comparator. |
| 816 | using Base::upper_bound; |
| 817 |
| 818 | // btree_multimap::get_allocator() |
| 819 | // |
| 820 | // Returns the allocator function associated with this `btree_multimap`. |
| 821 | using Base::get_allocator; |
| 822 |
| 823 | // btree_multimap::key_comp(); |
| 824 | // |
| 825 | // Returns the key comparator associated with this `btree_multimap`. |
| 826 | using Base::key_comp; |
| 827 |
| 828 | // btree_multimap::value_comp(); |
| 829 | // |
| 830 | // Returns the value comparator associated with this `btree_multimap`. |
| 831 | using Base::value_comp; |
| 832 | }; |
| 833 |
| 834 | // absl::swap(absl::btree_multimap<>, absl::btree_multimap<>) |
| 835 | // |
| 836 | // Swaps the contents of two `absl::btree_multimap` containers. |
| 837 | template <typename K, typename V, typename C, typename A> |
| 838 | void swap(btree_multimap<K, V, C, A> &x, btree_multimap<K, V, C, A> &y) { |
| 839 | return x.swap(y); |
| 840 | } |
| 841 |
| 842 | // absl::erase_if(absl::btree_multimap<>, Pred) |
| 843 | // |
| 844 | // Erases all elements that satisfy the predicate pred from the container. |
| 845 | // Returns the number of erased elements. |
| 846 | template <typename K, typename V, typename C, typename A, typename Pred> |
| 847 | typename btree_multimap<K, V, C, A>::size_type erase_if( |
| 848 | btree_multimap<K, V, C, A> &map, Pred pred) { |
| 849 | return container_internal::btree_access::erase_if(map, std::move(pred)); |
| 850 | } |
| 851 |
| 852 | namespace container_internal { |
| 853 |
| 854 | // A parameters structure for holding the type parameters for a btree_map. |
| 855 | // Compare and Alloc should be nothrow copy-constructible. |
| 856 | template <typename Key, typename Data, typename Compare, typename Alloc, |
| 857 | int TargetNodeSize, bool IsMulti> |
| 858 | struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti, |
| 859 | /*IsMap=*/true, map_slot_policy<Key, Data>> { |
| 860 | using super_type = typename map_params::common_params; |
| 861 | using mapped_type = Data; |
| 862 | // This type allows us to move keys when it is safe to do so. It is safe |
| 863 | // for maps in which value_type and mutable_value_type are layout compatible. |
| 864 | using slot_policy = typename super_type::slot_policy; |
| 865 | using slot_type = typename super_type::slot_type; |
| 866 | using value_type = typename super_type::value_type; |
| 867 | using init_type = typename super_type::init_type; |
| 868 |
| 869 | template <typename V> |
| 870 | static auto key(const V &value ABSL_ATTRIBUTE_LIFETIME_BOUND) |
| 871 | -> decltype((value.first)) { |
| 872 | return value.first; |
| 873 | } |
| 874 | static const Key &key(const slot_type *s) { return slot_policy::key(s); } |
| 875 | static const Key &key(slot_type *s) { return slot_policy::key(s); } |
| 876 | // For use in node handle. |
| 877 | static auto mutable_key(slot_type *s) |
| 878 | -> decltype(slot_policy::mutable_key(s)) { |
| 879 | return slot_policy::mutable_key(s); |
| 880 | } |
| 881 | static mapped_type &value(value_type *value) { return value->second; } |
| 882 | }; |
| 883 |
| 884 | } // namespace container_internal |
| 885 |
| 886 | ABSL_NAMESPACE_END |
| 887 | } // namespace absl |
| 888 |
| 889 | #endif // ABSL_CONTAINER_BTREE_MAP_H_ |
| 890 |