WARPXM v1.10.0
Loading...
Searching...
No Matches
wmsimplearrays.h
Go to the documentation of this file.
1#ifndef wmsimplearrays_h
2#define wmsimplearrays_h
3
4// Wx includes
5#include "lib/wxrange.h"
6
7// Wm includes
8#include "lib/wmindexer.h"
9#include "wmarraybase.h"
10
21template<int MAJOR_ORDER_TYPE>
22class WmTypeOpaqueArray : public WmArrayBase<MAJOR_ORDER_TYPE>
23{
24public:
35 : WmArrayBase<MAJOR_ORDER_TYPE>(range),
36 _data(data),
37 _numBytesPerElement(numBytesPerElement)
38 {
39 }
40
50 : WmArrayBase<MAJOR_ORDER_TYPE>(that),
51 _data(that._data),
52 _numBytesPerElement(that._numBytesPerElement)
53 {
54 }
55
56 /*
57 * Assignment operator does not allocate new memory. The created array
58 * represents the same data and geometry as the original one.
59 *
60 * Freeing of array memory is not the responsibility of this class.
61 *
62 * @param that array to copy
63 * @return reference to new array
64 */
67 {
68 if (this == &that)
69 return *this;
71 _data = that._data;
72 _numBytesPerElement = that._numBytesPerElement;
73 return *this;
74 }
75
81 void* data()
82 {
83 return _data;
84 }
85
91 const void* data() const
92 {
93 return _data;
94 }
95
102 void* data(const int* indices)
103 {
104 int element_offset = WmArrayBase<MAJOR_ORDER_TYPE>::_indexer.index(indices);
105 char* byte_loc = static_cast<char*>(_data) + element_offset * _numBytesPerElement;
106 return static_cast<void*>(byte_loc);
107 }
108
115 const void* data(const int* indices) const
116 {
117 int element_offset = WmArrayBase<MAJOR_ORDER_TYPE>::_indexer.index(indices);
118 const char* byte_loc =
119 static_cast<const char*>(_data) + element_offset * _numBytesPerElement;
120 return static_cast<const void*>(byte_loc);
121 }
122
130 void* dataSlab(const int* indices, int indicesRank)
131 {
132 int slabStartIndices[max_dims];
133 for (int i = 0; i < indicesRank; ++i)
134 slabStartIndices[i] = indices[i];
135 for (int i = indicesRank; i < rank(); ++i)
136 slabStartIndices[i] = this->range().lower(i);
137 int element_offset =
138 WmArrayBase<MAJOR_ORDER_TYPE>::_indexer.index(slabStartIndices);
139 char* byte_loc = static_cast<char*>(_data) + element_offset * _numBytesPerElement;
140 return static_cast<void*>(byte_loc);
141 }
142
150 const void* dataSlab(const int* indices, int indicesRank) const
151 {
152 int slabStartIndices[max_dims];
153 for (int i = 0; i < indicesRank; ++i)
154 slabStartIndices[i] = indices[i];
155 for (int i = indicesRank; i < rank(); ++i)
156 slabStartIndices[i] = this->range().lower(i);
157 int element_offset =
158 WmArrayBase<MAJOR_ORDER_TYPE>::_indexer.index(slabStartIndices);
159 const char* byte_loc =
160 static_cast<const char*>(_data) + element_offset * _numBytesPerElement;
161 return static_cast<const void*>(byte_loc);
162 }
163
165 {
166 return _numBytesPerElement;
167 }
168
181 // void put(const WxRange & selection, const void *bufin);
182
196 // void get(const WxRange & selection, void *bufout) const;
197
214 void put(const WxRange& selection, const WxRange& inputShape, const void* bufin);
215
234 void get(const WxRange& selection, const WxRange& outputShape, void* bufout) const;
235
236 using WmArrayBase<MAJOR_ORDER_TYPE>::rank;
237 using WmArrayBase<MAJOR_ORDER_TYPE>::max_dims;
238
251 {
252 }
253
254protected:
262 : WmArrayBase<MAJOR_ORDER_TYPE>(), _data(NULL), _numBytesPerElement(0)
263 {
264 }
265
272 {
274 _data = data;
275 _numBytesPerElement = numBytesPerElement;
276 }
277
278private:
279 // hide this otherwise public base-class interface
280 void setRange(const WxRange& range);
281
288 void checkContained(const WxRange& selection) const;
289
290 static void copyToRange(int currentDim,
291 int rangeRank,
292 const int jump_array[max_dims],
293 const int jump_buffer[max_dims],
294 const int extent_array[max_dims],
295 const char* sourceBuffer,
296 char* destinationPointer,
298 static void copyFromRange(int currentDim,
299 int rangeRank,
300 const int jump_array[max_dims],
301 const int jump_buffer[max_dims],
302 const int extent_array[max_dims],
303 const char* sourcePointer,
304 char* destBuffer,
306
307 void* _data;
308 int _numBytesPerElement;
309};
310
317template<int MAJOR_ORDER_TYPE, typename T>
318class WmTypedArray : public WmTypeOpaqueArray<MAJOR_ORDER_TYPE>
319{
320public:
330 : WmTypeOpaqueArray<MAJOR_ORDER_TYPE>(range, data, sizeof(T)),
331 _responsibleForFreeingElements(false)
332 {
333 }
334
344 : WmTypeOpaqueArray<MAJOR_ORDER_TYPE>(range, new T[range.size()], sizeof(T)),
345 _responsibleForFreeingElements(true)
346 {
347 }
348
358 : WmTypeOpaqueArray<MAJOR_ORDER_TYPE>(that), _responsibleForFreeingElements(false)
359 {
360 }
361
371 : WmTypeOpaqueArray<MAJOR_ORDER_TYPE>(that), _responsibleForFreeingElements(false)
372 {
373 }
374
375 /*
376 * Assignment operator does not allocate new memory. The created array
377 * represents the same data and geometry as the original one.
378 *
379 * Freeing of array memory is not the responsibility of this instance
380 *
381 * @param that array to copy
382 * @return reference to new array
383 */
386 {
387 if (this == &that)
388 return *this;
390 _responsibleForFreeingElements = false;
391 return *this;
392 }
393
400 {
401 if (_responsibleForFreeingElements)
402 {
403 delete[] data();
404 }
405 }
406
412 T operator()(int k0) const;
413 T& operator()(int k0);
414
421 T operator()(int k0, int k1) const;
422 T& operator()(int k0, int k1);
423
431 T operator()(int k0, int k1, int k2) const;
432 T& operator()(int k0, int k1, int k2);
433
442 T operator()(int k0, int k1, int k2, int k3) const;
443 T& operator()(int k0, int k1, int k2, int k3);
444
450 T* data()
451 {
452 return static_cast<T*>(WmTypeOpaqueArray<MAJOR_ORDER_TYPE>::data());
453 }
454
460 const T* data() const
461 {
462 return static_cast<const T*>(WmTypeOpaqueArray<MAJOR_ORDER_TYPE>::data());
463 }
464
472 T* dataSlab(const int* indices, int indicesRank)
473 {
474 return static_cast<T*>(
476 }
477
485 const T* dataSlab(const int* indices, int indicesRank) const
486 {
487 return static_cast<const T*>(
489 }
490
500 // void put(const WxRange & selection, const void *bufin);
501
512 // void get(const WxRange & selection, void *bufout) const;
513
514protected:
522 void resetArrayProperties(const WxRange& range, T* newdata)
523 {
524 if (_responsibleForFreeingElements)
525 {
526 delete[] data();
527 _responsibleForFreeingElements = false;
528 }
530 range, newdata, sizeof(T));
531 }
532
540 : WmTypeOpaqueArray<MAJOR_ORDER_TYPE>(), _responsibleForFreeingElements(false)
541 {
542 }
543
544private:
545 bool _responsibleForFreeingElements;
546};
547
550#endif // wmsimplearrays_h
Defines common interface for variables that are arrays.
Definition: wmarraybase.h:24
WmArrayBase< MAJOR_ORDER_TYPE > & operator=(const WmArrayBase< MAJOR_ORDER_TYPE > &that)
Assignment operator duplicates the array geometry properties of right hand side one.
Definition: wmarraybase.h:66
unsigned rank() const
Rank of array.
Definition: wmarraybase.h:114
void setRange(const WxRange &range)
Array spanning given range object.
Definition: wmarraybase.h:104
const WxRange & range() const
Definition: wmarraybase.h:26
static const int max_dims
Definition: wmarraybase.h:120
Defines common interface for arrays of elements.
Definition: wmsimplearrays.h:23
WmTypeOpaqueArray(const WmTypeOpaqueArray< MAJOR_ORDER_TYPE > &that)
Copy constructor does not allocate new memory.
Definition: wmsimplearrays.h:49
void * dataSlab(const int *indices, int indicesRank)
Access higher dimension slab of raw array element.
Definition: wmsimplearrays.h:130
const void * data() const
Access start of raw array data.
Definition: wmsimplearrays.h:91
void get(const WxRange &selection, const WxRange &outputShape, void *bufout) const
Copy data elements from the region described range selection to the bufout provided.
WmTypeOpaqueArray< MAJOR_ORDER_TYPE > & operator=(const WmTypeOpaqueArray< MAJOR_ORDER_TYPE > &that)
Definition: wmsimplearrays.h:66
int numBytesPerElement() const
Definition: wmsimplearrays.h:164
const void * dataSlab(const int *indices, int indicesRank) const
Access higher dimension slab of raw array element.
Definition: wmsimplearrays.h:150
void * data()
Access start of raw array data.
Definition: wmsimplearrays.h:81
void * data(const int *indices)
Access raw array element.
Definition: wmsimplearrays.h:102
void put(const WxRange &selection, const WxRange &inputShape, const void *bufin)
Copy data elements from bufin to the region described by the range selection.
WmTypeOpaqueArray(const WxRange &range, void *data, int numBytesPerElement)
Array spanning given range object.
Definition: wmsimplearrays.h:34
WmTypeOpaqueArray()
Default ctor does not set up array geometry initially, this must be done later, so it is only exposed...
Definition: wmsimplearrays.h:261
const void * data(const int *indices) const
Access raw array element.
Definition: wmsimplearrays.h:115
virtual ~WmTypeOpaqueArray()
Delete object which only describes the array geometry, no array allocation is deleted.
Definition: wmsimplearrays.h:250
void resetArrayProperties(const WxRange &range, void *data, int numBytesPerElement)
Reset the properties of this array.
Definition: wmsimplearrays.h:271
Defines common interface for arrays of type T elements.
Definition: wmsimplearrays.h:319
WmTypedArray< MAJOR_ORDER_TYPE, T > & operator=(const WmTypedArray< MAJOR_ORDER_TYPE, T > &that)
Definition: wmsimplearrays.h:385
void resetArrayProperties(const WxRange &range, T *newdata)
Copy data elements from bufin to the region described by the range selection.
Definition: wmsimplearrays.h:522
T & operator()(int k0)
T & operator()(int k0, int k1, int k2)
T & operator()(int k0, int k1, int k2, int k3)
const T * data() const
Access raw array data.
Definition: wmsimplearrays.h:460
WmTypedArray(const WmTypedArray< MAJOR_ORDER_TYPE, T > &that)
Copy constructor does not allocate new memory.
Definition: wmsimplearrays.h:357
T operator()(int k0) const
Rank-1 indexer.
WmTypedArray()
Default ctor does not set up array geometry initially, this must be done later, so it is only exposed...
Definition: wmsimplearrays.h:539
const T * dataSlab(const int *indices, int indicesRank) const
Access higher dimension slab of raw array element.
Definition: wmsimplearrays.h:485
T operator()(int k0, int k1, int k2) const
Rank-3 indexer.
T operator()(int k0, int k1) const
Rank-2 indexer.
T operator()(int k0, int k1, int k2, int k3) const
Rank-4 indexer.
WmTypedArray(const WxRange &range, T *data)
Array spanning given range object.
Definition: wmsimplearrays.h:329
T * dataSlab(const int *indices, int indicesRank)
Access higher dimension slab of raw array element.
Definition: wmsimplearrays.h:472
WmTypedArray(const WmTypeOpaqueArray< MAJOR_ORDER_TYPE > &that)
Special copy constructor does not allocate new memory.
Definition: wmsimplearrays.h:370
T & operator()(int k0, int k1)
WmTypedArray(const WxRange &range)
Array spanning given range object.
Definition: wmsimplearrays.h:343
T * data()
Access raw array data.
Definition: wmsimplearrays.h:450
~WmTypedArray()
Delete instance which may or may not involve freeing array element allocation.
Definition: wmsimplearrays.h:399
TYPE lower(unsigned dim) const
Lower bound along dimension 'dim'.
Definition: wxbox.h:124
WxRange represents a hyper-rectangular domain of an n-dimensional space of integers.
Definition: wxrange.h:23