OpendTect  6.3
valseriesimpl.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: Bert Bril & Kris Tingdahl
8  Date: Mar 2005
9 ________________________________________________________________________
10 
11 -*/
12 
13 #include "valseries.h"
14 
15 #ifdef __debug__
16 #include "debug.h"
17 #endif
18 
21 template <class T>
23 {
24 public:
25  inline OffsetValueSeries( ValueSeries<T>& src, od_int64 off );
26  inline OffsetValueSeries( const ValueSeries<T>& src,
27  od_int64 off);
28  inline ValueSeries<T>* clone() const;
29 
30  inline T value( od_int64 idx ) const;
31  inline void setValue( od_int64 idx, T v );
32  inline T* arr();
33  inline const T* arr() const;
34  inline bool writable() const;
35  inline bool canSetAll() const;
36  inline void setAll(T);
37 
38  inline od_int64 getOffset() const;
39  inline void setOffset(od_int64 no);
40 
41  const ValueSeries<T>& source() const { return src_; }
42 
43 protected:
46  bool writable_;
47 };
48 
49 
50 #include <typeinfo>
51 
52 #define mImplArr \
53 { return typeid(RT)==typeid(AT) ? (RT*) ptr_ : (RT*) 0;}
54 
61 template <class RT, class AT>
63 {
64 public:
65 
66  ArrayValueSeries( AT* ptr, bool memmine, od_int64 sz=-1 );
68  ~ArrayValueSeries() { if ( mine_ ) delete [] ptr_; }
69 
70  ValueSeries<RT>* clone() const;
71 
72  bool isOK() const { return ptr_; }
73 
74  RT value( od_int64 idx ) const;
75  bool writable() const { return true; }
76  void setValue( od_int64 idx, RT v );
77 
78  bool canSetAll() const { return writable(); }
79  void setAll(RT);
80 
81  const RT* arr() const mImplArr;
82  RT* arr() mImplArr;
83 
84  bool reSizeable() const { return mine_; }
85  inline bool setSize(od_int64);
86  od_int64 size() const { return cursize_; }
87  char bytesPerItem() const { return sizeof(AT); }
88 
89 protected:
90 
91  AT* ptr_;
92  bool mine_;
94 };
95 
96 #undef mImplArr
97 
98 #if defined __lux32__
99 #define mChunkSize 0x20000000
100 #elif defined __win32__
101 #define mChunkSize 0x20000000
102 #else
103 #define mChunkSize 0x800000000
104 #endif
105 
106 
115 template <class RT, class AT>
117 {
118 public:
122 
123  ValueSeries<RT>* clone() const;
124 
125  bool isOK() const { return cursize_>=0; }
126 
127  RT value( od_int64 idx ) const;
128  bool writable() const { return true; }
129  void setValue(od_int64 idx, RT v);
130 
131  bool canSetAll() const { return writable(); }
132  void setAll(RT);
133 
134  const RT* arr() const;
135  RT* arr();
136 
137  bool reSizeable() const { return true; }
138  inline bool setSize(od_int64);
139  od_int64 size() const { return cursize_; }
140  char bytesPerItem() const { return sizeof(AT); }
141 
142 protected:
146 };
147 
148 
149 template <class RT, class AT> inline
151 { return new MultiArrayValueSeries<RT,AT>( *this ); }
152 
153 
154 template <class T> inline
156  : src_( src ), off_( off ), writable_(true)
157 {}
158 
159 
160 template <class T> inline
162  : src_( const_cast<ValueSeries<T>& >(src) ), off_( off ), writable_(false)
163 {}
164 
165 
166 template <class T> inline
168 { return new OffsetValueSeries( src_, off_ ); }
169 
170 
171 template <class T> inline
173 { return src_.value(idx+off_); }
174 
175 template <class T> inline
177 {
178  if ( writable_ )
179  src_.setValue(idx+off_,v);
180  else
181  { pErrMsg("Attempting to write to write-protected array"); }
182 }
183 
184 
185 template <class T> inline
187 {
188  if ( writable_ )
189  src_.setAll( v );
190  else
191  { pErrMsg("Attempting to write to write-protected array"); }
192 }
193 
194 
195 
196 template <class T> inline
198 { return writable_ && src_.canSetAll(); }
199 
200 
201 template <class T> inline
203 { T* p = src_.arr(); return p ? p+off_ : 0; }
204 
205 
206 template <class T> inline
208 { T* p = src_.arr(); return p ? p+off_ : 0; }
209 
210 
211 template <class T> inline
213 { return off_; }
214 
215 
216 template <class T> inline
218 { off_ = no; }
219 
220 
221 template <class T> inline
223 { return writable_; }
224 
225 
226 template <class RT, class AT>
228  : ptr_(ptr), mine_(memmine), cursize_( cursz )
229 {}
230 
231 
232 template <class RT, class AT>
234  : ptr_( 0 ), mine_(true), cursize_( -1 )
235 {
236  setSize( sz );
237 }
238 
239 
240 template <class RT, class AT>
242 {
243  AT* ptr = ptr_;
244  if ( mine_ && cursize_>0 )
245  {
246  ptr = new AT[cursize_];
247  OD::memCopy( ptr, ptr_, sizeof(AT)*cursize_ );
248  }
249 
250  return new ArrayValueSeries( ptr, mine_, cursize_ );
251 }
252 
253 
254 template <class RT, class AT>
256 {
257 #ifdef __debug__
258  if ( idx<0 || (cursize_>=0 && idx>=cursize_ ) )
259  {
260  pErrMsg("Invalid access");
261  DBG::forceCrash(true);
262  }
263 #endif
264 
265  return (RT) ptr_[idx];
266 }
267 
268 
269 template <class RT, class AT>
271 {
272 #ifdef __debug__
273  if ( idx<0 || (cursize_>=0 && idx>=cursize_ ) )
274  {
275  pErrMsg("Invalid access");
276  DBG::forceCrash(true);
277  }
278 #endif
279 
280  ptr_[idx] = (AT) v;
281 }
282 
283 
284 template <class RT, class AT>
286 {
287  OD::memValueSet( ptr_, (AT)val, cursize_ );
288 }
289 
290 
291 template <class RT,class AT> inline
293 {
294  if ( cursize_!=-1 && cursize_==sz && ptr_ ) return true;
295  if ( !mine_ ) return false;
296 
297  AT* oldptr = ptr_;
298  if ( sz )
299  {
300  mTryAlloc( ptr_, AT[sz] );
301  }
302  else
303  ptr_ = 0;
304 
305  const od_int64 copysize = mMIN(sz,cursize_);
306  cursize_ = ptr_ ? sz : -1;
307  if ( ptr_ && copysize>0 )
308  OD::memCopy( ptr_, oldptr, (size_t) (copysize*sizeof(AT)) );
309 
310  delete [] oldptr;
311  return ptr_;
312 }
313 
314 
315 template <class RT, class AT> inline
317  : cursize_( -1 )
318  , chunksize_( mChunkSize/sizeof(AT) )
319 {
320  ptrs_.allowNull( true );
321  setSize( sz );
322 }
323 
324 
325 template <class RT, class AT> inline
327  const MultiArrayValueSeries<RT, AT>& mavs )
328  : ValueSeries<RT>( mavs )
329  , cursize_( -1 )
330  , chunksize_( mavs.chunksize_ )
331 {
332  ptrs_.allowNull( true );
333  if ( setSize( mavs.cursize_ ) && ptrs_.size() == mavs.ptrs_.size() )
334  {
335  for ( int idx=0; idx<ptrs_.size(); idx++ )
336  {
337  const od_int64 nextstart = ((od_int64) idx+1)*chunksize_;
338  od_int64 curchunksize = chunksize_;
339  if ( nextstart>cursize_ )
340  {
341  od_int64 diff = nextstart-cursize_;
342  curchunksize -= diff;
343  }
344  OD::memCopy( ptrs_[idx], mavs.ptrs_[idx], curchunksize*sizeof(AT) );
345  }
346  }
347 }
348 
349 
350 template <class RT, class AT> inline
352 {
353  deepEraseArr( ptrs_ );
354 }
355 
356 
357 template <class RT, class AT> inline
359 {
360 #ifdef __debug__
361  if ( idx<0 || idx>=cursize_ )
362  {
363  pErrMsg("Invalid access");
364  DBG::forceCrash(true);
365  }
366 #endif
367  const od_int64 arridx = idx/chunksize_;
368  if ( !ptrs_.validIdx(arridx) )
369  return RT();
370 
371  idx -= arridx*chunksize_;
372  return ptrs_[mCast(int,arridx)][idx];
373 }
374 
375 
376 template <class RT, class AT> inline
378 {
379 #ifdef __debug__
380  if ( idx<0 || idx>=cursize_ )
381  {
382  pErrMsg("Invalid access");
383  DBG::forceCrash(true);
384  }
385 #endif
386  const od_int64 arridx = idx/chunksize_;
387  if ( arridx>=ptrs_.size() )
388  return;
389 
390  idx -= arridx*chunksize_;
391  ptrs_[mCast(int,arridx)][idx] = v;
392 }
393 
394 
395 template <class RT, class AT> inline
397 {
398  if ( cursize_<=0 )
399  return;
400 
401  MemSetter<AT> memsetter;
402  memsetter.setValue( (AT)val );
403 
404  for ( int idx=ptrs_.size()-1; idx>=0; idx-- )
405  {
406  const od_int64 nextstart = ((od_int64) idx+1)*chunksize_;
407  od_int64 curchunksize = chunksize_;
408  if ( nextstart>cursize_ )
409  {
410  od_int64 diff = nextstart-cursize_;
411  curchunksize -= diff;
412  }
413 
414  memsetter.setTarget( ptrs_[idx] );
415  memsetter.setSize( curchunksize );
416  memsetter.execute();
417  }
418 }
419 
420 
421 template <class RT, class AT> inline
423 {
424  return cursize_>0 && cursize_<=chunksize_ && typeid(RT)==typeid(AT)
425  ? (RT*) ptrs_[0] : (RT*) 0;
426 }
427 
428 
429 template <class RT, class AT> inline
431 { return const_cast<MultiArrayValueSeries<RT,AT>*>( this )->arr(); }
432 
433 
434 template <class RT, class AT> inline
436 {
437  if ( cursize_==sz )
438  return true;
439 
440  od_uint64 lefttoalloc = sz > 0 ? (od_uint64)sz : 0;
441  deepEraseArr( ptrs_ );
442 
443  while ( lefttoalloc )
444  {
445  const od_uint64 allocsize = lefttoalloc>=chunksize_
446  ? (od_uint64)chunksize_ : lefttoalloc;
447 
448  AT* ptr;
449  mTryAlloc( ptr, AT[allocsize] );
450  if ( !ptr )
451  {
452  cursize_ = -1;
453  deepEraseArr( ptrs_ );
454  return false;
455  }
456 
457  ptrs_ += ptr;
458 
459  if ( lefttoalloc > allocsize )
460  lefttoalloc -= allocsize;
461  else
462  lefttoalloc = 0;
463  }
464 
465  cursize_ = sz;
466  return true;
467 }
468 
469 #undef mChunkSize
T * arr()
Definition: valseriesimpl.h:202
void setValue(int64_t idx, RT v)
Definition: valseriesimpl.h:270
ValueSeries< T > * clone() const
Definition: valseriesimpl.h:167
#define mImplArr
Definition: valseriesimpl.h:52
ValueSeries< RT > * clone() const
Definition: valseriesimpl.h:241
Valueseries that allocates its data in smaller chunks. By doing this, it performs better in environme...
Definition: valseriesimpl.h:116
Sets large amounts of values to a constant using multiple threads.
Definition: odmemory.h:40
ValueSeries< T > & src_
Definition: valseriesimpl.h:44
bool setSize(int64_t)
Definition: valseriesimpl.h:292
ArrayValueSeries(AT *ptr, bool memmine, int64_t sz=-1)
Definition: valseriesimpl.h:227
#define mCast(tp, v)
Definition: commondefs.h:120
bool writable_
Definition: valseriesimpl.h:46
#define od_int64
Definition: plftypes.h:34
T value(int64_t idx) const
Definition: valseriesimpl.h:172
bool canSetAll() const
Definition: valseriesimpl.h:197
bool writable() const
Definition: valseriesimpl.h:128
ValueSeries of offsets.
Definition: valseriesimpl.h:22
MultiArrayValueSeries(int64_t)
Definition: valseriesimpl.h:316
const RT * arr() const
Definition: valseriesimpl.h:430
#define mMIN(x, y)
Definition: commondefs.h:52
bool writable() const
Definition: valseriesimpl.h:222
size_type size() const
Definition: objectset.h:48
RT value(int64_t idx) const
Definition: valseriesimpl.h:255
Series of values from a pointer to some kind of array. If a more advanced conversion between the retu...
Definition: valseriesimpl.h:62
int64_t off_
Definition: valseriesimpl.h:45
bool isOK() const
Definition: valseriesimpl.h:125
RT value(int64_t idx) const
Definition: valseriesimpl.h:358
int64_t cursize_
Definition: valseriesimpl.h:144
const RT * arr() const
Definition: valseriesimpl.h:81
OffsetValueSeries(ValueSeries< T > &src, int64_t off)
Definition: valseriesimpl.h:155
char bytesPerItem() const
Definition: valseriesimpl.h:140
bool execute()
Definition: paralleltask.h:69
Interface to a series of values.
Definition: odmemory.h:15
#define mTryAlloc(var, stmt)
Catches bad_alloc and sets ptr to null as normal.
Definition: commondefs.h:244
int64_t getOffset() const
Definition: valseriesimpl.h:212
int64_t cursize_
Definition: valseriesimpl.h:93
#define mChunkSize
Definition: valseriesimpl.h:103
bool isOK() const
Definition: valseriesimpl.h:72
void memValueSet(T *, T, int64_t, TaskRunner *taskrun=0)
Definition: odmemory.h:501
bool canSetAll() const
Definition: valseriesimpl.h:131
void setOffset(int64_t no)
Definition: valseriesimpl.h:217
AT * ptr_
Definition: valseriesimpl.h:91
bool setSize(int64_t)
Definition: valseriesimpl.h:435
#define od_uint64
Definition: plftypes.h:35
void setValue(int64_t idx, RT v)
Definition: valseriesimpl.h:377
int64_t size() const
Definition: valseriesimpl.h:139
void setAll(RT)
Definition: valseriesimpl.h:396
const ValueSeries< T > & source() const
Definition: valseriesimpl.h:41
void deepEraseArr(ObjectSet< T > &os)
empty the ObjectSet deleting all objects pointed to.
Definition: objectset.h:126
~ArrayValueSeries()
Definition: valseriesimpl.h:68
~MultiArrayValueSeries()
Definition: valseriesimpl.h:351
void forceCrash(bool withdump)
const RefTree & RT()
void setAll(RT)
Definition: valseriesimpl.h:285
virtual bool validIdx(int64_t) const
Definition: objectset.h:306
ObjectSet< AT > ptrs_
Definition: valseriesimpl.h:143
bool writable() const
Definition: valseriesimpl.h:75
void setSize(int64_t sz)
Definition: odmemory.h:53
void allowNull(bool yn=true)
Definition: objectset.h:301
#define mClass(module)
Definition: commondefs.h:161
#define pErrMsg(msg)
Usual access point for programmer error messages.
Definition: errmsg.h:34
bool mine_
Definition: valseriesimpl.h:92
void setValue(int64_t idx, T v)
Definition: valseriesimpl.h:176
void setAll(T)
Definition: valseriesimpl.h:186
const int64_t chunksize_
Definition: valseriesimpl.h:145
void setTarget(T *ptr)
Definition: odmemory.h:51
void setValue(const T &val)
Definition: odmemory.h:50
char bytesPerItem() const
Definition: valseriesimpl.h:87
ValueSeries< RT > * clone() const
Definition: valseriesimpl.h:150
int64_t size() const
Definition: valseriesimpl.h:86
bool canSetAll() const
Definition: valseriesimpl.h:78
bool reSizeable() const
Definition: valseriesimpl.h:137

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