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

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