OpendTect  6.3
typeset.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 / many others
8  Date: Apr 1995 / Feb 2009
9 RCS: $Id:$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #ifndef odset_h
15 #include "odset.h"
16 #endif
17 #ifndef vectoraccess_h
18 #include "vectoraccess.h"
19 #endif
20 
21 
24 template <class T, class I>
25 mClass(Basic) TypeSetBase : public OD::Set
26 {
27 public:
28 
29  typedef I size_type;
30  typedef T object_type;
31 
32  inline virtual ~TypeSetBase();
33  inline TypeSetBase<T,I>& operator =( const TypeSetBase<T,I>& ts )
34  { return copy( ts ); }
35 
36  inline size_type size() const;
37  inline virtual od_int64 nrItems() const { return size(); }
38  inline virtual bool setSize(size_type,T val=T());
40  inline virtual bool setCapacity(size_type sz,bool withmargin);
42  inline virtual size_type getCapacity() const;
43  inline void setAll(T);
44  inline void replace(T,T);
45 
46  inline T& get(size_type);
47  inline const T& get(size_type) const;
48  inline T& first();
49  inline const T& first() const;
50  inline T& last();
51  inline const T& last() const;
52  inline virtual bool validIdx(od_int64) const;
53  inline virtual size_type indexOf(T,bool forward=true,
54  size_type start=-1) const;
55  inline bool isPresent(const T&) const;
56  inline size_type count(const T&) const;
57 
58  inline TypeSetBase<T,I>& add(const T&);
59  inline virtual void insert(size_type,const T&);
60  inline bool push(const T&);
61  inline T pop();
62  inline virtual bool append(const T*,size_type);
63  inline virtual bool append(const TypeSetBase<T,I>&);
64  inline virtual bool addIfNew(const T&);
65  inline virtual TypeSetBase<T,I>& copy(const T*,size_type);
66  inline virtual TypeSetBase<T,I>& copy(const TypeSetBase<T,I>&);
67  virtual inline void createUnion(const TypeSetBase<T,I>&);
69  virtual inline void createIntersection(const TypeSetBase<T,I>&);
71  virtual inline void createDifference(const TypeSetBase<T,I>&,
72  bool must_preserve_order=false);
74 
75  inline virtual void swap(od_int64,od_int64);
76  inline virtual void move(size_type from,size_type to);
77 
78  inline virtual void reverse();
79 
80  inline virtual void erase();
81  inline virtual void removeSingle(size_type,
82  bool preserver_order=true);
83  inline virtual void removeRange(size_type from,size_type to);
84 
86  inline virtual T* arr() { return gtArr(); }
87  inline virtual const T* arr() const { return gtArr(); }
88  inline std::vector<T>& vec();
89  inline const std::vector<T>& vec() const;
90 
91  inline T& operator[](size_type i) { return get(i); }
92  inline const T& operator[](size_type i) const { return get(i); }
93  inline TypeSetBase<T,I>& operator+=(const T& t) { return add(t); }
94  inline TypeSetBase<T,I>& operator-=(const T& t);
95 
96 protected:
97 
98  inline TypeSetBase();
99  inline TypeSetBase(size_type nr,T typ);
100  inline TypeSetBase(const T*,size_type nr);
101  inline TypeSetBase(const TypeSetBase<T,size_type>&);
102 
104 
105  inline virtual T* gtArr() const;
106 
107 };
108 
109  // useful for iterating over any OD::Set
110 template <class T,class I>
111 mGlobal(Basic) inline T& getRef( TypeSetBase<T,I>& ts, I i )
112 { return ts.get( i ); }
113 template <class T,class I>
114 mGlobal(Basic) inline const T& getRef( const TypeSetBase<T,I>& ts, I i )
115 { return ts.get( i ); }
116 
117 
130 template <class T>
131 mClass(Basic) TypeSet : public TypeSetBase<T,int>
132 {
133 public:
134 
136  : TypeSetBase<T,int>() {}
137  TypeSet( int nr, T typ )
138  : TypeSetBase<T,int>( nr, typ ) {}
139  TypeSet( const T* t, int nr )
140  : TypeSetBase<T,int>( t, nr ) {}
141  TypeSet( const TypeSet<T>& t )
142  : TypeSetBase<T,int>( t ) {}
143 
144 };
145 
146 
150 {
151 public:
152 
153  BoolTypeSetType( bool v=false )
154  : val_( v ) {}
155  operator bool() const { return (bool) val_; }
156  bool operator=( bool v ) { val_ = v; return v; }
157 
158 protected:
159 
160  char val_;
161 
162 };
163 
165 
166 
169 template <class T>
171 {
172 public:
173 
175  : TypeSetBase<T,od_int64>() {}
176  LargeValVec( od_int64 nr, T typ )
177  : TypeSetBase<T,od_int64>( nr, typ ) {}
178  LargeValVec( const T* t, od_int64 nr )
179  : TypeSetBase<T,od_int64>( t, nr ) {}
181  : TypeSetBase<T,od_int64>( t ) {}
182 
183 };
184 
185 
186 template <class T, class I>
187 inline bool operator ==( const TypeSetBase<T,I>& a, const TypeSetBase<T,I>& b )
188 {
189  if ( a.size() != b.size() ) return false;
190 
191  const I sz = a.size();
192  for ( I idx=0; idx<sz; idx++ )
193  if ( !(a[idx] == b[idx]) ) return false;
194 
195  return true;
196 }
197 
198 template <class T, class I>
199 inline bool operator !=( const TypeSetBase<T,I>& a, const TypeSetBase<T,I>& b )
200 { return !(a == b); }
201 
202 
204 template <class T, class I, class J, class S>
205 inline bool append( TypeSetBase<T,I>& to, const TypeSetBase<S,J>& from )
206 {
207  const J sz = from.size();
208  if ( !to.setCapacity( sz + to.size(), true ) )
209  return false;
210 
211  for ( J idx=0; idx<sz; idx++ )
212  to.add( (T)from[idx] );
213 
214  return true;
215 }
216 
217 
220 template <class T, class I,class S>
221 inline void copy( TypeSetBase<T,I>& to, const TypeSetBase<S,I>& from )
222 {
223  if ( &to == &from ) return;
224  to.erase();
225  append( to, from );
226 }
227 
228 
230 template <class T, class I>
231 inline void sort( TypeSetBase<T,I>& ts )
232 {
233  T tmp; const I sz = ts.size();
234  for ( I d=sz/2; d>0; d=d/2 )
235  for ( I i=d; i<sz; i++ )
236  for ( I j=i-d; j>=0 && ts[j]>ts[j+d]; j-=d )
237  { tmp = ts[j]; ts[j] = ts[j+d]; ts[j+d] = tmp; }
238 }
239 
240 
241 // Member function implementations
242 template <class T, class I> inline
244 {}
245 
246 template <class T, class I> inline
248 { setSize( nr, typ ); }
249 
250 template <class T, class I> inline
251 TypeSetBase<T,I>::TypeSetBase( const T* tarr, I nr )
252 { append( tarr, nr ); }
253 
254 template <class T, class I> inline
256  : OD::Set( t )
257 { append( t ); }
258 
259 template <class T, class I> inline
261 
262 template <class T, class I> inline
264 { return vec_.size(); }
265 
266 template <class T, class I> inline
267 bool TypeSetBase<T,I>::setSize( I sz, T val )
268 { return vec_.setSize( sz, val ); }
269 
270 template <class T, class I> inline
272 { return vec_.getCapacity(); }
273 
274 template <class T, class I> inline
275 bool TypeSetBase<T,I>::setCapacity( I sz, bool withmargin )
276 { return vec_.setCapacity( sz, withmargin ); }
277 
278 template <class T, class I> inline
280 { vec_.fillWith( val ); }
281 
282 template <class T, class I> inline
283  void TypeSetBase<T,I>::replace( T val, T newval )
284 { vec_.replace( val, newval ); }
285 
286 
287 template <class T, class I> inline
289 { return vec_.validIdx( (I)idx ); }
290 
291 template <class T, class I> inline
293 {
294 #ifdef __debug__
295  if ( !validIdx(idx) )
296  DBG::forceCrash(true);
297 #endif
298  return vec_[idx];
299 }
300 
301 template <class T, class I> inline
302 const T& TypeSetBase<T,I>::get( I idx ) const
303 {
304 #ifdef __debug__
305  if ( !validIdx(idx) )
306  DBG::forceCrash(true);
307 #endif
308  return vec_[idx];
309 }
310 
311 template <class T, class I> inline
313 { return vec_.first(); }
314 
315 template <class T, class I> inline
316 const T& TypeSetBase<T,I>::first() const
317 { return vec_.first(); }
318 
319 template <class T, class I> inline
321 { return vec_.last(); }
322 
323 template <class T, class I> inline
324 const T& TypeSetBase<T,I>::last() const
325 { return vec_.last(); }
326 
327 template <class T, class I> inline
329 { return vec_.pop_back(); }
330 
331 template <class T, class I> inline
332 I TypeSetBase<T,I>::indexOf( T typ, bool forward, I start ) const
333 { return vec_.indexOf( typ, forward, start ); }
334 
335 template <class T, class I> inline
336 bool TypeSetBase<T,I>::isPresent( const T& t ) const
337 { return vec_.isPresent(t); }
338 
339 template <class T, class I> inline
340 I TypeSetBase<T,I>::count( const T& typ ) const
341 { return vec_.count( typ ); }
342 
343 template <class T, class I> inline
345 { vec_.push_back( typ ); return *this; }
346 
347 template <class T, class I> inline
348 bool TypeSetBase<T,I>::push( const T& typ )
349 { return vec_.push_back( typ ); }
350 
351 template <class T, class I> inline
353 { vec_.erase( typ ); return *this; }
354 
355 template <class T, class I> inline
357 { return this == &ts ? *this : copy( ts.arr(), ts.size() ); }
358 
359 template <class T, class I> inline
361 { vec_.erase(); }
362 
363 template <class T, class I> inline
365 { vec_.remove( i1, i2 ); }
366 
367 template <class T, class I> inline
368 void TypeSetBase<T,I>::insert( I idx, const T& typ )
369 { vec_.insert( idx, typ );}
370 
371 template <class T, class I> inline
372 std::vector<T>& TypeSetBase<T,I>::vec()
373 { return vec_.vec(); }
374 
375 template <class T, class I> inline
376 const std::vector<T>& TypeSetBase<T,I>::vec() const
377 { return vec_.vec(); }
378 
379 template <class T, class I> inline
381 { return size()>0 ? const_cast<T*>(&(*this)[0]) : 0; }
382 
383 
384 template <class T, class I> inline
386 {
387  if ( !validIdx(idx0) || !validIdx(idx1) )
388  return;
389 
390  T tmp = vec_[(I)idx0];
391  vec_[(I)idx0] = vec_[(I)idx1];
392  vec_[(I)idx1] = tmp;
393 }
394 
395 
396 template <class T, class I> inline
397 void TypeSetBase<T,I>::move( I idxfrom, I idxto )
398 {
399  if ( !validIdx(idxfrom) || !validIdx(idxto) )
400  return;
401 
402  T tmp = vec_[idxfrom];
403  insert( idxto, tmp );
404  vec_.remove( idxfrom < idxto ? idxfrom : idxfrom+1 );
405 }
406 
407 
408 template <class T, class I> inline
410 {
411  const I sz = size();
412  const I hsz = sz/2;
413  for ( I idx=0; idx<hsz; idx++ )
414  swap( idx, sz-1-idx );
415 }
416 
417 
418 template <class T, class I> inline
420 {
421  if ( size() != sz )
422  { erase(); append(tarr,sz); }
423  else
424  {
425  for ( I idx=0; idx<sz; idx++ )
426  (*this)[idx] = tarr[idx];
427  }
428  return *this;
429 }
430 
431 
432 template <class T, class I> inline
434 {
435  if ( this != &ts )
436  return append( ts.arr(), ts.size() );
437 
438  const TypeSetBase<T,I> tscp( ts );
439  return append( tscp );
440 }
441 
442 
443 template <class T, class I> inline
444 bool TypeSetBase<T,I>::append( const T* tarr, I sz )
445 {
446  if ( !sz ) return true;
447 
448  if ( !setCapacity( sz+size(), true ) )
449  return false;
450 
451  for ( I idx=0; idx<sz; idx++ )
452  *this += tarr[idx];
453 
454  return true;
455 }
456 
457 
458 template <class T, class I>
460 {
461  const I sz = ts.size();
462  const T* ptr = ts.arr();
463  for ( I idx=0; idx<sz; idx++, ptr++ )
464  addIfNew( *ptr );
465 }
466 
467 
468 template <class T, class I>
470 {
471  for ( I idx=0; idx<size(); idx++ )
472  {
473  if ( ts.isPresent((*this)[idx]) )
474  continue;
475  removeSingle( idx--, false );
476  }
477 }
478 
479 
480 template <class T, class I>
482  bool kporder )
483 {
484  const I sz = ts.size();
485  for ( I idx=0; idx<sz; idx++ )
486  {
487  const T typ = ts[idx];
488  for ( I idy=0; idy<size(); idy++ )
489  {
490  if ( vec_[idy] == typ )
491  removeSingle( idy--, kporder );
492  }
493  }
494 }
495 
496 
497 template <class T, class I> inline
498 bool TypeSetBase<T,I>::addIfNew( const T& typ )
499 {
500  if ( !isPresent(typ) )
501  { *this += typ; return true; }
502  return false;
503 }
504 
505 
506 template <class T, class I> inline
507 void TypeSetBase<T,I>::removeSingle( I idx, bool kporder )
508 {
509  if ( kporder )
510  vec_.remove( idx );
511  else
512  {
513  const I lastidx = size()-1;
514  if ( idx != lastidx )
515  vec_[idx] = vec_[lastidx];
516  vec_.remove( lastidx );
517  }
518 }
Needed because the std lib has a crazy specialisation vector<bool>.
Definition: typeset.h:149
virtual void insert(size_type, const T &)
Definition: typeset.h:368
bool operator!=(const TypeSetBase< T, I > &a, const TypeSetBase< T, I > &b)
Definition: typeset.h:199
virtual void swap(int64_t, int64_t)
Definition: typeset.h:385
BoolTypeSetType(bool v=false)
Definition: typeset.h:153
Large Value Vector.
Definition: typeset.h:170
T to(const F &fr)
Definition: convert.h:31
char val_
Definition: typeset.h:160
TypeSet()
Definition: typeset.h:135
virtual void createUnion(const TypeSetBase< T, I > &)
Definition: typeset.h:459
#define mGlobal(module)
Definition: commondefs.h:160
bool operator==(const TypeSetBase< T, I > &a, const TypeSetBase< T, I > &b)
Definition: typeset.h:187
std::vector< T > & vec()
Definition: typeset.h:372
#define od_int64
Definition: plftypes.h:34
virtual ~TypeSetBase()
Definition: typeset.h:260
TypeSetBase< T, I > & add(const T &)
Definition: typeset.h:344
virtual void createIntersection(const TypeSetBase< T, I > &)
Only keeps common items.
Definition: typeset.h:469
T object_type
Definition: typeset.h:30
TypeSet(int nr, T typ)
Definition: typeset.h:137
T & get(size_type)
Definition: typeset.h:292
virtual bool setCapacity(size_type sz, bool withmargin)
Definition: typeset.h:275
virtual size_type getCapacity() const
Definition: typeset.h:271
ObjectSet< T >::size_type indexOf(const ObjectSet< T > &os, const S &val)
Locate object in set.
Definition: objectset.h:173
virtual void createDifference(const TypeSetBase< T, I > &, bool must_preserve_order=false)
Removes all items present in other set.
Definition: typeset.h:481
Base class for TypeSet, usually not used as such.
Definition: typeset.h:25
Simple vector-based container simplifying index-based work.
Definition: vectoraccess.h:36
T & getRef(TypeSetBase< T, I > &ts, I i)
Definition: typeset.h:111
virtual T * arr()
3rd party access
Definition: typeset.h:86
bool append(TypeSetBase< T, I > &to, const TypeSetBase< S, J > &from)
append allowing a different type to be merged into set
Definition: typeset.h:205
virtual T * gtArr() const
Definition: typeset.h:380
I size_type
Definition: typeset.h:29
Set of (small) copyable elements.
Definition: commontypes.h:26
virtual void reverse()
Definition: typeset.h:409
virtual bool addIfNew(const T &)
Definition: typeset.h:498
void sort(TypeSetBase< T, I > &ts)
Sort TypeSetBase. Must have operator > defined for elements.
Definition: typeset.h:231
LargeValVec(const TypeSet< T > &t)
Definition: typeset.h:180
virtual const T * arr() const
Definition: typeset.h:87
virtual void move(size_type from, size_type to)
Definition: typeset.h:397
TypeSetBase< T, I > & operator+=(const T &t)
Definition: typeset.h:93
LargeValVec(int64_t nr, T typ)
Definition: typeset.h:176
T & last()
Definition: typeset.h:320
T pop()
Definition: typeset.h:328
VectorAccess< T, I > vec_
Definition: typeset.h:103
void removeRange(ODSET &inst, size_type start, size_type stop)
Removes a range from the set.
Definition: odset.h:55
size_type count(const T &) const
Definition: typeset.h:340
T & operator[](size_type i)
Definition: typeset.h:91
virtual bool append(const T *, size_type)
Definition: typeset.h:444
bool push(const T &)
Definition: typeset.h:348
bool isPresent(const T &) const
Definition: typeset.h:336
TypeSet< BoolTypeSetType > BoolTypeSet
Definition: typeset.h:164
size_type size() const
Definition: typeset.h:263
virtual int64_t nrItems() const
Definition: typeset.h:37
void forceCrash(bool withdump)
TypeSetBase< T, I > & operator-=(const T &t)
Definition: typeset.h:352
virtual void erase()
Definition: typeset.h:360
OpendTect.
Definition: commontypes.h:25
TypeSet(const TypeSet< T > &t)
Definition: typeset.h:141
virtual bool setSize(size_type, T val=T())
Definition: typeset.h:267
const T & operator[](size_type i) const
Definition: typeset.h:92
LargeValVec(const T *t, int64_t nr)
Definition: typeset.h:178
void copy(TypeSetBase< T, I > &to, const TypeSetBase< S, I > &from)
Definition: typeset.h:221
void setAll(T)
Definition: typeset.h:279
virtual void removeSingle(size_type, bool preserver_order=true)
Definition: typeset.h:507
#define mClass(module)
Definition: commondefs.h:161
bool operator=(bool v)
Definition: typeset.h:156
virtual TypeSetBase< T, I > & copy(const T *, size_type)
Definition: typeset.h:419
virtual size_type indexOf(T, bool forward=true, size_type start=-1) const
Definition: typeset.h:332
virtual bool validIdx(int64_t) const
Definition: typeset.h:288
T & first()
Definition: typeset.h:312
TypeSetBase()
Definition: typeset.h:243
TypeSet(const T *t, int nr)
Definition: typeset.h:139
virtual void removeRange(size_type from, size_type to)
Definition: typeset.h:364
void replace(T, T)
Definition: typeset.h:283
LargeValVec()
Definition: typeset.h:174

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