From a17b1aed6ab205515adc31d19e953635e563e5c4 Mon Sep 17 00:00:00 2001 From: Zoe Carver Date: Tue, 16 Jul 2019 03:21:01 +0000 Subject: [PATCH] Add contains method to associative containers. This patch implements P0458R2, adding contains to map, multimap, unordered_map, unordered_multimap, set, multiset, unordered_set, and unordered_multiset. llvm-svn: 366170 --- libcxx/include/map | 16 +++++- libcxx/include/set | 19 +++++-- libcxx/include/unordered_map | 10 ++++ libcxx/include/unordered_set | 10 ++++ .../containers/associative/map/contains.pass.cpp | 62 ++++++++++++++++++++++ .../containers/associative/set/contains.pass.cpp | 44 +++++++++++++++ .../containers/unord/unord.map/contains.pass.cpp | 62 ++++++++++++++++++++++ .../containers/unord/unord.set/contains.pass.cpp | 44 +++++++++++++++ libcxx/www/cxx2a_status.html | 2 +- 9 files changed, 263 insertions(+), 6 deletions(-) create mode 100644 libcxx/test/std/containers/associative/map/contains.pass.cpp create mode 100644 libcxx/test/std/containers/associative/set/contains.pass.cpp create mode 100644 libcxx/test/std/containers/unord/unord.map/contains.pass.cpp create mode 100644 libcxx/test/std/containers/unord/unord.set/contains.pass.cpp diff --git a/libcxx/include/map b/libcxx/include/map index 6805a51..eb6ae57 100644 --- a/libcxx/include/map +++ b/libcxx/include/map @@ -193,8 +193,8 @@ public: const_iterator find(const K& x) const; // C++14 template size_type count(const K& x) const; // C++14 - size_type count(const key_type& k) const; + bool contains(const key_type& x) const; // C++20 iterator lower_bound(const key_type& k); const_iterator lower_bound(const key_type& k) const; template @@ -407,8 +407,8 @@ public: const_iterator find(const K& x) const; // C++14 template size_type count(const K& x) const; // C++14 - size_type count(const key_type& k) const; + bool contains(const key_type& x) const; // C++20 iterator lower_bound(const key_type& k); const_iterator lower_bound(const key_type& k) const; template @@ -1398,6 +1398,12 @@ public: typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type count(const _K2& __k) const {return __tree_.__count_multi(__k);} #endif + +#if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} +#endif // _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY iterator lower_bound(const key_type& __k) {return __tree_.lower_bound(__k);} @@ -2055,6 +2061,12 @@ public: typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type count(const _K2& __k) const {return __tree_.__count_multi(__k);} #endif + +#if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} +#endif // _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY iterator lower_bound(const key_type& __k) {return __tree_.lower_bound(__k);} diff --git a/libcxx/include/set b/libcxx/include/set index 79e8f29..70ab4d3 100644 --- a/libcxx/include/set +++ b/libcxx/include/set @@ -155,9 +155,9 @@ public: template const_iterator find(const K& x) const; // C++14 template - size_type count(const K& x) const; // C++14 - + size_type count(const K& x) const; // C++14 size_type count(const key_type& k) const; + bool contains(const key_type& x) const; // C++20 iterator lower_bound(const key_type& k); const_iterator lower_bound(const key_type& k) const; template @@ -354,8 +354,10 @@ public: iterator find(const K& x); template const_iterator find(const K& x) const; // C++14 - + template + size_type count(const K& x) const; // C++14 size_type count(const key_type& k) const; + bool contains(const key_type& x) const; // C++20 iterator lower_bound(const key_type& k); const_iterator lower_bound(const key_type& k) const; template @@ -787,6 +789,12 @@ public: typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type count(const _K2& __k) const {return __tree_.__count_multi(__k);} #endif + +#if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} +#endif // _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY iterator lower_bound(const key_type& __k) {return __tree_.lower_bound(__k);} @@ -1307,6 +1315,11 @@ public: count(const _K2& __k) const {return __tree_.__count_multi(__k);} #endif +#if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} +#endif // _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY iterator lower_bound(const key_type& __k) {return __tree_.lower_bound(__k);} diff --git a/libcxx/include/unordered_map b/libcxx/include/unordered_map index 63aecc8..ad17f77 100644 --- a/libcxx/include/unordered_map +++ b/libcxx/include/unordered_map @@ -174,6 +174,7 @@ public: iterator find(const key_type& k); const_iterator find(const key_type& k) const; size_type count(const key_type& k) const; + bool contains(const key_type& k) const; // C++20 pair equal_range(const key_type& k); pair equal_range(const key_type& k) const; @@ -355,6 +356,7 @@ public: iterator find(const key_type& k); const_iterator find(const key_type& k) const; size_type count(const key_type& k) const; + bool contains(const key_type& k) const; // C++20 pair equal_range(const key_type& k); pair equal_range(const key_type& k) const; @@ -1278,6 +1280,10 @@ public: const_iterator find(const key_type& __k) const {return __table_.find(__k);} _LIBCPP_INLINE_VISIBILITY size_type count(const key_type& __k) const {return __table_.__count_unique(__k);} + #if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} + #endif // _LIBCPP_STD_VER > 17 _LIBCPP_INLINE_VISIBILITY pair equal_range(const key_type& __k) {return __table_.__equal_range_unique(__k);} @@ -2049,6 +2055,10 @@ public: const_iterator find(const key_type& __k) const {return __table_.find(__k);} _LIBCPP_INLINE_VISIBILITY size_type count(const key_type& __k) const {return __table_.__count_multi(__k);} + #if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} + #endif // _LIBCPP_STD_VER > 17 _LIBCPP_INLINE_VISIBILITY pair equal_range(const key_type& __k) {return __table_.__equal_range_multi(__k);} diff --git a/libcxx/include/unordered_set b/libcxx/include/unordered_set index 4a9f030..68f777a 100644 --- a/libcxx/include/unordered_set +++ b/libcxx/include/unordered_set @@ -146,6 +146,7 @@ public: iterator find(const key_type& k); const_iterator find(const key_type& k) const; size_type count(const key_type& k) const; + bool contains(const key_type& k) const; // C++20 pair equal_range(const key_type& k); pair equal_range(const key_type& k) const; @@ -310,6 +311,7 @@ public: iterator find(const key_type& k); const_iterator find(const key_type& k) const; size_type count(const key_type& k) const; + bool contains(const key_type& k) const; // C++20 pair equal_range(const key_type& k); pair equal_range(const key_type& k) const; @@ -677,6 +679,10 @@ public: const_iterator find(const key_type& __k) const {return __table_.find(__k);} _LIBCPP_INLINE_VISIBILITY size_type count(const key_type& __k) const {return __table_.__count_unique(__k);} + #if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} + #endif // _LIBCPP_STD_VER > 17 _LIBCPP_INLINE_VISIBILITY pair equal_range(const key_type& __k) {return __table_.__equal_range_unique(__k);} @@ -1304,6 +1310,10 @@ public: const_iterator find(const key_type& __k) const {return __table_.find(__k);} _LIBCPP_INLINE_VISIBILITY size_type count(const key_type& __k) const {return __table_.__count_multi(__k);} + #if _LIBCPP_STD_VER > 17 + _LIBCPP_INLINE_VISIBILITY + bool contains(const key_type& __k) const {return find(__k) != end();} + #endif // _LIBCPP_STD_VER > 17 _LIBCPP_INLINE_VISIBILITY pair equal_range(const key_type& __k) {return __table_.__equal_range_multi(__k);} diff --git a/libcxx/test/std/containers/associative/map/contains.pass.cpp b/libcxx/test/std/containers/associative/map/contains.pass.cpp new file mode 100644 index 0000000..5b71eed --- /dev/null +++ b/libcxx/test/std/containers/associative/map/contains.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +#include +#include + +// + +// bool contains(const key_type& x) const; + +template +void test(B bad, Pairs... args) { + T map; + P pairs[] = {args...}; + + for (auto& p : pairs) map.insert(p); + for (auto& p : pairs) assert(map.contains(p.first)); + + assert(!map.contains(bad)); +} + +struct E { int a = 1; double b = 1; char c = 1; }; + +int main(int, char**) +{ + { + test, std::pair >( + 'e', std::make_pair('a', 10), std::make_pair('b', 11), + std::make_pair('c', 12), std::make_pair('d', 13)); + + test, std::pair >( + 'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'), + std::make_pair('c', 'a'), std::make_pair('d', 'b')); + + test, std::pair >( + -1, std::make_pair(1, E{}), std::make_pair(2, E{}), + std::make_pair(3, E{}), std::make_pair(4, E{})); + } + { + test, std::pair >( + 'e', std::make_pair('a', 10), std::make_pair('b', 11), + std::make_pair('c', 12), std::make_pair('d', 13)); + + test, std::pair >( + 'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'), + std::make_pair('c', 'a'), std::make_pair('d', 'b')); + + test, std::pair >( + -1, std::make_pair(1, E{}), std::make_pair(2, E{}), + std::make_pair(3, E{}), std::make_pair(4, E{})); + } + + return 0; +} + diff --git a/libcxx/test/std/containers/associative/set/contains.pass.cpp b/libcxx/test/std/containers/associative/set/contains.pass.cpp new file mode 100644 index 0000000..2b09729 --- /dev/null +++ b/libcxx/test/std/containers/associative/set/contains.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +#include +#include + +// + +// bool contains(const key_type& x) const; + +template +void test(B bad, Vals... args) { + T set; + V vals[] = {args...}; + + for (auto& v : vals) set.insert(v); + for (auto& v : vals) assert(set.contains(v)); + + assert(!set.contains(bad)); +} + +struct E { int a = 1; double b = 1; char c = 1; }; + +int main(int, char**) +{ + { + test, int>(14, 10, 11, 12, 13); + test, char>('e', 'a', 'b', 'c', 'd'); + } + { + test, int>(14, 10, 11, 12, 13); + test, char>('e', 'a', 'b', 'c', 'd'); + } + + return 0; +} + diff --git a/libcxx/test/std/containers/unord/unord.map/contains.pass.cpp b/libcxx/test/std/containers/unord/unord.map/contains.pass.cpp new file mode 100644 index 0000000..c591e19 --- /dev/null +++ b/libcxx/test/std/containers/unord/unord.map/contains.pass.cpp @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +#include +#include + +// + +// bool contains(const key_type& x) const; + +template +void test(B bad, Pairs... args) { + T map; + P pairs[] = {args...}; + + for (auto& p : pairs) map.insert(p); + for (auto& p : pairs) assert(map.contains(p.first)); + + assert(!map.contains(bad)); +} + +struct E { int a = 1; double b = 1; char c = 1; }; + +int main(int, char**) +{ + { + test, std::pair >( + 'e', std::make_pair('a', 10), std::make_pair('b', 11), + std::make_pair('c', 12), std::make_pair('d', 13)); + + test, std::pair >( + 'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'), + std::make_pair('c', 'a'), std::make_pair('d', 'b')); + + test, std::pair >( + -1, std::make_pair(1, E{}), std::make_pair(2, E{}), + std::make_pair(3, E{}), std::make_pair(4, E{})); + } + { + test, std::pair >( + 'e', std::make_pair('a', 10), std::make_pair('b', 11), + std::make_pair('c', 12), std::make_pair('d', 13)); + + test, std::pair >( + 'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'), + std::make_pair('c', 'a'), std::make_pair('d', 'b')); + + test, std::pair >( + -1, std::make_pair(1, E{}), std::make_pair(2, E{}), + std::make_pair(3, E{}), std::make_pair(4, E{})); + } + + return 0; +} + diff --git a/libcxx/test/std/containers/unord/unord.set/contains.pass.cpp b/libcxx/test/std/containers/unord/unord.set/contains.pass.cpp new file mode 100644 index 0000000..3b87f2f --- /dev/null +++ b/libcxx/test/std/containers/unord/unord.set/contains.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +#include +#include + +// + +// bool contains(const key_type& x) const; + +template +void test(B bad, Vals... args) { + T set; + V vals[] = {args...}; + + for (auto& v : vals) set.insert(v); + for (auto& v : vals) assert(set.contains(v)); + + assert(!set.contains(bad)); +} + +struct E { int a = 1; double b = 1; char c = 1; }; + +int main(int, char**) +{ + { + test, int>(14, 10, 11, 12, 13); + test, char>('e', 'a', 'b', 'c', 'd'); + } + { + test, int>(14, 10, 11, 12, 13); + test, char>('e', 'a', 'b', 'c', 'd'); + } + + return 0; +} + diff --git a/libcxx/www/cxx2a_status.html b/libcxx/www/cxx2a_status.html index 2b19eb8..9489c07 100644 --- a/libcxx/www/cxx2a_status.html +++ b/libcxx/www/cxx2a_status.html @@ -83,7 +83,7 @@ P0019R8LWGAtomic RefRapperswil - P0458R2LWGChecking for Existence of an Element in Associative ContainersRapperswil + P0458R2LWGChecking for Existence of an Element in Associative ContainersRapperswilComplete P0475R1LWGLWG 2511: guaranteed copy elision for piecewise constructionRapperswil P0476R2LWGBit-casting object representationsRapperswil P0528R3CWGThe Curious Case of Padding Bits, Featuring Atomic Compare-and-ExchangeRapperswil -- 2.7.4