Skip to content

Commit 13f7a1f

Browse files
authored
[libc++] LWG 3987 provide iterator.range access from flat_{map,set} (#137524)
fixes #105309
1 parent ca297cd commit 13f7a1f

File tree

6 files changed

+216
-5
lines changed

6 files changed

+216
-5
lines changed

libcxx/include/flat_map

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ namespace std {
7272
# include <version>
7373

7474
// standard required includes
75+
76+
// [iterator.range]
77+
# include <__iterator/access.h>
78+
# include <__iterator/data.h>
79+
# include <__iterator/empty.h>
80+
# include <__iterator/reverse_access.h>
81+
# include <__iterator/size.h>
82+
83+
// [flat.map.syn]
7584
# include <compare>
7685
# include <initializer_list>
7786

libcxx/include/flat_set

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ namespace std {
6565
# include <version>
6666

6767
// standard required includes
68+
69+
// [iterator.range]
70+
# include <__iterator/access.h>
71+
# include <__iterator/data.h>
72+
# include <__iterator/empty.h>
73+
# include <__iterator/reverse_access.h>
74+
# include <__iterator/size.h>
75+
76+
// [flat.set.syn]
6877
# include <compare>
6978
# include <initializer_list>
7079

libcxx/include/module.modulemap.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ module std [system] {
868868

869869
header "array"
870870
export *
871+
export std.iterator.reverse_iterator
871872
}
872873

873874
module atomic {
@@ -1225,6 +1226,7 @@ module std [system] {
12251226

12261227
header "deque"
12271228
export *
1229+
export std.iterator.reverse_iterator
12281230
}
12291231

12301232
module exception {
@@ -1301,6 +1303,7 @@ module std [system] {
13011303
header "flat_map"
13021304
export *
13031305
export std.algorithm.ranges_sort
1306+
export std.iterator.reverse_iterator
13041307
export std.ranges.zip_view
13051308
export std.tuple
13061309
}
@@ -1320,6 +1323,7 @@ module std [system] {
13201323
export std.flat_map.sorted_equivalent
13211324
export *
13221325
export std.algorithm.ranges_sort
1326+
export std.iterator.reverse_iterator
13231327
export std.ranges.zip_view
13241328
export std.tuple
13251329
}
@@ -1377,6 +1381,7 @@ module std [system] {
13771381
module forward_list {
13781382
header "forward_list"
13791383
export *
1384+
export std.iterator.reverse_iterator
13801385
}
13811386

13821387
module fstream {
@@ -1552,6 +1557,7 @@ module std [system] {
15521557
module list {
15531558
header "list"
15541559
export *
1560+
export std.iterator.reverse_iterator
15551561
}
15561562

15571563
module locale {
@@ -1603,6 +1609,7 @@ module std [system] {
16031609
module fwd { header "__fwd/map.h" }
16041610
header "map"
16051611
export *
1612+
export std.iterator.reverse_iterator
16061613
}
16071614

16081615
module mdspan {
@@ -1949,6 +1956,7 @@ module std [system] {
19491956
module regex {
19501957
header "regex"
19511958
export *
1959+
export std.iterator.reverse_iterator
19521960
}
19531961

19541962
module scoped_allocator {
@@ -1965,6 +1973,7 @@ module std [system] {
19651973
module fwd { header "__fwd/set.h" }
19661974
header "set"
19671975
export *
1976+
export std.iterator.reverse_iterator
19681977
}
19691978

19701979
module shared_mutex {
@@ -1982,6 +1991,7 @@ module std [system] {
19821991

19831992
header "span"
19841993
export *
1994+
export std.iterator.reverse_iterator
19851995
}
19861996

19871997
module sstream {
@@ -2034,13 +2044,15 @@ module std [system] {
20342044

20352045
header "string"
20362046
export *
2047+
export std.iterator.reverse_iterator
20372048
}
20382049

20392050
module string_view {
20402051
module fwd { header "__fwd/string_view.h" }
20412052

20422053
header "string_view"
20432054
export *
2055+
export std.iterator.reverse_iterator
20442056
}
20452057

20462058
module strstream {
@@ -2122,11 +2134,13 @@ module std [system] {
21222134
module unordered_map {
21232135
header "unordered_map"
21242136
export *
2137+
export std.iterator.reverse_iterator
21252138
}
21262139

21272140
module unordered_set {
21282141
header "unordered_set"
21292142
export *
2143+
export std.iterator.reverse_iterator
21302144
}
21312145

21322146
module utility {
@@ -2215,6 +2229,7 @@ module std [system] {
22152229
}
22162230

22172231
header "vector"
2232+
export std.iterator.reverse_iterator
22182233
export *
22192234
}
22202235

libcxx/test/configs/cmake-bridge.cfg.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#
1313

1414
import os, site
15+
import shlex
16+
import sys
1517
site.addsitedir(os.path.join('@LIBCXX_SOURCE_DIR@', 'utils'))
1618
import libcxx.test.format
1719

@@ -32,3 +34,4 @@ config.substitutions.append(('%{lib-dir}', '@LIBCXX_TESTING_INSTALL_PREFIX@/@LIB
3234
config.substitutions.append(('%{module-dir}', '@LIBCXX_TESTING_INSTALL_PREFIX@/@LIBCXX_INSTALL_MODULES_DIR@'))
3335
config.substitutions.append(('%{test-tools-dir}', '@LIBCXX_TEST_TOOLS_PATH@'))
3436
config.substitutions.append(('%{benchmark_flags}', '-I @LIBCXX_BINARY_DIR@/test/benchmarks/google-benchmark/include -L @LIBCXX_BINARY_DIR@/test/benchmarks/google-benchmark/lib -L @LIBCXX_BINARY_DIR@/test/benchmarks/google-benchmark/lib64 -l benchmark'))
37+
config.substitutions.append(("%{python}", shlex.quote(sys.executable)))

libcxx/test/libcxx/lit.local.cfg

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
# In addition to being available via inclusion of the <iterator> header,
10+
# the function templates in [iterator.range] are available when any of the following
11+
# headers are included: <array>, <deque>, <flat_map>, <flat_set>, <forward_list>,
12+
# <list>, <map>, <regex>, <set>, <span>, <string>, <string_view>, <unordered_map>,
13+
# <unordered_set>, <vector>.
14+
15+
# RUN: %{python} %s %{libcxx-dir}/utils
16+
# END.
17+
18+
import sys
19+
20+
sys.path.append(sys.argv[1])
21+
from libcxx.header_information import (
22+
lit_header_restrictions,
23+
lit_header_undeprecations,
24+
Header,
25+
)
26+
27+
headers = list(
28+
map(
29+
Header,
30+
[
31+
"array",
32+
"deque",
33+
"flat_map",
34+
"flat_set",
35+
"forward_list",
36+
"list",
37+
"map",
38+
"regex",
39+
"set",
40+
"span",
41+
"string",
42+
"string_view",
43+
"unordered_map",
44+
"unordered_set",
45+
"vector",
46+
],
47+
)
48+
)
49+
50+
for header in headers:
51+
print(
52+
f"""\
53+
//--- {header}.pass.cpp
54+
{lit_header_restrictions.get(header, '')}
55+
{lit_header_undeprecations.get(header, '')}
56+
// UNSUPPORTED: c++03
57+
58+
#include <{header}>
59+
#include <cassert>
60+
#include <initializer_list>
61+
62+
#include "test_macros.h"
63+
64+
struct Container {{
65+
int a[3] = {{1, 2, 3}};
66+
67+
int* begin() {{ return &a[0]; }}
68+
const int* begin() const {{ return &a[0]; }}
69+
int* rbegin() {{ return &a[2]; }}
70+
const int* rbegin() const {{ return &a[2]; }}
71+
int* end() {{ return &a[3]; }}
72+
const int* end() const {{ return &a[3]; }}
73+
int* rend() {{ return (&a[0]) - 1; }}
74+
const int* rend() const {{ return (&a[0]) - 1; }}
75+
std::size_t size() const {{ return 3; }}
76+
bool empty() const {{ return false; }}
77+
int* data() {{return &a[0]; }}
78+
const int* data() const {{ return &a[0]; }}
79+
}};
80+
81+
int main(int, char**) {{
82+
{{
83+
Container c;
84+
const auto& cc = c;
85+
assert(std::begin(c) == c.begin());
86+
assert(std::begin(cc) == cc.begin());
87+
assert(std::end(c) == c.end());
88+
assert(std::end(cc) == cc.end());
89+
#if TEST_STD_VER >= 14
90+
assert(std::cbegin(c) == cc.begin());
91+
assert(std::cbegin(cc) == cc.begin());
92+
assert(std::cend(c) == cc.end());
93+
assert(std::cend(cc) == cc.end());
94+
assert(std::rbegin(c) == c.rbegin());
95+
assert(std::rbegin(cc) == cc.rbegin());
96+
assert(std::rend(c) == cc.rend());
97+
assert(std::rend(cc) == cc.rend());
98+
assert(std::crbegin(c) == cc.rbegin());
99+
assert(std::crbegin(cc) == cc.rbegin());
100+
assert(std::crend(c) == cc.rend());
101+
assert(std::crend(cc) == cc.rend());
102+
#endif
103+
#if TEST_STD_VER >= 17
104+
assert(std::data(c) == c.data());
105+
assert(std::data(cc) == cc.data());
106+
assert(std::size(cc) == cc.size());
107+
assert(std::empty(cc) == cc.empty());
108+
#endif
109+
#if TEST_STD_VER >= 20
110+
assert(std::ssize(cc) == 3);
111+
#endif
112+
}}
113+
{{
114+
int a[] = {{1, 2, 3}};
115+
const auto& ca = a;
116+
assert(std::begin(a) == &a[0]);
117+
assert(std::begin(ca) == &ca[0]);
118+
assert(std::end(a) == &a[3]);
119+
assert(std::end(ca) == &ca[3]);
120+
#if TEST_STD_VER >= 14
121+
assert(std::cbegin(a) == &a[0]);
122+
assert(std::cbegin(ca) == &ca[0]);
123+
assert(std::cend(a) == &a[3]);
124+
assert(std::cend(ca) == &ca[3]);
125+
assert(std::rbegin(a) == std::reverse_iterator<int*>(std::end(a)));
126+
assert(std::rbegin(ca) == std::reverse_iterator<const int*>(std::end(ca)));
127+
assert(std::rend(a) == std::reverse_iterator<int*>(std::begin(a)));
128+
assert(std::rend(ca) == std::reverse_iterator<const int*>(std::begin(ca)));
129+
assert(std::crbegin(a) == std::reverse_iterator<const int*>(std::end(a)));
130+
assert(std::crbegin(ca) == std::reverse_iterator<const int*>(std::end(ca)));
131+
assert(std::crend(a) == std::reverse_iterator<const int*>(std::begin(a)));
132+
assert(std::crend(ca) == std::reverse_iterator<const int*>(std::begin(ca)));
133+
#endif
134+
#if TEST_STD_VER >= 17
135+
assert(std::size(ca) == 3);
136+
assert(!std::empty(ca));
137+
assert(std::data(a) == &a[0]);
138+
assert(std::data(ca) == &ca[0]);
139+
#endif
140+
#if TEST_STD_VER >= 20
141+
assert(std::ssize(ca) == 3);
142+
#endif
143+
}}
144+
{{
145+
auto il = {{1, 2, 3}};
146+
const auto& cil = il;
147+
assert(std::begin(il) == il.begin());
148+
assert(std::begin(cil) == cil.begin());
149+
assert(std::end(il) == il.end());
150+
assert(std::end(cil) == cil.end());
151+
#if TEST_STD_VER >= 14
152+
assert(std::cbegin(il) == cil.begin());
153+
assert(std::cbegin(cil) == cil.begin());
154+
assert(std::cend(il) == cil.end());
155+
assert(std::cend(cil) == cil.end());
156+
assert(std::rbegin(il) == std::reverse_iterator<const int*>(std::end(il)));
157+
assert(std::rbegin(cil) == std::reverse_iterator<const int*>(std::end(il)));
158+
assert(std::rend(il) == std::reverse_iterator<const int*>(std::begin(il)));
159+
assert(std::rend(cil) == std::reverse_iterator<const int*>(std::begin(il)));
160+
assert(std::crbegin(il) == std::reverse_iterator<const int*>(std::end(il)));
161+
assert(std::crbegin(cil) == std::reverse_iterator<const int*>(std::end(il)));
162+
assert(std::crend(il) == std::reverse_iterator<const int*>(std::begin(il)));
163+
assert(std::crend(cil) == std::reverse_iterator<const int*>(std::begin(il)));
164+
#endif
165+
#if TEST_STD_VER >= 17
166+
assert(std::size(cil) == 3);
167+
assert(!std::empty(cil));
168+
assert(std::data(il) == &*std::begin(il));
169+
assert(std::data(cil) == &*std::begin(il));
170+
#endif
171+
#if TEST_STD_VER >= 20
172+
assert(std::ssize(cil) == 3);
173+
#endif
174+
}}
175+
176+
return 0;
177+
}}
178+
179+
"""
180+
)

0 commit comments

Comments
 (0)