Go to the documentation of this file.
17 #ifndef MOMO_INCLUDE_GUARD_STDISH_UNORDERED_SET
18 #define MOMO_INCLUDE_GUARD_STDISH_UNORDERED_SET
20 #include "../HashSet.h"
51 template<
typename TKey,
52 typename THashFunc = HashCoder<TKey>,
53 typename TEqualFunc = std::equal_to<TKey>,
54 typename TAllocator = std::allocator<TKey>,
55 typename THashSet = HashSet<TKey, HashTraitsStd<TKey, THashFunc, TEqualFunc>,
56 MemManagerStd<TAllocator>>>
60 typedef THashSet HashSet;
97 template<
typename KeyArg>
98 struct IsValidKeyArg :
public HashTraits::template IsValidKeyArg<KeyArg>
136 template<
typename Iterator>
142 template<
typename Iterator>
150 template<
typename Iterator>
158 template<
typename Iterator>
191 : mHashSet(std::move(right.mHashSet))
196 noexcept(std::is_empty<allocator_type>::value)
197 : mHashSet(pvCreateSet(
std::move(right), alloc))
202 : mHashSet(right.mHashSet)
214 noexcept(std::is_empty<allocator_type>::value ||
215 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value)
219 bool propagate = std::is_empty<allocator_type>::value ||
220 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value;
222 mHashSet = pvCreateSet(std::move(right), alloc);
231 bool propagate = std::is_empty<allocator_type>::value ||
232 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value;
247 MOMO_ASSERT(std::allocator_traits<allocator_type>::propagate_on_container_swap::value
249 mHashSet.Swap(right.mHashSet);
269 return mHashSet.GetBegin();
276 return mHashSet.GetEnd();
301 throw std::out_of_range(
"invalid load factor");
302 HashTraits hashTraits(mHashSet.GetHashTraits(), maxLoadFactor);
306 mHashSet = std::move(hashSet);
311 return mHashSet.GetHashTraits().GetHashFunc();
316 return mHashSet.GetHashTraits().GetEqualFunc();
321 return allocator_type(mHashSet.GetMemManager().GetByteAllocator());
326 return std::allocator_traits<allocator_type>::max_size(
get_allocator());
331 return mHashSet.GetCount();
336 return mHashSet.IsEmpty();
346 bucketCount = std::minmax(bucketCount,
size_t{2}).second;
348 bucketCount =
size_t{1} << logBucketCount;
354 mHashSet.Reserve(
count);
359 return mHashSet.Find(key);
364 template<
typename KeyArg>
368 return mHashSet.Find(key);
380 template<
typename KeyArg>
389 return mHashSet.ContainsKey(key);
392 template<
typename KeyArg>
396 return mHashSet.ContainsKey(key);
407 template<
typename KeyArg>
409 std::pair<const_iterator, const_iterator>>
equal_range(
const KeyArg& key)
const
426 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
427 return mHashSet.Add(hint, std::move(value));
430 return insert(std::move(value)).first;
442 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
443 return mHashSet.Add(hint, value);
446 return insert(value).first;
455 std::move(NodeTypeProxy::GetExtractedItem(node)));
461 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
464 return mHashSet.Add(hint, std::move(NodeTypeProxy::GetExtractedItem(node)));
467 return insert(std::move(node)).position;
471 template<
typename Iterator>
472 void insert(Iterator first, Iterator last)
477 void insert(std::initializer_list<value_type> values)
479 mHashSet.Insert(values);
482 template<
typename... ValueArgs>
483 std::pair<iterator, bool>
emplace(ValueArgs&&... valueArgs)
485 MemManager& memManager = mHashSet.GetMemManager();
487 typedef typename HashSet::ItemTraits::template Creator<ValueArgs...> ValueCreator;
488 extItem.
Create(ValueCreator(memManager, std::forward<ValueArgs>(valueArgs)...));
493 template<
typename ValueArg>
495 std::pair<iterator, bool>>
emplace(ValueArg&& valueArg)
498 static_cast<const key_type&
>(valueArg), std::forward<ValueArg>(valueArg));
502 template<
typename... ValueArgs>
505 #ifdef MOMO_USE_UNORDERED_HINT_ITERATORS
506 return mHashSet.AddVar(hint, std::forward<ValueArgs>(valueArgs)...);
509 return emplace(std::forward<ValueArgs>(valueArgs)...).first;
515 return mHashSet.Remove(where);
522 if (first ==
begin() && last ==
end())
529 if (first !=
end() && std::next(first) == last)
531 throw std::invalid_argument(
"invalid unordered_set erase arguments");
536 return mHashSet.Remove(key) ? 1 : 0;
539 template<
typename ValueFilter>
542 return cont.mHashSet.Remove(valueFilter);
556 template<
typename Set>
570 return mHashSet.GetBucketCount();
575 return mHashSet.GetBucketBounds(bucketIndex).GetCount();
580 return mHashSet.GetBucketBounds(bucketIndex).GetBegin();
585 return mHashSet.GetBucketBounds(bucketIndex).GetBegin();
590 return mHashSet.GetBucketBounds(bucketIndex).GetEnd();
595 return mHashSet.GetBucketBounds(bucketIndex).GetEnd();
600 return begin(bucketIndex);
605 return end(bucketIndex);
610 return mHashSet.GetBucketIndex(key);
617 if (
count == 0 && bucketCount == 0)
619 return static_cast<float>(
count) /
static_cast<float>(bucketCount);
628 if (right.
find(ref) == right.
end())
636 return !(*
this == right);
642 if (right.get_allocator() == alloc)
643 return std::move(right.mHashSet);
644 HashSet hashSet(right.mHashSet.GetHashTraits(), MemManager(alloc));
645 hashSet.MergeFrom(right.mHashSet);
649 template<
typename Iterator>
650 void pvInsert(Iterator first, Iterator last, std::true_type )
652 mHashSet.Insert(first, last);
655 template<
typename Iterator>
656 void pvInsert(Iterator first, Iterator last, std::false_type )
658 for (Iterator iter = first; iter != last; ++iter)
675 template<
typename TKey,
676 typename THashFunc = HashCoder<TKey>,
677 typename TEqualFunc = std::equal_to<TKey>,
678 typename TAllocator = std::allocator<TKey>>
680 HashSet<TKey, HashTraitsStd<TKey, THashFunc, TEqualFunc, HashBucketOpenDefault>,
681 MemManagerStd<TAllocator>>>
684 typedef unordered_set<TKey, THashFunc, TEqualFunc, TAllocator,
693 using UnorderedSet::UnorderedSet;
708 template<
typename ValueFilter>
715 #ifdef MOMO_HAS_DEDUCTION_GUIDES
717 #define MOMO_DECLARE_DEDUCTION_GUIDES(unordered_set) \
718 template<typename Iterator, \
719 typename Key = typename std::iterator_traits<Iterator>::value_type> \
720 unordered_set(Iterator, Iterator) \
721 -> unordered_set<Key>; \
722 template<typename Iterator, \
723 typename Key = typename std::iterator_traits<Iterator>::value_type, \
724 typename Allocator = std::allocator<Key>, \
725 typename = decltype(std::declval<Allocator&>().allocate(size_t{}))> \
726 unordered_set(Iterator, Iterator, size_t, Allocator = Allocator()) \
727 -> unordered_set<Key, HashCoder<Key>, std::equal_to<Key>, Allocator>; \
728 template<typename Iterator, typename HashFunc, \
729 typename Key = typename std::iterator_traits<Iterator>::value_type, \
730 typename Allocator = std::allocator<Key>, \
731 typename = decltype(std::declval<HashFunc&>()(std::declval<const Key&>())), \
732 typename = decltype(std::declval<Allocator&>().allocate(size_t{}))> \
733 unordered_set(Iterator, Iterator, size_t, HashFunc, Allocator = Allocator()) \
734 -> unordered_set<Key, HashFunc, std::equal_to<Key>, Allocator>; \
735 template<typename Iterator, typename HashFunc, typename EqualFunc, \
736 typename Key = typename std::iterator_traits<Iterator>::value_type, \
737 typename Allocator = std::allocator<Key>, \
738 typename = decltype(std::declval<HashFunc&>()(std::declval<const Key&>())), \
739 typename = decltype(std::declval<EqualFunc&>()(std::declval<const Key&>(), std::declval<const Key&>()))> \
740 unordered_set(Iterator, Iterator, size_t, HashFunc, EqualFunc, Allocator = Allocator()) \
741 -> unordered_set<Key, HashFunc, EqualFunc, Allocator>; \
742 template<typename Key> \
743 unordered_set(std::initializer_list<Key>) \
744 -> unordered_set<Key>; \
745 template<typename Key, \
746 typename Allocator = std::allocator<Key>, \
747 typename = decltype(std::declval<Allocator&>().allocate(size_t{}))> \
748 unordered_set(std::initializer_list<Key>, size_t, Allocator = Allocator()) \
749 -> unordered_set<Key, HashCoder<Key>, std::equal_to<Key>, Allocator>; \
750 template<typename Key, typename HashFunc, \
751 typename Allocator = std::allocator<Key>, \
752 typename = decltype(std::declval<HashFunc&>()(std::declval<const Key&>())), \
753 typename = decltype(std::declval<Allocator&>().allocate(size_t{}))> \
754 unordered_set(std::initializer_list<Key>, size_t, HashFunc, Allocator = Allocator()) \
755 -> unordered_set<Key, HashFunc, std::equal_to<Key>, Allocator>; \
756 template<typename Key, typename HashFunc, typename EqualFunc, \
757 typename Allocator = std::allocator<Key>, \
758 typename = decltype(std::declval<HashFunc&>()(std::declval<const Key&>())), \
759 typename = decltype(std::declval<EqualFunc&>()(std::declval<const Key&>(), std::declval<const Key&>()))> \
760 unordered_set(std::initializer_list<Key>, size_t, HashFunc, EqualFunc, Allocator = Allocator()) \
761 -> unordered_set<Key, HashFunc, EqualFunc, Allocator>;
763 MOMO_DECLARE_DEDUCTION_GUIDES(unordered_set)
764 MOMO_DECLARE_DEDUCTION_GUIDES(unordered_set_open)
766 #undef MOMO_DECLARE_DEDUCTION_GUIDES
768 #endif // MOMO_HAS_DEDUCTION_GUIDES
774 #endif // MOMO_INCLUDE_GUARD_STDISH_UNORDERED_SET
unordered_set(size_type bucketCount, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:119
unordered_set(Iterator first, Iterator last, size_type bucketCount, const hasher &hashFunc, const key_equal &equalFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:159
size_type bucket_count() const noexcept
Definition: unordered_set.h:568
Position position
Definition: IteratorUtility.h:183
static const size_t maxSize
Definition: Utility.h:384
TMemManager MemManager
Definition: HashSet.h:468
iterator erase(const_iterator first, const_iterator last)
Definition: unordered_set.h:520
ptrdiff_t difference_type
Definition: unordered_set.h:73
iterator insert(const_iterator hint, value_type &&value)
Definition: unordered_set.h:424
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, size_type > count(const KeyArg &key) const
Definition: unordered_set.h:382
const nested_container_type & get_nested_container() const noexcept
Definition: unordered_set.h:257
size_t size_type
Definition: unordered_set.h:72
unordered_set & operator=(unordered_set &&right) noexcept(std::is_empty< allocator_type >::value||std::allocator_traits< allocator_type >::propagate_on_container_move_assignment::value)
Definition: unordered_set.h:213
const_local_iterator begin(size_type bucketIndex) const
Definition: unordered_set.h:583
const_iterator begin() const noexcept
Definition: unordered_set.h:267
insert_return_type insert(node_type &&node)
Definition: unordered_set.h:450
unordered_set(const unordered_set &right, const momo::internal::Identity< allocator_type > &alloc)
Definition: unordered_set.h:206
size_type max_size() const noexcept
Definition: unordered_set.h:324
unordered_set(std::initializer_list< value_type > values, size_type bucketCount, const hasher &hashFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:177
MOMO_FORCEINLINE std::pair< const_iterator, const_iterator > equal_range(const key_type &key) const
Definition: unordered_set.h:399
unordered_set(unordered_set &&right) noexcept
Definition: unordered_set.h:190
bool operator!=(const unordered_set &right) const
Definition: unordered_set.h:634
iterator insert(const_iterator hint, node_type &&node)
Definition: unordered_set.h:459
const_iterator end() const noexcept
Definition: unordered_set.h:274
Definition: node_handle.h:190
const Item * Pointer
Definition: HashSet.h:204
void clear() noexcept
Definition: unordered_set.h:339
static UInt Log2(UInt value) noexcept
Definition: Utility.h:322
node_type extract(const_iterator where)
Definition: unordered_set.h:545
size_type max_bucket_count() const noexcept
Definition: unordered_set.h:562
size_t size_type
Definition: unordered_set.h:72
void insert(std::initializer_list< value_type > values)
Definition: unordered_set.h:477
local_iterator end(size_type bucketIndex)
Definition: unordered_set.h:588
unordered_set(size_type bucketCount, const hasher &hashFunc, const key_equal &equalFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:130
const Item & Reference
Definition: HashSet.h:203
unordered_set(const allocator_type &alloc)
Definition: unordered_set.h:114
void Reserve(size_t capacity)
Definition: HashSet.h:707
float load_factor() const noexcept
Definition: unordered_set.h:613
std::pair< iterator, bool > insert(const value_type &value)
Definition: unordered_set.h:434
momo::stdish::set is similar to std::set, but much more efficient in memory usage....
Definition: set.h:58
unordered_set(std::initializer_list< value_type > values, size_type bucketCount, const hasher &hashFunc, const key_equal &equalFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:183
MemManagerStd uses allocator<unsigned char>::allocate and deallocate
Definition: MemManager.h:176
friend size_type erase_if(unordered_set &cont, const ValueFilter &valueFilter)
Definition: unordered_set.h:540
unordered_set_open()
Definition: unordered_set.h:695
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, std::pair< const_iterator, const_iterator > > equal_range(const KeyArg &key) const
Definition: unordered_set.h:409
ConstIterator Remove(ConstIterator iter)
Definition: HashSet.h:847
void insert(Iterator first, Iterator last)
Definition: unordered_set.h:472
Definition: IteratorUtility.h:151
unordered_set(Iterator first, Iterator last, size_type bucketCount, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:143
Definition: HashSet.h:277
key_type value_type
Definition: unordered_set.h:75
MOMO_NODISCARD bool empty() const noexcept
Definition: unordered_set.h:334
unordered_set()
Definition: unordered_set.h:110
InsertResult Insert(Item &&item)
Definition: HashSet.h:769
void max_load_factor(float maxLoadFactor)
Definition: unordered_set.h:296
unordered_set(const unordered_set &right)
Definition: unordered_set.h:201
THashTraits HashTraits
Definition: HashSet.h:467
typename std::enable_if< value, Type >::type EnableIf
Definition: Utility.h:174
bool operator==(const unordered_set &right) const
Definition: unordered_set.h:622
iterator emplace_hint(const_iterator hint, ValueArgs &&... valueArgs)
Definition: unordered_set.h:503
unordered_set & operator=(std::initializer_list< value_type > values)
Definition: unordered_set.h:239
TAllocator allocator_type
Definition: unordered_set.h:68
size_type bucket_size(size_type bucketIndex) const
Definition: unordered_set.h:573
MOMO_FORCEINLINE size_type count(const key_type &key) const
Definition: unordered_set.h:375
TKey key_type
Definition: unordered_set.h:65
hasher hash_function() const
Definition: unordered_set.h:309
const nested_container_type & get_nested_container() const noexcept
Definition: set.h:217
static const size_t bucketMaxItemCount
Definition: HashSet.h:502
const_local_iterator end(size_type bucketIndex) const
Definition: unordered_set.h:593
bool inserted
Definition: IteratorUtility.h:186
EnableIf< true, Type > Identity
Definition: Utility.h:177
HashSet::Iterator iterator
Definition: unordered_set.h:78
void reserve(size_type count)
Definition: unordered_set.h:352
allocator_type get_allocator() const noexcept
Definition: unordered_set.h:319
value_type & reference
Definition: unordered_set.h:81
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, bool > contains(const KeyArg &key) const
Definition: unordered_set.h:394
key_equal key_eq() const
Definition: unordered_set.h:314
momo::stdish::unordered_set_open is similar to std::unordered_set, but much more efficient in operati...
Definition: unordered_set.h:682
TSetExtractedItem SetExtractedItem
Definition: node_handle.h:31
const_local_iterator cbegin(size_type bucketIndex) const
Definition: unordered_set.h:598
Definition: HashTraits.h:196
HashSet::ConstIterator const_iterator
Definition: unordered_set.h:77
const_iterator::Reference const_reference
Definition: unordered_set.h:82
unordered_set(std::initializer_list< value_type > values, size_type bucketCount, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:171
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:495
size_type bucket(const key_type &key) const
Definition: unordered_set.h:608
friend size_type erase_if(unordered_set_open &cont, const ValueFilter &valueFilter)
Definition: unordered_set.h:709
THashFunc hasher
Definition: unordered_set.h:66
unordered_set & operator=(const unordered_set &right)
Definition: unordered_set.h:227
iterator insert(const_iterator hint, const value_type &value)
Definition: unordered_set.h:440
void merge(Set &&set)
Definition: unordered_set.h:557
Definition: ArrayUtility.h:299
#define MOMO_FORCEINLINE
Definition: UserSettings.h:114
MOMO_FORCEINLINE momo::internal::EnableIf< IsValidKeyArg< KeyArg >::value, const_iterator > find(const KeyArg &key) const
Definition: unordered_set.h:366
TEqualFunc key_equal
Definition: unordered_set.h:67
HashSet nested_container_type
Definition: unordered_set.h:70
void swap(unordered_set &right) noexcept
Definition: unordered_set.h:245
unordered_set(Iterator first, Iterator last, size_type bucketCount, const hasher &hashFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:151
node_type extract(const key_type &key)
Definition: unordered_set.h:550
const_local_iterator local_iterator
Definition: unordered_set.h:94
const_iterator::Pointer const_pointer
Definition: unordered_set.h:86
unordered_set(size_type bucketCount, const hasher &hashFunc, const allocator_type &alloc=allocator_type())
Definition: unordered_set.h:124
const_iterator cbegin() const noexcept
Definition: unordered_set.h:281
size_type size() const noexcept
Definition: unordered_set.h:329
Definition: node_handle.h:29
std::pair< iterator, bool > insert(value_type &&value)
Definition: unordered_set.h:418
HashSet::ConstBucketBounds::Iterator const_local_iterator
Definition: unordered_set.h:93
const_iterator cend() const noexcept
Definition: unordered_set.h:286
std::pair< iterator, bool > emplace(ValueArgs &&... valueArgs)
Definition: unordered_set.h:483
unordered_set(Iterator first, Iterator last)
Definition: unordered_set.h:137
void rehash(size_type bucketCount)
Definition: unordered_set.h:344
MOMO_FORCEINLINE bool contains(const key_type &key) const
Definition: unordered_set.h:387
unordered_set(std::initializer_list< value_type > values)
Definition: unordered_set.h:166
momo::stdish::unordered_set is similar to std::unordered_set, but much more efficient in memory usage...
Definition: unordered_set.h:58
unordered_set_open & operator=(std::initializer_list< value_type > values)
Definition: unordered_set.h:697
MOMO_FORCEINLINE const_iterator find(const key_type &key) const
Definition: unordered_set.h:357
size_type erase(const key_type &key)
Definition: unordered_set.h:534
nested_container_type & get_nested_container() noexcept
Definition: unordered_set.h:262
internal::set_node_handle< typename HashSet::ExtractedItem > node_type
Definition: unordered_set.h:90
unordered_set(unordered_set &&right, const momo::internal::Identity< allocator_type > &alloc) noexcept(std::is_empty< allocator_type >::value)
Definition: unordered_set.h:195
#define MOMO_ASSERT(expr)
Definition: UserSettings.h:162
friend void swap(unordered_set &left, unordered_set &right) noexcept
Definition: unordered_set.h:252
#define MOMO_DECLARE_PROXY_FUNCTION(Object, Func, Result)
Definition: Utility.h:94
Definition: SetUtility.h:344
value_type * pointer
Definition: unordered_set.h:85
iterator erase(const_iterator where)
Definition: unordered_set.h:513
internal::insert_return_type< iterator, node_type > insert_return_type
Definition: unordered_set.h:91
#define MOMO_NODISCARD
Definition: UserSettings.h:192
friend void swap(unordered_set_open &left, unordered_set_open &right) noexcept
Definition: unordered_set.h:703
float max_load_factor() const noexcept
Definition: unordered_set.h:291
local_iterator begin(size_type bucketIndex)
Definition: unordered_set.h:578
const_local_iterator cend(size_type bucketIndex) const
Definition: unordered_set.h:603