From 8237a106ea618393c4e7d21a81d81a071412819b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 31 May 2019 22:40:29 +0700 Subject: [PATCH] add contains function #25 --- headers/flat.hpp/flat_map.hpp | 12 ++++++++++++ headers/flat.hpp/flat_multimap.hpp | 12 ++++++++++++ headers/flat.hpp/flat_multiset.hpp | 12 ++++++++++++ headers/flat.hpp/flat_set.hpp | 12 ++++++++++++ untests/flat_map_tests.cpp | 6 ++++++ untests/flat_multimap_tests.cpp | 6 ++++++ untests/flat_multiset_tests.cpp | 6 ++++++ untests/flat_set_tests.cpp | 6 ++++++ 8 files changed, 72 insertions(+) diff --git a/headers/flat.hpp/flat_map.hpp b/headers/flat.hpp/flat_map.hpp index 25eb9b3..ac2e635 100644 --- a/headers/flat.hpp/flat_map.hpp +++ b/headers/flat.hpp/flat_map.hpp @@ -508,6 +508,18 @@ namespace flat_hpp : end(); } + bool contains(const key_type& key) const { + return find(key) != end(); + } + + template < typename K > + std::enable_if_t< + detail::is_transparent_v, + bool> + contains(const K& key) const { + return find(key) != end(); + } + std::pair equal_range(const key_type& key) { const base_type& comp = *this; return std::equal_range(begin(), end(), key, comp); diff --git a/headers/flat.hpp/flat_multimap.hpp b/headers/flat.hpp/flat_multimap.hpp index 580435d..a0adff7 100644 --- a/headers/flat.hpp/flat_multimap.hpp +++ b/headers/flat.hpp/flat_multimap.hpp @@ -458,6 +458,18 @@ namespace flat_hpp : end(); } + bool contains(const key_type& key) const { + return find(key) != end(); + } + + template < typename K > + std::enable_if_t< + detail::is_transparent_v, + bool> + contains(const K& key) const { + return find(key) != end(); + } + std::pair equal_range(const key_type& key) { const base_type& comp = *this; return std::equal_range(begin(), end(), key, comp); diff --git a/headers/flat.hpp/flat_multiset.hpp b/headers/flat.hpp/flat_multiset.hpp index ae1fcb1..54e46e1 100644 --- a/headers/flat.hpp/flat_multiset.hpp +++ b/headers/flat.hpp/flat_multiset.hpp @@ -386,6 +386,18 @@ namespace flat_hpp : end(); } + bool contains(const key_type& key) const { + return find(key) != end(); + } + + template < typename K > + std::enable_if_t< + detail::is_transparent_v, + bool> + contains(const K& key) const { + return find(key) != end(); + } + std::pair equal_range(const key_type& key) { return std::equal_range(begin(), end(), key, key_comp()); } diff --git a/headers/flat.hpp/flat_set.hpp b/headers/flat.hpp/flat_set.hpp index 18ca0aa..23244f1 100644 --- a/headers/flat.hpp/flat_set.hpp +++ b/headers/flat.hpp/flat_set.hpp @@ -436,6 +436,18 @@ namespace flat_hpp : end(); } + bool contains(const key_type& key) const { + return find(key) != end(); + } + + template < typename K > + std::enable_if_t< + detail::is_transparent_v, + bool> + contains(const K& key) const { + return find(key) != end(); + } + std::pair equal_range(const key_type& key) { return std::equal_range(begin(), end(), key, key_comp()); } diff --git a/untests/flat_map_tests.cpp b/untests/flat_map_tests.cpp index 487daa0..e5ad3a0 100644 --- a/untests/flat_map_tests.cpp +++ b/untests/flat_map_tests.cpp @@ -421,6 +421,9 @@ TEST_CASE("flat_map") { REQUIRE(my_as_const(s0).find(3) == s0.cbegin() + 2); REQUIRE(s0.find(6) == s0.end()); REQUIRE(my_as_const(s0).find(0) == s0.cend()); + REQUIRE(my_as_const(s0).contains(1)); + REQUIRE(my_as_const(s0).contains(3)); + REQUIRE_FALSE(my_as_const(s0).contains(0)); } { map_t s0{{1,2},{2,3},{3,4},{4,5},{5,6}}; @@ -445,6 +448,9 @@ TEST_CASE("flat_map") { REQUIRE(s0.find(std::string_view("42")) == s0.end()); REQUIRE(my_as_const(s0).find(std::string_view("42")) == s0.cend()); + REQUIRE(my_as_const(s0).contains(std::string_view("hello"))); + REQUIRE_FALSE(my_as_const(s0).contains(std::string_view("42"))); + REQUIRE(my_as_const(s0).count(std::string_view("hello")) == 1); REQUIRE(my_as_const(s0).count(std::string_view("hello_42")) == 0); diff --git a/untests/flat_multimap_tests.cpp b/untests/flat_multimap_tests.cpp index 9a3f90d..4c1559e 100644 --- a/untests/flat_multimap_tests.cpp +++ b/untests/flat_multimap_tests.cpp @@ -423,6 +423,9 @@ TEST_CASE("flat_multimap") { REQUIRE(my_as_const(s0).find(3) == s0.cbegin() + 2); REQUIRE(s0.find(6) == s0.end()); REQUIRE(my_as_const(s0).find(0) == s0.cend()); + REQUIRE(my_as_const(s0).contains(1)); + REQUIRE(my_as_const(s0).contains(3)); + REQUIRE_FALSE(my_as_const(s0).contains(0)); } { map_t s0{{1,2},{2,3},{2,1},{3,4},{4,5},{5,6}}; @@ -447,6 +450,9 @@ TEST_CASE("flat_multimap") { REQUIRE(s0.find(std::string_view("42")) == s0.end()); REQUIRE(my_as_const(s0).find(std::string_view("42")) == s0.cend()); + REQUIRE(my_as_const(s0).contains(std::string_view("hello"))); + REQUIRE_FALSE(my_as_const(s0).contains(std::string_view("42"))); + REQUIRE(my_as_const(s0).count(std::string_view("hello")) == 1); REQUIRE(my_as_const(s0).count(std::string_view("hello_42")) == 0); diff --git a/untests/flat_multiset_tests.cpp b/untests/flat_multiset_tests.cpp index aba9eea..985b1a8 100644 --- a/untests/flat_multiset_tests.cpp +++ b/untests/flat_multiset_tests.cpp @@ -399,6 +399,9 @@ TEST_CASE("flat_multiset") { REQUIRE(my_as_const(s0).find(3) == s0.cbegin() + 2); REQUIRE(s0.find(6) == s0.end()); REQUIRE(my_as_const(s0).find(0) == s0.cend()); + REQUIRE(my_as_const(s0).contains(1)); + REQUIRE(my_as_const(s0).contains(3)); + REQUIRE_FALSE(my_as_const(s0).contains(0)); } { set_t s0{1,2,3,3,4,5}; @@ -423,6 +426,9 @@ TEST_CASE("flat_multiset") { REQUIRE(s0.find(std::string_view("42")) == s0.end()); REQUIRE(my_as_const(s0).find(std::string_view("42")) == s0.cend()); + REQUIRE(my_as_const(s0).contains(std::string_view("hello"))); + REQUIRE_FALSE(my_as_const(s0).contains(std::string_view("42"))); + REQUIRE(my_as_const(s0).count(std::string_view("hello")) == 1); REQUIRE(my_as_const(s0).count(std::string_view("hello_42")) == 0); diff --git a/untests/flat_set_tests.cpp b/untests/flat_set_tests.cpp index ebbb99d..27866c9 100644 --- a/untests/flat_set_tests.cpp +++ b/untests/flat_set_tests.cpp @@ -397,6 +397,9 @@ TEST_CASE("flat_set") { REQUIRE(my_as_const(s0).find(3) == s0.cbegin() + 2); REQUIRE(s0.find(6) == s0.end()); REQUIRE(my_as_const(s0).find(0) == s0.cend()); + REQUIRE(my_as_const(s0).contains(1)); + REQUIRE(my_as_const(s0).contains(3)); + REQUIRE_FALSE(my_as_const(s0).contains(0)); } { set_t s0{1,2,3,4,5}; @@ -421,6 +424,9 @@ TEST_CASE("flat_set") { REQUIRE(s0.find(std::string_view("42")) == s0.end()); REQUIRE(my_as_const(s0).find(std::string_view("42")) == s0.cend()); + REQUIRE(my_as_const(s0).contains(std::string_view("hello"))); + REQUIRE_FALSE(my_as_const(s0).contains(std::string_view("42"))); + REQUIRE(my_as_const(s0).count(std::string_view("hello")) == 1); REQUIRE(my_as_const(s0).count(std::string_view("hello_42")) == 0);