OpendTect-6_4  6.4
posidxpairvalue.h
Go to the documentation of this file.
1 #ifndef posidxpairvalue_h
2 #define posidxpairvalue_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H.Bril
9  Date: 1996/Oct 2013
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "basicmod.h"
16 #include "posidxpair.h"
17 #include "typeset.h"
18 
19 
20 namespace Pos
21 {
22 
23 template <class IPT,class FT> class IdxPairValues;
24 
25 
28 template <class IPT,class FT>
29 mClass(Basic) ValueIdxPair : public IPT
30 {
31 public:
32 
33  typedef IPT pos_type;
34  typedef FT value_type;
35 
36  inline ValueIdxPair( int f=0, int s=0, FT v=mUdf(FT) )
37  : IPT(f,s), val_(v) {}
38  inline ValueIdxPair( const IPT& b, FT v=mUdf(FT) )
39  : IPT(b), val_(v) {}
40  inline ValueIdxPair( const IdxPairValues<IPT,FT>& ipvs, int idx=0 )
41  { set( ipvs, idx ); }
42 
43  inline bool operator==(const ValueIdxPair&) const;
44  inline bool operator!=( const ValueIdxPair& vip ) const
45  { return !(*this == vip); }
46  inline bool equalPos( const IPT& ip ) const
47  { return IPT::operator==(ip); }
48 
49  FT& val() { return val_; }
50  FT val() const { return val_; }
51  inline operator FT&() { return val_; }
52  inline operator FT() const { return val_; }
53 
54  inline void set( FT f ) { val_ = f; }
55  inline void set( const IPT& ipt ) { IPT::operator=( ipt ); }
56  inline void set( const IPT& ipt, FT f ) { set( ipt ); set( f ); }
57  inline void set(const IdxPairValues<IPT,FT>&,int idx=0);
58 
59  inline bool isUdf() const { return IPT::isUdf()
60  || mIsUdf(val_); }
61 
62 protected:
63 
64  FT val_;
65 
66 };
67 
68 
69 
75 template <class IPT,class FT>
76 mExpClass(Basic) IdxPairValues : public IPT
77 {
78 public:
79 
80  typedef IPT pos_type;
81  typedef FT value_type;
84 
85  IdxPairValues() { setSize(0); }
86  IdxPairValues( int i1, int i2, int nrvals )
87  : IPT(i1,i2) { setSize(nrvals); }
88  IdxPairValues( const IPT& p, int nrvals )
89  : IPT(p) { setSize(nrvals); }
91  : IPT() { *this = oth; }
93  : IPT(vip) { setSize(1); value(0) = vip; }
94 
95  inline bool operator==(const IdxPairValues&) const;
96  inline bool operator!=( const IdxPairValues& ipvs ) const
97  { return !(*this == ipvs); }
98  inline bool equalPos( const IPT& ip ) const
99  { return IPT::operator==(ip); }
100 
101  inline bool validIdx( int i ) const { return vals_.validIdx(i); }
102  inline size_type size() const { return vals_.size(); }
103  inline FT& value( int idx ) { return vals_[idx]; }
104  inline FT value( int idx ) const { return vals_[idx]; }
105  inline FT* values() { return vals_.arr(); }
106  inline const FT* values() const { return vals_.arr(); }
107  inline FT& operator[]( int idx ) { return value( idx ); }
108  inline FT operator[]( int idx ) const { return value( idx ); }
109  inline set_type& valSet() { return vals_; }
110  inline const set_type& valSet() const { return vals_; }
111  inline operator FT*() { return values(); }
112  inline operator const FT*() const { return values(); }
113 
114  inline void setSize(size_type,bool kpvals=false);
115  inline void setVals(const FT*);
116  inline void set(int,FT);
117  inline void set( const IPT& ipt ) { IPT::operator=( ipt ); }
118 
119  inline bool isUdf(bool allvalsudf=true) const;
120 
121 protected:
122 
124 
125 };
126 
127 
128 template <class IPT,class FT> inline
130 {
131  IPT::operator=( ipvs );
132  val_ = ipvs.validIdx( idx ) ? ipvs.value( idx ) : mUdf(FT);
133 }
134 
135 
136 template <class IPT,class FT> inline
138 {
139  if ( !equalPos( oth ) )
140  return false;
141  else if ( mIsUdf(val_) && mIsUdf(oth.val_) )
142  return true;
143 
144  const FT valdiff = val_ - oth.val_;
145  return mIsZero(valdiff,(FT)mDefEps);
146 }
147 
148 
149 template <class IPT,class FT> inline
151  const IdxPairValues<IPT,FT>& oth ) const
152 {
153  const size_type sz = size();
154  if ( sz != size() || !equalPos( oth ) )
155  return false;
156 
157  for ( size_type idx=0; idx<sz; idx++ )
158  {
159  const FT myval = value(idx);
160  const FT othval = oth.value(idx);
161 
162  if ( mIsUdf(myval) && mIsUdf(othval) )
163  continue;
164 
165  const FT valdiff = myval - othval;
166  if ( !mIsZero(valdiff,(FT)mDefEps) )
167  return false;
168  }
169 
170  return true;
171 }
172 
173 
174 template <class IPT,class FT> inline
176  typename IdxPairValues<IPT,FT>::size_type sz, bool kpvals )
177 {
178  if ( sz == size() )
179  {
180  if ( kpvals )
181  return;
182  vals_.erase();
183  }
184 
185  TypeSet<FT> tmp( vals_ );
186  const size_type orgsz = tmp.size();
187  for ( size_type idx=0; idx<sz; idx++ )
188  vals_ += idx<orgsz ? tmp[idx] : mUdf(float);
189 }
190 
191 
192 template <class IPT,class FT> inline
193 void IdxPairValues<IPT,FT>::setVals( const FT* newvals )
194 {
195  const size_type sz = size();
196  for ( size_type idx=0; idx<sz; idx++ )
197  vals_[idx] = newvals ? newvals[idx] : mUdf(float);
198 }
199 
200 
201 template <class IPT,class FT> inline
202 void IdxPairValues<IPT,FT>::set( int idx, FT val )
203 {
204  if ( validIdx(idx) )
205  vals_[idx] = val;
206 }
207 
208 
209 template <class IPT,class FT> inline
210 bool IdxPairValues<IPT,FT>::isUdf( bool anyvaludf ) const
211 {
212  if ( IPT::isUdf() )
213  return true;
214 
215  const size_type sz = size();
216  for ( size_type idx=0; idx<sz; idx++ )
217  {
218  const FT val = vals_[idx];
219  const bool isudf = mIsUdf(val);
220  if ( isudf && anyvaludf )
221  return true; // any val needs to be udf and this one is
222  else if ( !isudf && !anyvaludf )
223  return false; // all vals need to be udf and this one isn't
224  }
225 
226  return !anyvaludf;
227 }
228 
229 
230 } // namespace Pos
231 
232 
233 #endif
#define mExpClass(module)
Definition: commondefs.h:160
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:287
bool operator==(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:53
bool operator==(const IdxPairValues &) const
Definition: posidxpairvalue.h:150
TypeSet< FT >::size_type size_type
Definition: posidxpairvalue.h:83
bool equalPos(const IPT &ip) const
Definition: posidxpairvalue.h:98
#define mIsZero(x, eps)
Definition: commondefs.h:53
IdxPair with a value.
Definition: posidxpairvalue.h:29
const set_type & valSet() const
Definition: posidxpairvalue.h:110
IPT pos_type
Definition: posidxpairvalue.h:33
IdxPairValues(int i1, int i2, int nrvals)
Definition: posidxpairvalue.h:86
IdxPair with 0-N values.
Definition: posidxpairvalue.h:23
bool validIdx(int i) const
Definition: posidxpairvalue.h:101
size_type size() const
Definition: posidxpairvalue.h:102
set_type & valSet()
Definition: posidxpairvalue.h:109
FT * values()
Definition: posidxpairvalue.h:105
FT & value(int idx)
Definition: posidxpairvalue.h:103
ValueIdxPair(const IPT &b, FT v=Values::Undef< FT >::val())
Definition: posidxpairvalue.h:38
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
IdxPairValues()
Definition: posidxpairvalue.h:85
void setSize(size_type, bool kpvals=false)
Definition: posidxpairvalue.h:175
Position.
Definition: commontypes.h:42
bool isUdf() const
Definition: posidxpairvalue.h:59
TypeSet< FT > vals_
Definition: posidxpairvalue.h:123
FT operator[](int idx) const
Definition: posidxpairvalue.h:108
bool operator!=(const IdxPairValues &ipvs) const
Definition: posidxpairvalue.h:96
FT & operator[](int idx)
Definition: posidxpairvalue.h:107
ValueIdxPair(int f=0, int s=0, FT v=Values::Undef< FT >::val())
Definition: posidxpairvalue.h:36
void set(int, FT)
Definition: posidxpairvalue.h:202
#define mDefEps
Definition: commondefs.h:58
IdxPairValues(const IdxPairValues &oth)
Definition: posidxpairvalue.h:90
size_type size() const
Definition: typeset.h:254
FT value_type
Definition: posidxpairvalue.h:81
FT val_
Definition: posidxpairvalue.h:64
FT value(int idx) const
Definition: posidxpairvalue.h:104
bool operator==(const ValueIdxPair &) const
Definition: posidxpairvalue.h:137
bool operator!=(const ValueIdxPair &vip) const
Definition: posidxpairvalue.h:44
FT val() const
Definition: posidxpairvalue.h:50
IPT pos_type
Definition: posidxpairvalue.h:80
TypeSet< FT > set_type
Definition: posidxpairvalue.h:82
void setVals(const FT *)
Definition: posidxpairvalue.h:193
IdxPairValues(const ValueIdxPair< IPT, FT > &vip)
Definition: posidxpairvalue.h:92
bool isUdf(bool allvalsudf=true) const
Definition: posidxpairvalue.h:210
#define mClass(module)
Definition: commondefs.h:164
void set(FT f)
Definition: posidxpairvalue.h:54
bool isUdf(const T &t)
Definition: undefval.h:243
bool equalPos(const IPT &ip) const
Definition: posidxpairvalue.h:46
ValueIdxPair(const IdxPairValues< IPT, FT > &ipvs, int idx=0)
Definition: posidxpairvalue.h:40
FT & val()
Definition: posidxpairvalue.h:49
FT value_type
Definition: posidxpairvalue.h:34
IdxPairValues(const IPT &p, int nrvals)
Definition: posidxpairvalue.h:88
const FT * values() const
Definition: posidxpairvalue.h:106

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