Skip to content

Commit a17b1ae

Browse files
committed
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
1 parent e7e8789 commit a17b1ae

File tree

9 files changed

+263
-6
lines changed

9 files changed

+263
-6
lines changed

libcxx/include/map

+14-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ public:
193193
const_iterator find(const K& x) const; // C++14
194194
template<typename K>
195195
size_type count(const K& x) const; // C++14
196-
197196
size_type count(const key_type& k) const;
197+
bool contains(const key_type& x) const; // C++20
198198
iterator lower_bound(const key_type& k);
199199
const_iterator lower_bound(const key_type& k) const;
200200
template<typename K>
@@ -407,8 +407,8 @@ public:
407407
const_iterator find(const K& x) const; // C++14
408408
template<typename K>
409409
size_type count(const K& x) const; // C++14
410-
411410
size_type count(const key_type& k) const;
411+
bool contains(const key_type& x) const; // C++20
412412
iterator lower_bound(const key_type& k);
413413
const_iterator lower_bound(const key_type& k) const;
414414
template<typename K>
@@ -1398,6 +1398,12 @@ public:
13981398
typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
13991399
count(const _K2& __k) const {return __tree_.__count_multi(__k);}
14001400
#endif
1401+
1402+
#if _LIBCPP_STD_VER > 17
1403+
_LIBCPP_INLINE_VISIBILITY
1404+
bool contains(const key_type& __k) const {return find(__k) != end();}
1405+
#endif // _LIBCPP_STD_VER > 17
1406+
14011407
_LIBCPP_INLINE_VISIBILITY
14021408
iterator lower_bound(const key_type& __k)
14031409
{return __tree_.lower_bound(__k);}
@@ -2055,6 +2061,12 @@ public:
20552061
typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
20562062
count(const _K2& __k) const {return __tree_.__count_multi(__k);}
20572063
#endif
2064+
2065+
#if _LIBCPP_STD_VER > 17
2066+
_LIBCPP_INLINE_VISIBILITY
2067+
bool contains(const key_type& __k) const {return find(__k) != end();}
2068+
#endif // _LIBCPP_STD_VER > 17
2069+
20582070
_LIBCPP_INLINE_VISIBILITY
20592071
iterator lower_bound(const key_type& __k)
20602072
{return __tree_.lower_bound(__k);}

libcxx/include/set

+16-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ public:
155155
template<typename K>
156156
const_iterator find(const K& x) const; // C++14
157157
template<typename K>
158-
size_type count(const K& x) const; // C++14
159-
158+
size_type count(const K& x) const; // C++14
160159
size_type count(const key_type& k) const;
160+
bool contains(const key_type& x) const; // C++20
161161
iterator lower_bound(const key_type& k);
162162
const_iterator lower_bound(const key_type& k) const;
163163
template<typename K>
@@ -354,8 +354,10 @@ public:
354354
iterator find(const K& x);
355355
template<typename K>
356356
const_iterator find(const K& x) const; // C++14
357-
357+
template<typename K>
358+
size_type count(const K& x) const; // C++14
358359
size_type count(const key_type& k) const;
360+
bool contains(const key_type& x) const; // C++20
359361
iterator lower_bound(const key_type& k);
360362
const_iterator lower_bound(const key_type& k) const;
361363
template<typename K>
@@ -787,6 +789,12 @@ public:
787789
typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
788790
count(const _K2& __k) const {return __tree_.__count_multi(__k);}
789791
#endif
792+
793+
#if _LIBCPP_STD_VER > 17
794+
_LIBCPP_INLINE_VISIBILITY
795+
bool contains(const key_type& __k) const {return find(__k) != end();}
796+
#endif // _LIBCPP_STD_VER > 17
797+
790798
_LIBCPP_INLINE_VISIBILITY
791799
iterator lower_bound(const key_type& __k)
792800
{return __tree_.lower_bound(__k);}
@@ -1307,6 +1315,11 @@ public:
13071315
count(const _K2& __k) const {return __tree_.__count_multi(__k);}
13081316
#endif
13091317

1318+
#if _LIBCPP_STD_VER > 17
1319+
_LIBCPP_INLINE_VISIBILITY
1320+
bool contains(const key_type& __k) const {return find(__k) != end();}
1321+
#endif // _LIBCPP_STD_VER > 17
1322+
13101323
_LIBCPP_INLINE_VISIBILITY
13111324
iterator lower_bound(const key_type& __k)
13121325
{return __tree_.lower_bound(__k);}

libcxx/include/unordered_map

+10
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public:
174174
iterator find(const key_type& k);
175175
const_iterator find(const key_type& k) const;
176176
size_type count(const key_type& k) const;
177+
bool contains(const key_type& k) const; // C++20
177178
pair<iterator, iterator> equal_range(const key_type& k);
178179
pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
179180
@@ -355,6 +356,7 @@ public:
355356
iterator find(const key_type& k);
356357
const_iterator find(const key_type& k) const;
357358
size_type count(const key_type& k) const;
359+
bool contains(const key_type& k) const; // C++20
358360
pair<iterator, iterator> equal_range(const key_type& k);
359361
pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
360362
@@ -1278,6 +1280,10 @@ public:
12781280
const_iterator find(const key_type& __k) const {return __table_.find(__k);}
12791281
_LIBCPP_INLINE_VISIBILITY
12801282
size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
1283+
#if _LIBCPP_STD_VER > 17
1284+
_LIBCPP_INLINE_VISIBILITY
1285+
bool contains(const key_type& __k) const {return find(__k) != end();}
1286+
#endif // _LIBCPP_STD_VER > 17
12811287
_LIBCPP_INLINE_VISIBILITY
12821288
pair<iterator, iterator> equal_range(const key_type& __k)
12831289
{return __table_.__equal_range_unique(__k);}
@@ -2049,6 +2055,10 @@ public:
20492055
const_iterator find(const key_type& __k) const {return __table_.find(__k);}
20502056
_LIBCPP_INLINE_VISIBILITY
20512057
size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
2058+
#if _LIBCPP_STD_VER > 17
2059+
_LIBCPP_INLINE_VISIBILITY
2060+
bool contains(const key_type& __k) const {return find(__k) != end();}
2061+
#endif // _LIBCPP_STD_VER > 17
20522062
_LIBCPP_INLINE_VISIBILITY
20532063
pair<iterator, iterator> equal_range(const key_type& __k)
20542064
{return __table_.__equal_range_multi(__k);}

libcxx/include/unordered_set

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public:
146146
iterator find(const key_type& k);
147147
const_iterator find(const key_type& k) const;
148148
size_type count(const key_type& k) const;
149+
bool contains(const key_type& k) const; // C++20
149150
pair<iterator, iterator> equal_range(const key_type& k);
150151
pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
151152
@@ -310,6 +311,7 @@ public:
310311
iterator find(const key_type& k);
311312
const_iterator find(const key_type& k) const;
312313
size_type count(const key_type& k) const;
314+
bool contains(const key_type& k) const; // C++20
313315
pair<iterator, iterator> equal_range(const key_type& k);
314316
pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
315317
@@ -677,6 +679,10 @@ public:
677679
const_iterator find(const key_type& __k) const {return __table_.find(__k);}
678680
_LIBCPP_INLINE_VISIBILITY
679681
size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
682+
#if _LIBCPP_STD_VER > 17
683+
_LIBCPP_INLINE_VISIBILITY
684+
bool contains(const key_type& __k) const {return find(__k) != end();}
685+
#endif // _LIBCPP_STD_VER > 17
680686
_LIBCPP_INLINE_VISIBILITY
681687
pair<iterator, iterator> equal_range(const key_type& __k)
682688
{return __table_.__equal_range_unique(__k);}
@@ -1304,6 +1310,10 @@ public:
13041310
const_iterator find(const key_type& __k) const {return __table_.find(__k);}
13051311
_LIBCPP_INLINE_VISIBILITY
13061312
size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
1313+
#if _LIBCPP_STD_VER > 17
1314+
_LIBCPP_INLINE_VISIBILITY
1315+
bool contains(const key_type& __k) const {return find(__k) != end();}
1316+
#endif // _LIBCPP_STD_VER > 17
13071317
_LIBCPP_INLINE_VISIBILITY
13081318
pair<iterator, iterator> equal_range(const key_type& __k)
13091319
{return __table_.__equal_range_multi(__k);}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10+
11+
#include <cassert>
12+
#include <map>
13+
14+
// <map>
15+
16+
// bool contains(const key_type& x) const;
17+
18+
template <typename T, typename P, typename B, typename... Pairs>
19+
void test(B bad, Pairs... args) {
20+
T map;
21+
P pairs[] = {args...};
22+
23+
for (auto& p : pairs) map.insert(p);
24+
for (auto& p : pairs) assert(map.contains(p.first));
25+
26+
assert(!map.contains(bad));
27+
}
28+
29+
struct E { int a = 1; double b = 1; char c = 1; };
30+
31+
int main(int, char**)
32+
{
33+
{
34+
test<std::map<char, int>, std::pair<char, int> >(
35+
'e', std::make_pair('a', 10), std::make_pair('b', 11),
36+
std::make_pair('c', 12), std::make_pair('d', 13));
37+
38+
test<std::map<char, char>, std::pair<char, char> >(
39+
'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
40+
std::make_pair('c', 'a'), std::make_pair('d', 'b'));
41+
42+
test<std::map<int, E>, std::pair<int, E> >(
43+
-1, std::make_pair(1, E{}), std::make_pair(2, E{}),
44+
std::make_pair(3, E{}), std::make_pair(4, E{}));
45+
}
46+
{
47+
test<std::multimap<char, int>, std::pair<char, int> >(
48+
'e', std::make_pair('a', 10), std::make_pair('b', 11),
49+
std::make_pair('c', 12), std::make_pair('d', 13));
50+
51+
test<std::multimap<char, char>, std::pair<char, char> >(
52+
'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
53+
std::make_pair('c', 'a'), std::make_pair('d', 'b'));
54+
55+
test<std::multimap<int, E>, std::pair<int, E> >(
56+
-1, std::make_pair(1, E{}), std::make_pair(2, E{}),
57+
std::make_pair(3, E{}), std::make_pair(4, E{}));
58+
}
59+
60+
return 0;
61+
}
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10+
11+
#include <cassert>
12+
#include <set>
13+
14+
// <set>
15+
16+
// bool contains(const key_type& x) const;
17+
18+
template <typename T, typename V, typename B, typename... Vals>
19+
void test(B bad, Vals... args) {
20+
T set;
21+
V vals[] = {args...};
22+
23+
for (auto& v : vals) set.insert(v);
24+
for (auto& v : vals) assert(set.contains(v));
25+
26+
assert(!set.contains(bad));
27+
}
28+
29+
struct E { int a = 1; double b = 1; char c = 1; };
30+
31+
int main(int, char**)
32+
{
33+
{
34+
test<std::set<int>, int>(14, 10, 11, 12, 13);
35+
test<std::set<char>, char>('e', 'a', 'b', 'c', 'd');
36+
}
37+
{
38+
test<std::multiset<int>, int>(14, 10, 11, 12, 13);
39+
test<std::multiset<char>, char>('e', 'a', 'b', 'c', 'd');
40+
}
41+
42+
return 0;
43+
}
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10+
11+
#include <cassert>
12+
#include <unordered_map>
13+
14+
// <unordered_map>
15+
16+
// bool contains(const key_type& x) const;
17+
18+
template <typename T, typename P, typename B, typename... Pairs>
19+
void test(B bad, Pairs... args) {
20+
T map;
21+
P pairs[] = {args...};
22+
23+
for (auto& p : pairs) map.insert(p);
24+
for (auto& p : pairs) assert(map.contains(p.first));
25+
26+
assert(!map.contains(bad));
27+
}
28+
29+
struct E { int a = 1; double b = 1; char c = 1; };
30+
31+
int main(int, char**)
32+
{
33+
{
34+
test<std::unordered_map<char, int>, std::pair<char, int> >(
35+
'e', std::make_pair('a', 10), std::make_pair('b', 11),
36+
std::make_pair('c', 12), std::make_pair('d', 13));
37+
38+
test<std::unordered_map<char, char>, std::pair<char, char> >(
39+
'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
40+
std::make_pair('c', 'a'), std::make_pair('d', 'b'));
41+
42+
test<std::unordered_map<int, E>, std::pair<int, E> >(
43+
-1, std::make_pair(1, E{}), std::make_pair(2, E{}),
44+
std::make_pair(3, E{}), std::make_pair(4, E{}));
45+
}
46+
{
47+
test<std::unordered_multimap<char, int>, std::pair<char, int> >(
48+
'e', std::make_pair('a', 10), std::make_pair('b', 11),
49+
std::make_pair('c', 12), std::make_pair('d', 13));
50+
51+
test<std::unordered_multimap<char, char>, std::pair<char, char> >(
52+
'e', std::make_pair('a', 'a'), std::make_pair('b', 'a'),
53+
std::make_pair('c', 'a'), std::make_pair('d', 'b'));
54+
55+
test<std::unordered_multimap<int, E>, std::pair<int, E> >(
56+
-1, std::make_pair(1, E{}), std::make_pair(2, E{}),
57+
std::make_pair(3, E{}), std::make_pair(4, E{}));
58+
}
59+
60+
return 0;
61+
}
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10+
11+
#include <cassert>
12+
#include <unordered_set>
13+
14+
// <unordered_set>
15+
16+
// bool contains(const key_type& x) const;
17+
18+
template <typename T, typename V, typename B, typename... Vals>
19+
void test(B bad, Vals... args) {
20+
T set;
21+
V vals[] = {args...};
22+
23+
for (auto& v : vals) set.insert(v);
24+
for (auto& v : vals) assert(set.contains(v));
25+
26+
assert(!set.contains(bad));
27+
}
28+
29+
struct E { int a = 1; double b = 1; char c = 1; };
30+
31+
int main(int, char**)
32+
{
33+
{
34+
test<std::unordered_set<int>, int>(14, 10, 11, 12, 13);
35+
test<std::unordered_set<char>, char>('e', 'a', 'b', 'c', 'd');
36+
}
37+
{
38+
test<std::unordered_multiset<int>, int>(14, 10, 11, 12, 13);
39+
test<std::unordered_multiset<char>, char>('e', 'a', 'b', 'c', 'd');
40+
}
41+
42+
return 0;
43+
}
44+

libcxx/www/cxx2a_status.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ <h3>Paper Status</h3>
8383

8484
<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>
8585
<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>
86-
<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>
86+
<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>
8787
<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>
8888
<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>
8989
<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>

0 commit comments

Comments
 (0)