File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -217,6 +217,16 @@ class DenseMapBase : public DebugEpochBase {
217
217
return ValueT ();
218
218
}
219
219
220
+ // Return the entry with the specified key, or \p Default. This variant is
221
+ // useful, because `lookup` cannot be used with non-default-constructible
222
+ // values.
223
+ ValueT lookup_or (const_arg_type_t <KeyT> Val,
224
+ const_arg_type_t <ValueT> Default) const {
225
+ if (const BucketT *Bucket = doFind (Val))
226
+ return Bucket->getSecond ();
227
+ return Default;
228
+ }
229
+
220
230
// / at - Return the entry for the specified key, or abort if no such
221
231
// / entry exists.
222
232
const ValueT &at (const_arg_type_t <KeyT> Val) const {
Original file line number Diff line number Diff line change @@ -616,6 +616,26 @@ TEST(DenseMapCustomTest, StringRefTest) {
616
616
EXPECT_EQ (42 , M.lookup (StringRef (" a" , 0 )));
617
617
}
618
618
619
+ struct NonDefaultConstructible {
620
+ unsigned V;
621
+ NonDefaultConstructible (unsigned V) : V(V) {};
622
+ bool operator ==(const NonDefaultConstructible &Other) const {
623
+ return V == Other.V ;
624
+ }
625
+ };
626
+
627
+ TEST (DenseMapCustomTest, LookupOr) {
628
+ DenseMap<int , NonDefaultConstructible> M;
629
+
630
+ M.insert_or_assign (0 , 3u );
631
+ M.insert_or_assign (1 , 2u );
632
+ M.insert_or_assign (1 , 0u );
633
+
634
+ EXPECT_EQ (M.lookup_or (0 , 4u ), 3u );
635
+ EXPECT_EQ (M.lookup_or (1 , 4u ), 0u );
636
+ EXPECT_EQ (M.lookup_or (2 , 4u ), 4u );
637
+ }
638
+
619
639
// Key traits that allows lookup with either an unsigned or char* key;
620
640
// In the latter case, "a" == 0, "b" == 1 and so on.
621
641
struct TestDenseMapInfo {
You can’t perform that action at this time.
0 commit comments