OpendTect  6.3
arraynd.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 ________________________________________________________________________
4 
5  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
6  Author: K. Tingdahl
7  Date: 9-3-1999
8 ________________________________________________________________________
9 
10 */
11 
12 #include "basicmod.h"
13 #include "valseries.h"
14 #include "arrayndinfo.h"
15 #include "varlenarray.h"
16 #include "ptrman.h"
17 
28 template <class T>
30 {
31 public:
32 
33  virtual ~ArrayND() {}
34 
35  virtual inline bool isOK() const;
36  virtual inline bool isEmpty() const;
37 
38  virtual T getND(const int*) const = 0;
39  virtual bool isSettable() const { return true; }
40  virtual void setND(const int*,T) = 0;
41 
42  inline const ValueSeries<T>* getStorage() const
43  { return getStorage_(); }
44  inline ValueSeries<T>* getStorage();
45  virtual bool canSetStorage() const { return false; }
46  virtual bool setStorage(ValueSeries<T>* s)
47  { delete s; return true; }
51  inline const T* getData() const
52  { return getData_(); }
53  inline T* getData();
54  virtual const T* get1D(const int*) const;
55  virtual T* get1D(const int*);
56  virtual int get1DDim() const;
57 
58 
59  virtual const ArrayNDInfo& info() const = 0;
60  virtual bool canSetInfo() const
61  { return false; }
62  virtual bool canChangeNrDims() const
63  { return false; }
64  virtual bool setInfo( const ArrayNDInfo& )
65  { return false; }
66 
67  virtual void setAll(T);
68  virtual void getAll(T* ptr) const;
73  virtual void getAll(ValueSeries<T>& vs) const;
79 protected:
80 
81  virtual const ValueSeries<T>* getStorage_() const { return 0; }
82 
83  virtual const T* getData_() const
84  {
85  if ( getStorage_() )
86  return getStorage()->arr();
87  return 0;
88  }
89 };
90 
91 
96 template <class T>
97 mClass(Basic) Array1D : public ArrayND<T>
98  , public ValueSeries<T>
99 {
100 public:
101 
102  virtual void set(int,T) = 0;
103  virtual T get(int) const = 0;
104  void setND(const int* pos,T v) { set( pos[0], v ); }
105  T getND(const int* pos) const
106  { return get(pos[0]); }
107 
108  // implement ValueSeries interface
109  T value(od_int64 i) const { return get( (int) i);}
110  bool writable() const { return true; }
111  void setValue(od_int64 i,T t){ set( (int) i,t); }
112  virtual void setAll( T t ) { ArrayND<T>::setAll(t); }
113 
114  virtual const Array1DInfo& info() const = 0;
115 
116  inline T operator []( int idx ) const
117  { return get( idx ); }
118 
119 };
120 
121 
126 template <class T>
127 mClass(Basic) Array2D : public ArrayND<T>
128 {
129 public:
130  virtual void set( int, int, T ) = 0;
131  virtual T get( int p0, int p1 ) const = 0;
132  void setND( const int* pos, T v )
133  { set( pos[0], pos[1], v);}
134  T getND( const int* pos ) const
135  { return get( pos[0], pos[1] ); }
136 
137  virtual T** get2DData() { return 0; }
138  virtual const T** get2DData() const { return 0; }
139 
140  virtual const Array2DInfo& info() const = 0;
141 };
142 
143 
148 template <class T>
149 mClass(Basic) Array3D : public ArrayND<T>
150 {
151 public:
152 
153  virtual void set( int, int, int, T ) = 0;
154  virtual T get( int p0, int p1, int p2 ) const = 0;
155  void setND( const int* pos, T v )
156  { set( pos[0], pos[1], pos[2], v);}
157  T getND( const int* pos ) const
158  { return get( pos[0], pos[1], pos[2] ); }
159 
160  virtual T*** get3DData() { return 0; }
161  virtual const T*** get3DData() const { return 0; }
162 
163  virtual const Array3DInfo& info() const = 0;
164 };
165 
166 
176 {
177 public:
178  ArrayNDIter( const ArrayNDInfo& );
179  ~ArrayNDIter();
180 
181  bool next();
182  void reset();
183 
184  bool setGlobalPos(od_int64);
185 
186  template <class T> void inline setPos(const T& idxabl);
187  const int* getPos() const { return position_; }
188  int operator[](int) const;
189 
190 protected:
191 
192  bool inc(int);
193 
194  int* position_;
195  const ArrayNDInfo& sz_;
196 
197 };
198 
199 #define mArrayNDVSAdapterNrDim 20
200 
201 
208 template <class T>
210 {
211 public:
213  : array_( a )
214  {
215  if ( array_.getData() || array_.getStorage() )
216  {
217  pErrMsg("Not a good idea to use adapter. "
218  "Use getStorage() instead");
219  }
220  }
221 
222  bool isOK() const
223  {
224  if ( array_.info().getNDim()>mArrayNDVSAdapterNrDim)
225  {
226  pErrMsg( "Too many dimensions");
227  return false;
228  }
229 
230  return true;
231  }
232 
234  { return new ArrayNDValseriesAdapter<T>( *this ); }
235 
236  T value(od_int64 idx) const
237  {
238  int pos[mArrayNDVSAdapterNrDim];
239  array_.info().getArrayPos( idx, pos );
240  return array_.getND( pos );
241  }
242 
243  const T* arr() const { return array_.getData(); }
244  T* arr() { return 0; }
245 
246 protected:
247 
249 };
250 
251 
252 template <class T> inline void ArrayNDIter::setPos( const T& idxable )
253 {
254  for ( int idx=sz_.getNDim()-1; idx>=0; idx-- )
255  position_[idx] = idxable[idx];
256 }
257 
258 
259 inline
261 {
262  return sz_.getArrayPos(globalidx,position_);
263 }
264 
265 #define mDefArrayNDStdMembers(nd) \
266 public: \
267  Array##nd##Conv(Array##nd<TT>* arr) \
268  : arr_(arr) {} \
269  ~Array##nd##Conv() { delete arr_; } \
270  \
271  const Array##nd##Info& info() const { return arr_->info(); } \
272  \
273 protected: \
274  \
275  Array##nd<TT>* arr_; \
276  \
277 public:
278 
279 #define mDefArrayNDConverter(nd) \
280 template <class T, class TT> \
281 class Array##nd##Conv : public Array##nd<T> \
282 { mDefArrayNDStdMembers(nd);
283 
284 
285 
286 template <class T, class TT>
287 class Array1DConv : public Array1D<T>
289 public:
290  T get( int p0 ) const
291  { return (T)arr_->get( p0 ); }
292  void set( int p0, T v )
293  { arr_->set( p0, (TT)v ); }
294 
295 };
296 
297 
298 template <class T, class TT>
299 class Array2DConv : public Array2D<T>
301  T get( int p0, int p1 ) const
302  { return (T)arr_->get( p0, p1 ); }
303  void set( int p0, int p1, T v )
304  { arr_->set( p0, p1, (TT)v ); }
305 
306 };
307 
308 template <class T, class TT>
309 class Array3DConv : public Array3D<T>
311 
312  T get( int p0, int p1, int p2 ) const
313  { return (T)arr_->get( p0, p1, p2 ); }
314  void set( int p0, int p1, int p2, T v )
315  { arr_->set( p0, p1, p2, (TT)v ); }
316 
317 };
318 
319 
320 // Only implementations below
321 
322 template <class T> inline
323 bool ArrayND<T>::isOK() const
324 {
325  return getStorage() ? getStorage()->isOK() : true;
326 }
327 
328 
329 template <class T> inline
331 {
332  return !isOK() || info().getTotalSz() == 0;
333 }
334 
335 
336 template <class T> inline
337 const T* ArrayND<T>::get1D( const int* i ) const
338 {
339  const T* ptr = getData();
340  if ( !ptr ) return 0;
341 
342  int ndim = info().getNDim();
343 
344  mAllocLargeVarLenArr( int, pos, ndim );
345  OD::memCopy(pos,i, (int) sizeof(int)*(ndim-1));
346 
347  pos[ndim-1] = 0;
348 
349  return &ptr[info().getOffset( pos )];
350 }
351 
352 
353 template <class T> inline
355 { return info().getNDim()-1; }
356 
357 
358 template <class T> inline
360 {
361  return !isSettable() ? 0
362  : const_cast<T*>(((const ArrayND*)this)->getData_());
363 }
364 
365 
366 template <class T> inline
368 {
369  return !isSettable() ? 0 :
370  const_cast<ValueSeries<T>* >(((const ArrayND*)this)->getStorage_());
371 }
372 
373 
374 template <class T> inline
375 T* ArrayND<T>::get1D( const int* i )
376 {
377  return !isSettable() ? 0 : const_cast<T*>(((const ArrayND*)this)->get1D(i));
378 }
379 
380 
381 template <class T> inline
382 void ArrayND<T>::setAll( T val )
383 {
384  if ( isEmpty() )
385  return;
386 
387  ValueSeries<T>* stor = getStorage();
388  if ( stor )
389  {
390  if ( stor->canSetAll() )
391  stor->setAll( val );
392  else
393  {
394  MemSetter<T> setter( *stor, val, info().getTotalSz() );
395  setter.execute();
396  }
397 
398  return;
399  }
400 
401  ArrayNDIter iterator( info() );
402  do
403  {
404  setND( iterator.getPos(), val );
405  } while ( iterator.next() );
406 }
407 
412 template <class T>
414 {
415 public:
416  ArrayNDDataExtracter( T* ptr, const ArrayND<T>& arr )
417  : ptr_( ptr )
418  , arr_( arr )
419  , totalnr_( arr.info().getTotalSz() )
420  , vs_( 0 )
421  {}
422 
424  : ptr_( vs.arr() )
425  , arr_( arr )
426  , totalnr_( arr.info().getTotalSz() )
427  , vs_( vs.arr() ? 0 : &vs )
428  {}
429 
430  bool doWork( od_int64 start, od_int64 stop, int )
431  {
432  mAllocVarLenArr( int, pos, arr_.info().getNDim() );
433  if ( !arr_.info().getArrayPos( start, pos ) )
434  return false;
435 
436  ArrayNDIter iterator( arr_.info() );
437  iterator.setPos( (int*) pos );
438 
439  if ( vs_ )
440  {
441  for ( od_int64 idx=start; idx<=stop; idx++ )
442  {
443  vs_->setValue( idx,
444  arr_.getND( iterator.getPos() ) );
445  if ( idx==stop )
446  break;
447 
448  if ( !iterator.next() )
449  return false;
450  }
451  }
452  else
453  {
454  T* res = ptr_ + start;
455  for ( od_int64 idx=start; idx<=stop; idx++, res++ )
456  {
457  *res = arr_.getND( iterator.getPos() );
458  if ( idx==stop )
459  break;
460 
461  if ( !iterator.next() )
462  return false;
463  }
464  }
465 
466  return true;
467  }
468 
469  od_int64 nrIterations() const { return totalnr_; }
470 
471 protected:
472 
474  const ArrayND<T>& arr_;
475  T* ptr_;
477 
478 };
479 
480 
481 template <class T> inline
483 {
484  if ( vs.arr() )
485  { getAll( vs.arr() ); return; }
486 
487  const od_int64 totalsz = info().getTotalSz();
488  if ( !totalsz )
489  return;
490 
491  const ValueSeries<T>* stor = getStorage();
492  if ( stor )
493  stor->getValues( vs, totalsz );
494  else
495  {
496  ArrayNDDataExtracter<T> extr( vs, *this );
497  extr.execute();
498  }
499 }
500 
501 
502 template <class T> inline
503 void ArrayND<T>::getAll( T* ptr ) const
504 {
505  const od_int64 totalsz = info().getTotalSz();
506  if ( !totalsz )
507  return;
508 
509  const T* tdata = getData();
510  if ( tdata )
511  OD::memCopy( ptr, tdata, totalsz * sizeof(T) );
512  else
513  {
514  const ValueSeries<T>* stor = getStorage();
515  if ( stor )
516  {
517  MemCopier<T> cpier( ptr, *stor, totalsz );
518  cpier.execute();
519  }
520  else
521  {
522  ArrayNDDataExtracter<T> extr( ptr, *this );
523  extr.execute();
524  }
525  }
526 }
Array1D ( Subclass of ArrayND ) is a one dimensional array.
Definition: arraynd.h:97
#define mExpClass(module)
Definition: commondefs.h:157
virtual int getNDim() const =0
void getValues(ValueSeries< T > &, int64_t nrvals) const
Definition: valseries.h:126
virtual bool canSetStorage() const
Definition: arraynd.h:45
T value(int64_t i) const
Definition: arraynd.h:109
virtual bool setStorage(ValueSeries< T > *s)
Definition: arraynd.h:46
const T * arr() const
Definition: arraynd.h:243
virtual T ** get2DData()
Definition: arraynd.h:137
Sets large amounts of values to a constant using multiple threads.
Definition: odmemory.h:40
Contains the information about the size of Array3D, and in what order the data is stored (if accessab...
Definition: arrayndinfo.h:116
int64_t totalnr_
Definition: arraynd.h:473
const ValueSeries< T > * getStorage() const
Definition: arraynd.h:42
#define od_int64
Definition: plftypes.h:34
int64_t nrIterations() const
Definition: arraynd.h:469
ValueSeries< T > * clone() const
Definition: arraynd.h:233
virtual const T * get1D(const int *) const
Definition: arraynd.h:337
Contains the information about the size of ArrayND, and in what order the data is stored (if accessab...
Definition: arrayndinfo.h:21
Contains the information about the size of Array1D, and in what order the data is stored (if accessab...
Definition: arrayndinfo.h:69
ArrayNDDataExtracter(T *ptr, const ArrayND< T > &arr)
Definition: arraynd.h:416
int * position_
Definition: arraynd.h:194
T value(int64_t idx) const
Definition: arraynd.h:236
virtual bool getArrayPos(uint64_t, int *) const
virtual bool isSettable() const
Definition: arraynd.h:39
Definition: arraynd.h:309
ArrayNDValseriesAdapter(const ArrayND< T > &a)
Definition: arraynd.h:212
virtual const T ** get2DData() const
Definition: arraynd.h:138
const ArrayNDInfo & sz_
Definition: arraynd.h:195
virtual int get1DDim() const
Definition: arraynd.h:354
virtual bool isEmpty() const
Definition: arraynd.h:330
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:64
const ArrayND< T > & array_
Definition: arraynd.h:248
const T * getData() const
Definition: arraynd.h:51
virtual bool canSetInfo() const
Definition: arraynd.h:60
Definition: arraynd.h:287
T * arr()
Definition: arraynd.h:244
ValueSeries< T > * vs_
Definition: arraynd.h:476
bool doWork(int64_t start, int64_t stop, int)
Definition: arraynd.h:430
Array2D ( Subclass of ArrayND ) is a two dimensional array.
Definition: arraynd.h:127
bool writable() const
Definition: arraynd.h:110
virtual void setAll(T t)
Definition: arraynd.h:112
void setPos(const T &idxabl)
Definition: arraynd.h:252
bool execute()
Definition: paralleltask.h:69
Interface to a series of values.
Definition: odmemory.h:15
bool setGlobalPos(int64_t)
Definition: arraynd.h:260
virtual const ValueSeries< T > * getStorage_() const
Definition: arraynd.h:81
virtual const T *** get3DData() const
Definition: arraynd.h:161
const ArrayND< T > & arr_
Definition: arraynd.h:474
T getND(const int *pos) const
Definition: arraynd.h:157
virtual T *** get3DData()
Definition: arraynd.h:160
Contains the information about the size of Array2D, and in what order the data is stored (if accessab...
Definition: arrayndinfo.h:93
void setND(const int *pos, T v)
Definition: arraynd.h:104
#define mAllocVarLenArr(type, varnm, __size)
Definition: varlenarray.h:52
virtual bool canChangeNrDims() const
Definition: arraynd.h:62
virtual void setAll(T)
Definition: arraynd.h:382
virtual void setAll(T)
Definition: valseries.h:43
An ArrayND is an array with a given number of dimensions and a size.
Definition: arraynd.h:29
Iterates through all samples in an ArrayND.
Definition: arraynd.h:175
void setND(const int *pos, T v)
Definition: arraynd.h:132
T * ptr_
Definition: arraynd.h:475
ArrayNDDataExtracter(ValueSeries< T > &vs, const ArrayND< T > &arr)
Definition: arraynd.h:423
bool isEmpty(const char *)
virtual bool setInfo(const ArrayNDInfo &)
Definition: arraynd.h:64
const int * getPos() const
Definition: arraynd.h:187
#define mAllocLargeVarLenArr(type, varnm, __size)
Definition: varlenarray.h:29
virtual const T * getData_() const
Definition: arraynd.h:83
Definition: arraynd.h:299
ValueSeries Copier.
Definition: odmemory.h:87
virtual bool canSetAll() const
Definition: valseries.h:42
T getND(const int *pos) const
Definition: arraynd.h:105
void setND(const int *pos, T v)
Definition: arraynd.h:155
virtual bool isOK() const
Definition: arraynd.h:323
void setValue(int64_t i, T t)
Definition: arraynd.h:111
Gets a one dimensional array from an ArrayND.
Definition: arraynd.h:413
#define mClass(module)
Definition: commondefs.h:161
#define pErrMsg(msg)
Usual access point for programmer error messages.
Definition: errmsg.h:34
#define mDefArrayNDStdMembers(nd)
Definition: arraynd.h:265
T getND(const int *pos) const
Definition: arraynd.h:134
virtual void getAll(T *ptr) const
Definition: arraynd.h:503
Adapter that makes any ArrayND to a (slow) value series.
Definition: arraynd.h:209
#define mArrayNDVSAdapterNrDim
Definition: arraynd.h:199
Array3D ( Subclass of ArrayND ) is a three dimensional array.
Definition: arraynd.h:149
virtual ~ArrayND()
Definition: arraynd.h:33
virtual T * arr()
Definition: valseries.h:48
bool isOK() const
Definition: arraynd.h:222

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2017