Ignore:
Timestamp:
Feb 26, 2008, 11:09:04 PM (14 years ago)
Author:
Peter
Message:

refs #343 moving data to inherited classes and using SmartPtr?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/MatrixLookupWeighted.cc

    r1168 r1169  
    3838                                             const utility::Matrix& weights,
    3939                                             const bool own)
    40     : DataLookup2D(own), data_(&data), weights_(&weights),
    41       ref_count_weights_(NULL)
     40    : data_(MatrixP(&data, own)), weights_(MatrixP(&weights, own))
    4241  {
    4342    assert(data.rows()==weights.rows());
     
    4948
    5049  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data)
    51     : DataLookup2D(), data_(&data)
     50    : DataLookup2D(), data_(MatrixP(&data, false))
    5251  {
    5352    utility::Matrix weights;
    5453    utility::nan(*data_,weights);
    55     weights_= new utility::Matrix(weights);
    56     ref_count_weights_=new u_int(1);
     54    weights_= MatrixP(new utility::Matrix(weights));
    5755    row_index_ = utility::Index(data.rows());
    5856    column_index_ = utility::Index(data.columns());
     
    6361    : DataLookup2D(ml), data_(ml.data_)
    6462  {
    65     weights_= new utility::Matrix(data_->rows(), data_->columns(), 1.0);
    66     ref_count_weights_=new u_int(1);
    67     ref_count_=ml.ref_count_;
    68     if (ref_count_)
    69       ++(*ref_count_);
    70 
     63    weights_= MatrixP(new utility::Matrix(data_->rows(), data_->columns(),1.0));
    7164  }
    7265 
     
    7669                                             const utility::Index& row,
    7770                                             const utility::Index& col)
    78     : DataLookup2D(row,col), data_(&data), weights_(&weights),
    79       ref_count_weights_(NULL)
     71    : DataLookup2D(row,col), data_(MatrixP(new utility::Matrix(data), false)),
     72      weights_(MatrixP(new utility::Matrix(weights), false))
    8073  {
    8174  }
     
    8780                                             const utility::Index& index,
    8881                                             const bool row)
    89     : DataLookup2D(), data_(&data), weights_(&weights),
    90       ref_count_weights_(NULL)
     82    : DataLookup2D(), data_(MatrixP(new utility::Matrix(data), false)),
     83      weights_(MatrixP(new utility::Matrix(weights), false))
    9184  {
    9285    assert(data.rows()==weights.rows());
     
    115108    : DataLookup2D(other), data_(other.data_), weights_(other.weights_)
    116109  {
    117     ref_count_ = other.ref_count_;
    118     if (ref_count_)
    119       ++(*ref_count_);
    120     ref_count_weights_ = other.ref_count_weights_;
    121     if (ref_count_weights_)
    122       ++(*ref_count_weights_);
    123 
    124110  }
    125111
     
    131117    : DataLookup2D(other,row,col), data_(other.data_), weights_(other.weights_)
    132118  {
    133     ref_count_ = other.ref_count_;
    134     if (ref_count_)
    135       ++(*ref_count_);
    136     ref_count_weights_ = other.ref_count_weights_;
    137     if (ref_count_weights_)
    138       ++(*ref_count_weights_);
    139119  }
    140120 
     
    147127      weights_(other.weights_)
    148128  {
    149     ref_count_ = other.ref_count_;
    150     if (ref_count_)
    151       ++(*ref_count_);
    152     ref_count_weights_ = other.ref_count_weights_;
    153     if (ref_count_weights_)
    154       ++(*ref_count_weights_);
    155 
    156129  }
    157130 
     
    162135                                             const double value,
    163136                                             const double weight)
    164     : DataLookup2D(rows,columns)
    165   {
    166     data_ = new utility::Matrix(1,1,value);
    167     ref_count_=new u_int(1);
    168     weights_ = new utility::Matrix(1,1,weight);
    169     ref_count_weights_=new u_int(1);
     137    : DataLookup2D(rows,columns),
     138      data_(MatrixP(new utility::Matrix(1,1,value))),
     139      weights_(MatrixP(new utility::Matrix(1,1,weight)))
     140  {
    170141  }
    171142
     
    174145    : DataLookup2D()
    175146  {
    176     data_ = new utility::Matrix(is,sep);
    177     ref_count_=new u_int(1);
     147    data_ = MatrixP(new utility::Matrix(is,sep));
    178148    row_index_ = utility::Index(data_->rows());
    179149    column_index_ = utility::Index(data_->columns());
    180150    utility::Matrix weights;
    181151    utility::nan(*data_,weights);
    182     weights_= new utility::Matrix(weights);
    183     ref_count_weights_=new u_int(1);
     152    // Peter, should be possible to avoid this copying
     153    weights_= MatrixP(new utility::Matrix(weights));
    184154  }
    185155 
     
    187157  MatrixLookupWeighted::~MatrixLookupWeighted(void)
    188158  {
    189     if (ref_count_)
    190       if (!--(*ref_count_))
    191         delete data_;
    192     if (ref_count_weights_)
    193       if (!--(*ref_count_weights_))
    194         delete weights_;
    195159  }
    196160
     
    274238  {
    275239    if (this!=&other){
    276       if (ref_count_ && !--(*ref_count_))
    277         delete data_;
    278       if (ref_count_weights_ && !--(*ref_count_weights_))
    279         delete weights_;
    280240      DataLookup2D::operator=(other);
    281241      data_ = other.data_;
    282       ref_count_=other.ref_count_;
    283       if (ref_count_)
    284         ++(*ref_count_);
    285242      weights_ = other.weights_;
    286       ref_count_weights_ = other.ref_count_weights_;
    287       if (ref_count_weights_)
    288         ++(*ref_count_weights_);
    289243    }
    290244    return *this;
     
    292246
    293247
    294   std::ostream& operator<<(std::ostream& s, const MatrixLookupWeighted& m)
    295   {
    296     s.setf(std::ios::dec);
    297     s.precision(12);
    298     for(size_t i=0, j=0; i<m.rows(); i++)
    299       for (j=0; j<m.columns(); j++) {
    300         if (m.weight(i,j))
    301           s << m.data(i,j);
    302         if (j<m.columns()-1)
    303           s << s.fill();
    304         else if (i<m.rows()-1)
    305           s << "\n";
    306       }
    307     return s;
    308   }
    309 
    310 
    311 
    312248}}} // of namespace classifier, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.