Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
DataHyperRectangle.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_DataHyperRectangle_h_
5#define _Stroika_Foundation_Containers_DataHyperRectangle_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
10#include "Stroika/Foundation/Containers/Common.h"
12
13/**
14 * \file
15 *
16 * \note Code-Status: <a href="Code-Status.md#Alpha">Alpha</a>
17 *
18 * TODO:
19 * @todo http://stroika-bugs.sophists.com/browse/STK-580 - get DataHyperRectangle to the point where it is minimally usable!
20 *
21 * @todo DataHyperRectangle<int, int, int> x = Concrete::DenseDataHyperRectangle_Vector<int, int, int>{3, 4}; simplified - not saying int so many times.
22 * make use make_DataHyperRectangle () - cuz I think functions can do type inference where classes cannot.
23 *
24 * @todo MAYBE operator== ()/!= should be on _Sparse/_Dense - not here!
25 *
26 * @todo need ability to return reference somehow... a[3][4] = 3; or a[3][4].x = 3;
27 *
28 * @todo need ability create arbitrary subslices - like numpy ... a[3..4] or a[3..4,_,5]... will need some alternate syntax
29 *
30 */
31
33
35 using Traversal::Iterable;
36 using Traversal::Iterator;
37
38 /**
39 * \note Aliases: Data-Cube, Date Cube, Hyper-Cube, Hypercube, Tensor, Matrix, Vector
40 *
41 * \note @todo - CONSIDER THE POINT/UTILITY OF THIS IN LIGHT OF std::mdspan, and if still a point,
42 * must integrate well with mdspan - maybe just a Stroika v3.1 issue since mdspan in C++23
43 *
44 * PROBABLY - it still makes sense to keep. I THINK mdspan<> is restricted to DENSE_HYPERRECTANGLES of data
45 * Making this abstraction still potentially useful.
46 *
47 * \note <a href="Design-Overview.md#Comparisons">Comparisons</a>:
48 * o operator== and operator!= are supported
49 *
50 * Two DataHyperRectangle are considered equal if they contain the same elements (by comparing them with ELEMENT_EQUALS_COMPARER)
51 * in exactly the same order.
52 *
53 * A DataHyperRectangle<T, INDEXES...> doesn't generally require a comparison for individual elements
54 * be be defined, but obviously to compare if the containers are equal, you must
55 * compare the individual elements (at least sometimes).
56 *
57 * If == is predefined (on T), you can just call == - but if its not, or if you wish
58 * to compare with an alternative comparer, just pass it as a template parameter (via DataHyperRectangle<>::EqualsComparer).
59 */
60 template <typename T, typename... INDEXES>
61 class [[nodiscard]] DataHyperRectangle : public Iterable<tuple<T, INDEXES...>> {
62 private:
63 using inherited = Iterable<tuple<T, INDEXES...>>;
64
65 protected:
66 class _IRep;
67
68 public:
69 using value_type = typename inherited::value_type;
70
71 public:
72 /**
73 * Use this typedef in templates to recover the basic functional container pattern of concrete types.
74 */
76
77 public:
78 /**
79 * \note - its typically best to select a specific subtype of DataHyperRectangle to construct so you can
80 * specify the appropriate additional parameters. Typically use subclasses
81 * @see SparseDataHyperRectangle or @see DenseDataHyperRectangle.
82 *
83 * \par Example:
84 * DataHyperRectangle2<int> x1 = DenseDataHyperRectangle_Vector<int, size_t, size_t>{3, 4};
85 * DataHyperRectangle2<int> x1 = Concrete::DenseDataHyperRectangle_Vector<int, size_t, size_t>{3, 4};
86 * DataHyperRectangle2<int> x2 = Concrete::SparseDataHyperRectangle_stdmap<int, size_t, size_t>{};
87 */
90
91 protected:
92 explicit DataHyperRectangle (const shared_ptr<_IRep>& src) noexcept;
93 explicit DataHyperRectangle (shared_ptr<_IRep>&& src) noexcept;
94
95 public:
96 /**
97 */
98 nonvirtual DataHyperRectangle<T, INDEXES...>& operator= (const DataHyperRectangle<T, INDEXES...>& rhs) = default;
99 nonvirtual DataHyperRectangle<T, INDEXES...>& operator= (DataHyperRectangle<T, INDEXES...>&& rhs) = default;
100
101 public:
102 /**
103 */
104 nonvirtual T GetAt (INDEXES... indexes) const;
105
106 public:
107 /**
108 * \note mutates container
109 */
110 nonvirtual void SetAt (INDEXES... indexes, Common::ArgByValueType<T> v);
111
112 private:
113 template <typename INDEX, typename... REST_OF_INDEXES>
114 struct TemporarySliceReference_ {
115 const DataHyperRectangle<T, INDEXES...>& fCube;
116 tuple<REST_OF_INDEXES...> fSliceIdxes;
117 T operator[] (INDEX i) const
118 {
119 return fCube.GetAt (i, forward<REST_OF_INDEXES> (fSliceIdxes)...);
120 }
121 };
122
123 public:
124 /**
125 * EXAMPLE USAGE:
126 * DataHyperRectangle<double, int, int> m (2,2);
127 * Assert (m[1][1] == 0);
128 */
129 template <typename INDEX, typename... REST_OF_INDEXES>
130 nonvirtual TemporarySliceReference_<REST_OF_INDEXES...> operator[] (INDEX i1) const;
131
132 public:
133 template <typename ELEMENT_EQUALS_COMPARER = equal_to<T>>
134 struct EqualsComparer;
135
136 public:
137 /**
138 */
139 constexpr bool operator== (const DataHyperRectangle& rhs) const
140 requires (equality_comparable<T>);
141
142 protected:
143 /**
144 */
145 template <typename T2>
146 using _SafeReadRepAccessor = typename inherited::template _SafeReadRepAccessor<T2>;
147
148 protected:
149 /**
150 */
151 template <typename T2>
152 using _SafeReadWriteRepAccessor = typename inherited::template _SafeReadWriteRepAccessor<T2>;
153
154 protected:
155 nonvirtual void _AssertRepValidType () const;
156 };
157
158 /**
159 * \brief Implementation detail for DataHyperRectangle<T, INDEXES...> implementors.
160 *
161 * Protected abstract interface to support concrete implementations of
162 * the DataHyperRectangle<T, INDEXES...> container API.
163 */
164 template <typename T, typename... INDEXES>
165 class DataHyperRectangle<T, INDEXES...>::_IRep : public Iterable<tuple<T, INDEXES...>>::_IRep {
166 private:
167 using inherited = _IRep;
168
169 protected:
170 _IRep () = default;
171
172 public:
173 virtual ~_IRep () = default;
174
175 public:
176 virtual shared_ptr<_IRep> CloneEmpty () const = 0;
177 virtual T GetAt (INDEXES... indexes) const = 0;
178 virtual void SetAt (INDEXES... indexes, Common::ArgByValueType<T> v) = 0;
179 };
180
181 /**
182 */
183 template <typename T, typename... INDEXES>
184 template <typename ELEMENT_EQUALS_COMPARER>
185 struct DataHyperRectangle<T, INDEXES...>::EqualsComparer : Common::ComparisonRelationDeclarationBase<Common::ComparisonRelationType::eEquals> {
186 constexpr EqualsComparer (const ELEMENT_EQUALS_COMPARER& elementComparer = {});
187 nonvirtual bool operator() (const DataHyperRectangle& lhs, const DataHyperRectangle& rhs) const;
188 ELEMENT_EQUALS_COMPARER fElementComparer_;
189 };
190
191 namespace Private_DataHyperRectangle_ {
192 template <typename T, template <typename, typename...> class BASE_TEMPLATE>
193 struct NTemplate {
194 template <typename>
195 struct Helper_;
196 template <size_t... S>
197 struct Helper_<index_sequence<S...>> {
198 using CombinedType = BASE_TEMPLATE<T, decltype (S)...>;
199 };
200 };
201 }
202
203 /**
204 * using DataHyperRectangleN = DataHyperRectangle<T, size_t N REPEATED TIMES>
205 */
206 template <typename T, size_t N>
208 typename Private_DataHyperRectangle_::template NTemplate<T, DataHyperRectangle>::template Helper_<make_index_sequence<N>>::CombinedType;
209
210}
211
212/*
213 ********************************************************************************
214 ******************************* Implementation Details *************************
215 ********************************************************************************
216 */
217
218#include "DataHyperRectangle.inl"
219
220#endif /*_Stroika_Foundation_Containers_DataHyperRectangle_h_ */
Implementation detail for DataHyperRectangle<T, INDEXES...> implementors.
nonvirtual void SetAt(INDEXES... indexes, Common::ArgByValueType< T > v)
Iterable<T> is a base class for containers which easily produce an Iterator<T> to traverse them.
Definition Iterable.h:237
T value_type
value_type is an alias for the type iterated over - like vector<T>::value_type
Definition Iterable.h:248
conditional_t<(sizeof(CHECK_T)<=2 *sizeof(void *)) and is_trivially_copyable_v< CHECK_T >, CHECK_T, const CHECK_T & > ArgByValueType
This is an alias for 'T' - but how we want to pass it on stack as formal parameter.
Definition TypeHints.h:32
typename Private_DataHyperRectangle_::template NTemplate< T, DataHyperRectangle >::template Helper_< make_index_sequence< N > >::CombinedType DataHyperRectangleN