Skip to content

Commit

Permalink
Add contains() method to etl::unordered_map and etl::unordered_set
Browse files Browse the repository at this point in the history
  • Loading branch information
adejewski committed Dec 7, 2024
1 parent 99d7537 commit 3bc0c75
Show file tree
Hide file tree
Showing 4 changed files with 34 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 TKey& 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 @@ -166,6 +166,14 @@ namespace etl
return !(lhs == rhs);
}

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

protected:

typedef etl::intrusive_forward_list<node_t, link_t> bucket_t;
Expand Down
9 changes: 9 additions & 0 deletions test/test_unordered_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,5 +1207,14 @@ namespace
CHECK_TRUE(map1 == map2a);
CHECK_FALSE(map1 == map2b);
}

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

CHECK(data.contains(std::string("FF")));
CHECK(!data.contains(std::string("ZZ")));
}
};
}
9 changes: 9 additions & 0 deletions test/test_unordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,5 +909,14 @@ namespace
CHECK_TRUE(set1 == set2a);
CHECK_FALSE(set1 == set2b);
}

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

CHECK_TRUE(data.contains(NDC("FF")));
CHECK_FALSE(data.contains(NDC("ZZ")));
}
};
}

0 comments on commit 3bc0c75

Please sign in to comment.