source: trunk/c++_tools/classifier/Kernel.cc @ 640

Last change on this file since 640 was 640, checked in by Peter, 15 years ago

fixes #132 and #95, and refs #135 replaced bool owner in DataLookup2D with ref_count u_int*. When the DataLookup2D does not own underlying data ref_count=NULL otherwise it points to a u_int telling how many owner there are of the data. When the counter reaches zero the data is deleted.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 1.7 KB
Line 
1// $Id$
2
3#include <c++_tools/classifier/Kernel.h>
4
5#include <c++_tools/classifier/DataLookup1D.h>
6#include <c++_tools/classifier/DataLookupWeighted1D.h>
7#include <c++_tools/classifier/KernelFunction.h>
8#include <c++_tools/classifier/MatrixLookup.h>
9
10#include <vector>
11
12namespace theplu {
13namespace classifier {
14
15  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf) 
16    : data_(&data), data_w_(0), kf_(&kf), data_owner_(false), 
17      weight_owner_(false)
18  {
19  }
20
21
22  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf) 
23    : data_(&data), data_w_(&data), kf_(&kf), data_owner_(false), 
24      weight_owner_(false)
25  {
26  }
27
28
29  Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
30    : kf_(other.kf_), data_owner_(true)
31  {
32    data_ = other.data_->selected(index);
33    if (other.data_w_){
34      data_w_ = other.data_w_->selected(index);
35      weight_owner_=true;
36    }
37    else{
38      data_w_=NULL;
39      weight_owner_=false;
40    }
41
42  }
43
44  Kernel::~Kernel()
45  {
46    if (data_owner_)
47      delete data_;
48     
49    if (weight_owner_)
50      if (data_w_)
51        delete data_w_;
52      else 
53        std::cerr << "Error in Kernel implementation: probably a constructor"
54                  << std::endl;
55   
56  }
57
58 
59  double Kernel::element(const DataLookup1D& vec, const size_t i) const
60  {
61    if (data_w_)
62      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
63    else
64      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
65  }
66
67
68  double Kernel::element(const DataLookupWeighted1D& vec, const size_t i) const
69  {
70    if (data_w_)
71      return kf_->operator()(vec, DataLookupWeighted1D(*data_w_,i, false)); 
72    else
73      return kf_->operator()(vec, DataLookup1D(*data_,i, false)); 
74  }
75
76}} // of namespace classifier and namespace theplu
Note: See TracBrowser for help on using the repository browser.