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()`, `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 #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::extract_and_get_next()
355 //
356 // Extracts the indicated element, erasing it in the process, and returns it
357 // as a C++17-compatible node handle along with an iterator to the next
358 // element.
359 //
360 // extract_and_get_next_return_type extract_and_get_next(
361 // const_iterator position):
362 //
363 // Extracts the element at the indicated position, returns a struct
364 // containing a member named `node`: a node handle owning that extracted
365 // data and a member named `next`: an iterator pointing to the next element
366 // in the btree.
367 using Base::extract_and_get_next;
368
369 // btree_map::merge()
370 //
371 // Extracts elements from a given `source` btree_map into this
372 // `btree_map`. If the destination `btree_map` already contains an
373 // element with an equivalent key, that element is not extracted.
374 using Base::merge;
375
376 // btree_map::swap(btree_map& other)
377 //
378 // Exchanges the contents of this `btree_map` with those of the `other`
379 // btree_map, avoiding invocation of any move, copy, or swap operations on
380 // individual elements.
381 //
382 // All iterators and references on the `btree_map` remain valid, excepting
383 // for the past-the-end iterator, which is invalidated.
384 using Base::swap;
385
386 // btree_map::at()
387 //
388 // Returns a reference to the mapped value of the element with key equivalent
389 // to the passed key.
390 using Base::at;
391
392 // btree_map::contains()
393 //
394 // template <typename K> bool contains(const K& key) const:
395 //
396 // Determines whether an element comparing equal to the given `key` exists
397 // within the `btree_map`, returning `true` if so or `false` otherwise.
398 //
399 // Supports heterogeneous lookup, provided that the map has a compatible
400 // heterogeneous comparator.
401 using Base::contains;
402
403 // btree_map::count()
404 //
405 // template <typename K> size_type count(const K& key) const:
406 //
407 // Returns the number of elements comparing equal to the given `key` within
408 // the `btree_map`. Note that this function will return either `1` or `0`
409 // since duplicate elements are not allowed within a `btree_map`.
410 //
411 // Supports heterogeneous lookup, provided that the map has a compatible
412 // heterogeneous comparator.
413 using Base::count;
414
415 // btree_map::equal_range()
416 //
417 // Returns a half-open range [first, last), defined by a `std::pair` of two
418 // iterators, containing all elements with the passed key in the `btree_map`.
419 using Base::equal_range;
420
421 // btree_map::find()
422 //
423 // template <typename K> iterator find(const K& key):
424 // template <typename K> const_iterator find(const K& key) const:
425 //
426 // Finds an element with the passed `key` within the `btree_map`.
427 //
428 // Supports heterogeneous lookup, provided that the map has a compatible
429 // heterogeneous comparator.
430 using Base::find;
431
432 // btree_map::lower_bound()
433 //
434 // template <typename K> iterator lower_bound(const K& key):
435 // template <typename K> const_iterator lower_bound(const K& key) const:
436 //
437 // Finds the first element with a key that is not less than `key` within the
438 // `btree_map`.
439 //
440 // Supports heterogeneous lookup, provided that the map has a compatible
441 // heterogeneous comparator.
442 using Base::lower_bound;
443
444 // btree_map::upper_bound()
445 //
446 // template <typename K> iterator upper_bound(const K& key):
447 // template <typename K> const_iterator upper_bound(const K& key) const:
448 //
449 // Finds the first element with a key that is greater than `key` within the
450 // `btree_map`.
451 //
452 // Supports heterogeneous lookup, provided that the map has a compatible
453 // heterogeneous comparator.
454 using Base::upper_bound;
455
456 // btree_map::operator[]()
457 //
458 // Returns a reference to the value mapped to the passed key within the
459 // `btree_map`, performing an `insert()` if the key does not already
460 // exist.
461 //
462 // If an insertion occurs, any references, pointers, or iterators are
463 // invalidated. Otherwise iterators are not affected and references are not
464 // invalidated. Overloads are listed below.
465 //
466 // T& operator[](key_type&& key):
467 // T& operator[](const key_type& key):
468 //
469 // Inserts a value_type object constructed in-place if the element with the
470 // given key does not exist.
471 using Base::operator[];
472
473 // btree_map::get_allocator()
474 //
475 // Returns the allocator function associated with this `btree_map`.
476 using Base::get_allocator;
477
478 // btree_map::key_comp();
479 //
480 // Returns the key comparator associated with this `btree_map`.
481 using Base::key_comp;
482
483 // btree_map::value_comp();
484 //
485 // Returns the value comparator associated with this `btree_map`.
486 using Base::value_comp;
487 };
488
489 // absl::swap(absl::btree_map<>, absl::btree_map<>)
490 //
491 // Swaps the contents of two `absl::btree_map` containers.
492 template <typename K, typename V, typename C, typename A>
swap(btree_map<K,V,C,A> & x,btree_map<K,V,C,A> & y)493 void swap(btree_map<K, V, C, A> &x, btree_map<K, V, C, A> &y) {
494 return x.swap(y);
495 }
496
497 // absl::erase_if(absl::btree_map<>, Pred)
498 //
499 // Erases all elements that satisfy the predicate pred from the container.
500 // Returns the number of erased elements.
501 template <typename K, typename V, typename C, typename A, typename Pred>
erase_if(btree_map<K,V,C,A> & map,Pred pred)502 typename btree_map<K, V, C, A>::size_type erase_if(
503 btree_map<K, V, C, A> &map, Pred pred) {
504 return container_internal::btree_access::erase_if(map, std::move(pred));
505 }
506
507 // absl::btree_multimap
508 //
509 // An `absl::btree_multimap<K, V>` is an ordered associative container of
510 // keys and associated values designed to be a more efficient replacement for
511 // `std::multimap` (in most cases). Unlike `absl::btree_map`, a B-tree multimap
512 // allows multiple elements with equivalent keys.
513 //
514 // Keys are sorted using an (optional) comparison function, which defaults to
515 // `std::less<K>`.
516 //
517 // An `absl::btree_multimap<K, V>` uses a default allocator of
518 // `std::allocator<std::pair<const K, V>>` to allocate (and deallocate)
519 // nodes, and construct and destruct values within those nodes. You may
520 // instead specify a custom allocator `A` (which in turn requires specifying a
521 // custom comparator `C`) as in `absl::btree_multimap<K, V, C, A>`.
522 //
523 template <typename Key, typename Value, typename Compare = std::less<Key>,
524 typename Alloc = std::allocator<std::pair<const Key, Value>>>
525 class btree_multimap
526 : public container_internal::btree_multimap_container<
527 container_internal::btree<container_internal::map_params<
528 Key, Value, Compare, Alloc, /*TargetNodeSize=*/256,
529 /*IsMulti=*/true>>> {
530 using Base = typename btree_multimap::btree_multimap_container;
531
532 public:
533 // Constructors and Assignment Operators
534 //
535 // A `btree_multimap` supports the same overload set as `std::multimap`
536 // for construction and assignment:
537 //
538 // * Default constructor
539 //
540 // absl::btree_multimap<int, std::string> map1;
541 //
542 // * Initializer List constructor
543 //
544 // absl::btree_multimap<int, std::string> map2 =
545 // {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
546 //
547 // * Copy constructor
548 //
549 // absl::btree_multimap<int, std::string> map3(map2);
550 //
551 // * Copy assignment operator
552 //
553 // absl::btree_multimap<int, std::string> map4;
554 // map4 = map3;
555 //
556 // * Move constructor
557 //
558 // // Move is guaranteed efficient
559 // absl::btree_multimap<int, std::string> map5(std::move(map4));
560 //
561 // * Move assignment operator
562 //
563 // // May be efficient if allocators are compatible
564 // absl::btree_multimap<int, std::string> map6;
565 // map6 = std::move(map5);
566 //
567 // * Range constructor
568 //
569 // std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
570 // absl::btree_multimap<int, std::string> map7(v.begin(), v.end());
btree_multimap()571 btree_multimap() {}
572 using Base::Base;
573
574 // btree_multimap::begin()
575 //
576 // Returns an iterator to the beginning of the `btree_multimap`.
577 using Base::begin;
578
579 // btree_multimap::cbegin()
580 //
581 // Returns a const iterator to the beginning of the `btree_multimap`.
582 using Base::cbegin;
583
584 // btree_multimap::end()
585 //
586 // Returns an iterator to the end of the `btree_multimap`.
587 using Base::end;
588
589 // btree_multimap::cend()
590 //
591 // Returns a const iterator to the end of the `btree_multimap`.
592 using Base::cend;
593
594 // btree_multimap::empty()
595 //
596 // Returns whether or not the `btree_multimap` is empty.
597 using Base::empty;
598
599 // btree_multimap::max_size()
600 //
601 // Returns the largest theoretical possible number of elements within a
602 // `btree_multimap` under current memory constraints. This value can be
603 // thought of as the largest value of `std::distance(begin(), end())` for a
604 // `btree_multimap<Key, T>`.
605 using Base::max_size;
606
607 // btree_multimap::size()
608 //
609 // Returns the number of elements currently within the `btree_multimap`.
610 using Base::size;
611
612 // btree_multimap::clear()
613 //
614 // Removes all elements from the `btree_multimap`. Invalidates any references,
615 // pointers, or iterators referring to contained elements.
616 using Base::clear;
617
618 // btree_multimap::erase()
619 //
620 // Erases elements within the `btree_multimap`. If an erase occurs, any
621 // references, pointers, or iterators are invalidated.
622 // Overloads are listed below.
623 //
624 // iterator erase(iterator position):
625 // iterator erase(const_iterator position):
626 //
627 // Erases the element at `position` of the `btree_multimap`, returning
628 // the iterator pointing to the element after the one that was erased
629 // (or end() if none exists).
630 //
631 // iterator erase(const_iterator first, const_iterator last):
632 //
633 // Erases the elements in the open interval [`first`, `last`), returning
634 // the iterator pointing to the element after the interval that was erased
635 // (or end() if none exists).
636 //
637 // template <typename K> size_type erase(const K& key):
638 //
639 // Erases the elements matching the key, if any exist, returning the
640 // number of elements erased.
641 using Base::erase;
642
643 // btree_multimap::insert()
644 //
645 // Inserts an element of the specified value into the `btree_multimap`,
646 // returning an iterator pointing to the newly inserted element.
647 // Any references, pointers, or iterators are invalidated. Overloads are
648 // listed below.
649 //
650 // iterator insert(const value_type& value):
651 //
652 // Inserts a value into the `btree_multimap`, returning an iterator to the
653 // inserted element.
654 //
655 // iterator insert(value_type&& value):
656 //
657 // Inserts a moveable value into the `btree_multimap`, returning an iterator
658 // to the inserted element.
659 //
660 // iterator insert(const_iterator hint, const value_type& value):
661 // iterator insert(const_iterator hint, value_type&& value):
662 //
663 // Inserts a value, using the position of `hint` as a non-binding suggestion
664 // for where to begin the insertion search. Returns an iterator to the
665 // inserted element.
666 //
667 // void insert(InputIterator first, InputIterator last):
668 //
669 // Inserts a range of values [`first`, `last`).
670 //
671 // void insert(std::initializer_list<init_type> ilist):
672 //
673 // Inserts the elements within the initializer list `ilist`.
674 using Base::insert;
675
676 // btree_multimap::emplace()
677 //
678 // Inserts an element of the specified value by constructing it in-place
679 // within the `btree_multimap`. Any references, pointers, or iterators are
680 // invalidated.
681 using Base::emplace;
682
683 // btree_multimap::emplace_hint()
684 //
685 // Inserts an element of the specified value by constructing it in-place
686 // within the `btree_multimap`, using the position of `hint` as a non-binding
687 // suggestion for where to begin the insertion search.
688 //
689 // Any references, pointers, or iterators are invalidated.
690 using Base::emplace_hint;
691
692 // btree_multimap::extract()
693 //
694 // Extracts the indicated element, erasing it in the process, and returns it
695 // as a C++17-compatible node handle. Overloads are listed below.
696 //
697 // node_type extract(const_iterator position):
698 //
699 // Extracts the element at the indicated position and returns a node handle
700 // owning that extracted data.
701 //
702 // template <typename K> node_type extract(const K& k):
703 //
704 // Extracts the element with the key matching the passed key value and
705 // returns a node handle owning that extracted data. If the `btree_multimap`
706 // does not contain an element with a matching key, this function returns an
707 // empty node handle.
708 //
709 // NOTE: when compiled in an earlier version of C++ than C++17,
710 // `node_type::key()` returns a const reference to the key instead of a
711 // mutable reference. We cannot safely return a mutable reference without
712 // std::launder (which is not available before C++17).
713 //
714 // NOTE: In this context, `node_type` refers to the C++17 concept of a
715 // move-only type that owns and provides access to the elements in associative
716 // containers (https://en.cppreference.com/w/cpp/container/node_handle).
717 // It does NOT refer to the data layout of the underlying btree.
718 using Base::extract;
719
720 // btree_multimap::extract_and_get_next()
721 //
722 // Extracts the indicated element, erasing it in the process, and returns it
723 // as a C++17-compatible node handle along with an iterator to the next
724 // element.
725 //
726 // extract_and_get_next_return_type extract_and_get_next(
727 // const_iterator position):
728 //
729 // Extracts the element at the indicated position, returns a struct
730 // containing a member named `node`: a node handle owning that extracted
731 // data and a member named `next`: an iterator pointing to the next element
732 // in the btree.
733 using Base::extract_and_get_next;
734
735 // btree_multimap::merge()
736 //
737 // Extracts all elements from a given `source` btree_multimap into this
738 // `btree_multimap`.
739 using Base::merge;
740
741 // btree_multimap::swap(btree_multimap& other)
742 //
743 // Exchanges the contents of this `btree_multimap` with those of the `other`
744 // btree_multimap, avoiding invocation of any move, copy, or swap operations
745 // on individual elements.
746 //
747 // All iterators and references on the `btree_multimap` remain valid,
748 // excepting for the past-the-end iterator, which is invalidated.
749 using Base::swap;
750
751 // btree_multimap::contains()
752 //
753 // template <typename K> bool contains(const K& key) const:
754 //
755 // Determines whether an element comparing equal to the given `key` exists
756 // within the `btree_multimap`, returning `true` if so or `false` otherwise.
757 //
758 // Supports heterogeneous lookup, provided that the map has a compatible
759 // heterogeneous comparator.
760 using Base::contains;
761
762 // btree_multimap::count()
763 //
764 // template <typename K> size_type count(const K& key) const:
765 //
766 // Returns the number of elements comparing equal to the given `key` within
767 // the `btree_multimap`.
768 //
769 // Supports heterogeneous lookup, provided that the map has a compatible
770 // heterogeneous comparator.
771 using Base::count;
772
773 // btree_multimap::equal_range()
774 //
775 // Returns a half-open range [first, last), defined by a `std::pair` of two
776 // iterators, containing all elements with the passed key in the
777 // `btree_multimap`.
778 using Base::equal_range;
779
780 // btree_multimap::find()
781 //
782 // template <typename K> iterator find(const K& key):
783 // template <typename K> const_iterator find(const K& key) const:
784 //
785 // Finds an element with the passed `key` within the `btree_multimap`.
786 //
787 // Supports heterogeneous lookup, provided that the map has a compatible
788 // heterogeneous comparator.
789 using Base::find;
790
791 // btree_multimap::lower_bound()
792 //
793 // template <typename K> iterator lower_bound(const K& key):
794 // template <typename K> const_iterator lower_bound(const K& key) const:
795 //
796 // Finds the first element with a key that is not less than `key` within the
797 // `btree_multimap`.
798 //
799 // Supports heterogeneous lookup, provided that the map has a compatible
800 // heterogeneous comparator.
801 using Base::lower_bound;
802
803 // btree_multimap::upper_bound()
804 //
805 // template <typename K> iterator upper_bound(const K& key):
806 // template <typename K> const_iterator upper_bound(const K& key) const:
807 //
808 // Finds the first element with a key that is greater than `key` within the
809 // `btree_multimap`.
810 //
811 // Supports heterogeneous lookup, provided that the map has a compatible
812 // heterogeneous comparator.
813 using Base::upper_bound;
814
815 // btree_multimap::get_allocator()
816 //
817 // Returns the allocator function associated with this `btree_multimap`.
818 using Base::get_allocator;
819
820 // btree_multimap::key_comp();
821 //
822 // Returns the key comparator associated with this `btree_multimap`.
823 using Base::key_comp;
824
825 // btree_multimap::value_comp();
826 //
827 // Returns the value comparator associated with this `btree_multimap`.
828 using Base::value_comp;
829 };
830
831 // absl::swap(absl::btree_multimap<>, absl::btree_multimap<>)
832 //
833 // Swaps the contents of two `absl::btree_multimap` containers.
834 template <typename K, typename V, typename C, typename A>
swap(btree_multimap<K,V,C,A> & x,btree_multimap<K,V,C,A> & y)835 void swap(btree_multimap<K, V, C, A> &x, btree_multimap<K, V, C, A> &y) {
836 return x.swap(y);
837 }
838
839 // absl::erase_if(absl::btree_multimap<>, Pred)
840 //
841 // Erases all elements that satisfy the predicate pred from the container.
842 // Returns the number of erased elements.
843 template <typename K, typename V, typename C, typename A, typename Pred>
erase_if(btree_multimap<K,V,C,A> & map,Pred pred)844 typename btree_multimap<K, V, C, A>::size_type erase_if(
845 btree_multimap<K, V, C, A> &map, Pred pred) {
846 return container_internal::btree_access::erase_if(map, std::move(pred));
847 }
848
849 namespace container_internal {
850
851 // A parameters structure for holding the type parameters for a btree_map.
852 // Compare and Alloc should be nothrow copy-constructible.
853 template <typename Key, typename Data, typename Compare, typename Alloc,
854 int TargetNodeSize, bool IsMulti>
855 struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize, IsMulti,
856 /*IsMap=*/true, map_slot_policy<Key, Data>> {
857 using super_type = typename map_params::common_params;
858 using mapped_type = Data;
859 // This type allows us to move keys when it is safe to do so. It is safe
860 // for maps in which value_type and mutable_value_type are layout compatible.
861 using slot_policy = typename super_type::slot_policy;
862 using slot_type = typename super_type::slot_type;
863 using value_type = typename super_type::value_type;
864 using init_type = typename super_type::init_type;
865
866 template <typename V>
867 static auto key(const V &value) -> decltype(value.first) {
868 return value.first;
869 }
keymap_params870 static const Key &key(const slot_type *s) { return slot_policy::key(s); }
keymap_params871 static const Key &key(slot_type *s) { return slot_policy::key(s); }
872 // For use in node handle.
873 static auto mutable_key(slot_type *s)
874 -> decltype(slot_policy::mutable_key(s)) {
875 return slot_policy::mutable_key(s);
876 }
valuemap_params877 static mapped_type &value(value_type *value) { return value->second; }
878 };
879
880 } // namespace container_internal
881
882 ABSL_NAMESPACE_END
883 } // namespace absl
884
885 #endif // ABSL_CONTAINER_BTREE_MAP_H_
886