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// <unordered_set>
10
11// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12// class Alloc = allocator<Value>>
13// class unordered_set
14
15// local_iterator begin (size_type n);
16// local_iterator end (size_type n);
17// const_local_iterator begin (size_type n) const;
18// const_local_iterator end (size_type n) const;
19// const_local_iterator cbegin(size_type n) const;
20// const_local_iterator cend (size_type n) const;
21
22#include <unordered_set>
23#include <cassert>
24
25#include "test_macros.h"
26#include "min_allocator.h"
27
28int main(int, char**)
29{
30 {
31 typedef std::unordered_set<int> C;
32 typedef int P;
33 typedef C::local_iterator I;
34 P a[] =
35 {
36 P(1),
37 P(2),
38 P(3),
39 P(4),
40 P(1),
41 P(2)
42 };
43 C c(a, a + sizeof(a)/sizeof(a[0]));
44 assert(c.bucket_count() >= 5);
45 C::size_type b = c.bucket(key: 0);
46 I i = c.begin(n: b);
47 I j = c.end(n: b);
48 assert(std::distance(i, j) == 0);
49
50 b = c.bucket(key: 1);
51 i = c.begin(n: b);
52 j = c.end(n: b);
53 assert(std::distance(i, j) == 1);
54 assert(*i == 1);
55
56 b = c.bucket(key: 2);
57 i = c.begin(n: b);
58 j = c.end(n: b);
59 assert(std::distance(i, j) == 1);
60 assert(*i == 2);
61
62 b = c.bucket(key: 3);
63 i = c.begin(n: b);
64 j = c.end(n: b);
65 assert(std::distance(i, j) == 1);
66 assert(*i == 3);
67
68 b = c.bucket(key: 4);
69 i = c.begin(n: b);
70 j = c.end(n: b);
71 assert(std::distance(i, j) == 1);
72 assert(*i == 4);
73 }
74 {
75 typedef std::unordered_set<int> C;
76 typedef int P;
77 typedef C::const_local_iterator I;
78 P a[] =
79 {
80 P(1),
81 P(2),
82 P(3),
83 P(4),
84 P(1),
85 P(2)
86 };
87 const C c(a, a + sizeof(a)/sizeof(a[0]));
88 assert(c.bucket_count() >= 5);
89 C::size_type b = c.bucket(key: 0);
90 I i = c.begin(n: b);
91 I j = c.end(n: b);
92 assert(std::distance(i, j) == 0);
93
94 b = c.bucket(key: 1);
95 i = c.begin(n: b);
96 j = c.end(n: b);
97 assert(std::distance(i, j) == 1);
98 assert(*i == 1);
99
100 b = c.bucket(key: 2);
101 i = c.begin(n: b);
102 j = c.end(n: b);
103 assert(std::distance(i, j) == 1);
104 assert(*i == 2);
105
106 b = c.bucket(key: 3);
107 i = c.begin(n: b);
108 j = c.end(n: b);
109 assert(std::distance(i, j) == 1);
110 assert(*i == 3);
111
112 b = c.bucket(key: 4);
113 i = c.begin(n: b);
114 j = c.end(n: b);
115 assert(std::distance(i, j) == 1);
116 assert(*i == 4);
117 }
118 {
119 typedef std::unordered_set<int> C;
120 typedef int P;
121 typedef C::const_local_iterator I;
122 P a[] =
123 {
124 P(1),
125 P(2),
126 P(3),
127 P(4),
128 P(1),
129 P(2)
130 };
131 C c(a, a + sizeof(a)/sizeof(a[0]));
132 assert(c.bucket_count() >= 5);
133 C::size_type b = c.bucket(key: 0);
134 I i = c.cbegin(n: b);
135 I j = c.cend(n: b);
136 assert(std::distance(i, j) == 0);
137
138 b = c.bucket(key: 1);
139 i = c.cbegin(n: b);
140 j = c.cend(n: b);
141 assert(std::distance(i, j) == 1);
142 assert(*i == 1);
143
144 b = c.bucket(key: 2);
145 i = c.cbegin(n: b);
146 j = c.cend(n: b);
147 assert(std::distance(i, j) == 1);
148 assert(*i == 2);
149
150 b = c.bucket(key: 3);
151 i = c.cbegin(n: b);
152 j = c.cend(n: b);
153 assert(std::distance(i, j) == 1);
154 assert(*i == 3);
155
156 b = c.bucket(key: 4);
157 i = c.cbegin(n: b);
158 j = c.cend(n: b);
159 assert(std::distance(i, j) == 1);
160 assert(*i == 4);
161 }
162 {
163 typedef std::unordered_set<int> C;
164 typedef int P;
165 typedef C::const_local_iterator I;
166 P a[] =
167 {
168 P(1),
169 P(2),
170 P(3),
171 P(4),
172 P(1),
173 P(2)
174 };
175 const C c(a, a + sizeof(a)/sizeof(a[0]));
176 assert(c.bucket_count() >= 5);
177 C::size_type b = c.bucket(key: 0);
178 I i = c.cbegin(n: b);
179 I j = c.cend(n: b);
180 assert(std::distance(i, j) == 0);
181
182 b = c.bucket(key: 1);
183 i = c.cbegin(n: b);
184 j = c.cend(n: b);
185 assert(std::distance(i, j) == 1);
186 assert(*i == 1);
187
188 b = c.bucket(key: 2);
189 i = c.cbegin(n: b);
190 j = c.cend(n: b);
191 assert(std::distance(i, j) == 1);
192 assert(*i == 2);
193
194 b = c.bucket(key: 3);
195 i = c.cbegin(n: b);
196 j = c.cend(n: b);
197 assert(std::distance(i, j) == 1);
198 assert(*i == 3);
199
200 b = c.bucket(key: 4);
201 i = c.cbegin(n: b);
202 j = c.cend(n: b);
203 assert(std::distance(i, j) == 1);
204 assert(*i == 4);
205 }
206#if TEST_STD_VER >= 11
207 {
208 typedef std::unordered_set<int, std::hash<int>,
209 std::equal_to<int>, min_allocator<int>> C;
210 typedef int P;
211 typedef C::local_iterator I;
212 P a[] =
213 {
214 P(1),
215 P(2),
216 P(3),
217 P(4),
218 P(1),
219 P(2)
220 };
221 C c(a, a + sizeof(a)/sizeof(a[0]));
222 assert(c.bucket_count() >= 5);
223 C::size_type b = c.bucket(0);
224 I i = c.begin(b);
225 I j = c.end(b);
226 assert(std::distance(i, j) == 0);
227
228 b = c.bucket(1);
229 i = c.begin(b);
230 j = c.end(b);
231 assert(std::distance(i, j) == 1);
232 assert(*i == 1);
233
234 b = c.bucket(2);
235 i = c.begin(b);
236 j = c.end(b);
237 assert(std::distance(i, j) == 1);
238 assert(*i == 2);
239
240 b = c.bucket(3);
241 i = c.begin(b);
242 j = c.end(b);
243 assert(std::distance(i, j) == 1);
244 assert(*i == 3);
245
246 b = c.bucket(4);
247 i = c.begin(b);
248 j = c.end(b);
249 assert(std::distance(i, j) == 1);
250 assert(*i == 4);
251 }
252 {
253 typedef std::unordered_set<int, std::hash<int>,
254 std::equal_to<int>, min_allocator<int>> C;
255 typedef int P;
256 typedef C::const_local_iterator I;
257 P a[] =
258 {
259 P(1),
260 P(2),
261 P(3),
262 P(4),
263 P(1),
264 P(2)
265 };
266 const C c(a, a + sizeof(a)/sizeof(a[0]));
267 assert(c.bucket_count() >= 5);
268 C::size_type b = c.bucket(0);
269 I i = c.begin(b);
270 I j = c.end(b);
271 assert(std::distance(i, j) == 0);
272
273 b = c.bucket(1);
274 i = c.begin(b);
275 j = c.end(b);
276 assert(std::distance(i, j) == 1);
277 assert(*i == 1);
278
279 b = c.bucket(2);
280 i = c.begin(b);
281 j = c.end(b);
282 assert(std::distance(i, j) == 1);
283 assert(*i == 2);
284
285 b = c.bucket(3);
286 i = c.begin(b);
287 j = c.end(b);
288 assert(std::distance(i, j) == 1);
289 assert(*i == 3);
290
291 b = c.bucket(4);
292 i = c.begin(b);
293 j = c.end(b);
294 assert(std::distance(i, j) == 1);
295 assert(*i == 4);
296 }
297 {
298 typedef std::unordered_set<int, std::hash<int>,
299 std::equal_to<int>, min_allocator<int>> C;
300 typedef int P;
301 typedef C::const_local_iterator I;
302 P a[] =
303 {
304 P(1),
305 P(2),
306 P(3),
307 P(4),
308 P(1),
309 P(2)
310 };
311 C c(a, a + sizeof(a)/sizeof(a[0]));
312 assert(c.bucket_count() >= 5);
313 C::size_type b = c.bucket(0);
314 I i = c.cbegin(b);
315 I j = c.cend(b);
316 assert(std::distance(i, j) == 0);
317
318 b = c.bucket(1);
319 i = c.cbegin(b);
320 j = c.cend(b);
321 assert(std::distance(i, j) == 1);
322 assert(*i == 1);
323
324 b = c.bucket(2);
325 i = c.cbegin(b);
326 j = c.cend(b);
327 assert(std::distance(i, j) == 1);
328 assert(*i == 2);
329
330 b = c.bucket(3);
331 i = c.cbegin(b);
332 j = c.cend(b);
333 assert(std::distance(i, j) == 1);
334 assert(*i == 3);
335
336 b = c.bucket(4);
337 i = c.cbegin(b);
338 j = c.cend(b);
339 assert(std::distance(i, j) == 1);
340 assert(*i == 4);
341 }
342 {
343 typedef std::unordered_set<int, std::hash<int>,
344 std::equal_to<int>, min_allocator<int>> C;
345 typedef int P;
346 typedef C::const_local_iterator I;
347 P a[] =
348 {
349 P(1),
350 P(2),
351 P(3),
352 P(4),
353 P(1),
354 P(2)
355 };
356 const C c(a, a + sizeof(a)/sizeof(a[0]));
357 assert(c.bucket_count() >= 5);
358 C::size_type b = c.bucket(0);
359 I i = c.cbegin(b);
360 I j = c.cend(b);
361 assert(std::distance(i, j) == 0);
362
363 b = c.bucket(1);
364 i = c.cbegin(b);
365 j = c.cend(b);
366 assert(std::distance(i, j) == 1);
367 assert(*i == 1);
368
369 b = c.bucket(2);
370 i = c.cbegin(b);
371 j = c.cend(b);
372 assert(std::distance(i, j) == 1);
373 assert(*i == 2);
374
375 b = c.bucket(3);
376 i = c.cbegin(b);
377 j = c.cend(b);
378 assert(std::distance(i, j) == 1);
379 assert(*i == 3);
380
381 b = c.bucket(4);
382 i = c.cbegin(b);
383 j = c.cend(b);
384 assert(std::distance(i, j) == 1);
385 assert(*i == 4);
386 }
387#endif
388
389 return 0;
390}
391

source code of libcxx/test/std/containers/unord/unord.set/local_iterators.pass.cpp