FiberVISH 0.2
Fish - The Fiber Bundle API for the Vish Visualization Shell
TypedArray.hpp
1#ifndef __FIBER_TYPEDARRAY_HPP
2#define __FIBER_TYPEDARRAY_HPP
3
4#include "MemBase.hpp"
5#include <memcore/Chunk.hpp>
6#include <eagle/PhysicalSpace.hpp>
7
8namespace Fiber
9{
10
11template <Dims_t N, class T>
12 class MemArray;
13
14template <class T>
15 class TypedArray;
16
17
18template <class T>
19Iterator<T> getComponentArrayIterator(const TypedArray<T>&It, int component)
20{
21const CreativeIterator<T>*C = It.creativeIterator();
22 if (!C)
23 return Iterator<T>(0,0);
24
25 return *C;
26}
27
28
29template <class T, int Components>
30Iterator<T> getComponentArrayIterator(const TypedArray<FixedArray<T, Components> >&It, int component)
31{
32const CreativeIterator<FixedArray<T, Components>>*C = It.creativeIterator();
33 if (!C)
34 return Iterator<T>(0,0);
35
36const Iterator<FixedArray<T, Components>>&iter = *C;
37
38 return Iterator<T>(iter, component);
39}
40
41
56template <class T>
57 class TypedArray : virtual public MemBase
58{
59public:
61 typedef T value_type;
62
65 : MemBase(FiberType<T>::getFiberType()->getFiberTypeIndex(), C)
66 {}
67
68 TypedArray(const TypedArray<T>&) = delete;
69 void operator=(const TypedArray<T>&) = delete;
70
72 virtual T first() const = 0;
73
75 virtual T last() const = 0;
76
77 virtual CreativeIterator<T>* creativeIterator() = 0;
78
79 virtual const CreativeIterator<T>* creativeIterator() const = 0;
80
85
86
89 {
90 return myChunk();
91 }
92
93
95 const type_info&getType() const override
96 {
97 return typeid(T);
98 }
99
100
101static MemCore::WeakPtr<FiberTypeBase> getFiberType()
102 {
104 }
105
106 using MemBase::create;
107static RefPtr<MemBase> create(const RefPtr<MemBase>&SourceLayout,
108 const type_info&layout = typeid(void) )
109 {
110 return SourceLayout->create( FiberType<T>::getFiberType(), layout);
111 }
112
121 template <class UnaryOperator>
123 {
124 const CreativeIterator<T>*C = creativeIterator();
125 if (!C)
126 return MemCore::NullPtr();
127
128 const CreativeIterator<T>&CI = *C;
129 const Iterator<T>&It = CI;
130
132 for(index_t i=0; i<It.count(); i++)
133 {
134 const T&in = It[i];
135 T&out = Result[i];
136 out = theOperator( in );
137 }
138 return createEquallySizedMemArray( Result, theCreator);
139 }
140
141
143 template <class result_t>
144 RefPtr<MemBase> applyFunction(const std::function<result_t(const T&)>&theOperator) const
145 {
146 const CreativeIterator<T>*C = creativeIterator();
147 if (!C)
148 return MemCore::NullPtr();
149
150 const CreativeIterator<T>&CI = *C;
151 const Iterator<T>&It = CI;
152
155 if (!NewArray) return nullptr;
156
157 const CreativeIterator<result_t>*rC = NewArray->creativeIterator();
158 if (!rC) return nullptr;
159
161 const Iterator<result_t>&rIt = rCI;
162
163 for(index_t i=0; i<It.count(); i++)
164 {
165 const T&in = It[i];
166 result_t&out = rIt[i];
167 out = theOperator( in );
168 }
169 return NewArray;
170 }
171
173 RefPtr<MemBase> applyFunction(const std::function<T(const T&)>&theOperator) const
174 {
175 const CreativeIterator<T>*C = creativeIterator();
176 if (!C)
177 return MemCore::NullPtr();
178
179 const CreativeIterator<T>&CI = *C;
180 const Iterator<T>&It = CI;
181
184 if (!NewArray) return nullptr;
185
186 const CreativeIterator<T>*rC = NewArray->creativeIterator();
187 if (!rC) return nullptr;
188
189 const CreativeIterator<T>&rCI = *rC;
190 const Iterator<T>&rIt = rCI;
191
192 for(index_t i=0; i<It.count(); i++)
193 {
194 const T&in = It[i];
195 T&out = rIt[i];
196 out = theOperator( in );
197 }
198 return NewArray;
199 }
200
201
203 template <class result_t>
204 RefPtr<MemBase> applyFunction(const std::function<result_t(const T&, index_t I)>&theOperator) const
205 {
206 const CreativeIterator<T>*C = creativeIterator();
207 if (!C)
208 return MemCore::NullPtr();
209
210 const CreativeIterator<T>&CI = *C;
211 const Iterator<T>&It = CI;
212
215 if (!NewArray) return nullptr;
216
217 const CreativeIterator<result_t>*rC = NewArray->creativeIterator();
218 if (!rC) return nullptr;
219
221 const Iterator<result_t>&rIt = rCI;
222
223 for(index_t i=0; i<It.count(); i++)
224 {
225 const T&in = It[i];
226 result_t&out = rIt[i];
227 out = theOperator( in, i );
228 }
229 return NewArray;
230 }
231
233 RefPtr<MemBase> applyFunction(const std::function<T(const T&, index_t I)>&theOperator) const
234 {
235 const CreativeIterator<T>*C = creativeIterator();
236 if (!C)
237 return MemCore::NullPtr();
238
239 const CreativeIterator<T>&CI = *C;
240 const Iterator<T>&It = CI;
241
244 if (!NewArray) return nullptr;
245
246 const CreativeIterator<T>*rC = NewArray->creativeIterator();
247 if (!rC) return nullptr;
248
249 const CreativeIterator<T>&rCI = *rC;
250 const Iterator<T>&rIt = rCI;
251
252 for(index_t i=0; i<It.count(); i++)
253 {
254 const T&in = It[i];
255 T&out = rIt[i];
256 out = theOperator( in, i );
257 }
258 return NewArray;
259 }
260
261
263 template <class result_t, class SecondType>
264 RefPtr<MemBase> applyFunction(const TypedArray<SecondType>&R, const std::function<result_t(const T&, const SecondType&ST, index_t I)>&theOperator) const
265 {
266 const CreativeIterator<T>*C = creativeIterator();
267 if (!C) return nullptr;
268 const CreativeIterator<T>&CI = *C;
269 const Iterator<T>&It = CI;
270
271 const CreativeIterator<SecondType>*C2 = R.creativeIterator();
272 if (!C2) return nullptr;
275
278 if (!NewArray) return nullptr;
279
280 const CreativeIterator<result_t>*rC = NewArray->creativeIterator();
281 if (!rC) return nullptr;
282
284 const Iterator<result_t>&rIt = rCI;
285
286 for(index_t i=0; i<It.count(); i++)
287 {
288 const T&in = It[i];
289 const SecondType&in2 = It2[i];
290 result_t&out = rIt[i];
291 out = theOperator( in, in2, i );
292 }
293 return NewArray;
294 }
295
296
303 template <class BinaryOperator>
305 {
306 const CreativeIterator<T>*C = creativeIterator();
307 if (!C)
308 return MemCore::NullPtr();
309
311 if (!RightMemArray)
312 return MemCore::NullPtr();
313
314 const CreativeIterator<T>*CR = RightMemArray->creativeIterator();
315 if (!CR)
316 return MemCore::NullPtr();
317
318 const CreativeIterator<T>&CI = *C;
319 const CreativeIterator<T>& R = *CR;
320
321 if (CI.count() != R.count() )
322 return MemCore::NullPtr();
323
325 for(index_t i=0; i<CI.count(); i++)
326 {
327 const T&in = C[i];
328 const T&inR = R[i];
329 T&out = Result[i];
330 out = theOperator( in, inR );
331 }
332 return createEquallySizedMemArray( Result, theCreator);
333 }
334
335
336#ifdef HAVE_MEMARRAY_INDIRECTION
337 template<class UINT>
339 {
341 return false;
342
343 for(index_t i = 0; i<theNumberOfElements; i++)
344 {
346
347 Itdst[i] = It[ src_index ];
348 }
349
350 return true;
351 }
352#endif
353
354 auto getComponent(int Component) -> decltype( getComponentArrayIterator(*this, Component))
355 {
356 return getComponentArrayIterator(*this, Component);
357 }
358
359 auto operator()(int Component) -> decltype( getComponentArrayIterator(*this, Component))
360 {
361 return getComponentArrayIterator(*this, Component);
362 }
363
364 ranged_for_iterator<T> begin()
365 {
366 CreativeIterator<T>*C = creativeIterator();
367 if (!C)
368 {
369 return ranged_for_iterator<T>(0,*C);
370 }
371
372 return C->begin();
373 }
374
375 ranged_for_iterator<T> end()
376 {
377 CreativeIterator<T>*C = creativeIterator();
378 if (!C)
379 {
380 return ranged_for_iterator<T>(0,*C);
381 }
382
383 return C->end();
384 }
385
386 const_ranged_for_iterator<T> begin() const
387 {
388 const CreativeIterator<T>*C = creativeIterator();
389 if (!C)
390 {
391 return const_ranged_for_iterator<T>(0,*C);
392 }
393
394 return C->begin();
395 }
396
397 const_ranged_for_iterator<T> end() const
398 {
399 const CreativeIterator<T>*C = creativeIterator();
400 if (!C)
401 {
402 return const_ranged_for_iterator<T>(0,*C);
403 }
404
405 return C->end();
406 }
407
408#ifdef HAVE_MEMARRAY_INDIRECTION
409
411 RefPtr<MemBase> createIndirection(const DynamicSize&newSize,
412 const RefPtr<MemCore::ChunkBase>&Indirection,
413 const MemBase::Creator_t&theCreator) const override
414 {
415 const CreativeIterator<T>*C = creativeIterator();
416 if (!C)
417 {
418 puts("TypedArray::createIndirection() C failed");
419 return MemCore::NullPtr();
420 }
421 const CreativeIterator<T>&CI = *C;
422 const Iterator<T>&It = CI;
423
424#ifdef HAVE_OLD_MEMARRAY
425 RefPtr<MemBase> newData = this->newMemArray(newSize, theCreator);
426#else
427 RefPtr<MemBase> newData = this->create(newSize);
428#endif
429 if (!newData)
430 {
431 puts("TypedArray::createIndirection() newData failed");
432 return MemCore::NullPtr();
433 }
434 RefPtr<TypedArray> dst = newData;
435 if (!dst)
436 {
437 puts("TypedArray::createIndirection() dst failed");
438 return MemCore::NullPtr();
439 }
440 const CreativeIterator<T>*Cdst = dst->creativeIterator();
441 if (!Cdst)
442 {
443 puts("TypedArray::createIndirection() dst->creativeIterator() failed");
444 return MemCore::NullPtr();
445 }
446 const CreativeIterator<T>&CIdst = *Cdst;
447 const Iterator<T>&Itdst = CIdst;
448
449 index_t mynElements = newSize.nElements();
450
451 // 32 bit?
452 if( RefPtr<MemCore::TypedChunk<uint32_t> >indirection32_chunk = Indirection )
453 {
454#ifdef VERBOSE
455 puts("TypedArray::createIndirection() found uint32_t indirection chunk");
456#endif
457 if( !doIndirection<uint32_t>(indirection32_chunk->get_vector(), It, Itdst, mynElements) )
458 {
459 puts("TypedArray::createIndirection() found uint32_t indirection chunk but FAILED");
460 return MemCore::NullPtr();
461 }
462 }
463 // 64 bit?
464 else if( RefPtr<MemCore::TypedChunk<uint64_t> >indirection64_chunk = Indirection )
465 {
466#ifdef VERBOSE
467 puts("TypedArray::createIndirection() found uint64_t indirection chunk");
468#endif
469 if( !doIndirection<uint64_t>(indirection64_chunk->get_vector(), It, Itdst, mynElements) )
470 {
471 puts("TypedArray::createIndirection() found uint64_t indirection chunk but FAILED");
472 return MemCore::NullPtr();
473 }
474 }
475 else
476 {
477 puts("TypedArray::createIndirection() ERROR: no 32 or 64 uint found for indirection chunk");
478 return MemCore::NullPtr();
479 }
480// if (Indirection32_chunk.size() != newSize.nElements() )
481// return MemCore::NullPtr();
482
483 assert( newData);
484
485 return newData;
486 }
487#endif
488
489/*
491 // ugly copy paste code from function above (mr)
492 // Maybe a encapsulated container instead of the vector should be used?
493 RefPtr<MemBase> createIndirection(const DynamicSize&newSize, const std::vector<unsigned int>&Indirection, const MemBase::Creator_t&theCreator) const override
494 {
495 if (Indirection.size() != newSize.nElements() )
496 return MemCore::NullPtr();
497
498 const CreativeIterator<T>*C = creativeIterator();
499 if (!C)
500 return MemCore::NullPtr();
501
502 const CreativeIterator<T>&CI = *C;
503 const Iterator<T>&It = CI;
504
505 RefPtr<MemBase> newData = this->newMemArray(newSize, theCreator);
506 if (!newData)
507 return MemCore::NullPtr();
508
509 RefPtr<TypedArray> dst = newData;
510 if (!dst)
511 return MemCore::NullPtr();
512
513 const CreativeIterator<T>*Cdst = dst->creativeIterator();
514 if (!Cdst)
515 return MemCore::NullPtr();
516
517 const CreativeIterator<T>&CIdst = *Cdst;
518 const Iterator<T>&Itdst = CIdst;
519
520 unsigned nElements = Indirection.size();
521 for(unsigned i=0; i<nElements; i++)
522 {
523 unsigned src_index = Indirection[ i ];
524
525 Itdst[i] = It[ src_index ];
526 }
527
528 return newData;
529 }
530*/
531};
532
533//extern template class TypedArray<Eagle::PhysicalSpace::point>;
534
535/* @ingroup field
536 A convenience class that behaves like a (strong) reference to a TypedArray,
537 but is more critical, in that it appears to be valid only if this TypedArray
538 may provide a CreativeIterator.
539
540 @author Werner Benger
541 */
542template <class T>
544{
546public:
547
551
554 : SRC(src)
555 {}
556
559 : SRC(src)
560 {}
561
564 {
565 SRC = src;
566 if (SRC) return true;
567 else return false;
568 }
569
572 {
573 SRC = src;
574 if (SRC) return true;
575 else return false;
576 }
577
579 template <int N>
581 {
582 SRC = src;
583 if (SRC) return true;
584 else return false;
585 }
586
591 operator bool() const
592 {
593 if (!SRC)
594 return false;
595
596 CreativeIterator<T>*C = SRC ->creativeIterator();
597 if (!C)
598 return false;
599
600 return true;
601 }
602
607 {
608 return SRC;
609 }
610
619 {
620 assert(SRC);
621
622 CreativeIterator<T>*C = SRC ->creativeIterator();
623 assert( C );
624 return *C;
625 }
626};
627
628} // namespace Fiber
629
630#endif /* __FIBER_TYPEDARRAY_HPP */
An iterator with an optional DataCreator, which is just a class to intercept creation of data along a...
Definition CreativeIterator.hpp:34
Description of types, which is meta-information like what is the number of elements of some array-lik...
Definition FiberType.hpp:61
static WeakPtr_t getFiberType()
Get the fiber type information for the current type.
Definition FiberType.hpp:126
index_t count() const
Return the number of steps which can be traversed through this ElementIterator.
Definition HyperslabParameters.hpp:147
Base class for multidimensional arrays with MemCore memory management.
Definition MemBase.hpp:70
virtual DynamicSize getSize() const =0
Return the size of this dataset.
virtual RefPtr< MemBase > createEquallySizedMemArray(const RefPtr< MemCore::ChunkBase > &Storage, const MemBase::Creator_t &C) const =0
Create a memory array of the same dimensions as the current array, but with data as provided by the S...
static RefPtr< MemBase > create(const RefPtr< FiberTypeBase > &FT, const DynamicSize &DS, const type_info &layout=typeid(void))
Given a description of a certain data type for each element on array, allocate a multidimensional arr...
Definition MemBase.cpp:106
An intermediate class that allows to operate on the pure type information of some memory array.
Definition TypedArray.hpp:58
RefPtr< MemBase > applyFunction(const std::function< T(const T &, index_t I)> &theOperator) const
Apply a functor yielding the same type.
Definition TypedArray.hpp:233
virtual T last() const =0
Retrieve the last element.
RefPtr< MemBase > applyOperator(const BinaryOperator &theOperator, const RefPtr< MemBase > &rightValue, const MemBase::Creator_t &theCreator) const
Apply a binary operator on the data contained here, and provide a new memory array of the same size a...
Definition TypedArray.hpp:304
const type_info & getType() const override
Return the element type of the certain array.
Definition TypedArray.hpp:95
RefPtr< MemCore::ChunkBase > getChunk() const override
Implement the virtual getChunk() function.
Definition TypedArray.hpp:88
RefPtr< MemBase > applyFunction(const std::function< T(const T &)> &theOperator) const
Apply a functor yielding the same type.
Definition TypedArray.hpp:173
virtual RefPtr< MemCore::TypedChunk< T > > myChunk() const =0
Access to a chunk of contiguous data, if such exists.
virtual T first() const =0
Retrieve the first element.
RefPtr< MemBase > applyFunction(const TypedArray< SecondType > &R, const std::function< result_t(const T &, const SecondType &ST, index_t I)> &theOperator) const
Apply a binary functor, possibly yielding a different type.
Definition TypedArray.hpp:264
RefPtr< MemBase > applyFunction(const std::function< result_t(const T &, index_t I)> &theOperator) const
Apply a functor, possibly yielding a different type.
Definition TypedArray.hpp:204
RefPtr< MemBase > applyFunction(const std::function< result_t(const T &)> &theOperator) const
Apply a functor, possibly yielding a different type.
Definition TypedArray.hpp:144
RefPtr< MemBase > applyOperator(const UnaryOperator &theOperator, const MemBase::Creator_t &theCreator) const
Apply an unary operator on the data contained here, and provide a new memory array of the same size a...
Definition TypedArray.hpp:122
Definition TypedArray.hpp:544
TypedIterator()
Default constructor.
Definition TypedArray.hpp:549
TypedIterator(const RefPtr< MemBase > &src)
Construct from untyped array.
Definition TypedArray.hpp:553
bool operator=(const RefPtr< MemArray< N, T > > &src)
assign from typed array
Definition TypedArray.hpp:580
bool operator=(const RefPtr< MemBase > &src)
assign from untyped array
Definition TypedArray.hpp:563
TypedIterator(const RefPtr< TypedArray< T > > &src)
Construct from typed array.
Definition TypedArray.hpp:558
CreativeIterator< T > & operator*() const
Retrieve an iterator to the underlying data, if possible.
Definition TypedArray.hpp:618
const RefPtr< TypedArray< T > > & operator()() const
Retrieve the associated pointer to the typed array.
Definition TypedArray.hpp:606
bool operator=(const RefPtr< TypedArray< T > > &src)
assign from typed array
Definition TypedArray.hpp:571
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::nullptr_t NullPtr
StrongPtr< Object, ObjectBase > RefPtr
Implementing a binary operation on two fields with on-demand computation per fragment and discarding ...
Definition BinaryOperator.hpp:36
Implementing an unary operation on a fields with on-demand computation per fragment and discarding da...
Definition UnaryOperator.hpp:49