Go to the documentation of this file.
17 #ifndef MOMO_INCLUDE_GUARD_STDISH_UNORDERED_SET
18 #define MOMO_INCLUDE_GUARD_STDISH_UNORDERED_SET
21 # if __has_include(<momo/Utility.h>)
25 #ifndef MOMO_PARENT_HEADER
26 # include "../Utility.h"
29 #include MOMO_PARENT_HEADER(HashSet)
60 template<
typename TKey,
61 typename THasher = HashCoder<TKey>,
62 typename TEqualComparer = std::equal_to<TKey>,
63 typename TAllocator = std::allocator<TKey>,
64 typename THashSet = HashSet<TKey, HashTraitsStd<TKey, THasher, TEqualComparer>,
65 MemManagerStd<TAllocator>>>
69 typedef THashSet HashSet;
106 template<
typename KeyArg>
107 struct IsValidKeyArg :
public HashTraits::template IsValidKeyArg<KeyArg>
144 template<
typename Iterator>
150 template<
typename Iterator>
158 template<
typename Iterator>
166 template<
typename Iterator>
198 #ifdef MOMO_HAS_CONTAINERS_RANGES
199 template<std::ranges::input_range Range>
200 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
203 insert_range(std::forward<Range>(values));
206 template<std::ranges::input_range Range>
207 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
212 insert_range(std::forward<Range>(values));
215 template<std::ranges::input_range Range>
216 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
221 insert_range(std::forward<Range>(values));
224 template<std::ranges::input_range Range>
225 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
230 insert_range(std::forward<Range>(values));
232 #endif // MOMO_HAS_CONTAINERS_RANGES
240 : mHashSet(right.mHashSet.GetHashTraits(),
MemManager(alloc))
242 if (right.get_allocator() == alloc)
244 mHashSet.Swap(right.mHashSet);
248 mHashSet.MergeFrom(right.mHashSet);
254 : mHashSet(right.mHashSet)
304 return mHashSet.GetBegin();
311 return mHashSet.GetEnd();
336 throw std::out_of_range(
"invalid load factor");
337 HashTraits hashTraits(mHashSet.GetHashTraits(), maxLoadFactor);
341 mHashSet = std::move(hashSet);
346 return mHashSet.GetHashTraits().GetHasher();
351 return mHashSet.GetHashTraits().GetEqualComparer();
356 return allocator_type(mHashSet.GetMemManager().GetByteAllocator());
361 return std::allocator_traits<allocator_type>::max_size(
get_allocator());
366 return mHashSet.GetCount();
371 return mHashSet.IsEmpty();
381 bucketCount = std::minmax(bucketCount,
size_t{2}).second;
383 bucketCount =
size_t{1} << logBucketCount;
389 mHashSet.Reserve(
count);
394 return mHashSet.Find(key);
399 template<
typename KeyArg>
403 return mHashSet.Find(key);
415 template<
typename KeyArg>
424 return mHashSet.ContainsKey(key);
427 template<
typename KeyArg>
431 return mHashSet.ContainsKey(key);
442 template<
typename KeyArg>
444 std::pair<const_iterator, const_iterator>>
equal_range(
const KeyArg& key)
const
461 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
462 return mHashSet.Add(hint, std::move(value));
465 return insert(std::move(value)).first;
477 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
478 return mHashSet.Add(hint, value);
481 return insert(value).first;
490 std::move(NodeTypeProxy::GetExtractedItem(node)));
496 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
499 return mHashSet.Add(hint, std::move(NodeTypeProxy::GetExtractedItem(node)));
502 return insert(std::move(node)).position;
506 template<
typename Iterator>
507 void insert(Iterator first, Iterator last)
509 pvInsertRange(first, last);
512 void insert(std::initializer_list<value_type> values)
514 mHashSet.Insert(values);
517 #ifdef MOMO_HAS_CONTAINERS_RANGES
518 template<std::ranges::input_range Range>
519 requires std::convertible_to<std::ranges::range_reference_t<Range>,
value_type>
520 void insert_range(Range&& values)
522 pvInsertRange(std::ranges::begin(values), std::ranges::end(values));
524 #endif // MOMO_HAS_CONTAINERS_RANGES
526 template<
typename... ValueArgs>
527 std::pair<iterator, bool>
emplace(ValueArgs&&... valueArgs)
529 MemManager& memManager = mHashSet.GetMemManager();
531 typedef typename HashSet::ItemTraits::template Creator<ValueArgs...> ValueCreator;
532 extItem.
Create(ValueCreator(memManager, std::forward<ValueArgs>(valueArgs)...));
537 template<
typename ValueArg>
539 std::pair<iterator, bool>>
emplace(ValueArg&& valueArg)
542 static_cast<const key_type&
>(valueArg), std::forward<ValueArg>(valueArg));
546 template<
typename... ValueArgs>
549 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
550 return mHashSet.AddVar(hint, std::forward<ValueArgs>(valueArgs)...);
553 return emplace(std::forward<ValueArgs>(valueArgs)...).first;
559 return mHashSet.Remove(where);
566 if (first ==
begin() && last ==
end())
573 if (first !=
end() && std::next(first) == last)
575 throw std::invalid_argument(
"invalid unordered_set erase arguments");
580 return mHashSet.Remove(key) ? 1 : 0;
583 template<
typename ValueFilter>
586 return cont.mHashSet.Remove(valueFilter);
600 template<
typename Set>
614 return mHashSet.GetBucketCount();
619 return mHashSet.GetBucketBounds(bucketIndex).GetCount();
624 return mHashSet.GetBucketBounds(bucketIndex).GetBegin();
629 return mHashSet.GetBucketBounds(bucketIndex).GetBegin();
634 return mHashSet.GetBucketBounds(bucketIndex).GetEnd();
639 return mHashSet.GetBucketBounds(bucketIndex).GetEnd();
644 return begin(bucketIndex);
649 return end(bucketIndex);
654 return mHashSet.GetBucketIndex(key);
661 if (
count == 0 && bucketCount == 0)
663 return static_cast<float>(
count) /
static_cast<float>(bucketCount);
672 if (right.
find(ref) == right.
end())
680 return !(left == right);
684 template<
typename Iterator,
typename Sentinel>
686 void> pvInsertRange(Iterator
begin, Sentinel
end)
688 mHashSet.Insert(std::move(
begin), std::move(
end));
691 template<
typename Iterator,
typename Sentinel>
693 void> pvInsertRange(Iterator
begin, Sentinel
end)
695 for (Iterator iter = std::move(
begin); iter !=
end; ++iter)
712 template<
typename TKey,
713 typename THasher = HashCoder<TKey>,
714 typename TEqualComparer = std::equal_to<TKey>,
715 typename TAllocator = std::allocator<TKey>>
717 HashSet<TKey, HashTraitsStd<TKey, THasher, TEqualComparer, HashBucketOpenDefault>,
718 MemManagerStd<TAllocator>>>
721 typedef unordered_set<TKey, THasher, TEqualComparer, TAllocator,
730 using UnorderedSet::UnorderedSet;
743 template<
typename ValueFilter>
750 #ifdef MOMO_HAS_DEDUCTION_GUIDES
752 #define MOMO_DECLARE_DEDUCTION_GUIDES(unordered_set) \
753 template<typename Iterator, \
754 typename Key = typename std::iterator_traits<Iterator>::value_type> \
755 unordered_set(Iterator, Iterator) \
756 -> unordered_set<Key>; \
757 template<typename Iterator, \
758 typename Key = typename std::iterator_traits<Iterator>::value_type, \
759 typename Allocator = std::allocator<Key>, \
760 typename = internal::unordered_checker<Key, Allocator, HashCoder<Key>>> \
761 unordered_set(Iterator, Iterator, size_t, Allocator = Allocator()) \
762 -> unordered_set<Key, HashCoder<Key>, std::equal_to<Key>, Allocator>; \
763 template<typename Iterator, typename Hasher, \
764 typename Key = typename std::iterator_traits<Iterator>::value_type, \
765 typename Allocator = std::allocator<Key>, \
766 typename = internal::unordered_checker<Key, Allocator, Hasher>> \
767 unordered_set(Iterator, Iterator, size_t, Hasher, Allocator = Allocator()) \
768 -> unordered_set<Key, Hasher, std::equal_to<Key>, Allocator>; \
769 template<typename Iterator, typename Hasher, typename EqualComparer, \
770 typename Key = typename std::iterator_traits<Iterator>::value_type, \
771 typename Allocator = std::allocator<Key>, \
772 typename = internal::unordered_checker<Key, Allocator, Hasher, EqualComparer>> \
773 unordered_set(Iterator, Iterator, size_t, Hasher, EqualComparer, Allocator = Allocator()) \
774 -> unordered_set<Key, Hasher, EqualComparer, Allocator>; \
775 template<typename Key> \
776 unordered_set(std::initializer_list<Key>) \
777 -> unordered_set<Key>; \
778 template<typename Key, \
779 typename Allocator = std::allocator<Key>, \
780 typename = internal::unordered_checker<Key, Allocator, HashCoder<Key>>> \
781 unordered_set(std::initializer_list<Key>, size_t, Allocator = Allocator()) \
782 -> unordered_set<Key, HashCoder<Key>, std::equal_to<Key>, Allocator>; \
783 template<typename Key, typename Hasher, \
784 typename Allocator = std::allocator<Key>, \
785 typename = internal::unordered_checker<Key, Allocator, Hasher>> \
786 unordered_set(std::initializer_list<Key>, size_t, Hasher, Allocator = Allocator()) \
787 -> unordered_set<Key, Hasher, std::equal_to<Key>, Allocator>; \
788 template<typename Key, typename Hasher, typename EqualComparer, \
789 typename Allocator = std::allocator<Key>, \
790 typename = internal::unordered_checker<Key, Allocator, Hasher, EqualComparer>> \
791 unordered_set(std::initializer_list<Key>, size_t, Hasher, EqualComparer, Allocator = Allocator()) \
792 -> unordered_set<Key, Hasher, EqualComparer, Allocator>;
794 MOMO_DECLARE_DEDUCTION_GUIDES(unordered_set)
795 MOMO_DECLARE_DEDUCTION_GUIDES(unordered_set_open)
797 #undef MOMO_DECLARE_DEDUCTION_GUIDES
799 #ifdef MOMO_HAS_CONTAINERS_RANGES
801 #define MOMO_DECLARE_DEDUCTION_GUIDES_RANGES(unordered_set) \
802 template<std::ranges::input_range Range, \
803 typename Key = std::ranges::range_value_t<Range>> \
804 unordered_set(std::from_range_t, Range&&) \
805 -> unordered_set<Key>; \
806 template<std::ranges::input_range Range, \
807 typename Key = std::ranges::range_value_t<Range>, \
808 typename Allocator = std::allocator<Key>, \
809 typename = internal::unordered_checker<Key, Allocator, HashCoder<Key>>> \
810 unordered_set(std::from_range_t, Range&&, size_t, Allocator = Allocator()) \
811 -> unordered_set<Key, HashCoder<Key>, std::equal_to<Key>, Allocator>; \
812 template<std::ranges::input_range Range, typename Hasher, \
813 typename Key = std::ranges::range_value_t<Range>, \
814 typename Allocator = std::allocator<Key>, \
815 typename = internal::unordered_checker<Key, Allocator, Hasher>> \
816 unordered_set(std::from_range_t, Range&&, size_t, Hasher, Allocator = Allocator()) \
817 -> unordered_set<Key, Hasher, std::equal_to<Key>, Allocator>; \
818 template<std::ranges::input_range Range, typename Hasher, typename EqualComparer, \
819 typename Key = std::ranges::range_value_t<Range>, \
820 typename Allocator = std::allocator<Key>, \
821 typename = internal::unordered_checker<Key, Allocator, Hasher, EqualComparer>> \
822 unordered_set(std::from_range_t, Range&&, size_t, Hasher, EqualComparer, Allocator = Allocator()) \
823 -> unordered_set<Key, Hasher, EqualComparer, Allocator>;
825 MOMO_DECLARE_DEDUCTION_GUIDES_RANGES(unordered_set)
826 MOMO_DECLARE_DEDUCTION_GUIDES_RANGES(unordered_set_open)
828 #undef MOMO_DECLARE_DEDUCTION_GUIDES_RANGES
830 #endif // MOMO_HAS_CONTAINERS_RANGES
832 #endif // MOMO_HAS_DEDUCTION_GUIDES
838 #endif // MOMO_INCLUDE_GUARD_STDISH_UNORDERED_SET
iterator erase(const_iterator first, const_iterator last)
Definition: unordered_set.h:564
hasher hash_function() const
Definition: unordered_set.h:344
insert_return_type insert(node_type &&node)
Definition: unordered_set.h:485
Position position
Definition: IteratorUtility.h:168
static const size_t maxSize
Definition: Utility.h:457
value_type & reference
Definition: unordered_set.h:90
unordered_set & operator=(std::initializer_list< value_type > values)
Definition: unordered_set.h:276
unordered_set(Iterator first, Iterator last)
Definition: unordered_set.h:145
TMemManager MemManager
Definition: HashSet.h:470
const_iterator cbegin() const noexcept
Definition: unordered_set.h:316
std::pair< iterator, bool > insert(value_type &&value)
Definition: unordered_set.h:453
void reserve(size_type count)
Definition: unordered_set.h:387
HashSet nested_container_type
Definition: unordered_set.h:79
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, bool > contains(const KeyArg &key) const
Definition: unordered_set.h:429
MOMO_FORCEINLINE const_iterator find(const key_type &key) const
Definition: unordered_set.h:392
iterator insert(const_iterator hint, const value_type &value)
Definition: unordered_set.h:475
const nested_container_type & get_nested_container() const noexcept
Definition: set.h:236
iterator emplace_hint(const_iterator hint, ValueArgs &&... valueArgs)
Definition: unordered_set.h:547
unordered_set(unordered_set &&right, const momo::internal::Identity< allocator_type > &alloc)
Definition: unordered_set.h:239
unordered_set(const allocator_type &alloc)
Definition: unordered_set.h:122
iterator insert(const_iterator hint, node_type &&node)
Definition: unordered_set.h:494
friend bool operator==(const unordered_set &left, const unordered_set &right)
Definition: unordered_set.h:666
size_type bucket_count() const noexcept
Definition: unordered_set.h:612
unordered_set(std::initializer_list< momo::internal::Identity< value_type >> values, size_type bucketCount, const hasher &hashFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:185
Definition: set_map_utility.h:202
const Item * Pointer
Definition: HashSet.h:206
size_type max_bucket_count() const noexcept
Definition: unordered_set.h:606
unordered_set(unordered_set &&right)
Definition: unordered_set.h:234
static UInt Log2(UInt value) noexcept
Definition: Utility.h:395
size_t size_type
Definition: unordered_set.h:81
iterator erase(const_iterator where)
Definition: unordered_set.h:557
const_iterator::Pointer const_pointer
Definition: unordered_set.h:95
const Item & Reference
Definition: HashSet.h:205
void Reserve(size_t capacity)
Definition: HashSet.h:714
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, std::pair< const_iterator, const_iterator > > equal_range(const KeyArg &key) const
Definition: unordered_set.h:444
unordered_set_open & operator=(std::initializer_list< value_type > values)
Definition: unordered_set.h:732
MOMO_FORCEINLINE bool contains(const key_type &key) const
Definition: unordered_set.h:422
momo::stdish::set is similar to std::set, but much more efficient in memory usage....
Definition: set.h:67
void rehash(size_type bucketCount)
Definition: unordered_set.h:379
MemManagerStd uses allocator<unsigned char>::allocate and deallocate
Definition: MemManager.h:177
unordered_set(std::initializer_list< momo::internal::Identity< value_type >> values, size_type bucketCount, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:179
THasher hasher
Definition: unordered_set.h:75
void merge(Set &&set)
Definition: unordered_set.h:601
friend size_type erase_if(unordered_set &cont, const ValueFilter &valueFilter)
Definition: unordered_set.h:584
ConstIterator Remove(ConstIterator iter)
Definition: HashSet.h:854
size_type erase(const key_type &key)
Definition: unordered_set.h:578
static Container & Move(Container &&srcCont, Container &dstCont) noexcept(IsNothrowMoveAssignable< Container >::value)
Definition: Utility.h:496
Definition: IteratorUtility.h:136
Definition: HashSet.h:279
const_iterator cend() const noexcept
Definition: unordered_set.h:321
float load_factor() const noexcept
Definition: unordered_set.h:657
const_local_iterator cbegin(size_type bucketIndex) const
Definition: unordered_set.h:642
ptrdiff_t difference_type
Definition: unordered_set.h:82
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, size_type > count(const KeyArg &key) const
Definition: unordered_set.h:417
InsertResult Insert(Item &&item)
Definition: HashSet.h:776
HashSet::Iterator iterator
Definition: unordered_set.h:87
THashTraits HashTraits
Definition: HashSet.h:469
typename std::enable_if< value, Type >::type EnableIf
Definition: Utility.h:209
size_type bucket(const key_type &key) const
Definition: unordered_set.h:652
const_iterator::Reference const_reference
Definition: unordered_set.h:91
const_local_iterator end(size_type bucketIndex) const
Definition: unordered_set.h:637
BoolConstant< std::is_empty< Allocator >::value||std::allocator_traits< Allocator >::propagate_on_container_move_assignment::value > IsNothrowMoveAssignable
Definition: Utility.h:492
unordered_set()
Definition: unordered_set.h:118
static const size_t bucketMaxItemCount
Definition: HashSet.h:504
const_iterator begin() const noexcept
Definition: unordered_set.h:302
bool inserted
Definition: IteratorUtility.h:171
EnableIf< true, Type > Identity
Definition: Utility.h:212
const_local_iterator begin(size_type bucketIndex) const
Definition: unordered_set.h:627
momo::internal::EnableIf< std::is_same< key_type, typename std::decay< ValueArg >::type >::value, std::pair< iterator, bool > > emplace(ValueArg &&valueArg)
Definition: unordered_set.h:539
allocator_type get_allocator() const noexcept
Definition: unordered_set.h:354
key_equal key_eq() const
Definition: unordered_set.h:349
node_type extract(const key_type &key)
Definition: unordered_set.h:594
HashSet::ConstIterator const_iterator
Definition: unordered_set.h:86
unordered_set(size_type bucketCount, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:127
key_type value_type
Definition: unordered_set.h:84
TEqualComparer key_equal
Definition: unordered_set.h:76
unordered_set(Iterator first, Iterator last, size_type bucketCount, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:151
internal::insert_return_type< iterator, node_type > insert_return_type
Definition: unordered_set.h:100
unordered_set(std::initializer_list< momo::internal::Identity< value_type >> values)
Definition: unordered_set.h:174
momo::stdish::unordered_set_open is similar to std::unordered_set, but much more efficient in operati...
Definition: unordered_set.h:719
MOMO_FORCEINLINE std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: unordered_set.h:434
void insert(Iterator first, Iterator last)
Definition: unordered_set.h:507
Definition: HashTraits.h:196
size_type bucket_size(size_type bucketIndex) const
Definition: unordered_set.h:617
unordered_set(Iterator first, Iterator last, size_type bucketCount, const hasher &hashFunc, const key_equal &equalComp, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:167
const_local_iterator cend(size_type bucketIndex) const
Definition: unordered_set.h:647
unordered_set & operator=(unordered_set &&right) noexcept(momo::internal::ContainerAssignerStd::IsNothrowMoveAssignable< unordered_set >::value)
Definition: unordered_set.h:265
local_iterator begin(size_type bucketIndex)
Definition: unordered_set.h:622
friend size_type erase_if(unordered_set_open &cont, const ValueFilter &valueFilter)
Definition: unordered_set.h:744
#define MOMO_DECLARE_PROXY_FUNCTION(Object, Func)
Definition: Utility.h:126
std::pair< iterator, bool > insert(const value_type &value)
Definition: unordered_set.h:469
void clear() noexcept
Definition: unordered_set.h:374
node_type extract(const_iterator where)
Definition: unordered_set.h:589
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, const_iterator > find(const KeyArg &key) const
Definition: unordered_set.h:401
unordered_set(const unordered_set &right, const momo::internal::Identity< allocator_type > &alloc)
Definition: unordered_set.h:258
local_iterator end(size_type bucketIndex)
Definition: unordered_set.h:632
unordered_set(std::initializer_list< momo::internal::Identity< value_type >> values, size_type bucketCount, const hasher &hashFunc, const key_equal &equalComp, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:191
nested_container_type & get_nested_container() noexcept
Definition: unordered_set.h:297
#define MOMO_FORCEINLINE
Definition: UserSettings.h:119
value_type * pointer
Definition: unordered_set.h:94
HashSet::ConstBucketBounds::Iterator const_local_iterator
Definition: unordered_set.h:102
size_type max_size() const noexcept
Definition: unordered_set.h:359
size_t size_type
Definition: unordered_set.h:81
float max_load_factor() const noexcept
Definition: unordered_set.h:326
iterator insert(const_iterator hint, value_type &&value)
Definition: unordered_set.h:459
void swap(unordered_set &right) noexcept
Definition: unordered_set.h:282
void insert(std::initializer_list< value_type > values)
Definition: unordered_set.h:512
TKey key_type
Definition: unordered_set.h:74
Definition: set_map_utility.h:42
unordered_set(size_type bucketCount, const hasher &hashFunc, const key_equal &equalComp, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:138
static Container & Copy(const Container &srcCont, Container &dstCont)
Definition: Utility.h:512
TAllocator allocator_type
Definition: unordered_set.h:77
friend bool operator!=(const unordered_set &left, const unordered_set &right)
Definition: unordered_set.h:678
void max_load_factor(float maxLoadFactor)
Definition: unordered_set.h:331
momo::stdish::unordered_set is similar to std::unordered_set, but much more efficient in memory usage...
Definition: unordered_set.h:67
unordered_set(Iterator first, Iterator last, size_type bucketCount, const hasher &hashFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:159
size_type size() const noexcept
Definition: unordered_set.h:364
friend void swap(unordered_set &left, unordered_set &right) noexcept
Definition: unordered_set.h:287
unordered_set & operator=(const unordered_set &right)
Definition: unordered_set.h:271
static void Swap(Container &cont1, Container &cont2) noexcept
Definition: Utility.h:527
const_local_iterator local_iterator
Definition: unordered_set.h:103
const nested_container_type & get_nested_container() const noexcept
Definition: unordered_set.h:292
internal::set_node_handle< typename HashSet::ExtractedItem > node_type
Definition: unordered_set.h:99
std::pair< iterator, bool > emplace(ValueArgs &&... valueArgs)
Definition: unordered_set.h:527
MOMO_FORCEINLINE size_type count(const key_type &key) const
Definition: unordered_set.h:410
#define MOMO_NODISCARD
Definition: UserSettings.h:217
unordered_set(size_type bucketCount, const hasher &hashFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:132
friend void swap(unordered_set_open &left, unordered_set_open &right) noexcept
Definition: unordered_set.h:738
unordered_set(const unordered_set &right)
Definition: unordered_set.h:253
const_iterator end() const noexcept
Definition: unordered_set.h:309
MOMO_NODISCARD bool empty() const noexcept
Definition: unordered_set.h:369