Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
KeyedCollection_Factory.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
7
8namespace Stroika::Foundation::Containers::Factory {
9
10 /*
11 ********************************************************************************
12 ****** KeyedCollection_Factory<T, KEY_TYPE, TRAITS, KEY_EQUALS_COMPARER> *******
13 ********************************************************************************
14 */
15 template <typename T, typename KEY_TYPE, typename TRAITS, typename KEY_EQUALS_COMPARER>
17 : fFactory_{f}
18 {
19 }
20 template <typename T, typename KEY_TYPE, typename TRAITS, typename KEY_EQUALS_COMPARER>
25 template <typename T, typename KEY_TYPE, typename TRAITS, typename KEY_EQUALS_COMPARER>
27 : fFactory_{nullptr}
28 {
29 }
30 template <typename T, typename KEY_TYPE, typename TRAITS, typename KEY_EQUALS_COMPARER>
32 {
33 return AccessDefault_ ();
34 }
35 template <typename T, typename KEY_TYPE, typename TRAITS, typename KEY_EQUALS_COMPARER>
37 const KEY_EQUALS_COMPARER& keyComparer) const -> ConstructedType
38 {
39 if (this->fFactory_ == nullptr) [[likely]] {
40 if constexpr (same_as<KEY_EQUALS_COMPARER, equal_to<KEY_TYPE>>
41#if defined(__clang__) || defined(__GNUC__)
42 // if compilerbug add define - but inviestigate a bit first...
43 // SHOULD BE DONE AUTOMATICALLY WITH constructible_from below
44 // but fails on ??? (list - maybe macosx and g++14 on ubuntu 24.04)
45 and Cryptography::Digest::IHashFunction<hash<KEY_TYPE>, KEY_TYPE> and IEqualsComparer<equal_to<KEY_TYPE>, KEY_TYPE>
46#else
47 // this works on VisualStudio --LGP 2025-04-10
48 and constructible_from<Concrete::KeyedCollection_HashTable<T, KEY_TYPE, TRAITS>, KeyExtractorType>
49#endif
50 ) {
52 }
53 else if constexpr (default_initializable<Concrete::SortedKeyedCollection_stdset<T, KEY_TYPE, TRAITS>> and
54 same_as<KEY_EQUALS_COMPARER, equal_to<KEY_TYPE>>) {
55 return Concrete::SortedKeyedCollection_stdset<T, KEY_TYPE, TRAITS>{keyExtractor}; // if using == as equals comparer, just map to < for in-order comparison
56 }
57 else {
58 /*
59 * Note - though this is not an efficient implementation of KeyedCollection<> for large sizes, its probably the most
60 * efficient representation which adds no requirements to KEY_TYPE, such as operator< (or a traits less) or
61 * a hash function. And its quite reasonable for small KeyedCollection's - which are often the case.
62 *
63 * Note, array CAN be slower than LinkedList as the size grows (array faster when small due to better locality).
64 * But the whole thing bogs down no matter what, when larger, cuz you really need some indexed data structure like a tree.
65 */
66 return Concrete::KeyedCollection_Array<T, KEY_TYPE, TRAITS>{keyExtractor, keyComparer};
67 }
68 }
69 else {
70 return this->fFactory_ (keyExtractor, keyComparer);
71 }
72 }
73 template <typename T, typename KEY_TYPE, typename TRAITS, typename KEY_EQUALS_COMPARER>
75 {
76 AccessDefault_ () = f.has_value () ? *f : KeyedCollection_Factory{Hints{}};
77 }
78 template <typename T, typename KEY_TYPE, typename TRAITS, typename KEY_EQUALS_COMPARER>
80 {
81 static KeyedCollection_Factory sDefault_{Hints{}};
82 return sDefault_;
83 }
84
85}
KeyedCollection_Array<T> is an Array-based concrete implementation of the KeyedCollection<T> containe...
KeyedCollection_HashTable<T,KEY_TYPE> is a HashTable based concrete implementation of the KeyedCollec...
SortedKeyedCollection_stdset<KEY_TYPE,MAPPED_VALUE_TYPE> is an std::map-based concrete implementation...
Singleton factory object - Used to create the default backend implementation of a KeyedCollection<> c...
nonvirtual ConstructedType operator()(const KeyExtractorType &keyExtractor, const KEY_EQUALS_COMPARER &keyComparer) const
static void Register(const optional< KeyedCollection_Factory > &f=nullopt)
a cross between Mapping<KEY, T> and Collection<T> and Set<T>
check argument FUNCTION is callable with a HASHABLE_T, and produces (something convertible to) size_t
Definition HashBase.h:28