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