Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
KeyedCollection_stdhashset.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Containers_Concrete_KeyedCollection_stdhashset_h_
5#define _Stroika_Foundation_Containers_Concrete_KeyedCollection_stdhashset_h_
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <set>
10#include <unordered_set>
11
12#include "Stroika/Foundation/Containers/KeyedCollection.h"
13#include "Stroika/Foundation/Cryptography/Digest/HashBase.h"
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 */
21
22 /**
23 * \brief KeyedCollection_stdhashset<KEY_TYPE,MAPPED_VALUE_TYPE> is an std::map-based concrete implementation of the KeyedCollection<KEY_TYPE,MAPPED_VALUE_TYPE> container pattern.
24 *
25 * \note Runtime performance/complexity:
26 * @todo
27 *
28 * \note \em Thread-Safety <a href="Thread-Safety.md#C++-Standard-Thread-Safety">C++-Standard-Thread-Safety</a>
29 */
30 template <typename T, typename KEY_TYPE, typename TRAITS = KeyedCollection_DefaultTraits<T, KEY_TYPE>>
31 class KeyedCollection_stdhashset : public KeyedCollection<T, KEY_TYPE, TRAITS> {
32 private:
34
35 public:
36 using TraitsType = typename inherited::TraitsType;
37 using KeyExtractorType = typename inherited::KeyExtractorType;
38 using KeyEqualityComparerType = typename inherited::KeyEqualityComparerType;
39 using KeyType = typename inherited::KeyType;
40 using key_type = typename inherited::key_type;
41 using value_type = typename inherited::value_type;
42
43 public:
44 /**
45 * An ELEMENT is of type T, but the KEY is of a separate type.
46 * For a keyed collection, we auto-extract the key from the type T, but store the type T.
47 * We internally must count on comparing elements of type KEY_TYPE (after extraction).
48 *
49 * This helper allows comparing either KEY or T types interchangeably.
50 *
51 * Used in STDHASHSET definition, and typically nowhere else
52 */
53 template <typename KEY_EQUALS_COMPARER = equal_to<key_type>>
55 static_assert (not is_reference_v<KEY_EQUALS_COMPARER>);
56 constexpr ElementEqualsComparer (const KeyExtractorType& keyExtractor = {}, const KEY_EQUALS_COMPARER& keyEqualsComparer = {})
57 : fKeyExtractor_{keyExtractor}
58 , fKeyComparer{keyEqualsComparer}
59 {
60 }
61 constexpr int operator() (const value_type& lhs, const KEY_TYPE& rhs) const
62 {
63 return fKeyComparer (fKeyExtractor_ (lhs), rhs);
64 };
65 constexpr int operator() (const KEY_TYPE& lhs, const value_type& rhs) const
66 {
67 return fKeyComparer (lhs, fKeyExtractor_ (rhs));
68 };
69 constexpr int operator() (const value_type& lhs, const value_type& rhs) const
70 {
71 return fKeyComparer (fKeyExtractor_ (lhs), fKeyExtractor_ (rhs));
72 };
73 [[no_unique_address]] const KeyExtractorType fKeyExtractor_;
74 [[no_unique_address]] const KEY_EQUALS_COMPARER fKeyComparer;
75 using is_transparent = int; // see https://en.cppreference.com/w/cpp/container/set/find - allows overloads to lookup by key
76 };
77
78 public:
79 /**
80 * An ELEMENT is of type T, but the KEY is of a separate type.
81 * For a keyed collection, we auto-extract the key from the type T, but store the type T.
82 * We internally must count on comparing elements of type KEY_TYPE (after extraction).
83 *
84 * This helper allows hashing either KEY or T types interchangeably.
85 *
86 * Used in STDHASHSET definition, and typically nowhere else
87 */
88 template <typename KEY_HASHER = std::hash<key_type>>
89 struct ElementHash {
90 constexpr ElementHash (const KeyExtractorType& keyExtractor = {}, const KEY_HASHER& kh = {})
91 : fKeyExtractor_{keyExtractor}
92 , fKeyHasher{kh}
93 {
94 }
95 auto operator() (const key_type& k) const noexcept
96 {
97 return fKeyHasher (k);
98 }
99 auto operator() (const value_type& v) const noexcept
100 {
101 return fKeyHasher (fKeyExtractor_ (v));
102 }
103 [[no_unique_address]] const KeyExtractorType fKeyExtractor_;
104 [[no_unique_address]] const KEY_HASHER fKeyHasher;
105
106 using is_transparent = int; // see https://en.cppreference.com/w/cpp/container/set/find - allows overloads to lookup by key
107 };
108
109 public:
110 /**
111 * \brief STDHASHSET is std::unordered_set<> that can be used inside KeyedCollection_stdhashset (need specific traits on unordered set)
112 *
113 * user providers a hasher on KEY_TYPE, but the std::unordered_set holds a T, so it needs a hasher
114 * that works on either T or KEY_TYPE. Similarly for the EqualsComparers
115 *
116 * \see https://en.cppreference.com/w/cpp/container/unordered_set
117 */
118 template <typename KEY_HASH = std::hash<key_type>, typename KEY_EQUALS_COMPARER = std::equal_to<key_type>>
119 using STDHASHSET = unordered_set<T, ElementHash<KEY_HASH>, ElementEqualsComparer<KEY_EQUALS_COMPARER>>;
120
121 public:
122 /**
123 * @todo consider adding more CTOR overloads... Like with base class KeyedCollection
124 */
125 template <typename KEY_HASH = std::hash<KEY_TYPE>, typename KEY_EQUALS_COMPARER = equal_to<KEY_TYPE>>
126 KeyedCollection_stdhashset (const KeyExtractorType& keyExtractor = {}, KEY_HASH&& keyHasher = {},
127 KEY_EQUALS_COMPARER&& keyComparer = KEY_EQUALS_COMPARER{})
128 requires (IEqualsComparer<KEY_EQUALS_COMPARER, KEY_TYPE> and Cryptography::Digest::IHashFunction<KEY_HASH, KEY_TYPE>);
129
130 KeyedCollection_stdhashset (KeyedCollection_stdhashset&& src) noexcept = default;
131 KeyedCollection_stdhashset (const KeyedCollection_stdhashset& src) noexcept = default;
132
133 public:
134 /**
135 */
136 nonvirtual KeyedCollection_stdhashset& operator= (KeyedCollection_stdhashset&& rhs) noexcept = default;
137 nonvirtual KeyedCollection_stdhashset& operator= (const KeyedCollection_stdhashset& rhs) = default;
138
139 private:
140 using IImplRepBase_ = typename KeyedCollection<T, KEY_TYPE, TRAITS>::_IRep;
141 template <typename KEY_EXTRACTOR, typename KEY_INORDER_COMPARER>
142 class Rep_;
143
144 private:
145 nonvirtual void AssertRepValidType_ () const;
146 };
147
148}
149
150/*
151 ********************************************************************************
152 ******************************* Implementation Details *************************
153 ********************************************************************************
154 */
155#include "KeyedCollection_stdhashset.inl"
156
157#endif /*_Stroika_Foundation_Containers_Concrete_KeyedCollection_stdhashset_h_ */
KeyedCollection_stdhashset<KEY_TYPE,MAPPED_VALUE_TYPE> is an std::map-based concrete implementation o...
unordered_set< T, ElementHash< KEY_HASH >, ElementEqualsComparer< KEY_EQUALS_COMPARER > > STDHASHSET
STDHASHSET is std::unordered_set<> that can be used inside KeyedCollection_stdhashset (need specific ...
a cross between Mapping<KEY, T> and Collection<T> and Set<T>
typename TRAITS::KeyExtractorType KeyExtractorType
Common::ComparisonRelationDeclaration< Common::ComparisonRelationType::eEquals, function< bool(ArgByValueType< KEY_TYPE >, ArgByValueType< KEY_TYPE >)> > KeyEqualityComparerType