[libcxx-commits] [libcxx] r366170 - 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.

Zoe Carver via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jul 15 20:21:01 PDT 2019


Author: zoecarver
Date: Mon Jul 15 20:21:01 2019
New Revision: 366170

URL: http://llvm.org/viewvc/llvm-project?rev=366170&view=rev
Log:
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.

Added:
    libcxx/trunk/test/std/containers/associative/map/contains.pass.cpp
    libcxx/trunk/test/std/containers/associative/set/contains.pass.cpp
    libcxx/trunk/test/std/containers/unord/unord.map/contains.pass.cpp
    libcxx/trunk/test/std/containers/unord/unord.set/contains.pass.cpp
Modified:
    libcxx/trunk/include/map
    libcxx/trunk/include/set
    libcxx/trunk/include/unordered_map
    libcxx/trunk/include/unordered_set
    libcxx/trunk/www/cxx2a_status.html

Modified: libcxx/trunk/include/map
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/map?rev=366170&r1=366169&r2=366170&view=diff
==============================================================================
--- libcxx/trunk/include/map (original)
+++ libcxx/trunk/include/map Mon Jul 15 20:21:01 2019
@@ -193,8 +193,8 @@ public:
         const_iterator find(const K& x) const;  // C++14
     template<typename K>
       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<typename K>
@@ -407,8 +407,8 @@ public:
         const_iterator find(const K& x) const;  // C++14
     template<typename K>
       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<typename K>
@@ -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);}

Modified: libcxx/trunk/include/set
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/set?rev=366170&r1=366169&r2=366170&view=diff
==============================================================================
--- libcxx/trunk/include/set (original)
+++ libcxx/trunk/include/set Mon Jul 15 20:21:01 2019
@@ -155,9 +155,9 @@ public:
     template<typename K>
         const_iterator find(const K& x) const;  // C++14
     template<typename K>
-      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<typename K>
@@ -354,8 +354,10 @@ public:
         iterator find(const K& x);
     template<typename K>
         const_iterator find(const K& x) const;  // C++14
-
+    template<typename K>
+        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<typename K>
@@ -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);}

Modified: libcxx/trunk/include/unordered_map
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/unordered_map?rev=366170&r1=366169&r2=366170&view=diff
==============================================================================
--- libcxx/trunk/include/unordered_map (original)
+++ libcxx/trunk/include/unordered_map Mon Jul 15 20:21:01 2019
@@ -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<iterator, iterator>             equal_range(const key_type& k);
     pair<const_iterator, const_iterator> 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<iterator, iterator>             equal_range(const key_type& k);
     pair<const_iterator, const_iterator> 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<iterator, iterator>             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<iterator, iterator>             equal_range(const key_type& __k)
         {return __table_.__equal_range_multi(__k);}

Modified: libcxx/trunk/include/unordered_set
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/unordered_set?rev=366170&r1=366169&r2=366170&view=diff
==============================================================================
--- libcxx/trunk/include/unordered_set (original)
+++ libcxx/trunk/include/unordered_set Mon Jul 15 20:21:01 2019
@@ -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<iterator, iterator>             equal_range(const key_type& k);
     pair<const_iterator, const_iterator> 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<iterator, iterator>             equal_range(const key_type& k);
     pair<const_iterator, const_iterator> 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<iterator, iterator>             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<iterator, iterator>             equal_range(const key_type& __k)
         {return __table_.__equal_range_multi(__k);}

Added: libcxx/trunk/test/std/containers/associative/map/contains.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/associative/map/contains.pass.cpp?rev=366170&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/associative/map/contains.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/associative/map/contains.pass.cpp Mon Jul 15 20:21:01 2019
@@ -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 <cassert>
+#include <map>
+
+// <map>
+
+// bool contains(const key_type& x) const;
+
+template <typename T, typename P, typename B, typename... Pairs>
+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::map<char, int>, std::pair<char, int> >(
+            'e', std::make_pair('a', 10), std::make_pair('b', 11),
+            std::make_pair('c', 12), std::make_pair('d', 13));
+
+        test<std::map<char, char>, std::pair<char, char> >(
+            'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
+            std::make_pair('c', 'a'), std::make_pair('d', 'b'));
+
+        test<std::map<int, E>, std::pair<int, E> >(
+            -1, std::make_pair(1, E{}), std::make_pair(2, E{}),
+            std::make_pair(3, E{}), std::make_pair(4, E{}));
+    }
+    {
+        test<std::multimap<char, int>, std::pair<char, int> >(
+            'e', std::make_pair('a', 10), std::make_pair('b', 11),
+            std::make_pair('c', 12), std::make_pair('d', 13));
+
+        test<std::multimap<char, char>, std::pair<char, char> >(
+            'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
+            std::make_pair('c', 'a'), std::make_pair('d', 'b'));
+
+        test<std::multimap<int, E>, std::pair<int, E> >(
+            -1, std::make_pair(1, E{}), std::make_pair(2, E{}),
+            std::make_pair(3, E{}), std::make_pair(4, E{}));
+    }
+
+    return 0;
+}
+

Added: libcxx/trunk/test/std/containers/associative/set/contains.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/associative/set/contains.pass.cpp?rev=366170&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/associative/set/contains.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/associative/set/contains.pass.cpp Mon Jul 15 20:21:01 2019
@@ -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 <cassert>
+#include <set>
+
+// <set>
+
+// bool contains(const key_type& x) const;
+
+template <typename T, typename V, typename B, typename... Vals>
+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<std::set<int>, int>(14, 10, 11, 12, 13);
+        test<std::set<char>, char>('e', 'a', 'b', 'c', 'd');
+    }
+    {
+        test<std::multiset<int>, int>(14, 10, 11, 12, 13);
+        test<std::multiset<char>, char>('e', 'a', 'b', 'c', 'd');
+    }
+
+    return 0;
+}
+

Added: libcxx/trunk/test/std/containers/unord/unord.map/contains.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/unord/unord.map/contains.pass.cpp?rev=366170&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/unord/unord.map/contains.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/unord/unord.map/contains.pass.cpp Mon Jul 15 20:21:01 2019
@@ -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 <cassert>
+#include <unordered_map>
+
+// <unordered_map>
+
+// bool contains(const key_type& x) const;
+
+template <typename T, typename P, typename B, typename... Pairs>
+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::unordered_map<char, int>, std::pair<char, int> >(
+            'e', std::make_pair('a', 10), std::make_pair('b', 11),
+            std::make_pair('c', 12), std::make_pair('d', 13));
+
+        test<std::unordered_map<char, char>, std::pair<char, char> >(
+            'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
+            std::make_pair('c', 'a'), std::make_pair('d', 'b'));
+
+        test<std::unordered_map<int, E>, std::pair<int, E> >(
+            -1, std::make_pair(1, E{}), std::make_pair(2, E{}),
+            std::make_pair(3, E{}), std::make_pair(4, E{}));
+    }
+    {
+        test<std::unordered_multimap<char, int>, std::pair<char, int> >(
+            'e', std::make_pair('a', 10), std::make_pair('b', 11),
+            std::make_pair('c', 12), std::make_pair('d', 13));
+
+        test<std::unordered_multimap<char, char>, std::pair<char, char> >(
+            'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
+            std::make_pair('c', 'a'), std::make_pair('d', 'b'));
+
+        test<std::unordered_multimap<int, E>, std::pair<int, E> >(
+            -1, std::make_pair(1, E{}), std::make_pair(2, E{}),
+            std::make_pair(3, E{}), std::make_pair(4, E{}));
+    }
+
+    return 0;
+}
+

Added: libcxx/trunk/test/std/containers/unord/unord.set/contains.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/unord/unord.set/contains.pass.cpp?rev=366170&view=auto
==============================================================================
--- libcxx/trunk/test/std/containers/unord/unord.set/contains.pass.cpp (added)
+++ libcxx/trunk/test/std/containers/unord/unord.set/contains.pass.cpp Mon Jul 15 20:21:01 2019
@@ -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 <cassert>
+#include <unordered_set>
+
+// <unordered_set>
+
+// bool contains(const key_type& x) const;
+
+template <typename T, typename V, typename B, typename... Vals>
+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<std::unordered_set<int>, int>(14, 10, 11, 12, 13);
+        test<std::unordered_set<char>, char>('e', 'a', 'b', 'c', 'd');
+    }
+    {
+        test<std::unordered_multiset<int>, int>(14, 10, 11, 12, 13);
+        test<std::unordered_multiset<char>, char>('e', 'a', 'b', 'c', 'd');
+    }
+
+    return 0;
+}
+

Modified: libcxx/trunk/www/cxx2a_status.html
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/www/cxx2a_status.html?rev=366170&r1=366169&r2=366170&view=diff
==============================================================================
--- libcxx/trunk/www/cxx2a_status.html (original)
+++ libcxx/trunk/www/cxx2a_status.html Mon Jul 15 20:21:01 2019
@@ -83,7 +83,7 @@
 
   	<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>
 	<tr><td><a href="https://wg21.link/P0019R8">P0019R8</a></td><td>LWG</td><td>Atomic Ref</td><td>Rapperswil</td><td></td><td></td></tr>
-	<tr><td><a href="https://wg21.link/P0458R2">P0458R2</a></td><td>LWG</td><td>Checking for Existence of an Element in Associative Containers</td><td>Rapperswil</td><td></td><td></td></tr>
+	<tr><td><a href="https://wg21.link/P0458R2">P0458R2</a></td><td>LWG</td><td>Checking for Existence of an Element in Associative Containers</td><td>Rapperswil</td><td>Complete</td><td></td></tr>
 	<tr><td><a href="https://wg21.link/P0475R1">P0475R1</a></td><td>LWG</td><td>LWG 2511: guaranteed copy elision for piecewise construction</td><td>Rapperswil</td><td></td><td></td></tr>
 	<tr><td><a href="https://wg21.link/P0476R2">P0476R2</a></td><td>LWG</td><td>Bit-casting object representations</td><td>Rapperswil</td><td></td><td></td></tr>
 	<tr><td><a href="https://wg21.link/P0528R3">P0528R3</a></td><td>CWG</td><td>The Curious Case of Padding Bits, Featuring Atomic Compare-and-Exchange</td><td>Rapperswil</td><td></td><td></td></tr>




More information about the libcxx-commits mailing list