Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Collection_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 ****************************** Collection_Factory<T> ***************************
13 ********************************************************************************
14 */
15 template <typename T>
16 constexpr Collection_Factory<T>::Collection_Factory (const FactoryFunctionType& f)
17 : fFactory_{f}
18 {
19 }
20 template <typename T>
22 : Collection_Factory{AccessDefault_ ()}
23 {
24 }
25 template <typename T>
26 constexpr Collection_Factory<T>::Collection_Factory (const Hints& hints)
27 : fFactory_{nullptr}
28 , fHints_OptimizeForLookupSpeedOverUpdateSpeed{hints.fOptimizeForLookupSpeedOverUpdateSpeed.value_or (true)}
29 {
30 }
31 template <typename T>
33 {
34 return AccessDefault_ ();
35 }
36 template <typename T>
38 {
39 if (this->fFactory_ == nullptr) [[likely]] {
40 if constexpr (totally_ordered<T>) {
41 // faster adds/removes - same size - so better if possible to use (unless very small collections maybe)
42 static const auto kDefault_ = Concrete::SortedCollection_stdmultiset<T>{};
43 return kDefault_;
44 }
45 else {
46 if (fHints_OptimizeForLookupSpeedOverUpdateSpeed) [[likely]] {
47 // questionable choice. For smaller sizes, probably faster, due to better locality.
48 // but adds can occasionally be slow (realloc/O(N)) instead of O(1).
49 static const auto kDefault_ = Concrete::Collection_Array<T>{};
50 return kDefault_;
51 }
52 else {
53 // This generally performs well, so long as you don't call 'size'
54 static const auto kDefault_ = Concrete::Collection_LinkedList<T>{};
55 return kDefault_;
56 }
57 }
58 }
59 else {
60 return this->fFactory_ ();
61 }
62 }
63 template <typename T>
64 void Collection_Factory<T>::Register (const optional<Collection_Factory>& f)
65 {
66 AccessDefault_ () = f.has_value () ? *f : Collection_Factory{Hints{}};
67 }
68 template <typename T>
70 {
71 static Collection_Factory sDefault_{Hints{}};
72 return sDefault_;
73 }
74
75}
A Collection<T> is a container to manage an un-ordered collection of items, without equality defined ...
Definition Collection.h:102
Collection_Array<T> is an Array-based concrete implementation of the Collection<T> container pattern.
Collection_LinkedList<T> is an LinkedList-based concrete implementation of the Collection<T> containe...
SortedCollection_stdmultiset<T> is an stdmultiset-based concrete implementation of the SortedCollecti...
Singleton factory object - Used to create the default backend implementation of a Collection<> contai...
static void Register(const optional< Collection_Factory > &f=nullopt)