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

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