Skip to content

Commit

Permalink
Add contains() method to etl::unordered_map and etl::unordered_set (#990
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
adejewski authored Dec 19, 2024
1 parent 99d7537 commit 602261c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/etl/unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,14 @@ namespace etl
}
#endif

//*************************************************************************
/// Check if the unordered_map contains the key.
//*************************************************************************
bool contains(const_key_reference key) const
{
return find(key) != end();
}

protected:

//*********************************************************************
Expand Down
8 changes: 8 additions & 0 deletions include/etl/unordered_multimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,14 @@ namespace etl
}
#endif

//*************************************************************************
/// Check if the unordered_multimap contains the key.
//*************************************************************************
bool contains(const_key_reference key) const
{
return find(key) != end();
}

protected:

//*********************************************************************
Expand Down
8 changes: 8 additions & 0 deletions include/etl/unordered_multiset.h
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,14 @@ namespace etl
}
#endif

//*************************************************************************
/// Check if the unordered_multiset contains the key.
//*************************************************************************
bool contains(key_parameter_t key) const
{
return find(key) != end();
}

protected:

//*********************************************************************
Expand Down
8 changes: 8 additions & 0 deletions include/etl/unordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,14 @@ namespace etl
}
#endif

//*************************************************************************
/// Check if the unordered_set contains the key.
//*************************************************************************
bool contains(key_parameter_t key) const
{
return find(key) != end();
}

protected:

//*********************************************************************
Expand Down
11 changes: 11 additions & 0 deletions test/test_unordered_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,5 +1207,16 @@ namespace
CHECK_TRUE(map1 == map2a);
CHECK_FALSE(map1 == map2b);
}

//*************************************************************************
TEST(test_contains)
{
DataNDC data(initial_data.begin(), initial_data.end());

const char* not_inserted = "ZZ";

CHECK(data.contains(std::string(K0)));
CHECK(!data.contains(std::string(not_inserted)));
}
};
}
11 changes: 11 additions & 0 deletions test/test_unordered_multimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,5 +1068,16 @@ namespace
CHECK_TRUE(map1 == map2a);
CHECK_FALSE(map1 == map2b);
}

//*************************************************************************
TEST(test_contains)
{
DataNDC data(initial_data.begin(), initial_data.end());

const char* not_inserted = "ZZ";

CHECK(data.contains(K0));
CHECK(!data.contains(not_inserted));
}
};
}
11 changes: 11 additions & 0 deletions test/test_unordered_multiset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,5 +943,16 @@ namespace
CHECK_TRUE(set1 == set2a);
CHECK_FALSE(set1 == set2b);
}

//*************************************************************************
TEST(test_contains)
{
DataNDC data(initial_data.begin(), initial_data.end());

NDC not_inserted = NDC("ZZ");

CHECK_TRUE(data.contains(N0));
CHECK_FALSE(data.contains(not_inserted));
}
};
}
11 changes: 11 additions & 0 deletions test/test_unordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,5 +909,16 @@ namespace
CHECK_TRUE(set1 == set2a);
CHECK_FALSE(set1 == set2b);
}

//*************************************************************************
TEST(test_contains)
{
DataNDC data(initial_data.begin(), initial_data.end());

NDC not_inserted = NDC("ZZ");

CHECK_TRUE(data.contains(N0));
CHECK_FALSE(data.contains(not_inserted));
}
};
}

0 comments on commit 602261c

Please sign in to comment.