@@ -240,43 +240,30 @@ class DenseMapBase : public DebugEpochBase {
240
240
// If the key is already in the map, it returns false and doesn't update the
241
241
// value.
242
242
std::pair<iterator, bool > insert (const std::pair<KeyT, ValueT> &KV) {
243
- return try_emplace (KV.first , KV.second );
243
+ return try_emplace_impl (KV.first , KV.second );
244
244
}
245
245
246
246
// Inserts key,value pair into the map if the key isn't already in the map.
247
247
// If the key is already in the map, it returns false and doesn't update the
248
248
// value.
249
249
std::pair<iterator, bool > insert (std::pair<KeyT, ValueT> &&KV) {
250
- return try_emplace (std::move (KV.first ), std::move (KV.second ));
250
+ return try_emplace_impl (std::move (KV.first ), std::move (KV.second ));
251
251
}
252
252
253
253
// Inserts key,value pair into the map if the key isn't already in the map.
254
254
// The value is constructed in-place if the key is not in the map, otherwise
255
255
// it is not moved.
256
256
template <typename ... Ts>
257
257
std::pair<iterator, bool > try_emplace (KeyT &&Key, Ts &&...Args) {
258
- BucketT *TheBucket;
259
- if (LookupBucketFor (Key, TheBucket))
260
- return {makeInsertIterator (TheBucket), false }; // Already in map.
261
-
262
- // Otherwise, insert the new element.
263
- TheBucket =
264
- InsertIntoBucket (TheBucket, std::move (Key), std::forward<Ts>(Args)...);
265
- return {makeInsertIterator (TheBucket), true };
258
+ return try_emplace_impl (std::move (Key), std::forward<Ts>(Args)...);
266
259
}
267
260
268
261
// Inserts key,value pair into the map if the key isn't already in the map.
269
262
// The value is constructed in-place if the key is not in the map, otherwise
270
263
// it is not moved.
271
264
template <typename ... Ts>
272
265
std::pair<iterator, bool > try_emplace (const KeyT &Key, Ts &&...Args) {
273
- BucketT *TheBucket;
274
- if (LookupBucketFor (Key, TheBucket))
275
- return {makeInsertIterator (TheBucket), false }; // Already in map.
276
-
277
- // Otherwise, insert the new element.
278
- TheBucket = InsertIntoBucket (TheBucket, Key, std::forward<Ts>(Args)...);
279
- return {makeInsertIterator (TheBucket), true };
266
+ return try_emplace_impl (Key, std::forward<Ts>(Args)...);
280
267
}
281
268
282
269
// / Alternate version of insert() which allows a different, and possibly
@@ -360,19 +347,11 @@ class DenseMapBase : public DebugEpochBase {
360
347
}
361
348
362
349
ValueT &operator [](const KeyT &Key) {
363
- BucketT *TheBucket;
364
- if (LookupBucketFor (Key, TheBucket))
365
- return TheBucket->second ;
366
-
367
- return InsertIntoBucket (TheBucket, Key)->second ;
350
+ return try_emplace_impl (Key).first ->second ;
368
351
}
369
352
370
353
ValueT &operator [](KeyT &&Key) {
371
- BucketT *TheBucket;
372
- if (LookupBucketFor (Key, TheBucket))
373
- return TheBucket->second ;
374
-
375
- return InsertIntoBucket (TheBucket, std::move (Key))->second ;
354
+ return try_emplace_impl (std::move (Key)).first ->second ;
376
355
}
377
356
378
357
// / isPointerIntoBucketsArray - Return true if the specified pointer points
@@ -496,6 +475,18 @@ class DenseMapBase : public DebugEpochBase {
496
475
static const KeyT getTombstoneKey () { return KeyInfoT::getTombstoneKey (); }
497
476
498
477
private:
478
+ template <typename KeyArgT, typename ... Ts>
479
+ std::pair<iterator, bool > try_emplace_impl (KeyArgT &&Key, Ts &&...Args) {
480
+ BucketT *TheBucket = nullptr ;
481
+ if (LookupBucketFor (Key, TheBucket))
482
+ return {makeInsertIterator (TheBucket), false }; // Already in the map.
483
+
484
+ // Otherwise, insert the new element.
485
+ TheBucket = InsertIntoBucket (TheBucket, std::forward<KeyArgT>(Key),
486
+ std::forward<Ts>(Args)...);
487
+ return {makeInsertIterator (TheBucket), true };
488
+ }
489
+
499
490
iterator makeIterator (BucketT *P, BucketT *E, DebugEpochBase &Epoch,
500
491
bool NoAdvance = false ) {
501
492
if (shouldReverseIterate<KeyT>()) {
0 commit comments