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/internal/raw_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
#ifndef ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
16
#define ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_
17
18
#include <tuple>
19
#include <type_traits>
20
#include <utility>
21
22
#include "absl/base/attributes.h"
23
#include "absl/base/config.h"
24
#include "absl/base/throw_delegate.h"
25
#include "absl/container/internal/common_policy_traits.h"
26
#include "absl/container/internal/container_memory.h"
27
#include "absl/container/internal/raw_hash_set.h"  // IWYU pragma: export
28
#include "absl/meta/type_traits.h"
29
30
namespace absl {
31
ABSL_NAMESPACE_BEGIN
32
namespace container_internal {
33
34
template <class Policy, class... Params>
35
class raw_hash_map;
36
37
template <typename Policy, typename Hash, typename Eq, typename Alloc>
38
struct InstantiateRawHashMap {
39
  using type = typename ApplyWithoutDefaultSuffix<
40
      raw_hash_map,
41
      TypeList<int, typename Policy::DefaultHash, typename Policy::DefaultEq,
42
               typename Policy::DefaultAlloc>,
43
      TypeList<Policy, Hash, Eq, Alloc>>::type;
44
};
45
46
template <class Policy, class... Params>
47
class raw_hash_map : public raw_hash_set<Policy, Params...> {
48
  // P is Policy. It's passed as a template argument to support maps that have
49
  // incomplete types as values, as in unordered_map<K, IncompleteType>.
50
  // MappedReference<> may be a non-reference type.
51
  template <class P>
52
  using MappedReference = decltype(P::value(
53
      std::addressof(std::declval<typename raw_hash_map::reference>())));
54
55
  // MappedConstReference<> may be a non-reference type.
56
  template <class P>
57
  using MappedConstReference = decltype(P::value(
58
      std::addressof(std::declval<typename raw_hash_map::const_reference>())));
59
60
  using Hash = typename raw_hash_map::raw_hash_set::hasher;
61
  using Eq = typename raw_hash_map::raw_hash_set::key_equal;
62
  using Alloc = typename raw_hash_map::raw_hash_set::allocator_type;
63
64
  template <class K>
65
  using key_arg =
66
      typename KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>::
67
          template type<K, typename Policy::key_type>;
68
69
  // NOTE: The mess here is to shorten the code for the (very repetitive)
70
  // function overloads, and to allow the lifetime-bound overloads to dispatch
71
  // to the non-lifetime-bound overloads, to ensure there is a single source of
72
  // truth for each overload set.
73
  //
74
  // Enabled if an assignment from the given type would require the
75
  // source object to remain alive for the life of the element.
76
  //
77
  // TODO(b/402804213): Remove these traits and simplify the overloads whenever
78
  // we have a better mechanism available to handle lifetime analysis.
79
  template <class K, bool Value, typename = void>
80
  using LifetimeBoundK = HasValue<
81
      Value, std::conditional_t<policy_trait_element_is_owner<Policy>::value,
82
                                std::false_type,
83
                                type_traits_internal::IsLifetimeBoundAssignment<
84
                                    typename Policy::key_type, K>>>;
85
  template <class V, bool Value, typename = void>
86
  using LifetimeBoundV =
87
      HasValue<Value, type_traits_internal::IsLifetimeBoundAssignment<
88
                          typename Policy::mapped_type, V>>;
89
  template <class K, bool KValue, class V, bool VValue, typename... Dummy>
90
  using LifetimeBoundKV =
91
      std::conjunction<LifetimeBoundK<K, KValue, std::void_t<Dummy...>>,
92
                       LifetimeBoundV<V, VValue>>;
93
94
 public:
95
  using key_type = typename Policy::key_type;
96
  using mapped_type = typename Policy::mapped_type;
97
98
  static_assert(!std::is_reference<key_type>::value, "");
99
100
  // TODO(b/187807849): Evaluate whether to support reference mapped_type and
101
  // remove this assertion if/when it is supported.
102
  static_assert(!std::is_reference<mapped_type>::value, "");
103
104
  using iterator = typename raw_hash_map::raw_hash_set::iterator;
105
  using const_iterator = typename raw_hash_map::raw_hash_set::const_iterator;
106
107
2
  raw_hash_map() {}
108
  using raw_hash_map::raw_hash_set::raw_hash_set;
109
110
  // The last two template parameters ensure that both arguments are rvalues
111
  // (lvalue arguments are handled by the overloads below). This is necessary
112
  // for supporting bitfield arguments.
113
  //
114
  //   union { int n : 1; };
115
  //   flat_hash_map<int, int> m;
116
  //   m.insert_or_assign(n, n);
117
  //
118
  // TODO(b/402804213): Remove these macros whenever we have a better mechanism
119
  // available to handle lifetime analysis.
120
#define ABSL_INTERNAL_X(Func, Callee, KQual, VQual, KValue, VValue, Tail, ...) \
121
  template <                                                                   \
122
      typename K = key_type, class V = mapped_type,                            \
123
      ABSL_INTERNAL_IF_##KValue##_NOR_##VValue(                                \
124
          int = (EnableIf<LifetimeBoundKV<K, KValue, V, VValue,                \
125
                                          IfRRef<int KQual>::AddPtr<K>,        \
126
                                          IfRRef<int VQual>::AddPtr<V>>>()),   \
127
          ABSL_INTERNAL_SINGLE_ARG(                                            \
128
              int&...,                                                         \
129
              decltype(EnableIf<LifetimeBoundKV<K, KValue, V, VValue>>()) =    \
130
                  0))>                                                         \
131
  decltype(auto) Func(                                                         \
132
      __VA_ARGS__ key_arg<K> KQual k ABSL_INTERNAL_IF_##KValue(                \
133
          ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this)),                          \
134
      V VQual v ABSL_INTERNAL_IF_##VValue(ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY( \
135
          this))) ABSL_ATTRIBUTE_LIFETIME_BOUND {                              \
136
    return ABSL_INTERNAL_IF_##KValue##_OR_##VValue(                            \
137
        (this->template Func<K, V, 0>), Callee)(                               \
138
        std::forward<decltype(k)>(k), std::forward<decltype(v)>(v)) Tail;      \
139
  }                                                                            \
140
  static_assert(true, "This is to force a semicolon.")
141
142
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&,
143
                  false, false, ABSL_INTERNAL_SINGLE_ARG());
144
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&,
145
                  false, true, ABSL_INTERNAL_SINGLE_ARG());
146
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&, true,
147
                  false, ABSL_INTERNAL_SINGLE_ARG());
148
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&, true,
149
                  true, ABSL_INTERNAL_SINGLE_ARG());
150
151
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, false,
152
                  false, ABSL_INTERNAL_SINGLE_ARG());
153
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, false,
154
                  true, ABSL_INTERNAL_SINGLE_ARG());
155
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, true,
156
                  false, ABSL_INTERNAL_SINGLE_ARG());
157
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, true,
158
                  true, ABSL_INTERNAL_SINGLE_ARG());
159
160
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, false,
161
                  false, ABSL_INTERNAL_SINGLE_ARG());
162
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, false,
163
                  true, ABSL_INTERNAL_SINGLE_ARG());
164
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, true,
165
                  false, ABSL_INTERNAL_SINGLE_ARG());
166
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, true,
167
                  true, ABSL_INTERNAL_SINGLE_ARG());
168
169
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, false, false,
170
                  ABSL_INTERNAL_SINGLE_ARG());
171
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, false, true,
172
                  ABSL_INTERNAL_SINGLE_ARG());
173
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, true, false,
174
                  ABSL_INTERNAL_SINGLE_ARG());
175
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, true, true,
176
                  ABSL_INTERNAL_SINGLE_ARG());
177
178
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&,
179
                  false, false, .first, const_iterator ABSL_INTERNAL_COMMA);
180
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&,
181
                  false, true, .first, const_iterator ABSL_INTERNAL_COMMA);
182
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&, true,
183
                  false, .first, const_iterator ABSL_INTERNAL_COMMA);
184
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, const&, true,
185
                  true, .first, const_iterator ABSL_INTERNAL_COMMA);
186
187
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, false,
188
                  false, .first, const_iterator ABSL_INTERNAL_COMMA);
189
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, false,
190
                  true, .first, const_iterator ABSL_INTERNAL_COMMA);
191
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, true,
192
                  false, .first, const_iterator ABSL_INTERNAL_COMMA);
193
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, const&, &&, true,
194
                  true, .first, const_iterator ABSL_INTERNAL_COMMA);
195
196
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, false,
197
                  false, .first, const_iterator ABSL_INTERNAL_COMMA);
198
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, false,
199
                  true, .first, const_iterator ABSL_INTERNAL_COMMA);
200
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, true,
201
                  false, .first, const_iterator ABSL_INTERNAL_COMMA);
202
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, const&, true,
203
                  true, .first, const_iterator ABSL_INTERNAL_COMMA);
204
205
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, false, false,
206
                  .first, const_iterator ABSL_INTERNAL_COMMA);
207
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, false, true,
208
                  .first, const_iterator ABSL_INTERNAL_COMMA);
209
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, true, false,
210
                  .first, const_iterator ABSL_INTERNAL_COMMA);
211
  ABSL_INTERNAL_X(insert_or_assign, insert_or_assign_impl, &&, &&, true, true,
212
                  .first, const_iterator ABSL_INTERNAL_COMMA);
213
#undef ABSL_INTERNAL_X
214
215
  // All `try_emplace()` overloads make the same guarantees regarding rvalue
216
  // arguments as `std::unordered_map::try_emplace()`, namely that these
217
  // functions will not move from rvalue arguments if insertions do not happen.
218
  template <class K = key_type, int = EnableIf<LifetimeBoundK<K, false, K*>>(),
219
            class... Args,
220
            typename std::enable_if<
221
                !std::is_convertible<K, const_iterator>::value, int>::type = 0>
222
  std::pair<iterator, bool> try_emplace(key_arg<K>&& k, Args&&... args)
223
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
224
    return try_emplace_impl(std::forward<key_arg<K>>(k),
225
                            std::forward<Args>(args)...);
226
  }
227
228
  template <class K = key_type, class... Args,
229
            EnableIf<LifetimeBoundK<K, true, K*>> = 0,
230
            typename std::enable_if<
231
                !std::is_convertible<K, const_iterator>::value, int>::type = 0>
232
  std::pair<iterator, bool> try_emplace(
233
      key_arg<K>&& k ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this),
234
      Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
235
    return this->template try_emplace<K, 0>(std::forward<key_arg<K>>(k),
236
                                            std::forward<Args>(args)...);
237
  }
238
239
  template <class K = key_type, int = EnableIf<LifetimeBoundK<K, false>>(),
240
            class... Args,
241
            typename std::enable_if<
242
                !std::is_convertible<K, const_iterator>::value, int>::type = 0>
243
  std::pair<iterator, bool> try_emplace(const key_arg<K>& k, Args&&... args)
244
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
245
    return try_emplace_impl(k, std::forward<Args>(args)...);
246
  }
247
  template <class K = key_type, class... Args,
248
            EnableIf<LifetimeBoundK<K, true>> = 0,
249
            typename std::enable_if<
250
                !std::is_convertible<K, const_iterator>::value, int>::type = 0>
251
  std::pair<iterator, bool> try_emplace(
252
      const key_arg<K>& k ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this),
253
      Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
254
    return this->template try_emplace<K, 0>(k, std::forward<Args>(args)...);
255
  }
256
257
  template <class K = key_type, int = EnableIf<LifetimeBoundK<K, false, K*>>(),
258
            class... Args>
259
  iterator try_emplace(const_iterator, key_arg<K>&& k,
260
                       Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
261
    return try_emplace(std::forward<key_arg<K>>(k), std::forward<Args>(args)...)
262
        .first;
263
  }
264
  template <class K = key_type, class... Args,
265
            EnableIf<LifetimeBoundK<K, true, K*>> = 0>
266
  iterator try_emplace(const_iterator hint,
267
                       key_arg<K>&& k ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this),
268
                       Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
269
    return this->template try_emplace<K, 0>(hint, std::forward<key_arg<K>>(k),
270
                                            std::forward<Args>(args)...);
271
  }
272
273
  template <class K = key_type, int = EnableIf<LifetimeBoundK<K, false>>(),
274
            class... Args>
275
  iterator try_emplace(const_iterator, const key_arg<K>& k,
276
                       Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
277
    return try_emplace(k, std::forward<Args>(args)...).first;
278
  }
279
  template <class K = key_type, class... Args,
280
            EnableIf<LifetimeBoundK<K, true>> = 0>
281
  iterator try_emplace(const_iterator hint,
282
                       const key_arg<K>& k
283
                           ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this),
284
                       Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
285
    return this->template try_emplace<K, 0>(hint, k,
286
                                            std::forward<Args>(args)...);
287
  }
288
289
  template <class K = key_type, class P = Policy>
290
  MappedReference<P> at(const key_arg<K>& key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
291
    auto it = this->find(key);
292
    if (it == this->end()) {
293
      ThrowStdOutOfRange("absl::container_internal::raw_hash_map<>::at");
294
    }
295
    return Policy::value(&*it);
296
  }
297
298
  template <class K = key_type, class P = Policy>
299
  MappedConstReference<P> at(const key_arg<K>& key) const
300
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
301
    auto it = this->find(key);
302
    if (it == this->end()) {
303
      ThrowStdOutOfRange("absl::container_internal::raw_hash_map<>::at");
304
    }
305
    return Policy::value(&*it);
306
  }
307
308
  template <class K = key_type, class P = Policy,
309
            int = EnableIf<LifetimeBoundK<K, false, K*>>()>
310
  MappedReference<P> operator[](key_arg<K>&& key)
311
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
312
    // It is safe to use unchecked_deref here because try_emplace
313
    // will always return an iterator pointing to a valid item in the table,
314
    // since it inserts if nothing is found for the given key.
315
    return Policy::value(&this->unchecked_deref(
316
        try_emplace(std::forward<key_arg<K>>(key)).first));
317
  }
318
  template <class K = key_type, class P = Policy, int&...,
319
            EnableIf<LifetimeBoundK<K, true, K*>> = 0>
320
  MappedReference<P> operator[](
321
      key_arg<K>&& key ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
322
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
323
    return this->template operator[]<K, P, 0>(std::forward<key_arg<K>>(key));
324
  }
325
326
  template <class K = key_type, class P = Policy,
327
            int = EnableIf<LifetimeBoundK<K, false>>()>
328
  MappedReference<P> operator[](const key_arg<K>& key)
329
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
330
    // It is safe to use unchecked_deref here because try_emplace
331
    // will always return an iterator pointing to a valid item in the table,
332
    // since it inserts if nothing is found for the given key.
333
    return Policy::value(&this->unchecked_deref(try_emplace(key).first));
334
  }
335
  template <class K = key_type, class P = Policy, int&...,
336
            EnableIf<LifetimeBoundK<K, true>> = 0>
337
  MappedReference<P> operator[](
338
      const key_arg<K>& key ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
339
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
340
    return this->template operator[]<K, P, 0>(key);
341
  }
342
343
 private:
344
  static_assert(
345
      std::is_same_v<
346
          typename InstantiateRawHashMap<Policy, Hash, Eq, Alloc>::type,
347
          raw_hash_map>,
348
      "Redundant template parameters were passed. Use InstantiateRawHashMap<> "
349
      "instead");
350
351
  template <class K, class V>
352
  std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v)
353
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
354
    auto res = this->find_or_prepare_insert(k);
355
    if (res.second) {
356
      this->emplace_at(res.first, std::forward<K>(k), std::forward<V>(v));
357
    } else {
358
      Policy::value(&*res.first) = std::forward<V>(v);
359
    }
360
    return res;
361
  }
362
363
  template <class K = key_type, class... Args>
364
  std::pair<iterator, bool> try_emplace_impl(K&& k, Args&&... args)
365
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
366
    auto res = this->find_or_prepare_insert(k);
367
    if (res.second) {
368
      this->emplace_at(res.first, std::piecewise_construct,
369
                       std::forward_as_tuple(std::forward<K>(k)),
370
                       std::forward_as_tuple(std::forward<Args>(args)...));
371
    }
372
    return res;
373
  }
374
};
375
376
}  // namespace container_internal
377
ABSL_NAMESPACE_END
378
}  // namespace absl
379
380
#endif  // ABSL_CONTAINER_INTERNAL_RAW_HASH_MAP_H_