OpendTect-6_4  6.4
valseries.h
Go to the documentation of this file.
1 #ifndef valseries_h
2 #define valseries_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Bert Bril & Kris Tingdahl
9  Date: Mar 2005
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "basicmod.h"
16 #include "gendefs.h"
17 #include "odmemory.h"
18 
19 #ifdef __debug__
20 #include "debug.h"
21 #endif
22 
23 
30 template <class T>
31 mClass(Basic) ValueSeries
32 {
33 public:
34 
35  virtual ~ValueSeries() {}
36 
37  void getValues(ValueSeries<T>&,od_int64 nrvals) const;
38  void getValues(T*,od_int64 nrvals) const;
39 
40  virtual ValueSeries<T>* clone() const = 0;
41  virtual bool isOK() const { return true; }
42 
43  virtual T value(od_int64) const = 0;
44  virtual bool writable() const { return false; }
45  virtual void setValue(od_int64,T) {}
46 
47  virtual bool canSetAll() const { return false; }
48  virtual void setAll(T) {}
49 
50  virtual bool selfSufficient() const { return false; }
52  virtual bool reSizeable() const { return false; }
53  virtual bool setSize(od_int64) { return false; }
54 
55  virtual T* arr() { return 0; }
56  virtual const T* arr() const { return 0; }
57 
58  virtual char bytesPerItem() const { return sizeof(T); }
59 
60  inline T operator[](od_int64 idx) const { return value(idx); }
61 };
62 
63 
68 template <class T>
70 {
71 public:
72  inline OffsetValueSeries( ValueSeries<T>& src, od_int64 off );
73  inline OffsetValueSeries( const ValueSeries<T>& src,
74  od_int64 off);
75  inline ValueSeries<T>* clone() const;
76 
77  inline T value( od_int64 idx ) const;
78  inline void setValue( od_int64 idx, T v );
79  inline T* arr();
80  inline const T* arr() const;
81  inline bool writable() const;
82  inline bool canSetAll() const;
83  inline void setAll(T);
84 
85  inline od_int64 getOffset() const;
86  inline void setOffset(od_int64 no);
87 
88  const ValueSeries<T>& source() const { return src_; }
89 
90 protected:
93  bool writable_;
94 };
95 
96 
97 #include <typeinfo>
98 
99 #define mImplArr \
100 { return typeid(RT)==typeid(AT) ? (RT*) ptr_ : (RT*) 0;}
101 
108 template <class RT, class AT>
110 {
111 public:
112 
113  ArrayValueSeries( AT* ptr, bool memmine, od_int64 sz=-1 );
115  ~ArrayValueSeries() { if ( mine_ ) delete [] ptr_; }
116 
117  ValueSeries<RT>* clone() const;
118 
119  bool isOK() const { return ptr_; }
120 
121  RT value( od_int64 idx ) const;
122  bool writable() const { return true; }
123  void setValue( od_int64 idx, RT v );
124 
125  bool canSetAll() const { return writable(); }
126  void setAll(RT);
127 
128  const RT* arr() const mImplArr;
129  RT* arr() mImplArr;
130 
131  bool selfSufficient() const { return mine_; }
132  bool reSizeable() const { return mine_; }
133  inline bool setSize(od_int64);
134  od_int64 size() const { return cursize_; }
135  char bytesPerItem() const { return sizeof(AT); }
136 
137 protected:
138 
139  AT* ptr_;
140  bool mine_;
142 };
143 
144 #undef mImplArr
145 
146 #if defined __lux32__
147 #define mChunkSize 0x20000000
148 #elif defined __win32__
149 #define mChunkSize 0x20000000
150 #else
151 #define mChunkSize 0x800000000
152 #endif
153 
154 
163 template <class RT, class AT>
165 {
166 public:
170 
171  ValueSeries<RT>* clone() const;
172 
173  bool isOK() const { return cursize_>=0; }
174 
175  RT value( od_int64 idx ) const;
176  bool writable() const { return true; }
177  void setValue(od_int64 idx, RT v);
178 
179  bool canSetAll() const { return writable(); }
180  void setAll(RT);
181 
182  const RT* arr() const;
183  RT* arr();
184 
185  bool selfSufficient() const { return true; }
186  bool reSizeable() const { return true; }
187  inline bool setSize(od_int64);
188  od_int64 size() const { return cursize_; }
189  char bytesPerItem() const { return sizeof(AT); }
190 
191 protected:
195 };
196 
197 
202 template <class T>
204 {
205 public:
207  ValueSeries<T>& to, od_int64 nriterations )
208  : from_( from )
209  , to_( &to )
210  , toptr_( 0 )
211  , nriterations_( nriterations )
212  {}
213 
215  od_int64 nriterations )
216  : from_( from )
217  , toptr_( to )
218  , to_( 0 )
219  , nriterations_( nriterations )
220  {}
221 
222 od_int64 nrIterations() const { return nriterations_; }
223 bool doWork( od_int64 start, od_int64 stop, int )
224  {
225  od_int64 nrleft = stop-start+1;
226  const T* fromarr = from_.arr();
227  T* toarr = toptr_ ? toptr_ : to_->arr();
228  if ( toarr && fromarr )
229  OD::sysMemCopy( toarr+start, fromarr+start,
230  (size_t) (nrleft*from_.bytesPerItem()) );
231  else if ( toarr )
232  {
233  toarr += start;
234  for ( od_int64 idx=start; idx<=stop; idx++, toarr++ )
235  *toarr = from_.value( idx );
236  }
237  else if ( fromarr )
238  {
239  fromarr += start;
240  for ( od_int64 idx=start; idx<=stop; idx++, fromarr++ )
241  to_->setValue(idx, *fromarr );
242  }
243  else
244  {
245  for ( od_int64 idx=start; idx<=stop; idx++ )
246  to_->setValue(idx,from_.value(idx));
247  }
248 
249  return true;
250  }
251 
252 protected:
257 };
258 
259 
260 template <class T> inline
262 {
263  ValueSeriesGetAll<T> setter( *this, to, nrvals );
264  setter.execute();
265 }
266 
267 
268 template <class T> inline
269 void ValueSeries<T>::getValues( T* to, od_int64 nrvals ) const
270 {
271  ValueSeriesGetAll<T> setter( *this, to, nrvals );
272  setter.execute();
273 }
274 
275 
276 template <class RT, class AT> inline
278 { return new MultiArrayValueSeries<RT,AT>( *this ); }
279 
280 
281 template <class T> inline
283  : src_( src ), off_( off ), writable_(true)
284 {}
285 
286 
287 template <class T> inline
289  : src_( const_cast<ValueSeries<T>& >(src) ), off_( off ), writable_(false)
290 {}
291 
292 
293 template <class T> inline
295 { return new OffsetValueSeries( src_, off_ ); }
296 
297 
298 template <class T> inline
300 { return src_.value(idx+off_); }
301 
302 template <class T> inline
304 {
305  if ( writable_ )
306  src_.setValue(idx+off_,v);
307  else
308  { pErrMsg("Attempting to write to write-protected array"); }
309 }
310 
311 
312 template <class T> inline
314 {
315  if ( writable_ )
316  src_.setAll( v );
317  else
318  { pErrMsg("Attempting to write to write-protected array"); }
319 }
320 
321 
322 
323 template <class T> inline
325 { return writable_ && src_.canSetAll(); }
326 
327 
328 template <class T> inline
330 { T* p = src_.arr(); return p ? p+off_ : 0; }
331 
332 
333 template <class T> inline
335 { T* p = src_.arr(); return p ? p+off_ : 0; }
336 
337 
338 template <class T> inline
340 { return off_; }
341 
342 
343 template <class T> inline
345 { off_ = no; }
346 
347 
348 template <class T> inline
350 { return writable_; }
351 
352 
353 template <class RT, class AT>
355  : ptr_(ptr), mine_(memmine), cursize_( cursz )
356 {}
357 
358 
359 template <class RT, class AT>
361  : ptr_( 0 ), mine_(true), cursize_( -1 )
362 {
363  setSize( sz );
364 }
365 
366 
367 template <class RT, class AT>
369 {
370  AT* ptr = ptr_;
371  if ( mine_ && cursize_>0 )
372  {
373  ptr = new AT[cursize_];
374  OD::memCopy( ptr, ptr_, sizeof(AT)*cursize_ );
375  }
376 
377  return new ArrayValueSeries( ptr, mine_, cursize_ );
378 }
379 
380 
381 template <class RT, class AT>
383 {
384 #ifdef __debug__
385  if ( idx<0 || (cursize_>=0 && idx>=cursize_ ) )
386  {
387  pErrMsg("Invalid access");
388  DBG::forceCrash(true);
389  }
390 #endif
391 
392  return (RT) ptr_[idx];
393 }
394 
395 
396 template <class RT, class AT>
398 {
399 #ifdef __debug__
400  if ( idx<0 || (cursize_>=0 && idx>=cursize_ ) )
401  {
402  pErrMsg("Invalid access");
403  DBG::forceCrash(true);
404  }
405 #endif
406 
407  ptr_[idx] = (AT) v;
408 }
409 
410 
411 template <class RT, class AT>
413 {
414  OD::memValueSet( ptr_, (AT)val, cursize_ );
415 }
416 
417 
418 template <class RT,class AT> inline
420 {
421  if ( cursize_!=-1 && cursize_==sz && ptr_ ) return true;
422  if ( !mine_ ) return false;
423 
424  AT* oldptr = ptr_;
425  if ( sz )
426  {
427  mTryAlloc( ptr_, AT[sz] );
428  }
429  else
430  ptr_ = 0;
431 
432  const od_int64 copysize = mMIN(sz,cursize_);
433  cursize_ = ptr_ ? sz : -1;
434  if ( ptr_ && copysize>0 )
435  OD::memCopy( ptr_, oldptr, (size_t) (copysize*sizeof(AT)) );
436 
437  delete [] oldptr;
438  return ptr_;
439 }
440 
441 
442 template <class RT, class AT> inline
444  : cursize_( -1 )
445  , chunksize_( mChunkSize/sizeof(AT) )
446 {
447  ptrs_.allowNull( true );
448  setSize( sz );
449 }
450 
451 
452 template <class RT, class AT> inline
454  const MultiArrayValueSeries<RT, AT>& mavs )
455  : ValueSeries<RT>( mavs )
456  , cursize_( -1 )
457  , chunksize_( mavs.chunksize_ )
458 {
459  ptrs_.allowNull( true );
460  if ( setSize( mavs.cursize_ ) && ptrs_.size() == mavs.ptrs_.size() )
461  {
462  for ( int idx=0; idx<ptrs_.size(); idx++ )
463  {
464  const od_int64 nextstart = ((od_int64) idx+1)*chunksize_;
465  od_int64 curchunksize = chunksize_;
466  if ( nextstart>cursize_ )
467  {
468  od_int64 diff = nextstart-cursize_;
469  curchunksize -= diff;
470  }
471  OD::memCopy( ptrs_[idx], mavs.ptrs_[idx], curchunksize*sizeof(AT) );
472  }
473  }
474 }
475 
476 
477 template <class RT, class AT> inline
479 {
480  deepEraseArr( ptrs_ );
481 }
482 
483 
484 template <class RT, class AT> inline
486 {
487 #ifdef __debug__
488  if ( idx<0 || idx>=cursize_ )
489  {
490  pErrMsg("Invalid access");
491  DBG::forceCrash(true);
492  }
493 #endif
494  const od_int64 arridx = idx/chunksize_;
495  if ( !ptrs_.validIdx(arridx) )
496  return RT();
497 
498  idx -= arridx*chunksize_;
499  return ptrs_[mCast(int,arridx)][idx];
500 }
501 
502 
503 template <class RT, class AT> inline
505 {
506 #ifdef __debug__
507  if ( idx<0 || idx>=cursize_ )
508  {
509  pErrMsg("Invalid access");
510  DBG::forceCrash(true);
511  }
512 #endif
513  const od_int64 arridx = idx/chunksize_;
514  if ( arridx>=ptrs_.size() )
515  return;
516 
517  idx -= arridx*chunksize_;
518  ptrs_[mCast(int,arridx)][idx] = v;
519 }
520 
521 
522 template <class RT, class AT> inline
524 {
525  if ( cursize_<=0 )
526  return;
527 
528  MemSetter<AT> memsetter;
529  memsetter.setValue( (AT)val );
530 
531  for ( int idx=ptrs_.size()-1; idx>=0; idx-- )
532  {
533  const od_int64 nextstart = ((od_int64) idx+1)*chunksize_;
534  od_int64 curchunksize = chunksize_;
535  if ( nextstart>cursize_ )
536  {
537  od_int64 diff = nextstart-cursize_;
538  curchunksize -= diff;
539  }
540 
541  memsetter.setTarget( ptrs_[idx] );
542  memsetter.setSize( curchunksize );
543  memsetter.execute();
544  }
545 }
546 
547 
548 template <class RT, class AT> inline
550 {
551  return cursize_>0 && cursize_<=chunksize_ && typeid(RT)==typeid(AT)
552  ? (RT*) ptrs_[0] : (RT*) 0;
553 }
554 
555 
556 template <class RT, class AT> inline
558 { return const_cast<MultiArrayValueSeries<RT,AT>*>( this )->arr(); }
559 
560 
561 template <class RT, class AT> inline
563 {
564  if ( cursize_==sz )
565  return true;
566 
567  od_uint64 lefttoalloc = sz > 0 ? (od_uint64)sz : 0;
568  deepEraseArr( ptrs_ );
569 
570  while ( lefttoalloc )
571  {
572  const od_uint64 allocsize = lefttoalloc>=chunksize_
573  ? (od_uint64)chunksize_ : lefttoalloc;
574 
575  AT* ptr;
576  mTryAlloc( ptr, AT[allocsize] );
577  if ( !ptr )
578  {
579  cursize_ = -1;
580  deepEraseArr( ptrs_ );
581  return false;
582  }
583 
584  ptrs_ += ptr;
585 
586  if ( lefttoalloc > allocsize )
587  lefttoalloc -= allocsize;
588  else
589  lefttoalloc = 0;
590  }
591 
592  cursize_ = sz;
593  return true;
594 }
595 
596 #undef mChunkSize
597 
598 #endif
T * arr()
Definition: valseries.h:329
void setValue(int64_t idx, RT v)
Definition: valseries.h:397
void getValues(ValueSeries< T > &, int64_t nrvals) const
Definition: valseries.h:261
T operator[](int64_t idx) const
Definition: valseries.h:60
ValueSeries< T > * clone() const
Definition: valseries.h:294
T to(const F &fr)
Definition: convert.h:33
ValueSeries< RT > * clone() const
Definition: valseries.h:368
Valueseries that allocates its data in smaller chunks. By doing this, it performs better in environme...
Definition: valseries.h:164
bool doWork(int64_t start, int64_t stop, int)
Definition: valseries.h:223
Sets large amounts of values to a constant using multiple threads.
Definition: odmemory.h:42
ValueSeries< T > & src_
Definition: valseries.h:91
bool setSize(int64_t)
Definition: valseries.h:419
int64_t nrIterations() const
Definition: valseries.h:222
ArrayValueSeries(AT *ptr, bool memmine, int64_t sz=-1)
Definition: valseries.h:354
#define mCast(tp, v)
Definition: commondefs.h:124
bool writable_
Definition: valseries.h:93
#define od_int64
Definition: plftypes.h:36
virtual bool reSizeable() const
Definition: valseries.h:52
virtual bool selfSufficient() const
Definition: valseries.h:50
ValueSeries< T > * to_
Definition: valseries.h:255
T value(int64_t idx) const
Definition: valseries.h:299
bool canSetAll() const
Definition: valseries.h:324
virtual bool setSize(int64_t)
Definition: valseries.h:53
bool writable() const
Definition: valseries.h:176
ValueSeries of offsets.
Definition: valseries.h:69
Gets ValueSeries.
Definition: valseries.h:203
MultiArrayValueSeries(int64_t)
Definition: valseries.h:443
#define mImplArr
Definition: valseries.h:99
const RT * arr() const
Definition: valseries.h:557
#define mMIN(x, y)
Definition: commondefs.h:49
bool writable() const
Definition: valseries.h:349
bool selfSufficient() const
Definition: valseries.h:185
Generalization of a task that can be run in parallel.
Definition: paralleltask.h:66
size_type size() const
Definition: objectset.h:50
bool reSizeable() const
Definition: valseries.h:132
RT value(int64_t idx) const
Definition: valseries.h:382
Series of values from a pointer to some kind of array. If a more advanced conversion between the retu...
Definition: valseries.h:109
virtual bool isOK() const
Definition: valseries.h:41
int64_t off_
Definition: valseries.h:92
bool isOK() const
Definition: valseries.h:173
RT value(int64_t idx) const
Definition: valseries.h:485
virtual bool writable() const
Definition: valseries.h:44
int64_t cursize_
Definition: valseries.h:193
const RT * arr() const
Definition: valseries.h:128
T * toptr_
Definition: valseries.h:256
OffsetValueSeries(ValueSeries< T > &src, int64_t off)
Definition: valseries.h:282
char bytesPerItem() const
Definition: valseries.h:189
bool execute()
Definition: paralleltask.h:71
Interface to a series of values.
Definition: odmemory.h:17
#define mTryAlloc(var, stmt)
Catches bad_alloc and sets ptr to null as normal.
Definition: commondefs.h:241
int64_t getOffset() const
Definition: valseries.h:339
int64_t cursize_
Definition: valseries.h:141
bool isOK() const
Definition: valseries.h:119
void memValueSet(T *, T, int64_t, TaskRunner *taskrun=0)
Definition: odmemory.h:480
bool canSetAll() const
Definition: valseries.h:179
ValueSeriesGetAll(const ValueSeries< T > &from, ValueSeries< T > &to, int64_t nriterations)
Definition: valseries.h:206
void setOffset(int64_t no)
Definition: valseries.h:344
AT * ptr_
Definition: valseries.h:139
virtual void setValue(int64_t, T)
Definition: valseries.h:45
virtual void setAll(T)
Definition: valseries.h:48
bool setSize(int64_t)
Definition: valseries.h:562
#define od_uint64
Definition: plftypes.h:37
void sysMemCopy(void *, const void *, int64_t)
virtual char bytesPerItem() const
Definition: valseries.h:58
void setValue(int64_t idx, RT v)
Definition: valseries.h:504
int64_t size() const
Definition: valseries.h:188
void setAll(RT)
Definition: valseries.h:523
const ValueSeries< T > & source() const
Definition: valseries.h:88
void deepEraseArr(ObjectSet< T > &os)
empty the ObjectSet deleting all objects pointed to.
Definition: objectset.h:122
~ArrayValueSeries()
Definition: valseries.h:115
~MultiArrayValueSeries()
Definition: valseries.h:478
void forceCrash(bool withdump)
const RefTree & RT()
#define mChunkSize
Definition: valseries.h:151
ValueSeriesGetAll(const ValueSeries< T > &from, T *to, int64_t nriterations)
Definition: valseries.h:214
void setAll(RT)
Definition: valseries.h:412
virtual bool validIdx(int64_t) const
Definition: objectset.h:302
ObjectSet< AT > ptrs_
Definition: valseries.h:192
bool writable() const
Definition: valseries.h:122
int64_t nriterations_
Definition: valseries.h:253
virtual bool canSetAll() const
Definition: valseries.h:47
void setSize(int64_t sz)
Definition: odmemory.h:55
void allowNull(bool yn=true)
Definition: objectset.h:297
#define mClass(module)
Definition: commondefs.h:164
#define pErrMsg(msg)
Definition: errmsg.h:60
const ValueSeries< T > & from_
Definition: valseries.h:254
bool mine_
Definition: valseries.h:140
void setValue(int64_t idx, T v)
Definition: valseries.h:303
void setAll(T)
Definition: valseries.h:313
virtual T * arr()
Definition: valseries.h:55
const int64_t chunksize_
Definition: valseries.h:194
void setTarget(T *ptr)
Definition: odmemory.h:53
void setValue(const T &val)
Definition: odmemory.h:52
char bytesPerItem() const
Definition: valseries.h:135
ValueSeries< RT > * clone() const
Definition: valseries.h:277
int64_t size() const
Definition: valseries.h:134
virtual ~ValueSeries()
Definition: valseries.h:35
virtual const T * arr() const
Definition: valseries.h:56
bool canSetAll() const
Definition: valseries.h:125
bool reSizeable() const
Definition: valseries.h:186

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