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

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