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

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