Coverage Report

Created: 2026-08-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/container/flat_hash_map.h
Line
Count
Source
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: flat_hash_map.h
17
// -----------------------------------------------------------------------------
18
//
19
// An `absl::flat_hash_map<K, V>` is an unordered associative container of
20
// unique keys and associated values designed to be a more efficient replacement
21
// for `std::unordered_map`. Like `unordered_map`, search, insertion, and
22
// deletion of map elements can be done as an `O(1)` operation. However,
23
// `flat_hash_map` (and other unordered associative containers known as the
24
// collection of Abseil "Swiss tables") contain other optimizations that result
25
// in both memory and computation advantages.
26
//
27
// In most cases, your default choice for a hash map should be a map of type
28
// `flat_hash_map`.
29
//
30
// `flat_hash_map` is not exception-safe.
31
32
#ifndef ABSL_CONTAINER_FLAT_HASH_MAP_H_
33
#define ABSL_CONTAINER_FLAT_HASH_MAP_H_
34
35
#include <cstddef>
36
#include <memory>
37
#include <type_traits>
38
#include <utility>
39
40
#include "absl/algorithm/container.h"
41
#include "absl/base/attributes.h"
42
#include "absl/base/macros.h"
43
#include "absl/container/hash_container_defaults.h"
44
#include "absl/container/internal/container_memory.h"
45
#include "absl/container/internal/raw_hash_map.h"  // IWYU pragma: export
46
#include "absl/meta/type_traits.h"
47
48
namespace absl {
49
ABSL_NAMESPACE_BEGIN
50
namespace container_internal {
51
template <class K, class V>
52
struct FlatHashMapPolicy;
53
}  // namespace container_internal
54
55
// -----------------------------------------------------------------------------
56
// absl::flat_hash_map
57
// -----------------------------------------------------------------------------
58
//
59
// An `absl::flat_hash_map<K, V>` is an unordered associative container which
60
// has been optimized for both speed and memory footprint in most common use
61
// cases. Its interface is similar to that of `std::unordered_map<K, V>` with
62
// the following notable differences:
63
//
64
// * Requires keys that are CopyConstructible
65
// * Requires values that are MoveConstructible
66
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
67
//   `insert()`, provided that the map is provided a compatible heterogeneous
68
//   hashing function and equality operator. See below for details.
69
// * Invalidates any references and pointers to elements within the table after
70
//   `rehash()` and when the table is moved.
71
// * Contains a `capacity()` member function indicating the number of element
72
//   slots (open, deleted, and empty) within the hash map.
73
// * Returns `void` from the `erase(iterator)` overload.
74
//
75
// By default, `flat_hash_map` uses the `absl::Hash` hashing framework.
76
// All fundamental and Abseil types that support the `absl::Hash` framework have
77
// a compatible equality operator for comparing insertions into `flat_hash_map`.
78
// If your type is not yet supported by the `absl::Hash` framework, see
79
// absl/hash/hash.h for information on extending Abseil hashing to user-defined
80
// types.
81
//
82
// Using `absl::flat_hash_map` at interface boundaries in dynamically loaded
83
// libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
84
// be randomized across dynamically loaded libraries.
85
//
86
// To achieve heterogeneous lookup for custom types either `Hash` and `Eq` type
87
// parameters can be used or `T` should have public inner types
88
// `absl_container_hash` and (optionally) `absl_container_eq`. In either case,
89
// `typename Hash::is_transparent` and `typename Eq::is_transparent` should be
90
// well-formed. Both types are basically functors:
91
// * `Hash` should support `size_t operator()(U val) const` that returns a hash
92
// for the given `val`.
93
// * `Eq` should support `bool operator()(U lhs, V rhs) const` that returns true
94
// if `lhs` is equal to `rhs`.
95
//
96
// In most cases `T` needs only to provide the `absl_container_hash`. In this
97
// case `std::equal_to<void>` will be used instead of `eq` part.
98
//
99
// NOTE: A `flat_hash_map` stores its value types directly inside its
100
// implementation array to avoid memory indirection. Because a `flat_hash_map`
101
// is designed to move data when rehashed, map values will not retain pointer
102
// stability. If you require pointer stability, or if your values are large,
103
// consider using `absl::flat_hash_map<Key, std::unique_ptr<Value>>` instead.
104
// If your types are not moveable or you require pointer stability for keys,
105
// consider `absl::node_hash_map`.
106
//
107
// PERFORMANCE WARNING: Erasure & sparsity can negatively affect performance:
108
//  * Iteration takes O(capacity) time, not O(size).
109
//  * erase() slows down begin() and ++iterator.
110
//  * Capacity only shrinks on rehash() or clear() -- not on erase().
111
//
112
// Example:
113
//
114
//   // Create a flat hash map of three strings (that map to strings)
115
//   absl::flat_hash_map<std::string, std::string> ducks =
116
//     {{"a", "huey"}, {"b", "dewey"}, {"c", "louie"}};
117
//
118
//   // Insert a new element into the flat hash map
119
//   ducks.insert({"d", "donald"});
120
//
121
//   // Force a rehash of the flat hash map
122
//   ducks.rehash(0);
123
//
124
//   // Find the element with the key "b"
125
//   std::string search_key = "b";
126
//   auto result = ducks.find(search_key);
127
//   if (result != ducks.end()) {
128
//     std::cout << "Result: " << result->second << std::endl;
129
//   }
130
template <
131
    class K, class V,
132
    class Hash =
133
        typename container_internal::FlatHashMapPolicy<K, V>::DefaultHash,
134
    class Eq = typename container_internal::FlatHashMapPolicy<K, V>::DefaultEq,
135
    class Allocator =
136
        typename container_internal::FlatHashMapPolicy<K, V>::DefaultAlloc>
137
class ABSL_ATTRIBUTE_OWNER flat_hash_map
138
    : public absl::container_internal::InstantiateRawHashMap<
139
          absl::container_internal::FlatHashMapPolicy<K, V>, Hash, Eq,
140
          Allocator>::type {
141
  using Base = typename flat_hash_map::raw_hash_map;
142
143
 public:
144
  // Constructors and Assignment Operators
145
  //
146
  // A flat_hash_map supports the same overload set as `std::unordered_map`
147
  // for construction and assignment:
148
  //
149
  // *  Default constructor
150
  //
151
  //    // No allocation for the table's elements is made.
152
  //    absl::flat_hash_map<int, std::string> map1;
153
  //
154
  // * Initializer List constructor
155
  //
156
  //   absl::flat_hash_map<int, std::string> map2 =
157
  //       {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
158
  //
159
  // * Copy constructor
160
  //
161
  //   absl::flat_hash_map<int, std::string> map3(map2);
162
  //
163
  // * Copy assignment operator
164
  //
165
  //   // Hash functor and Comparator are copied as well
166
  //   absl::flat_hash_map<int, std::string> map4;
167
  //   map4 = map3;
168
  //
169
  // * Move constructor
170
  //
171
  //   // Move is guaranteed efficient
172
  //   absl::flat_hash_map<int, std::string> map5(std::move(map4));
173
  //
174
  //   // After the move, map4 is in a valid but unspecified state. The only
175
  //   // operations guaranteed to be safe on a moved-from map are destruction,
176
  //   // assignment, and clear(). Any other operation (e.g. size(), empty(),
177
  //   // iteration) results in undefined behavior.
178
  //
179
  // * Move assignment operator
180
  //
181
  //   // May be efficient if allocators are compatible
182
  //   absl::flat_hash_map<int, std::string> map6;
183
  //   map6 = std::move(map5);
184
  //
185
  //   // Same moved-from guarantees apply to map5 after this operation.
186
  //
187
  // * Range constructor
188
  //
189
  //   std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
190
  //   absl::flat_hash_map<int, std::string> map7(v.begin(), v.end());
191
  //
192
  // * from_range constructor (C++23)
193
  //
194
  //   std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
195
  //   absl::flat_hash_map<int, std::string> map8(std::from_range, v);
196
2
  flat_hash_map() {}
197
  using Base::Base;
198
199
  // flat_hash_map::begin()
200
  //
201
  // Returns an iterator to the beginning of the `flat_hash_map`.
202
  using Base::begin;
203
204
  // flat_hash_map::cbegin()
205
  //
206
  // Returns a const iterator to the beginning of the `flat_hash_map`.
207
  using Base::cbegin;
208
209
  // flat_hash_map::cend()
210
  //
211
  // Returns a const iterator to the end of the `flat_hash_map`.
212
  using Base::cend;
213
214
  // flat_hash_map::end()
215
  //
216
  // Returns an iterator to the end of the `flat_hash_map`.
217
  using Base::end;
218
219
  // flat_hash_map::capacity()
220
  //
221
  // Returns the number of element slots (assigned, deleted, and empty)
222
  // available within the `flat_hash_map`.
223
  //
224
  // NOTE: this member function is particular to `absl::flat_hash_map` and is
225
  // not provided in the `std::unordered_map` API.
226
  using Base::capacity;
227
228
  // flat_hash_map::empty()
229
  //
230
  // Returns whether or not the `flat_hash_map` is empty.
231
  using Base::empty;
232
233
  // flat_hash_map::max_size()
234
  //
235
  // Returns the largest theoretical possible number of elements within a
236
  // `flat_hash_map` under current memory constraints. This value can be thought
237
  // of the largest value of `std::distance(begin(), end())` for a
238
  // `flat_hash_map<K, V>`.
239
  using Base::max_size;
240
241
  // flat_hash_map::size()
242
  //
243
  // Returns the number of elements currently within the `flat_hash_map`.
244
  using Base::size;
245
246
  // flat_hash_map::clear()
247
  //
248
  // Removes all elements from the `flat_hash_map`. Invalidates any references,
249
  // pointers, or iterators referring to contained elements.
250
  //
251
  // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
252
  // the underlying buffer call `erase(begin(), end())`.
253
  using Base::clear;
254
255
  // flat_hash_map::erase()
256
  //
257
  // Erases elements within the `flat_hash_map`. Erasing does not trigger a
258
  // rehash. Overloads are listed below.
259
  //
260
  // void erase(const_iterator pos):
261
  //
262
  //   Erases the element at `position` of the `flat_hash_map`, returning
263
  //   `void`.
264
  //
265
  //   NOTE: returning `void` in this case is different than that of STL
266
  //   containers in general and `std::unordered_map` in particular (which
267
  //   return an iterator to the element following the erased element). If that
268
  //   iterator is needed, simply post increment the iterator:
269
  //
270
  //     map.erase(it++);
271
  //
272
  // iterator erase(const_iterator first, const_iterator last):
273
  //
274
  //   Erases the elements in the open interval [`first`, `last`), returning an
275
  //   iterator pointing to `last`. The special case of calling
276
  //   `erase(begin(), end())` resets the reserved growth such that if
277
  //   `reserve(N)` has previously been called and there has been no intervening
278
  //   call to `clear()`, then after calling `erase(begin(), end())`, it is safe
279
  //   to assume that inserting N elements will not cause a rehash.
280
  //
281
  // size_type erase(const key_type& key):
282
  //
283
  //   Erases the element with the matching key, if it exists, returning the
284
  //   number of elements erased (0 or 1).
285
  using Base::erase;
286
287
  // flat_hash_map::insert()
288
  //
289
  // Inserts an element of the specified value into the `flat_hash_map`,
290
  // returning an iterator pointing to the newly inserted element, provided that
291
  // an element with the given key does not already exist. If rehashing occurs
292
  // due to the insertion, all iterators are invalidated. Overloads are listed
293
  // below.
294
  //
295
  // std::pair<iterator,bool> insert(const init_type& value):
296
  //
297
  //   Inserts a value into the `flat_hash_map`. Returns a pair consisting of an
298
  //   iterator to the inserted element (or to the element that prevented the
299
  //   insertion) and a bool denoting whether the insertion took place.
300
  //
301
  // std::pair<iterator,bool> insert(T&& value):
302
  // std::pair<iterator,bool> insert(init_type&& value):
303
  //
304
  //   Inserts a moveable value into the `flat_hash_map`. Returns a pair
305
  //   consisting of an iterator to the inserted element (or to the element that
306
  //   prevented the insertion) and a bool denoting whether the insertion took
307
  //   place.
308
  //
309
  // iterator insert(const_iterator hint, const init_type& value):
310
  // iterator insert(const_iterator hint, T&& value):
311
  // iterator insert(const_iterator hint, init_type&& value);
312
  //
313
  //   Inserts a value, using the position of `hint` as a non-binding suggestion
314
  //   for where to begin the insertion search. Returns an iterator to the
315
  //   inserted element, or to the existing element that prevented the
316
  //   insertion.
317
  //
318
  // void insert(InputIterator first, InputIterator last):
319
  //
320
  //   Inserts a range of values [`first`, `last`).
321
  //
322
  //   NOTE: Although the STL does not specify which element may be inserted if
323
  //   multiple keys compare equivalently, for `flat_hash_map` we guarantee the
324
  //   first match is inserted.
325
  //
326
  // void insert(std::initializer_list<init_type> ilist):
327
  //
328
  //   Inserts the elements within the initializer list `ilist`.
329
  //
330
  //   NOTE: Although the STL does not specify which element may be inserted if
331
  //   multiple keys compare equivalently within the initializer list, for
332
  //   `flat_hash_map` we guarantee the first match is inserted.
333
  using Base::insert;
334
335
  // flat_hash_map::insert_or_assign()
336
  //
337
  // Inserts an element of the specified value into the `flat_hash_map` provided
338
  // that a value with the given key does not already exist, or replaces it with
339
  // the element value if a key for that value already exists, returning an
340
  // iterator pointing to the newly inserted element.  If rehashing occurs due
341
  // to the insertion, all existing iterators are invalidated. Overloads are
342
  // listed below.
343
  //
344
  // pair<iterator, bool> insert_or_assign(const init_type& k, T&& obj):
345
  // pair<iterator, bool> insert_or_assign(init_type&& k, T&& obj):
346
  //
347
  //   Inserts/Assigns (or moves) the element of the specified key into the
348
  //   `flat_hash_map`.
349
  //
350
  // iterator insert_or_assign(const_iterator hint,
351
  //                           const init_type& k, T&& obj):
352
  // iterator insert_or_assign(const_iterator hint, init_type&& k, T&& obj):
353
  //
354
  //   Inserts/Assigns (or moves) the element of the specified key into the
355
  //   `flat_hash_map` using the position of `hint` as a non-binding suggestion
356
  //   for where to begin the insertion search.
357
  using Base::insert_or_assign;
358
359
  // flat_hash_map::emplace()
360
  //
361
  // Inserts an element of the specified value by constructing it in-place
362
  // within the `flat_hash_map`, provided that no element with the given key
363
  // already exists.
364
  //
365
  // The element may be constructed even if there already is an element with the
366
  // key in the container, in which case the newly constructed element will be
367
  // destroyed immediately. Prefer `try_emplace()` unless your key is not
368
  // copyable or moveable.
369
  //
370
  // If rehashing occurs due to the insertion, all iterators are invalidated.
371
  using Base::emplace;
372
373
  // flat_hash_map::emplace_hint()
374
  //
375
  // Inserts an element of the specified value by constructing it in-place
376
  // within the `flat_hash_map`, using the position of `hint` as a non-binding
377
  // suggestion for where to begin the insertion search, and only inserts
378
  // provided that no element with the given key already exists.
379
  //
380
  // The element may be constructed even if there already is an element with the
381
  // key in the container, in which case the newly constructed element will be
382
  // destroyed immediately. Prefer `try_emplace()` unless your key is not
383
  // copyable or moveable.
384
  //
385
  // If rehashing occurs due to the insertion, all iterators are invalidated.
386
  using Base::emplace_hint;
387
388
  // flat_hash_map::try_emplace()
389
  //
390
  // Inserts an element of the specified value by constructing it in-place
391
  // within the `flat_hash_map`, provided that no element with the given key
392
  // already exists. Unlike `emplace()`, if an element with the given key
393
  // already exists, we guarantee that no element is constructed.
394
  //
395
  // If rehashing occurs due to the insertion, all iterators are invalidated.
396
  // Overloads are listed below.
397
  //
398
  //   pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
399
  //   pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
400
  //
401
  // Inserts (via copy or move) the element of the specified key into the
402
  // `flat_hash_map`.
403
  //
404
  //   iterator try_emplace(const_iterator hint,
405
  //                        const key_type& k, Args&&... args):
406
  //   iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args):
407
  //
408
  // Inserts (via copy or move) the element of the specified key into the
409
  // `flat_hash_map` using the position of `hint` as a non-binding suggestion
410
  // for where to begin the insertion search.
411
  //
412
  // All `try_emplace()` overloads make the same guarantees regarding rvalue
413
  // arguments as `std::unordered_map::try_emplace()`, namely that these
414
  // functions will not move from rvalue arguments if insertions do not happen.
415
  using Base::try_emplace;
416
417
  // flat_hash_map::extract()
418
  //
419
  // Extracts the indicated element, erasing it in the process, and returns it
420
  // as a C++17-compatible node handle. Overloads are listed below.
421
  //
422
  // node_type extract(const_iterator position):
423
  //
424
  //   Extracts the key,value pair of the element at the indicated position and
425
  //   returns a node handle owning that extracted data.
426
  //
427
  // node_type extract(const key_type& x):
428
  //
429
  //   Extracts the key,value pair of the element with a key matching the passed
430
  //   key value and returns a node handle owning that extracted data. If the
431
  //   `flat_hash_map` does not contain an element with a matching key, this
432
  //   function returns an empty node handle.
433
  //
434
  // NOTE: when compiled in an earlier version of C++ than C++17,
435
  // `node_type::key()` returns a const reference to the key instead of a
436
  // mutable reference. We cannot safely return a mutable reference without
437
  // std::launder (which is not available before C++17).
438
  using Base::extract;
439
440
  // flat_hash_map::merge()
441
  //
442
  // Extracts elements from a given `source` flat hash map into this
443
  // `flat_hash_map`. If the destination `flat_hash_map` already contains an
444
  // element with an equivalent key, that element is not extracted.
445
  using Base::merge;
446
447
  // flat_hash_map::swap(flat_hash_map& other)
448
  //
449
  // Exchanges the contents of this `flat_hash_map` with those of the `other`
450
  // flat hash map.
451
  //
452
  // All iterators and references on the `flat_hash_map` remain valid, excepting
453
  // for the past-the-end iterator, which is invalidated.
454
  //
455
  // `swap()` requires that the flat hash map's hashing and key equivalence
456
  // functions be Swappable, and are exchanged using unqualified calls to
457
  // non-member `swap()`. If the map's allocator has
458
  // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
459
  // set to `true`, the allocators are also exchanged using an unqualified call
460
  // to non-member `swap()`; otherwise, the allocators are not swapped.
461
  using Base::swap;
462
463
  // flat_hash_map::rehash(count)
464
  //
465
  // Rehashes the `flat_hash_map`, setting the number of slots to be at least
466
  // the passed value. If the new number of slots increases the load factor more
467
  // than the current maximum load factor
468
  // (`count` < `size()` / `max_load_factor()`), then the new number of slots
469
  // will be at least `size()` / `max_load_factor()`.
470
  //
471
  // To force a rehash, pass rehash(0).
472
  //
473
  // NOTE: unlike behavior in `std::unordered_map`, references are also
474
  // invalidated upon a `rehash()`.
475
  using Base::rehash;
476
477
  // flat_hash_map::reserve(count)
478
  //
479
  // Sets the number of slots in the `flat_hash_map` to the number needed to
480
  // accommodate at least `count` total elements without exceeding the current
481
  // maximum load factor, and may rehash the container if needed. After this
482
  // returns, it is guaranteed that `count - size()` elements can be inserted
483
  // into the `flat_hash_map` without another rehash.
484
  using Base::reserve;
485
486
  // flat_hash_map::at()
487
  //
488
  // Returns a reference to the mapped value of the element with key equivalent
489
  // to the passed key.
490
  using Base::at;
491
492
  // flat_hash_map::contains()
493
  //
494
  // Determines whether an element with a key comparing equal to the given `key`
495
  // exists within the `flat_hash_map`, returning `true` if so or `false`
496
  // otherwise.
497
  using Base::contains;
498
499
  // flat_hash_map::count(const Key& key) const
500
  //
501
  // Returns the number of elements with a key comparing equal to the given
502
  // `key` within the `flat_hash_map`. note that this function will return
503
  // either `1` or `0` since duplicate keys are not allowed within a
504
  // `flat_hash_map`.
505
  using Base::count;
506
507
  // flat_hash_map::equal_range()
508
  //
509
  // Returns a closed range [first, last], defined by a `std::pair` of two
510
  // iterators, containing all elements with the passed key in the
511
  // `flat_hash_map`.
512
  using Base::equal_range;
513
514
  // flat_hash_map::find()
515
  //
516
  // Finds an element with the passed `key` within the `flat_hash_map`.
517
  using Base::find;
518
519
  // flat_hash_map::operator[]()
520
  //
521
  // Returns a reference to the value mapped to the passed key within the
522
  // `flat_hash_map`, performing an `insert()` if the key does not already
523
  // exist.
524
  //
525
  // If an insertion occurs and results in a rehashing of the container, all
526
  // iterators are invalidated. Otherwise iterators are not affected and
527
  // references are not invalidated. Overloads are listed below.
528
  //
529
  // T& operator[](const Key& key):
530
  //
531
  //   Inserts an init_type object constructed in-place if the element with the
532
  //   given key does not exist.
533
  //
534
  // T& operator[](Key&& key):
535
  //
536
  //   Inserts an init_type object constructed in-place provided that an element
537
  //   with the given key does not exist.
538
  using Base::operator[];
539
540
  // flat_hash_map::bucket_count()
541
  //
542
  // Returns the number of "buckets" within the `flat_hash_map`. Note that
543
  // because a flat hash map contains all elements within its internal storage,
544
  // this value simply equals the current capacity of the `flat_hash_map`.
545
  using Base::bucket_count;
546
547
  // flat_hash_map::load_factor()
548
  //
549
  // Returns the current load factor of the `flat_hash_map` (the average number
550
  // of slots occupied with a value within the hash map).
551
  using Base::load_factor;
552
553
  // flat_hash_map::max_load_factor()
554
  //
555
  // Manages the maximum load factor of the `flat_hash_map`. Overloads are
556
  // listed below.
557
  //
558
  // float flat_hash_map::max_load_factor()
559
  //
560
  //   Returns the current maximum load factor of the `flat_hash_map`.
561
  //
562
  // void flat_hash_map::max_load_factor(float ml)
563
  //
564
  //   Sets the maximum load factor of the `flat_hash_map` to the passed value.
565
  //
566
  //   NOTE: This overload is provided only for API compatibility with the STL;
567
  //   `flat_hash_map` will ignore any set load factor and manage its rehashing
568
  //   internally as an implementation detail.
569
  using Base::max_load_factor;
570
571
  // flat_hash_map::get_allocator()
572
  //
573
  // Returns the allocator function associated with this `flat_hash_map`.
574
  using Base::get_allocator;
575
576
  // flat_hash_map::hash_function()
577
  //
578
  // Returns the hashing function used to hash the keys within this
579
  // `flat_hash_map`.
580
  using Base::hash_function;
581
582
  // flat_hash_map::key_eq()
583
  //
584
  // Returns the function used for comparing keys equality.
585
  using Base::key_eq;
586
};
587
588
// erase_if(flat_hash_map<>, Pred)
589
//
590
// Erases all elements that satisfy the predicate `pred` from the container `c`.
591
// Returns the number of erased elements.
592
template <typename K, typename V, typename H, typename E, typename A,
593
          typename Predicate>
594
typename flat_hash_map<K, V, H, E, A>::size_type erase_if(
595
    flat_hash_map<K, V, H, E, A>& c, Predicate pred) {
596
  return container_internal::EraseIf(pred, &c);
597
}
598
599
// swap(flat_hash_map<>, flat_hash_map<>)
600
//
601
// Swaps the contents of two `flat_hash_map` containers.
602
//
603
// NOTE: we need to define this function template in order for
604
// `flat_hash_set::swap` to be called instead of `std::swap`. Even though we
605
// have `swap(raw_hash_set&, raw_hash_set&)` defined, that function requires a
606
// derived-to-base conversion, whereas `std::swap` is a function template so
607
// `std::swap` will be preferred by compiler.
608
template <typename K, typename V, typename H, typename E, typename A>
609
void swap(flat_hash_map<K, V, H, E, A>& x,
610
          flat_hash_map<K, V, H, E, A>& y) noexcept(noexcept(x.swap(y))) {
611
  x.swap(y);
612
}
613
614
namespace container_internal {
615
616
// c_for_each_fast(flat_hash_map<>, Function)
617
//
618
// Container-based version of the <algorithm> `std::for_each()` function to
619
// apply a function to a container's elements.
620
// There is no guarantees on the order of the function calls.
621
// Erasure and/or insertion of elements in the function is not allowed.
622
template <typename K, typename V, typename H, typename E, typename A,
623
          typename Function>
624
std::decay_t<Function> c_for_each_fast(const flat_hash_map<K, V, H, E, A>& c,
625
                                       Function&& f) {
626
  container_internal::ForEach(f, &c);
627
  return f;
628
}
629
template <typename K, typename V, typename H, typename E, typename A,
630
          typename Function>
631
std::decay_t<Function> c_for_each_fast(flat_hash_map<K, V, H, E, A>& c,
632
                                       Function&& f) {
633
  container_internal::ForEach(f, &c);
634
  return f;
635
}
636
template <typename K, typename V, typename H, typename E, typename A,
637
          typename Function>
638
std::decay_t<Function> c_for_each_fast(flat_hash_map<K, V, H, E, A>&& c,
639
                                       Function&& f) {
640
  container_internal::ForEach(f, &c);
641
  return f;
642
}
643
644
}  // namespace container_internal
645
646
namespace container_internal {
647
648
template <class K, class V>
649
struct FlatHashMapPolicy {
650
  using slot_policy = container_internal::map_slot_policy<K, V>;
651
  using slot_type = typename slot_policy::slot_type;
652
  using key_type = K;
653
  using mapped_type = V;
654
  using init_type = std::pair</*non const*/ key_type, mapped_type>;
655
656
  using DefaultHash = DefaultHashContainerHash<K>;
657
  using DefaultEq = DefaultHashContainerEq<K>;
658
  using DefaultAlloc = std::allocator<std::pair<const K, V>>;
659
660
  template <class Allocator, class... Args>
661
16
  static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
662
16
    slot_policy::construct(alloc, slot, std::forward<Args>(args)...);
663
16
  }
void absl::lts_20260526::container_internal::FlatHashMapPolicy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>::construct<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::lts_20260526::CommandLineFlag*> >, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&&>, std::__1::tuple<absl::lts_20260526::CommandLineFlag*&&> >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::lts_20260526::CommandLineFlag*> >*, absl::lts_20260526::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>*, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> > const&&>&&, std::__1::tuple<absl::lts_20260526::CommandLineFlag*&&>&&)
Line
Count
Source
661
16
  static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
662
16
    slot_policy::construct(alloc, slot, std::forward<Args>(args)...);
663
16
  }
Unexecuted instantiation: void absl::lts_20260526::container_internal::FlatHashMapPolicy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>::construct<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::lts_20260526::CommandLineFlag*> >, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> >&&>, std::__1::tuple<absl::lts_20260526::CommandLineFlag*&&> >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::lts_20260526::CommandLineFlag*> >*, absl::lts_20260526::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>*, std::__1::piecewise_construct_t const&, std::__1::tuple<std::__1::basic_string_view<char, std::__1::char_traits<char> >&&>&&, std::__1::tuple<absl::lts_20260526::CommandLineFlag*&&>&&)
664
665
  // Returns std::true_type in case destroy is trivial.
666
  template <class Allocator>
667
0
  static auto destroy(Allocator* alloc, slot_type* slot) {
668
0
    return slot_policy::destroy(alloc, slot);
669
0
  }
670
671
  template <class Allocator>
672
  static auto transfer(Allocator* alloc, slot_type* new_slot,
673
0
                       slot_type* old_slot) {
674
0
    return slot_policy::transfer(alloc, new_slot, old_slot);
675
0
  }
Unexecuted instantiation: auto absl::lts_20260526::container_internal::FlatHashMapPolicy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>::transfer<std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::lts_20260526::CommandLineFlag*> > >(std::__1::allocator<std::__1::pair<std::__1::basic_string_view<char, std::__1::char_traits<char> > const, absl::lts_20260526::CommandLineFlag*> >*, absl::lts_20260526::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>*, absl::lts_20260526::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>*)
Unexecuted instantiation: auto absl::lts_20260526::container_internal::FlatHashMapPolicy<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>::transfer<std::__1::allocator<char> >(std::__1::allocator<char>*, absl::lts_20260526::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>*, absl::lts_20260526::container_internal::map_slot_type<std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::lts_20260526::CommandLineFlag*>*)
676
677
  template <class F, class... Args>
678
  static decltype(absl::container_internal::DecomposePair(
679
      std::declval<F>(), std::declval<Args>()...))
680
46
  apply(F&& f, Args&&... args) {
681
46
    return absl::container_internal::DecomposePair(std::forward<F>(f),
682
46
                                                   std::forward<Args>(args)...);
683
46
  }
_ZN4absl12lts_2026052618container_internal17FlatHashMapPolicyINSt3__117basic_string_viewIcNS3_11char_traitsIcEEEEPNS0_15CommandLineFlagEE5applyINS1_12EqualElementIS7_NS1_8StringEqEEEJRNS3_4pairIKS7_S9_EEEEEDTclsr4absl18container_internalE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSJ_DpOSK_
Line
Count
Source
680
16
  apply(F&& f, Args&&... args) {
681
16
    return absl::container_internal::DecomposePair(std::forward<F>(f),
682
16
                                                   std::forward<Args>(args)...);
683
16
  }
_ZN4absl12lts_2026052618container_internal17FlatHashMapPolicyINSt3__117basic_string_viewIcNS3_11char_traitsIcEEEEPNS0_15CommandLineFlagEE5applyINS1_12raw_hash_setISA_JEE19EmplaceDecomposableEJNS3_4pairIKS7_S9_EEEEEDTclsr4absl18container_internalE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSI_DpOSJ_
Line
Count
Source
680
16
  apply(F&& f, Args&&... args) {
681
16
    return absl::container_internal::DecomposePair(std::forward<F>(f),
682
16
                                                   std::forward<Args>(args)...);
683
16
  }
Unexecuted instantiation: _ZN4absl12lts_2026052618container_internal17FlatHashMapPolicyINSt3__117basic_string_viewIcNS3_11char_traitsIcEEEEPNS0_15CommandLineFlagEE5applyINS1_11HashElementINS1_10StringHashELb1EEEJRNS3_4pairIKS7_S9_EEEEEDTclsr4absl18container_internalE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSJ_DpOSK_
_ZN4absl12lts_2026052618container_internal17FlatHashMapPolicyINSt3__117basic_string_viewIcNS3_11char_traitsIcEEEEPNS0_15CommandLineFlagEE5applyINS1_12raw_hash_setISA_JEE11FindElementEJRNS3_4pairIKS7_S9_EEEEEDTclsr4absl18container_internalE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSJ_DpOSK_
Line
Count
Source
680
14
  apply(F&& f, Args&&... args) {
681
14
    return absl::container_internal::DecomposePair(std::forward<F>(f),
682
14
                                                   std::forward<Args>(args)...);
683
14
  }
Unexecuted instantiation: _ZN4absl12lts_2026052618container_internal17FlatHashMapPolicyINSt3__117basic_string_viewIcNS3_11char_traitsIcEEEEPNS0_15CommandLineFlagEE5applyINS1_12raw_hash_setISA_JEE19EmplaceDecomposableEJNS3_4pairIS7_S9_EEEEEDTclsr4absl18container_internalE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSH_DpOSI_
684
685
  template <class Hash, bool kIsDefault>
686
0
  static constexpr HashSlotFn get_hash_slot_fn() {
687
0
    return memory_internal::IsLayoutCompatible<K, V>::value
688
0
               ? &TypeErasedApplyToSlotFn<Hash, K, kIsDefault>
689
0
               : nullptr;
690
0
  }
691
692
  static size_t space_used(const slot_type*) { return 0; }
693
694
30
  static std::pair<const K, V>& element(slot_type* slot) { return slot->value; }
695
696
  static V& value(std::pair<const K, V>* kv) { return kv->second; }
697
  static const V& value(const std::pair<const K, V>* kv) { return kv->second; }
698
};
699
700
}  // namespace container_internal
701
702
namespace container_algorithm_internal {
703
704
// Specialization of trait in absl/algorithm/container.h
705
template <class Key, class T, class Hash, class KeyEqual, class Allocator>
706
struct IsUnorderedContainer<
707
    absl::flat_hash_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {};
708
709
}  // namespace container_algorithm_internal
710
711
ABSL_NAMESPACE_END
712
}  // namespace absl
713
714
#endif  // ABSL_CONTAINER_FLAT_HASH_MAP_H_