FiberVISH 0.2
Fish - The Fiber Bundle API for the Vish Visualization Shell
field/DynamicSize.hpp
1
2//
3// $Id: DynamicSize.hpp,v 1.5 2008/03/10 20:20:50 werner Exp $
4//
6#ifndef __FIBER_DYNAMICSIZE_HPP
7#define __FIBER_DYNAMICSIZE_HPP
8
9#include "FieldAPI.h"
10#include <memcore/RefPtr.hpp>
11#include <memcore/Interface.hpp>
12#include <fiber/vector/MultiIndex.hpp>
13
14namespace Fiber
15{
16
29{
30public:
31 using rank_t = int;
32
33private:
34
35 rank_t rank = 0;
36 index_t *dims = nullptr;
37
38public:
40 template <Dims_t N>
42 : rank(N)
43 {
44 dims = new index_t[ N ];
45 for(Dims_t i=0; i<N; i++)
46 dims[i] = MI[i];
47 }
48
50 template <class T>
51 DynamicSize(Dims_t N, const T values[])
52 : rank(N)
53 {
54 dims = new index_t[ N ];
55 for(Dims_t i=0; i<N; i++)
56 dims[i] = values[i];
57 }
58
64 : rank(N)
65 {
66 if (N>0)
67 {
68 dims = new index_t[ N ];
69 for(Dims_t i=0; i<N; i++)
70 dims[i] = NValue;
71 }
72 else
73 dims = 0;
74 }
75
77 DynamicSize() = default;
78
81
86
88 DynamicSize(int N, const DynamicSize&L, const DynamicSize&R);
89
92
95
97 bool operator!=(const DynamicSize&DS) const noexcept;
98
100 bool operator==(const DynamicSize&DS) const noexcept
101 {
102 return !( (*this) != DS );
103 }
104
106 template <class T>
107 DynamicSize&setSize(int N, const T values[])
108 {
109 delete [] dims;
110 if (N>0)
111 {
112 rank = N;
113 dims = new index_t[ N ];
114 for(int i=0; i<N; i++)
115 dims[i] = values[i];
116 }
117 else
118 {
119 rank = 0;
120 dims = 0;
121 }
122 return *this;
123 }
124
128 template <Dims_t N>
130 {
131 delete [] dims;
132 if (N>0)
133 {
134 rank = N;
135 dims = new index_t[ N ];
136 for(Dims_t i=0; i<N; i++)
137 dims[i] = MI[i];
138 }
139 else
140 {
141 rank = 0;
142 dims = 0;
143 }
144 return *this;
145 }
146
148 bool hasIncompatibleLowerDims(const DynamicSize&DS) const noexcept;
149
151 bool hasIncompatibleHigherDims(const DynamicSize&DS) const noexcept;
152
154 bool hasCompatibleHigherDims(const DynamicSize&DS) const noexcept
155 {
156 return !hasIncompatibleHigherDims(DS);
157 }
158
168 {
169 if (getNumberOfElements() == DataFragmentDims.getNumberOfElements())
170 return 1; // independent of rank!!
171
172 if (DataFragmentDims.getRank() == getRank()+1 &&
173 DataFragmentDims.hasCompatibleHigherDims(*this) )
174 {
175 return DataFragmentDims[ 0 ];
176 }
177
178 return 0; // mismatch
179 }
180
181
183 bool hasCompatibleLowerDims(const DynamicSize&DS) const noexcept
184 {
185 return !hasIncompatibleLowerDims(DS);
186 }
187
192 {
193 if (!dims) return 0;
194
195 index_t N = 1;
196 for(rank_t i=0; i<rank; i++)
197 {
198 N *= dims[i];
199 }
200 return N;
201 }
202
204 index_t getNumberOfElements() const
205 {
206 return nElements();
207 }
208
212 rank_t Rank() const
213 {
214 return rank;
215 }
216
221 {
222 return rank;
223 }
224
229 {
230 return rank;
231 }
232
239 index_t operator[](int i) const
240 {
241 if (!dims)
242 return 0;
243
244 if (i>=0 && i<rank)
245 return dims[i];
246 else
247 return 1;
248 }
249
253 const index_t *dims_ptr() const
254 {
255 return dims;
256 }
257
261 bool isValid() const
262 {
263 return nullptr != dims;
264 }
265
272 bool setSize(int i, index_t what)
273 {
274 if (!dims)
275 return false;
276
277 if (i>=0 && i<rank)
278 {
279 dims[i] = what;
280 return true;
281 }
282
283 return true;
284 }
285
286
290 template <Dims_t N>
292 {
293 if (!dims)
294 return false;
295
296 int I = rank;
297 if (I>N) I = N;
298
299 for(Dims_t i=0; i<I; i++)
300 {
301 dims[i] = MI[i];
302 }
303 return true;
304 }
305
306
311 {
312 if (!dims)
313 return false;
314
315 for(int i=0; i<rank; i++)
316 {
317 dims[i] += incr;
318 }
319 return true;
320 }
321
326 {
327 if (!dims)
328 return false;
329
330 for(int i=0; i<rank; i++)
331 {
332 dims[i] *= incr;
333 }
334 return true;
335 }
336
337 DynamicSize&increment(const DynamicSize&D)
338 {
339 if (!dims)
340 {
341 dims = new index_t[D.rank];
342 rank = D.rank;
343 for(rank_t i=0; i<rank; i++)
344 dims[i] = D.dims[i];
345 return *this;
346 }
347
348 if (rank<D.rank)
349 {
350 index_t *new_dims = new index_t[D.rank];
351 for(rank_t i=0; i<D.rank; i++)
352 new_dims[i] = D.dims[i];
353
354 for(rank_t i=0; i<rank; i++)
355 new_dims[i] += dims[i];
356
357 delete [] dims;
358 dims = new_dims;
359 rank = D.rank;
360 return *this;
361 }
362
363 for(rank_t i=0; i<D.rank; i++)
364 {
365 dims[i] += D.dims[i];
366 }
367
368 return *this;
369 }
370
371 DynamicSize&operator+=(const DynamicSize&D)
372 {
373 return increment(D);
374 }
375
382 bool setSize(const DynamicSize&D, int FillUp = 1) const
383 {
384 if (!dims)
385 return false;
386
387 if (!D.dims)
388 return false;
389
390 for(rank_t i=0; i<rank; i++)
391 {
392 if (i<D.rank)
393 {
394 dims[i] = D.dims[i];
395 }
396 else
397 dims[i] = FillUp;
398 }
399
400 return true;
401 }
402
403
404 template <typename T>
405 rank_t getDims(T*Dims, int MaxEntries) const
406 {
407 if (!dims)
408 return 0;
409
410 rank_t i = 0;
411 for(; i<rank; i++)
412 {
413 if (i>=MaxEntries)
414 break;
415
416 Dims[i] = dims[i];
417 }
418
419 for(; i<MaxEntries; i++)
420 Dims[i] = 0;
421
422 return rank;
423 }
424
425 template <typename T, int N>
426 rank_t getDims(T(&Dims)[N]) const
427 {
428 return getDims(Dims, N);
429 }
430
435 template <Dims_t N>
436 bool get(MultiIndex<N>&MI) const
437 {
438 if (rank != N)
439 return false;
440
441 for(Dims_t i=0; i<N; i++)
442 MI[i] = dims[i];
443
444 return true;
445 }
446
455 template <Dims_t N>
457 {
458 if (rank != N)
459 return MultiIndex<N>(0);
460
461 MultiIndex<N> MI(1);
462 for(Dims_t i=0; i<N; i++)
463 MI[i] = dims[i];
464
465 return MI;
466 }
467
469 std::string str() const;
470};
471
472inline size_t ElementArraySize(const DynamicSize&FragmentBaseSpaceDims, const DynamicSize&DataFragmentDims) noexcept
473{
474 if (FragmentBaseSpaceDims.getNumberOfElements() == DataFragmentDims.getNumberOfElements())
475 return 1; // independent of rank!!
476
477 if (DataFragmentDims.getRank() == FragmentBaseSpaceDims.getRank()+1 &&
478 DataFragmentDims.hasCompatibleHigherDims(FragmentBaseSpaceDims) )
479 {
480 return DataFragmentDims[ 0 ];
481 }
482
483 return 0; // mismatch
484}
485
486
499class FIELD_API SizeInterface : public MemCore::Interface<SizeInterface>, public DynamicSize
500{
501public:
503 SizeInterface(int rank, const SizeInterface&L, const SizeInterface&R);
504
505public:
507 template <int N>
511
516
518 template <class T>
519 SizeInterface(int N, const T values[])
520 : DynamicSize(N, values)
521 {}
522
527 : DynamicSize(N, 0)
528 {}
529
533
536
537protected:
540public:
541
542 using DynamicSize::operator[];
543 using DynamicSize::operator!=;
544 using DynamicSize::operator=;
545
552
555 {
556 if (L && R)
557 L->increment(*R);
558
559 return L;
560 }
561};
562
563} /* namespace Fiber */
564
565namespace std
566{
567
568inline string to_string(const Fiber::DynamicSize&Sz)
569{
570 return Sz.str();
571}
572
573inline string to_string(const Fiber::SizeInterface&SI)
574{
575 return SI.str();
576}
577
578}
579
580
581#endif /* __FIBER_DYNAMICSIZE_HPP */
constexpr complex< _Tp > & operator+=(const complex< _Up > &)
constexpr complex< _Tp > & operator=(const _Tp &)
basic_string< char > string
An iterator with an optional DataCreator, which is just a class to intercept creation of data along a...
Definition CreativeIterator.hpp:34
A class describing an n-dimensional space at runtime.
Definition field/DynamicSize.hpp:29
rank_t dimensionality() const
Dimensionality of the domain (same as rank).
Definition field/DynamicSize.hpp:228
bool changeSize(const MultiIndex< N > &MI)
Adjust size for lowest matching dimensions.
Definition field/DynamicSize.hpp:291
bool get(MultiIndex< N > &MI) const
Retrieve a dynamic size into a multidimensional index.
Definition field/DynamicSize.hpp:436
DynamicSize(const MultiIndex< N > &MI)
Initialize from a MultiIndex of certain dimension.
Definition field/DynamicSize.hpp:41
const index_t * dims_ptr() const
Return c pointer to dimension storage.
Definition field/DynamicSize.hpp:253
bool isValid() const
Check if some size has been defined here.
Definition field/DynamicSize.hpp:261
size_t getElementArraySize(const DynamicSize &DataFragmentDims) const noexcept
Given a base space (this), return the number of elements provided by the given data dims,...
Definition field/DynamicSize.hpp:167
DynamicSize & setSize(int N, const T values[])
Assign values from arbitrary type that is convertible into integers.
Definition field/DynamicSize.hpp:107
rank_t getRank() const
Dimensionality of the domain (same as rank).
Definition field/DynamicSize.hpp:220
DynamicSize(Dims_t N, index_t NValue)
Construct a dynamic size of N dimensions, but all extensions set to the given value.
Definition field/DynamicSize.hpp:63
MultiIndex< N > getSize() const
Provide the size stored here as a MultiIndex.
Definition field/DynamicSize.hpp:456
bool setSize(const DynamicSize &D, int FillUp=1) const
Set the size from another DynamicSize without changing the rank of this DynamicSize,...
Definition field/DynamicSize.hpp:382
bool multiply_dims(int incr)
Multiply each dimensional element by the same factor.
Definition field/DynamicSize.hpp:325
index_t nElements() const
How many elements here?
Definition field/DynamicSize.hpp:191
bool setSize(int i, index_t what)
Set the extension of this dimensional size in a certain dimension.
Definition field/DynamicSize.hpp:272
std::string str() const
Create textual version of this string.
Definition field/DynamicSize.cpp:114
bool increment_dims(int incr)
Increment each dimensional element by the same value.
Definition field/DynamicSize.hpp:310
bool operator==(const DynamicSize &DS) const noexcept
Equality Comparison.
Definition field/DynamicSize.hpp:100
index_t operator[](int i) const
Element access operator.
Definition field/DynamicSize.hpp:239
DynamicSize & resize(const MultiIndex< N > &MI)
Set a new size from the given MultiIndex.
Definition field/DynamicSize.hpp:129
DynamicSize()=default
Empty default constructor, zero rank.
rank_t Rank() const
Dimensionality of the domain (same as rank).
Definition field/DynamicSize.hpp:212
DynamicSize(Dims_t N, const T values[])
Assign values from arbitrary type that is convertible into integers.
Definition field/DynamicSize.hpp:51
An interface telling size and dimensionality of a dataset, a refcounted version of DynamicSize.
Definition field/DynamicSize.hpp:500
SizeInterface(int N, const T values[])
Assign values from arbitrary type that is convertible into integers.
Definition field/DynamicSize.hpp:519
SizeInterface(const MultiIndex< N > &MI)
Create from multidimensional index.
Definition field/DynamicSize.hpp:508
SizeInterface(int N)
Create size interface of N dimensions with zero extension.
Definition field/DynamicSize.hpp:526
SizeInterface()
Default constructor.
Definition field/DynamicSize.hpp:531
SizeInterface(const DynamicSize &DS)
Create from given dynamic size.
Definition field/DynamicSize.hpp:513
Given a fragmented field of curvilinear coordinates, (3D array of coordinates), build a uniform Grid ...
Definition FAQ.dox:2
IndexTypeConfig< sizeof(void *)>::index_t index_t
Define the index type as according to the size of a pointer, i.e.
Definition Index.hpp:22
std::string to_string(const span< char > &s)
VAcceptInfoList_t operator+(VAcceptInfoList_t &&l, VAcceptInfoList_t &&r)
bool operator!=(const MultipleStringSelection &a, const MultipleStringSelection &b)
STL namespace.