2 //===------------------------- hash_set ------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_HASH_SET
11 #define _LIBCPP_HASH_SET
20 template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
21 class Alloc = allocator<Value>>
26 typedef Value key_type;
27 typedef key_type value_type;
29 typedef Pred key_equal;
30 typedef Alloc allocator_type;
31 typedef value_type& reference;
32 typedef const value_type& const_reference;
33 typedef typename allocator_traits<allocator_type>::pointer pointer;
34 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
35 typedef typename allocator_traits<allocator_type>::size_type size_type;
36 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
38 typedef /unspecified/ iterator;
39 typedef /unspecified/ const_iterator;
41 explicit hash_set(size_type n = 193, const hasher& hf = hasher(),
42 const key_equal& eql = key_equal(),
43 const allocator_type& a = allocator_type());
44 template <class InputIterator>
45 hash_set(InputIterator f, InputIterator l,
46 size_type n = 193, const hasher& hf = hasher(),
47 const key_equal& eql = key_equal(),
48 const allocator_type& a = allocator_type());
49 hash_set(const hash_set&);
51 hash_set& operator=(const hash_set&);
53 allocator_type get_allocator() const;
56 size_type size() const;
57 size_type max_size() const;
61 const_iterator begin() const;
62 const_iterator end() const;
64 pair<iterator, bool> insert(const value_type& obj);
65 template <class InputIterator>
66 void insert(InputIterator first, InputIterator last);
68 void erase(const_iterator position);
69 size_type erase(const key_type& k);
70 void erase(const_iterator first, const_iterator last);
75 hasher hash_funct() const;
76 key_equal key_eq() const;
78 iterator find(const key_type& k);
79 const_iterator find(const key_type& k) const;
80 size_type count(const key_type& k) const;
81 pair<iterator, iterator> equal_range(const key_type& k);
82 pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
84 size_type bucket_count() const;
85 size_type max_bucket_count() const;
87 size_type elems_in_bucket(size_type n) const;
89 void resize(size_type n);
92 template <class Value, class Hash, class Pred, class Alloc>
93 void swap(hash_set<Value, Hash, Pred, Alloc>& x,
94 hash_set<Value, Hash, Pred, Alloc>& y);
96 template <class Value, class Hash, class Pred, class Alloc>
98 operator==(const hash_set<Value, Hash, Pred, Alloc>& x,
99 const hash_set<Value, Hash, Pred, Alloc>& y);
101 template <class Value, class Hash, class Pred, class Alloc>
103 operator!=(const hash_set<Value, Hash, Pred, Alloc>& x,
104 const hash_set<Value, Hash, Pred, Alloc>& y);
106 template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
107 class Alloc = allocator<Value>>
112 typedef Value key_type;
113 typedef key_type value_type;
115 typedef Pred key_equal;
116 typedef Alloc allocator_type;
117 typedef value_type& reference;
118 typedef const value_type& const_reference;
119 typedef typename allocator_traits<allocator_type>::pointer pointer;
120 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
121 typedef typename allocator_traits<allocator_type>::size_type size_type;
122 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
124 typedef /unspecified/ iterator;
125 typedef /unspecified/ const_iterator;
127 explicit hash_multiset(size_type n = 193, const hasher& hf = hasher(),
128 const key_equal& eql = key_equal(),
129 const allocator_type& a = allocator_type());
130 template <class InputIterator>
131 hash_multiset(InputIterator f, InputIterator l,
132 size_type n = 193, const hasher& hf = hasher(),
133 const key_equal& eql = key_equal(),
134 const allocator_type& a = allocator_type());
135 hash_multiset(const hash_multiset&);
137 hash_multiset& operator=(const hash_multiset&);
139 allocator_type get_allocator() const;
142 size_type size() const;
143 size_type max_size() const;
147 const_iterator begin() const;
148 const_iterator end() const;
150 iterator insert(const value_type& obj);
151 template <class InputIterator>
152 void insert(InputIterator first, InputIterator last);
154 void erase(const_iterator position);
155 size_type erase(const key_type& k);
156 void erase(const_iterator first, const_iterator last);
159 void swap(hash_multiset&);
161 hasher hash_funct() const;
162 key_equal key_eq() const;
164 iterator find(const key_type& k);
165 const_iterator find(const key_type& k) const;
166 size_type count(const key_type& k) const;
167 pair<iterator, iterator> equal_range(const key_type& k);
168 pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
170 size_type bucket_count() const;
171 size_type max_bucket_count() const;
173 size_type elems_in_bucket(size_type n) const;
175 void resize(size_type n);
178 template <class Value, class Hash, class Pred, class Alloc>
179 void swap(hash_multiset<Value, Hash, Pred, Alloc>& x,
180 hash_multiset<Value, Hash, Pred, Alloc>& y);
182 template <class Value, class Hash, class Pred, class Alloc>
184 operator==(const hash_multiset<Value, Hash, Pred, Alloc>& x,
185 const hash_multiset<Value, Hash, Pred, Alloc>& y);
187 template <class Value, class Hash, class Pred, class Alloc>
189 operator!=(const hash_multiset<Value, Hash, Pred, Alloc>& x,
190 const hash_multiset<Value, Hash, Pred, Alloc>& y);
196 #include <__hash_table>
197 #include <functional>
198 #include <ext/__hash>
201 #if defined(_LIBCPP_WARNING)
202 _LIBCPP_WARNING("Use of the header <ext/hash_set> is deprecated. Migrate to <unordered_set>")
204 # warning Use of the header <ext/hash_set> is deprecated. Migrate to <unordered_set>
208 namespace __gnu_cxx {
211 template <class _Value, class _Hash = hash<_Value>, class _Pred = std::equal_to<_Value>,
212 class _Alloc = std::allocator<_Value> >
213 class _LIBCPP_TEMPLATE_VIS hash_set
217 typedef _Value key_type;
218 typedef key_type value_type;
219 typedef _Hash hasher;
220 typedef _Pred key_equal;
221 typedef _Alloc allocator_type;
222 typedef value_type& reference;
223 typedef const value_type& const_reference;
226 typedef std::__hash_table<value_type, hasher, key_equal, allocator_type> __table;
231 typedef typename __table::pointer pointer;
232 typedef typename __table::const_pointer const_pointer;
233 typedef typename __table::size_type size_type;
234 typedef typename __table::difference_type difference_type;
236 typedef typename __table::const_iterator iterator;
237 typedef typename __table::const_iterator const_iterator;
239 _LIBCPP_INLINE_VISIBILITY
241 explicit hash_set(size_type __n, const hasher& __hf = hasher(),
242 const key_equal& __eql = key_equal());
243 hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
244 const allocator_type& __a);
245 template <class _InputIterator>
246 hash_set(_InputIterator __first, _InputIterator __last);
247 template <class _InputIterator>
248 hash_set(_InputIterator __first, _InputIterator __last,
249 size_type __n, const hasher& __hf = hasher(),
250 const key_equal& __eql = key_equal());
251 template <class _InputIterator>
252 hash_set(_InputIterator __first, _InputIterator __last,
253 size_type __n, const hasher& __hf, const key_equal& __eql,
254 const allocator_type& __a);
255 hash_set(const hash_set& __u);
257 _LIBCPP_INLINE_VISIBILITY
258 allocator_type get_allocator() const
259 {return allocator_type(__table_.__node_alloc());}
261 _LIBCPP_INLINE_VISIBILITY
262 bool empty() const {return __table_.size() == 0;}
263 _LIBCPP_INLINE_VISIBILITY
264 size_type size() const {return __table_.size();}
265 _LIBCPP_INLINE_VISIBILITY
266 size_type max_size() const {return __table_.max_size();}
268 _LIBCPP_INLINE_VISIBILITY
269 iterator begin() {return __table_.begin();}
270 _LIBCPP_INLINE_VISIBILITY
271 iterator end() {return __table_.end();}
272 _LIBCPP_INLINE_VISIBILITY
273 const_iterator begin() const {return __table_.begin();}
274 _LIBCPP_INLINE_VISIBILITY
275 const_iterator end() const {return __table_.end();}
277 _LIBCPP_INLINE_VISIBILITY
278 std::pair<iterator, bool> insert(const value_type& __x)
279 {return __table_.__insert_unique(__x);}
280 _LIBCPP_INLINE_VISIBILITY
281 iterator insert(const_iterator, const value_type& __x) {return insert(__x).first;}
282 template <class _InputIterator>
283 _LIBCPP_INLINE_VISIBILITY
284 void insert(_InputIterator __first, _InputIterator __last);
286 _LIBCPP_INLINE_VISIBILITY
287 void erase(const_iterator __p) {__table_.erase(__p);}
288 _LIBCPP_INLINE_VISIBILITY
289 size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
290 _LIBCPP_INLINE_VISIBILITY
291 void erase(const_iterator __first, const_iterator __last)
292 {__table_.erase(__first, __last);}
293 _LIBCPP_INLINE_VISIBILITY
294 void clear() {__table_.clear();}
296 _LIBCPP_INLINE_VISIBILITY
297 void swap(hash_set& __u) {__table_.swap(__u.__table_);}
299 _LIBCPP_INLINE_VISIBILITY
300 hasher hash_funct() const {return __table_.hash_function();}
301 _LIBCPP_INLINE_VISIBILITY
302 key_equal key_eq() const {return __table_.key_eq();}
304 _LIBCPP_INLINE_VISIBILITY
305 iterator find(const key_type& __k) {return __table_.find(__k);}
306 _LIBCPP_INLINE_VISIBILITY
307 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
308 _LIBCPP_INLINE_VISIBILITY
309 size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
310 _LIBCPP_INLINE_VISIBILITY
311 std::pair<iterator, iterator> equal_range(const key_type& __k)
312 {return __table_.__equal_range_unique(__k);}
313 _LIBCPP_INLINE_VISIBILITY
314 std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
315 {return __table_.__equal_range_unique(__k);}
317 _LIBCPP_INLINE_VISIBILITY
318 size_type bucket_count() const {return __table_.bucket_count();}
319 _LIBCPP_INLINE_VISIBILITY
320 size_type max_bucket_count() const {return __table_.max_bucket_count();}
322 _LIBCPP_INLINE_VISIBILITY
323 size_type elems_in_bucket(size_type __n) const {return __table_.bucket_size(__n);}
325 _LIBCPP_INLINE_VISIBILITY
326 void resize(size_type __n) {__table_.rehash(__n);}
329 template <class _Value, class _Hash, class _Pred, class _Alloc>
330 hash_set<_Value, _Hash, _Pred, _Alloc>::hash_set(size_type __n,
331 const hasher& __hf, const key_equal& __eql)
332 : __table_(__hf, __eql)
334 __table_.rehash(__n);
337 template <class _Value, class _Hash, class _Pred, class _Alloc>
338 hash_set<_Value, _Hash, _Pred, _Alloc>::hash_set(size_type __n,
339 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
340 : __table_(__hf, __eql, __a)
342 __table_.rehash(__n);
345 template <class _Value, class _Hash, class _Pred, class _Alloc>
346 template <class _InputIterator>
347 hash_set<_Value, _Hash, _Pred, _Alloc>::hash_set(
348 _InputIterator __first, _InputIterator __last)
350 insert(__first, __last);
353 template <class _Value, class _Hash, class _Pred, class _Alloc>
354 template <class _InputIterator>
355 hash_set<_Value, _Hash, _Pred, _Alloc>::hash_set(
356 _InputIterator __first, _InputIterator __last, size_type __n,
357 const hasher& __hf, const key_equal& __eql)
358 : __table_(__hf, __eql)
360 __table_.rehash(__n);
361 insert(__first, __last);
364 template <class _Value, class _Hash, class _Pred, class _Alloc>
365 template <class _InputIterator>
366 hash_set<_Value, _Hash, _Pred, _Alloc>::hash_set(
367 _InputIterator __first, _InputIterator __last, size_type __n,
368 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
369 : __table_(__hf, __eql, __a)
371 __table_.rehash(__n);
372 insert(__first, __last);
375 template <class _Value, class _Hash, class _Pred, class _Alloc>
376 hash_set<_Value, _Hash, _Pred, _Alloc>::hash_set(
378 : __table_(__u.__table_)
380 __table_.rehash(__u.bucket_count());
381 insert(__u.begin(), __u.end());
384 template <class _Value, class _Hash, class _Pred, class _Alloc>
385 template <class _InputIterator>
388 hash_set<_Value, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
389 _InputIterator __last)
391 for (; __first != __last; ++__first)
392 __table_.__insert_unique(*__first);
395 template <class _Value, class _Hash, class _Pred, class _Alloc>
396 inline _LIBCPP_INLINE_VISIBILITY
398 swap(hash_set<_Value, _Hash, _Pred, _Alloc>& __x,
399 hash_set<_Value, _Hash, _Pred, _Alloc>& __y)
404 template <class _Value, class _Hash, class _Pred, class _Alloc>
406 operator==(const hash_set<_Value, _Hash, _Pred, _Alloc>& __x,
407 const hash_set<_Value, _Hash, _Pred, _Alloc>& __y)
409 if (__x.size() != __y.size())
411 typedef typename hash_set<_Value, _Hash, _Pred, _Alloc>::const_iterator
413 for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
416 const_iterator __j = __y.find(*__i);
417 if (__j == __ey || !(*__i == *__j))
423 template <class _Value, class _Hash, class _Pred, class _Alloc>
424 inline _LIBCPP_INLINE_VISIBILITY
426 operator!=(const hash_set<_Value, _Hash, _Pred, _Alloc>& __x,
427 const hash_set<_Value, _Hash, _Pred, _Alloc>& __y)
429 return !(__x == __y);
432 template <class _Value, class _Hash = hash<_Value>, class _Pred = std::equal_to<_Value>,
433 class _Alloc = std::allocator<_Value> >
434 class _LIBCPP_TEMPLATE_VIS hash_multiset
438 typedef _Value key_type;
439 typedef key_type value_type;
440 typedef _Hash hasher;
441 typedef _Pred key_equal;
442 typedef _Alloc allocator_type;
443 typedef value_type& reference;
444 typedef const value_type& const_reference;
447 typedef std::__hash_table<value_type, hasher, key_equal, allocator_type> __table;
452 typedef typename __table::pointer pointer;
453 typedef typename __table::const_pointer const_pointer;
454 typedef typename __table::size_type size_type;
455 typedef typename __table::difference_type difference_type;
457 typedef typename __table::const_iterator iterator;
458 typedef typename __table::const_iterator const_iterator;
460 _LIBCPP_INLINE_VISIBILITY
462 explicit hash_multiset(size_type __n, const hasher& __hf = hasher(),
463 const key_equal& __eql = key_equal());
464 hash_multiset(size_type __n, const hasher& __hf,
465 const key_equal& __eql, const allocator_type& __a);
466 template <class _InputIterator>
467 hash_multiset(_InputIterator __first, _InputIterator __last);
468 template <class _InputIterator>
469 hash_multiset(_InputIterator __first, _InputIterator __last,
470 size_type __n, const hasher& __hf = hasher(),
471 const key_equal& __eql = key_equal());
472 template <class _InputIterator>
473 hash_multiset(_InputIterator __first, _InputIterator __last,
474 size_type __n , const hasher& __hf,
475 const key_equal& __eql, const allocator_type& __a);
476 hash_multiset(const hash_multiset& __u);
478 _LIBCPP_INLINE_VISIBILITY
479 allocator_type get_allocator() const
480 {return allocator_type(__table_.__node_alloc());}
482 _LIBCPP_INLINE_VISIBILITY
483 bool empty() const {return __table_.size() == 0;}
484 _LIBCPP_INLINE_VISIBILITY
485 size_type size() const {return __table_.size();}
486 _LIBCPP_INLINE_VISIBILITY
487 size_type max_size() const {return __table_.max_size();}
489 _LIBCPP_INLINE_VISIBILITY
490 iterator begin() {return __table_.begin();}
491 _LIBCPP_INLINE_VISIBILITY
492 iterator end() {return __table_.end();}
493 _LIBCPP_INLINE_VISIBILITY
494 const_iterator begin() const {return __table_.begin();}
495 _LIBCPP_INLINE_VISIBILITY
496 const_iterator end() const {return __table_.end();}
498 _LIBCPP_INLINE_VISIBILITY
499 iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
500 _LIBCPP_INLINE_VISIBILITY
501 iterator insert(const_iterator, const value_type& __x) {return insert(__x);}
502 template <class _InputIterator>
503 _LIBCPP_INLINE_VISIBILITY
504 void insert(_InputIterator __first, _InputIterator __last);
506 _LIBCPP_INLINE_VISIBILITY
507 void erase(const_iterator __p) {__table_.erase(__p);}
508 _LIBCPP_INLINE_VISIBILITY
509 size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
510 _LIBCPP_INLINE_VISIBILITY
511 void erase(const_iterator __first, const_iterator __last)
512 {__table_.erase(__first, __last);}
513 _LIBCPP_INLINE_VISIBILITY
514 void clear() {__table_.clear();}
516 _LIBCPP_INLINE_VISIBILITY
517 void swap(hash_multiset& __u) {__table_.swap(__u.__table_);}
519 _LIBCPP_INLINE_VISIBILITY
520 hasher hash_funct() const {return __table_.hash_function();}
521 _LIBCPP_INLINE_VISIBILITY
522 key_equal key_eq() const {return __table_.key_eq();}
524 _LIBCPP_INLINE_VISIBILITY
525 iterator find(const key_type& __k) {return __table_.find(__k);}
526 _LIBCPP_INLINE_VISIBILITY
527 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
528 _LIBCPP_INLINE_VISIBILITY
529 size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
530 _LIBCPP_INLINE_VISIBILITY
531 std::pair<iterator, iterator> equal_range(const key_type& __k)
532 {return __table_.__equal_range_multi(__k);}
533 _LIBCPP_INLINE_VISIBILITY
534 std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
535 {return __table_.__equal_range_multi(__k);}
537 _LIBCPP_INLINE_VISIBILITY
538 size_type bucket_count() const {return __table_.bucket_count();}
539 _LIBCPP_INLINE_VISIBILITY
540 size_type max_bucket_count() const {return __table_.max_bucket_count();}
542 _LIBCPP_INLINE_VISIBILITY
543 size_type elems_in_bucket(size_type __n) const {return __table_.bucket_size(__n);}
545 _LIBCPP_INLINE_VISIBILITY
546 void resize(size_type __n) {__table_.rehash(__n);}
549 template <class _Value, class _Hash, class _Pred, class _Alloc>
550 hash_multiset<_Value, _Hash, _Pred, _Alloc>::hash_multiset(
551 size_type __n, const hasher& __hf, const key_equal& __eql)
552 : __table_(__hf, __eql)
554 __table_.rehash(__n);
557 template <class _Value, class _Hash, class _Pred, class _Alloc>
558 hash_multiset<_Value, _Hash, _Pred, _Alloc>::hash_multiset(
559 size_type __n, const hasher& __hf, const key_equal& __eql,
560 const allocator_type& __a)
561 : __table_(__hf, __eql, __a)
563 __table_.rehash(__n);
566 template <class _Value, class _Hash, class _Pred, class _Alloc>
567 template <class _InputIterator>
568 hash_multiset<_Value, _Hash, _Pred, _Alloc>::hash_multiset(
569 _InputIterator __first, _InputIterator __last)
571 insert(__first, __last);
574 template <class _Value, class _Hash, class _Pred, class _Alloc>
575 template <class _InputIterator>
576 hash_multiset<_Value, _Hash, _Pred, _Alloc>::hash_multiset(
577 _InputIterator __first, _InputIterator __last, size_type __n,
578 const hasher& __hf, const key_equal& __eql)
579 : __table_(__hf, __eql)
581 __table_.rehash(__n);
582 insert(__first, __last);
585 template <class _Value, class _Hash, class _Pred, class _Alloc>
586 template <class _InputIterator>
587 hash_multiset<_Value, _Hash, _Pred, _Alloc>::hash_multiset(
588 _InputIterator __first, _InputIterator __last, size_type __n,
589 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
590 : __table_(__hf, __eql, __a)
592 __table_.rehash(__n);
593 insert(__first, __last);
596 template <class _Value, class _Hash, class _Pred, class _Alloc>
597 hash_multiset<_Value, _Hash, _Pred, _Alloc>::hash_multiset(
598 const hash_multiset& __u)
599 : __table_(__u.__table_)
601 __table_.rehash(__u.bucket_count());
602 insert(__u.begin(), __u.end());
605 template <class _Value, class _Hash, class _Pred, class _Alloc>
606 template <class _InputIterator>
609 hash_multiset<_Value, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
610 _InputIterator __last)
612 for (; __first != __last; ++__first)
613 __table_.__insert_multi(*__first);
616 template <class _Value, class _Hash, class _Pred, class _Alloc>
617 inline _LIBCPP_INLINE_VISIBILITY
619 swap(hash_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
620 hash_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
625 template <class _Value, class _Hash, class _Pred, class _Alloc>
627 operator==(const hash_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
628 const hash_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
630 if (__x.size() != __y.size())
632 typedef typename hash_multiset<_Value, _Hash, _Pred, _Alloc>::const_iterator
634 typedef std::pair<const_iterator, const_iterator> _EqRng;
635 for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
637 _EqRng __xeq = __x.equal_range(*__i);
638 _EqRng __yeq = __y.equal_range(*__i);
639 if (_VSTD::distance(__xeq.first, __xeq.second) !=
640 _VSTD::distance(__yeq.first, __yeq.second) ||
641 !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
648 template <class _Value, class _Hash, class _Pred, class _Alloc>
649 inline _LIBCPP_INLINE_VISIBILITY
651 operator!=(const hash_multiset<_Value, _Hash, _Pred, _Alloc>& __x,
652 const hash_multiset<_Value, _Hash, _Pred, _Alloc>& __y)
654 return !(__x == __y);
659 #endif // _LIBCPP_HASH_SET