Skip to content

Commit 602261c

Browse files
authored
Add contains() method to etl::unordered_map and etl::unordered_set (#990)
* Add contains() method to etl::unordered_map and etl::unordered_set * Add contains() method to etl::unordered_multiset and etl::unordered_multimap Use predefined variables in UT Move contains() method to correct place in etl::unordered_set * Fix contains() parameter type
1 parent 99d7537 commit 602261c

8 files changed

+76
-0
lines changed

include/etl/unordered_map.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,14 @@ namespace etl
13611361
}
13621362
#endif
13631363

1364+
//*************************************************************************
1365+
/// Check if the unordered_map contains the key.
1366+
//*************************************************************************
1367+
bool contains(const_key_reference key) const
1368+
{
1369+
return find(key) != end();
1370+
}
1371+
13641372
protected:
13651373

13661374
//*********************************************************************

include/etl/unordered_multimap.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,14 @@ namespace etl
12161216
}
12171217
#endif
12181218

1219+
//*************************************************************************
1220+
/// Check if the unordered_multimap contains the key.
1221+
//*************************************************************************
1222+
bool contains(const_key_reference key) const
1223+
{
1224+
return find(key) != end();
1225+
}
1226+
12191227
protected:
12201228

12211229
//*********************************************************************

include/etl/unordered_multiset.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,14 @@ namespace etl
11941194
}
11951195
#endif
11961196

1197+
//*************************************************************************
1198+
/// Check if the unordered_multiset contains the key.
1199+
//*************************************************************************
1200+
bool contains(key_parameter_t key) const
1201+
{
1202+
return find(key) != end();
1203+
}
1204+
11971205
protected:
11981206

11991207
//*********************************************************************

include/etl/unordered_set.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,14 @@ namespace etl
12141214
}
12151215
#endif
12161216

1217+
//*************************************************************************
1218+
/// Check if the unordered_set contains the key.
1219+
//*************************************************************************
1220+
bool contains(key_parameter_t key) const
1221+
{
1222+
return find(key) != end();
1223+
}
1224+
12171225
protected:
12181226

12191227
//*********************************************************************

test/test_unordered_map.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,5 +1207,16 @@ namespace
12071207
CHECK_TRUE(map1 == map2a);
12081208
CHECK_FALSE(map1 == map2b);
12091209
}
1210+
1211+
//*************************************************************************
1212+
TEST(test_contains)
1213+
{
1214+
DataNDC data(initial_data.begin(), initial_data.end());
1215+
1216+
const char* not_inserted = "ZZ";
1217+
1218+
CHECK(data.contains(std::string(K0)));
1219+
CHECK(!data.contains(std::string(not_inserted)));
1220+
}
12101221
};
12111222
}

test/test_unordered_multimap.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,5 +1068,16 @@ namespace
10681068
CHECK_TRUE(map1 == map2a);
10691069
CHECK_FALSE(map1 == map2b);
10701070
}
1071+
1072+
//*************************************************************************
1073+
TEST(test_contains)
1074+
{
1075+
DataNDC data(initial_data.begin(), initial_data.end());
1076+
1077+
const char* not_inserted = "ZZ";
1078+
1079+
CHECK(data.contains(K0));
1080+
CHECK(!data.contains(not_inserted));
1081+
}
10711082
};
10721083
}

test/test_unordered_multiset.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,5 +943,16 @@ namespace
943943
CHECK_TRUE(set1 == set2a);
944944
CHECK_FALSE(set1 == set2b);
945945
}
946+
947+
//*************************************************************************
948+
TEST(test_contains)
949+
{
950+
DataNDC data(initial_data.begin(), initial_data.end());
951+
952+
NDC not_inserted = NDC("ZZ");
953+
954+
CHECK_TRUE(data.contains(N0));
955+
CHECK_FALSE(data.contains(not_inserted));
956+
}
946957
};
947958
}

test/test_unordered_set.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,5 +909,16 @@ namespace
909909
CHECK_TRUE(set1 == set2a);
910910
CHECK_FALSE(set1 == set2b);
911911
}
912+
913+
//*************************************************************************
914+
TEST(test_contains)
915+
{
916+
DataNDC data(initial_data.begin(), initial_data.end());
917+
918+
NDC not_inserted = NDC("ZZ");
919+
920+
CHECK_TRUE(data.contains(N0));
921+
CHECK_FALSE(data.contains(not_inserted));
922+
}
912923
};
913924
}

0 commit comments

Comments
 (0)