OpendTect-6_4  6.4
classify.h
Go to the documentation of this file.
1 #ifndef classify_h
2 #define classify_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: Nov 2005
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 -*/
14 
15 #include "generalmod.h"
16 #include "typeset.h"
17 
18 
23 template <class T>
25 {
26 public:
27  WeightedClassCounter( bool canbeneg=true )
28  : wts_(0)
29  , canbeneg_(canbeneg) {}
30 
31  void add(int val,T wt=1);
32 
33  bool valid() const { return wts_; }
34  int result() const;
35 
36  void clear() { delete wts_; wts_ = 0; }
37 
38 protected:
39 
40  bool canbeneg_;
42 
43 };
44 
45 
46 template <class T>
47 inline void WeightedClassCounter<T>::add( int val, T wt )
48 {
49  const int arridx = !canbeneg_ ? val
50  : (val < 0 ? -1 - 2 * val : 2 * val);
51  // encode 0=>0 -1<=1 1=>2 -2=>3 2=>4 etc.
52 
53  if ( !wts_ || wts_->size() <= arridx )
54  {
55  TypeSet<T>* tmp = wts_ ? new TypeSet<T>( *wts_ ) : 0;
56  delete wts_;
57  wts_ = new TypeSet<T>( arridx+1, -2. );
58  if ( tmp )
59  OD::memCopy( wts_->arr(), tmp->arr(), tmp->size() * sizeof(T) );
60  }
61 
62  T& curwt = (*wts_)[arridx];
63  if ( curwt < -1 )
64  curwt = wt;
65  else
66  curwt += wt;
67 }
68 
69 
70 template <class T>
72 {
73  if ( !valid() ) return 0;
74 
75  int winner = -1; T maxwt = -1;
76  for ( int idx=0; idx<wts_->size(); idx++ )
77  {
78  if ( (*wts_)[idx] > maxwt )
79  { winner = idx; maxwt = (*wts_)[idx]; }
80  }
81 
82  int res = winner;
83  if ( canbeneg_ )
84  {
85  // decode 0=>0 1=>-1 2=>1 3=>-2 4=>2 etc.
86  res /= 2;
87  if ( winner % 2 ) res = -res;
88  }
89  return res;
90 }
91 
92 
93 #endif
#define mExpClass(module)
Definition: commondefs.h:160
bool canbeneg_
Definition: classify.h:40
void clear()
Definition: classify.h:36
void add(int val, T wt=1)
Definition: classify.h:47
virtual T * arr()
3rd party access
Definition: typeset.h:92
TypeSet< T > * wts_
Definition: classify.h:41
Set of (small) copyable elements.
Definition: commontypes.h:30
bool valid() const
Definition: classify.h:33
WeightedClassCounter(bool canbeneg=true)
Definition: classify.h:27
size_type size() const
Finds out which class is the best from a series of weighted data points.
Definition: classify.h:24
int result() const
Definition: classify.h:71

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