Changeset 1134


Ignore:
Timestamp:
Feb 23, 2008, 11:52:43 PM (13 years ago)
Author:
Peter
Message:

using Index class instead of std::vector<size_t>

Location:
trunk
Files:
29 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/data_lookup_1d_test.cc

    r1121 r1134  
    6565  index_even.push_back(0);
    6666  index_even.push_back(4);
    67   classifier::MatrixLookup m1(gsl_m1,index_odd,index_even);
     67  classifier::MatrixLookup m1(gsl_m1,utility::Index(index_odd),
     68                              utility::Index(index_even));
    6869  *error << gsl_m1 << std::endl << '\n';
    6970  *error << m1 << std::endl;
  • trunk/test/feature_selection_test.cc

    r1121 r1134  
    8080  f2.update(dataviewweighted,targets);
    8181  *error << "\nRandomly ordered features (top 12):\n";
    82   std::vector<size_t> features=f2.features();
     82  std::vector<size_t> features=f2.features().vector();
    8383  std::copy(features.begin(),features.end(),
    8484            std::ostream_iterator<size_t>(*error," "));
     
    8787  f.update(dataviewweighted,targets);
    8888  *error << "\nSNR ordered ordered features (top 12):\n";
    89   features=f.features();
     89  features=f.features().vector();
    9090  std::copy(features.begin(),features.end(),
    9191            std::ostream_iterator<size_t>(*error," "));
     
    108108  classifier::MatrixLookupWeighted ranked=f.get(dataviewweighted);
    109109  f.update(ranked,targets);
    110   features=f.features();
     110  features=f.features().vector();
    111111  for(size_t i=0;i<features.size();i++) {
    112112    if(features[i]!=i) {
  • trunk/test/kernel_lookup_test.cc

    r1121 r1134  
    8989         << "                           const vector<size_t>&,\n"
    9090         << "                           const vector<size_t>&)...";
    91   classifier::KernelLookup k2(kernel,index_odd,index_even);
     91  classifier::KernelLookup k2(kernel,utility::Index(index_odd),
     92                              utility::Index(index_even));
    9293  if (k2.rows()!=index_odd.size() || k2.columns()!=index_even.size()) {
    9394    ok =false;
     
    112113         << "                           const vector<size_t>&)...";
    113114  std::vector<size_t> one(1,1);
    114   classifier::KernelLookup k3(k2,one,one);
     115  classifier::KernelLookup k3(k2,utility::Index(one),utility::Index(one));
    115116  if (k3.rows()!=one.size() || k3.columns()!=one.size()) {
    116117    ok =false;
     
    149150  }
    150151 
    151   KernelLookup k5(k1,index_even,index_even);
     152  KernelLookup k5(k1,utility::Index(index_even),utility::Index(index_even));
    152153  std::vector<size_t> index5;
    153154  index5.push_back(0);
    154155  index5.push_back(2);
    155   const KernelLookup* k6 = k5.training_data(index5);
     156  const KernelLookup* k6 = k5.training_data(utility::Index(index5));
    156157  for (size_t s=0; s<k6->rows(); s++)
    157158    for (size_t t=0; t<k6->rows(); t++)
  • trunk/test/kernel_test.cc

    r1133 r1134  
    156156
    157157  // checking view
    158   std::vector<size_t> index(3);
    159   index[0]=1;
    160   index[1]=2;
    161   index[2]=3;
    162   classifier::KernelLookup kv_raw(kernel);
     158  std::vector<size_t> index_vec(3);
     159  index_vec[0]=1;
     160  index_vec[1]=2;
     161  index_vec[2]=3;
     162  utility::Index index(index_vec);
     163  classifier::KernelLookup kv_raw(kernel);
    163164  classifier::KernelLookup kv(kv_raw,index,index);
    164165  if (kv.rows()!=index.size()){
     
    194195
    195196  // checking view
    196   std::vector<size_t> index(3);
    197   index[0]=1;
    198   index[1]=2;
    199   index[2]=3;
     197  std::vector<size_t> index_vec(3);
     198  index_vec[0]=1;
     199  index_vec[1]=2;
     200  index_vec[2]=3;
     201  utility::Index index(index_vec);
    200202  classifier::KernelLookup kv_raw(kernel);
    201203  classifier::KernelLookup kv(kv_raw,index, index);
  • trunk/test/matrix_lookup_test.cc

    r1121 r1134  
    7272  index_even.push_back(0);
    7373 
    74   classifier::MatrixLookup m2(gsl_m2,index_odd, index_even);
     74  classifier::MatrixLookup m2(gsl_m2,utility::Index(index_odd),
     75                              utility::Index(index_even));
    7576  if (m2.rows()!=2 || m2.columns()!=2 ||
    7677      m2(0,0)!=gsl_m2(1,2) || m2(0,1)!=gsl_m2(1,0) ||
     
    8687         << "                           const bool)...";
    8788  std::vector<size_t> one(1,1);
    88   classifier::MatrixLookup m3(gsl_m2,one,true);
     89  classifier::MatrixLookup m3(gsl_m2,utility::Index(one),true);
    8990  if (m3.rows()!=1 || m3.columns()!=gsl_m2.columns() || m3(0,0)!=gsl_m2(1,0) ||
    9091      m3(0,1)!=gsl_m2(1,1) || m3(0,2)!=gsl_m2(1,2) || m3(0,3)!=gsl_m2(1,3)) {
     
    111112         << "                           const std::vector<size_t>&,\n"
    112113         << "                           const std::vector<size_t>&)...";
    113   classifier::MatrixLookup m5(m2,one,one);
     114  classifier::MatrixLookup m5(m2,utility::Index(one),utility::Index(one));
    114115  if (m5.rows()!=1 || m5.columns()!=1 || m5(0,0)!=m2(1,1) ) {
    115116    ok =false;
     
    124125         << "                           const std::vector<size_t>&,\n"
    125126         << "                           const bool)...";
    126   classifier::MatrixLookup m6(m2,one,true);
     127  classifier::MatrixLookup m6(m2,utility::Index(one),true);
    127128  if (m6.rows()!=1 || m6.columns()!=m2.columns() || m6(0,0)!=m2(1,0) ||
    128129      m6(0,1)!=m2(1,1)) {
     
    145146
    146147  *error << "MatrixLookup::training_data(const std::vector<size_t>)...";
    147   const classifier::MatrixLookup* TrnData = m2.training_data(one);
     148  const classifier::MatrixLookup* TrnData =m2.training_data(utility::Index(one));
    148149  if (TrnData->rows() != m2.rows() || TrnData->columns()!=one.size()){
    149150    ok =false;
     
    157158         << "                              const std::vector<size_t>)...";
    158159  std::vector<size_t> val(23,2);
    159   const classifier::MatrixLookup* ValData = m2.validation_data(one, val);
     160  const classifier::MatrixLookup* ValData =
     161    m2.validation_data(utility::Index(one), utility::Index(val));
    160162  if (ValData->rows() != m2.rows() || TrnData->columns()!=val.size()){
    161163    ok =false;
  • trunk/test/subset_generator_test.cc

    r1121 r1134  
    143143    const classifier::DataLookup2D& tv_view=cv_test.training_data(k);
    144144    const classifier::Target& tv_target=cv_test.training_target(k);
    145     const std::vector<size_t>& tv_index=cv_test.training_index(k);
     145    const utility::Index& tv_index=cv_test.training_index(k);
    146146    const classifier::DataLookup2D& test_view=cv_test.validation_data(k);
    147147    const classifier::Target& test_target=cv_test.validation_target(k);
    148     const std::vector<size_t>& test_index=cv_test.validation_index(k);
     148    const utility::Index& test_index=cv_test.validation_index(k);
    149149
    150150    for (size_t i=0; i<test_index.size(); i++) {
     
    173173      const classifier::DataLookup2D& t_view=cv_training.training_data(l);
    174174      const classifier::Target& t_target=cv_training.training_target(l);
    175       const std::vector<size_t>& t_index=cv_training.training_index(l);
     175      const utility::Index& t_index=cv_training.training_index(l);
    176176      const classifier::DataLookup2D& v_view=cv_training.validation_data(l);
    177177      const classifier::Target& v_target=cv_training.validation_target(l);
    178       const std::vector<size_t>& v_index=cv_training.validation_index(l);
     178      const utility::Index& v_index=cv_training.validation_index(l);
    179179     
    180180      if (test_index.size()+tv_index.size()!=target.size()
  • trunk/test/target_test.cc

    r1000 r1134  
    2323
    2424#include "yat/classifier/Target.h"
     25#include "yat/utility/Index.h"
    2526
    2627#include <fstream>
     
    6768  sub.push_back(2);
    6869  sub.push_back(1);
    69   classifier::Target sub_target(target3,sub);
     70  classifier::Target sub_target(target3,utility::Index(sub));
    7071  if (target3(sub[0])!=sub_target(0) || target3(sub[1])!=sub_target(1)){
    7172    ok = false;
  • trunk/yat/classifier/BootstrapSampler.cc

    r1000 r1134  
    2727
    2828#include <cassert>
    29 
    30 //#include <algorithm>
    31 //#include <utility>
    32 //#include <vector>
    3329
    3430namespace theplu {
     
    7571          }
    7672      }
    77       training_index_.push_back(training_index);
    78       training_target_.push_back(Target(target,training_index));
    79       validation_index_.push_back(validation_index);
    80       validation_target_.push_back(Target(target,validation_index));
     73      training_index_.push_back(utility::Index(training_index));
     74      training_target_.push_back(Target(target,utility::Index(training_index)));
     75      validation_index_.push_back(utility::Index(validation_index));
     76      validation_target_.push_back(Target(target,
     77                                          utility::Index(validation_index)));
    8178    }
    8279
  • trunk/yat/classifier/CrossValidationSampler.cc

    r1004 r1134  
    8181        }
    8282
    83         training_index_.push_back(training_index);
    84         validation_index_.push_back(validation_index);
     83        training_index_.push_back(utility::Index(training_index));
     84        validation_index_.push_back(utility::Index(validation_index));
    8585      }
    8686    }
  • trunk/yat/classifier/DataLookup2D.cc

    r1088 r1134  
    4646
    4747  DataLookup2D::DataLookup2D(const DataLookup2D& m,
    48                              const std::vector<size_t>& row,
    49                              const std::vector<size_t>& col)
     48                             const utility::Index& row,
     49                             const utility::Index& col)
    5050    : ref_count_(NULL)
    5151  {
    52     assert(row_index_.empty());
    53     row_index_.reserve(row.size());
    54     for (size_t i=0; i<row.size(); i++) {
    55       assert(row[i]<m.row_index_.size());
    56       row_index_.push_back(m.row_index_[row[i]]);
    57     }
    58     assert(column_index_.empty());
    59     column_index_.reserve(col.size());
    60     for (size_t i=0; i<col.size(); i++) {
    61       assert(col[i]<m.column_index_.size());
    62       column_index_.push_back(m.column_index_[col[i]]);
    63     }
     52    row_index_ = utility::Index(m.row_index_, row);
     53    column_index_ = utility::Index(m.column_index_, col);
    6454  }
    6555   
     
    6757
    6858  DataLookup2D::DataLookup2D(const DataLookup2D& m,
    69                              const std::vector<size_t>& index,
     59                             const utility::Index& index,
    7060                             const bool row)
    7161    : ref_count_(NULL)
    7262  {
    7363    if (row){
    74       assert(row_index_.empty());
    75       row_index_.reserve(index.size());
    76       for (size_t i=0; i<index.size(); i++) {
    77         assert(index[i]<m.row_index_.size());
    78         row_index_.push_back(m.row_index_[index[i]]);
    79       }
     64      row_index_ = utility::Index(m.row_index_, index);
    8065      column_index_= m.column_index_;
    8166    }
    8267    else{
    83       assert(column_index_.empty());
    84       column_index_.reserve(index.size());
    85       for (size_t i=0; i<index.size(); i++) {
    86         column_index_.push_back(m.column_index_[index[i]]);
    87       }
     68      column_index_ = utility::Index(m.column_index_, index);
    8869      row_index_= m.row_index_;
    8970    }
     
    9172
    9273
    93   DataLookup2D::DataLookup2D(const std::vector<size_t>& row,
    94                              const std::vector<size_t>& col,
     74  DataLookup2D::DataLookup2D(const utility::Index& row,
     75                             const utility::Index& col,
    9576                             const bool own)
    9677    : row_index_(row),column_index_(col), ref_count_(NULL)
  • trunk/yat/classifier/DataLookup2D.h

    r1125 r1134  
    2626  02111-1307, USA.
    2727*/
     28
     29#include "yat/utility/Index.h"
    2830
    2931#include <vector>
     
    6971    /// vector as input.
    7072    ///
    71     DataLookup2D(const std::vector<size_t>& row,
    72                  const std::vector<size_t>& column,
     73    DataLookup2D(const utility::Index& row,
     74                 const utility::Index& column,
    7375                 const bool own=false);
    7476
     
    8284    /// Copy the index such that new(i,j) = old(row[i],col[j])
    8385    ///
    84     DataLookup2D(const DataLookup2D&, const std::vector<size_t>& row,
    85                  const std::vector<size_t>& col);
     86    DataLookup2D(const DataLookup2D&, const utility::Index& row,
     87                 const utility::Index& col);
    8688
    8789    ///
     
    8991    /// Else indices are copied so new(i,j)=old(i,index[j])
    9092    ///
    91     DataLookup2D(const DataLookup2D&, const std::vector<size_t>& index,
     93    DataLookup2D(const DataLookup2D&, const utility::Index& index,
    9294                 const bool row);
    9395
     
    124126    /// @return Data based on selected features.
    125127    ///
    126     virtual const DataLookup2D* selected(const std::vector< size_t > &) const=0;
     128    virtual const DataLookup2D* selected(const utility::Index&) const=0;
    127129
    128130    ///
     
    133135    ///
    134136    virtual const DataLookup2D*
    135     training_data(const std::vector<size_t>&) const=0;
     137    training_data(const utility::Index&) const=0;
    136138
    137139    ///
     
    142144    ///
    143145    virtual const DataLookup2D*
    144     validation_data(const std::vector<size_t>& train,
    145                     const std::vector<size_t>& val) const=0;
     146    validation_data(const utility::Index& train,
     147                    const utility::Index& val) const=0;
    146148
    147149    /**
     
    171173    /// @brief which rows to look into
    172174    ///
    173     std::vector<size_t> row_index_;
     175    utility::Index row_index_;
    174176
    175177    ///
    176178    /// @brief which columns to look into
    177179    ///
    178     std::vector<size_t> column_index_;
     180    utility::Index column_index_;
    179181
    180182    ///
  • trunk/yat/classifier/FeatureSelector.cc

    r1000 r1134  
    5050
    5151
    52   const std::vector<size_t> FeatureSelector::features(void) const
     52  const utility::Index FeatureSelector::features(void) const
    5353  {
    5454    return features_;
     
    5858  const MatrixLookup& FeatureSelector::get(const MatrixLookup& matrix)
    5959  {
    60     garbage_.push_back(new MatrixLookup(matrix,features_,true));
     60    garbage_.push_back(new MatrixLookup(matrix,utility::Index(features_),true));
    6161    return *garbage_.back();
    6262  }
     
    6666  FeatureSelector::get(const MatrixLookupWeighted& matrix)
    6767  {
    68     garbage_weighted_.push_back(new MatrixLookupWeighted(matrix,features_,
     68    garbage_weighted_.push_back(new MatrixLookupWeighted(matrix,
     69                                                         utility::Index(features_),
    6970                                                         true));
    7071    return *garbage_weighted_.back();
  • trunk/yat/classifier/FeatureSelector.h

    r1000 r1134  
    2525*/
    2626
     27#include "yat/utility/Index.h"
     28
    2729#include <list>
    28 #include <vector>
    2930
    3031namespace theplu {
     
    6465
    6566    ///
    66     /// @return vector of indices corresponding to selected features.
     67    /// @return indices corresponding to selected features.
    6768    ///
    68     const std::vector<size_t> features(void) const;
     69    const utility::Index features(void) const;
    6970
    7071    ///
     
    8384    /// @brief features
    8485    ///
    85     std::vector<size_t> features_;
     86    utility::Index features_;
    8687
    8788    ///
  • trunk/yat/classifier/FeatureSelectorIR.cc

    r1000 r1134  
    6464    assert(data.columns()==target.size());
    6565    InputRanker ir = InputRanker(data, target, score_);
    66     features_.resize(N_);
     66    std::vector<size_t>* features = new std::vector<size_t>(N_);
    6767    std::copy(ir.id().begin()+first_, ir.id().begin()+first_+N_,
    68               features_.begin());
     68              features->begin());
     69    features_ =
     70      utility::Index(utility::SmartPtr<const std::vector<size_t> >(features));
    6971  }
    7072
     
    7577    assert(data.columns()==target.size());
    7678    InputRanker ir = InputRanker(data, target, score_);
    77     features_.resize(N_);
     79    std::vector<size_t>* features = new std::vector<size_t>(N_);
    7880    std::copy(ir.id().begin()+first_, ir.id().begin()+first_+N_,
    79               features_.begin());
    80    
     81              features->begin());
     82    features_ =
     83      utility::Index(utility::SmartPtr<const std::vector<size_t> >(features));
    8184  }
    8285
  • trunk/yat/classifier/FeatureSelectorRandom.cc

    r1004 r1134  
    6262  void FeatureSelectorRandom::update(size_t total_N)
    6363  {
    64     features_.resize(0);
    65     features_.reserve(total_N);
     64    std::vector<size_t>* features = new std::vector<size_t>;
     65    features->reserve(total_N);
    6666    for (size_t i=0; i<total_N; ++i)
    67       features_.push_back(i);
     67      features->push_back(i);
    6868    // Peter should use random_sample here, but not included in std
    69     random::random_shuffle(features_.begin(), features_.end());
    70     features_.resize(N_);
     69    random::random_shuffle(features->begin(), features->end());
     70    features->resize(N_);
     71    features_ =
     72      utility::Index(utility::SmartPtr<const std::vector<size_t> >(features));
    7173  }
    7274
  • trunk/yat/classifier/Kernel.cc

    r1000 r1134  
    6565    : kf_(other.kf_)
    6666  {
    67     data_ = other.data_->selected(index);
     67    data_ = other.data_->selected(utility::Index(index));
    6868    ref_count_ = new u_int(1);
    6969   
    7070    if (other.data_w_){
    71       data_w_ = other.data_w_->selected(index);
     71      data_w_ = other.data_w_->selected(utility::Index(index));
    7272      ref_count_w_ = new u_int(1);
    7373    }
  • trunk/yat/classifier/KernelLookup.cc

    r1133 r1134  
    4040    : kernel_(utility::SmartPtr<const Kernel>(&kernel, own))
    4141  {
    42     column_index_.reserve(kernel.size());
    43     for(size_t i=0; i<kernel.size(); i++)
    44       column_index_.push_back(i);
     42    column_index_ = utility::Index(kernel.size());
    4543    row_index_=column_index_;
    4644  }
     
    4846
    4947  KernelLookup::KernelLookup(const Kernel& kernel,
    50                              const std::vector<size_t>& row,
    51                              const std::vector<size_t>& column,
     48                             const utility::Index& row,
     49                             const utility::Index& column,
    5250                             const bool owner)
    5351    : column_index_(column),
     
    5654  {
    5755    // Checking that each row index is less than kernel.rows()
    58     assert(row.empty() ||
    59            *(std::max_element(row.begin(),row.end()))<kernel_->size());
     56    assert(validate(row_index_));
    6057    // Checking that each column index is less than kernel.column()
    61     assert(column.empty() ||
    62            *(std::max_element(column.begin(),column.end()))<kernel_->size());
     58    assert(validate(column_index_));
    6359  }
    6460
    6561
    6662  KernelLookup::KernelLookup(const KernelLookup& other,
    67                              const std::vector<size_t>& row,
    68                              const std::vector<size_t>& column)
     63                             const utility::Index& row,
     64                             const utility::Index& column)
    6965    : kernel_(other.kernel_)
    7066  {
    71     assert(row_index_.empty());
    72     row_index_.reserve(row.size());
    73     for (size_t i=0; i<row.size(); i++) {
    74       assert(row[i]<other.row_index_.size());
    75       row_index_.push_back(other.row_index_[row[i]]);
    76     }
    77     assert(column_index_.empty());
    78     column_index_.reserve(column.size());
    79     for (size_t i=0; i<column.size(); i++) {
    80       assert(column[i]<other.column_index_.size());
    81       column_index_.push_back(other.column_index_[column[i]]);
    82     }
     67    row_index_ = utility::Index(other.row_index_, row);
     68    column_index_ = utility::Index(other.column_index_, column);
    8369  }
    8470 
     
    8874      row_index_(other.row_index_)
    8975  {
    90     // Checking that no index is out of range
    91     assert(row_index_.empty() ||
    92            *(max_element(row_index_.begin(), row_index_.end()))<
    93            kernel_->size());
    94     assert(column_index_.empty() ||
    95            *(max_element(column_index_.begin(), column_index_.end()))<
    96            kernel_->size());
     76    // Checking that each row index is less than kernel.rows()
     77    assert(validate(row_index_));
     78    // Checking that each column index is less than kernel.column()
     79    assert(validate(column_index_));
    9780  }
    9881 
    9982
    10083  KernelLookup::KernelLookup(const KernelLookup& other,
    101                              const std::vector<size_t>& index,
     84                             const utility::Index& index,
    10285                             const bool row)
    10386    : kernel_(other.kernel_)
    10487  {
    10588    if (row){
    106       assert(row_index_.empty());
    107       row_index_.reserve(index.size());
    108       for (size_t i=0; i<index.size(); i++) {
    109         assert(index[i]<other.row_index_.size());
    110         row_index_.push_back(other.row_index_[index[i]]);
    111       }
     89      row_index_ = utility::Index(other.row_index_, index);
    11290      column_index_= other.column_index_;
    11391    }
    11492    else{
    115       assert(column_index_.empty());
    116       column_index_.reserve(index.size());
    117       for (size_t i=0; i<index.size(); i++) {
    118         column_index_.push_back(other.column_index_[index[i]]);
    119       }
     93      column_index_ = utility::Index(other.column_index_, index);
    12094      row_index_= other.row_index_;
    12195    }
    12296    assert(kernel_->size());
    12397
    124     // Checking that no index is out of range
    125     assert(row_index_.empty() ||
    126            *(max_element(row_index_.begin(), row_index_.end()))<
    127            kernel_->size());
    128     assert(column_index_.empty() ||
    129            *(max_element(column_index_.begin(), column_index_.end()))<
    130            kernel_->size());
     98    // Checking that each row index is less than kernel.rows()
     99    assert(validate(row_index_));
     100    // Checking that each column index is less than kernel.column()
     101    assert(validate(column_index_));
    131102  }
    132103 
     
    209180
    210181  const KernelLookup*
    211   KernelLookup::selected(const std::vector<size_t>& inputs) const
     182  KernelLookup::selected(const utility::Index& inputs) const
    212183  {
    213184    const Kernel* kernel;
     
    267238        kernel_->make_kernel(*tmp, true);
    268239
    269       return new KernelLookup(*kernel, row_index, column_index, true);
     240      return new KernelLookup(*kernel, utility::Index(row_index),
     241                              utility::Index(column_index), true);
    270242    }
    271243
     
    305277                                                         *weight_all, true);
    306278    const Kernel* kernel = kernel_->make_kernel(*tmp, true);
    307     return new KernelLookup(*kernel, row_index_, column_index, true);
     279    return new KernelLookup(*kernel, row_index_,
     280                            utility::Index(column_index), true);
    308281  }
    309282
     
    355328    const Kernel* kernel =
    356329      kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true));
    357     return new KernelLookup(*kernel, row_index_, column_index, true);
     330    return new KernelLookup(*kernel, row_index_,
     331                            utility::Index(column_index), true);
    358332  }
    359333
    360334
    361335  const KernelLookup*
    362   KernelLookup::training_data(const std::vector<size_t>& train) const
     336  KernelLookup::training_data(const utility::Index& train) const
    363337  {
    364338    return new KernelLookup(*this,train,train);
     
    366340
    367341
     342  bool KernelLookup::validate(const utility::Index& index) const
     343  {
     344    for (size_t i=0; i<index.size(); ++i)
     345      if (index[i]>=kernel_->size())
     346        return false;
     347    return true;
     348  }
     349
     350
    368351  const KernelLookup*
    369   KernelLookup::validation_data(const std::vector<size_t>& train,
    370                                 const std::vector<size_t>& validation) const
     352  KernelLookup::validation_data(const utility::Index& train,
     353                                const utility::Index& validation) const
    371354  {
    372355    return new KernelLookup(*this,train,validation);
  • trunk/yat/classifier/KernelLookup.h

    r1133 r1134  
    2929#include "Kernel.h"
    3030#include "yat/utility/Container2DIterator.h"
     31#include "yat/utility/Index.h"
    3132#include "yat/utility/iterator_traits.h"
    3233#include "yat/utility/SmartPtr.h"
    3334#include "yat/utility/StrideIterator.h"
    34 
    35 #include <vector>
    3635
    3736namespace theplu {
     
    120119    /// column index.
    121120    ///
    122     KernelLookup(const Kernel& kernel, const std::vector<size_t>& row,
    123                  const std::vector<size_t>& column, const bool owner=false);
     121    KernelLookup(const Kernel& kernel, const utility::Index& row,
     122                 const utility::Index& column, const bool owner=false);
    124123
    125124    ///
     
    152151    /// column index.
    153152    ///
    154     KernelLookup(const KernelLookup& kl, const std::vector<size_t>& row,
    155                  const std::vector<size_t>& column);
     153    KernelLookup(const KernelLookup& kl, const utility::Index& row,
     154                 const utility::Index& column);
    156155
    157156    ///
     
    167166    /// undefined.
    168167    ///
    169     KernelLookup(const KernelLookup& kernel, const std::vector<size_t>&,
     168    KernelLookup(const KernelLookup& kernel, const utility::Index&,
    170169                 const bool row=false);
    171170
     
    264263       to be deleted by the caller to avoid memory leaks.
    265264    */
    266     const KernelLookup* selected(const std::vector<size_t>& index) const;
     265    const KernelLookup* selected(const utility::Index& index) const;
    267266   
    268267    /**
     
    306305       to be deleted by the caller to avoid memory leaks.
    307306    */
    308     const KernelLookup* training_data(const std::vector<size_t>& train) const;
     307    const KernelLookup* training_data(const utility::Index& train) const;
    309308
    310309    /**
     
    320319    */
    321320    const KernelLookup*
    322     validation_data(const std::vector<size_t>& train,
    323                     const std::vector<size_t>& validation) const;
     321    validation_data(const utility::Index& train,
     322                    const utility::Index& validation) const;
    324323
    325324    /**
     
    336335  private:
    337336    const KernelLookup& operator=(const KernelLookup&);
    338 
    339     std::vector<size_t> column_index_;
     337    bool validate(const utility::Index&) const;
     338
     339    utility::Index column_index_;
    340340    utility::SmartPtr<const Kernel> kernel_;
    341     std::vector<size_t> row_index_;
     341    utility::Index row_index_;
    342342   
    343343  }; // class KernelLookup
  • trunk/yat/classifier/MatrixLookup.cc

    r1121 r1134  
    3838    : DataLookup2D(own), data_(&data)
    3939  {
    40     row_index_.reserve(data_->rows());
    41     for(size_t i=0;i<(*data_).rows();i++)
    42       row_index_.push_back(i);
    43     column_index_.reserve(data_->columns());
    44     for(size_t i=0;i<(*data_).columns();i++)
    45       column_index_.push_back(i);
     40    column_index_ = utility::Index(data.columns());
     41    row_index_ = utility::Index(data.rows());
    4642  }
    4743 
     
    4945
    5046  MatrixLookup::MatrixLookup(const utility::Matrix& data,
    51                              const std::vector<size_t>& row,
    52                              const std::vector<size_t>& col)
     47                             const utility::Index& row,
     48                             const utility::Index& col)
    5349    : DataLookup2D(row,col), data_(&data)
    5450  {
    55     // Checking that each row index is less than data.rows()
    56     assert(row.empty() ||
    57            *(std::max_element(row.begin(),row.end()))<data.rows());
    58     // Checking that each column index is less than data.column()
    59     assert(col.empty() ||
    60            *(std::max_element(col.begin(),col.end()))<data.columns());
    6151  }
    6252 
     
    6454
    6555  MatrixLookup::MatrixLookup(const utility::Matrix& data,
    66                              const std::vector<size_t>& index,
     56                             const utility::Index& index,
    6757                             const bool row)
    6858    : DataLookup2D(), data_(&data)
    6959  {
    7060    if (row){
    71       // Checking that each row index is less than data.rows()
    72       assert(index.empty() ||
    73              *(std::max_element(index.begin(),index.end()))<data.rows());
    7461      row_index_=index;
    75       assert(column_index_.empty());
    76       column_index_.reserve(data.columns());
    77       for (size_t i=0; i<data.columns(); i++)
    78         column_index_.push_back(i);
     62      column_index_ = utility::Index(data.columns());
    7963    }
    8064    else{
    81       // Checking that each column index is less than data.column()
    82       assert(index.empty() ||
    83              *(std::max_element(index.begin(),index.end()))<data.columns());
    8465      column_index_=index;
    85       assert(row_index_.empty());
    86       column_index_.reserve(data.rows());
    87       for (size_t i=0; i<data.rows(); i++)
    88         row_index_.push_back(i);
     66      row_index_ = utility::Index(data.rows());
    8967    }
    9068  }
     
    10381
    10482  MatrixLookup::MatrixLookup(const MatrixLookup& other,
    105                              const std::vector<size_t>& row,
    106                              const std::vector<size_t>& col)
     83                             const utility::Index& row,
     84                             const utility::Index& col)
    10785    : DataLookup2D(other,row,col), data_(other.data_)
    10886  {
     
    11593
    11694  MatrixLookup::MatrixLookup(const MatrixLookup& other,
    117                              const std::vector<size_t>& index, bool row)
     95                             const utility::Index& index, bool row)
    11896    : DataLookup2D(other,index,row), data_(other.data_)
    11997  {
     
    122100      ++(*ref_count_);
    123101
    124     // Checking that no index is out of range
    125     assert(row_index_.empty() ||
    126            *(max_element(row_index_.begin(), row_index_.end()))<data_->rows());
    127     assert(column_index_.empty() ||
    128            *(max_element(column_index_.begin(), column_index_.end()))<
    129            data_->columns());
    130102  }
    131103 
     
    146118    data_ = new utility::Matrix(is,sep);
    147119    ref_count_= new u_int(1);
    148     for(size_t i=0;i<(*data_).rows();i++)
    149       row_index_.push_back(i);
    150     for(size_t i=0;i<(*data_).columns();i++)
    151       column_index_.push_back(i);
     120    row_index_ = utility::Index(data_->rows());
     121    column_index_ = utility::Index(data_->columns());
    152122  }
    153123
     
    203173
    204174  const MatrixLookup*
    205   MatrixLookup::selected(const std::vector<size_t>& i) const
     175  MatrixLookup::selected(const utility::Index& i) const
    206176  {
    207177    return new MatrixLookup(*this,i, true);
     
    211181
    212182  const MatrixLookup*
    213   MatrixLookup::training_data(const std::vector<size_t>& i) const
     183  MatrixLookup::training_data(const utility::Index& i) const
    214184  {
    215185    return new MatrixLookup(*this,i, false);
     
    219189
    220190  const MatrixLookup*
    221   MatrixLookup::validation_data(const std::vector<size_t>& train,
    222                                 const std::vector<size_t>& val) const
     191  MatrixLookup::validation_data(const utility::Index& train,
     192                                const utility::Index& val) const
    223193  {
    224194    return new MatrixLookup(*this,val, false);
  • trunk/yat/classifier/MatrixLookup.h

    r1125 r1134  
    2929#include "DataLookup2D.h"
    3030#include "yat/utility/Container2DIterator.h"
     31#include "yat/utility/Index.h"
    3132#include "yat/utility/iterator_traits.h"
    3233#include "yat/utility/StrideIterator.h"
     
    118119    /// undefined.
    119120    ///
    120     MatrixLookup(const utility::Matrix& matrix, const std::vector<size_t>& row,
    121                  const std::vector<size_t>& column);
     121    MatrixLookup(const utility::Matrix& matrix, const utility::Index& row,
     122                 const utility::Index& column);
    122123
    123124    ///
     
    139140    ///
    140141    MatrixLookup(const utility::Matrix& matrix,
    141                  const std::vector<size_t>& index,
     142                 const utility::Index& index,
    142143                 const bool row_vectors);
    143144
     
    178179    /// undefined.
    179180    ///
    180     MatrixLookup(const MatrixLookup& ml, const std::vector<size_t>& row,
    181                  const std::vector<size_t>& column);
     181    MatrixLookup(const MatrixLookup& ml, const utility::Index& row,
     182                 const utility::Index& column);
    182183
    183184    ///
     
    203204    /// undefined.
    204205    ///
    205     MatrixLookup(const MatrixLookup& ml, const std::vector<size_t>&,
     206    MatrixLookup(const MatrixLookup& ml, const utility::Index&,
    206207                 const bool row_vectors);
    207208
     
    279280   
    280281    */
    281     const MatrixLookup* selected(const std::vector<size_t>&) const;
     282    const MatrixLookup* selected(const utility::Index&) const;
    282283
    283284    ///
     
    296297    /// to be deleted by the caller to avoid memory leaks.
    297298    ///
    298     const MatrixLookup* training_data(const std::vector<size_t>& index) const;
     299    const MatrixLookup* training_data(const utility::Index& index) const;
    299300   
    300301    ///
     
    310311    /// undefined.
    311312    ///
    312     const MatrixLookup* validation_data(const std::vector<size_t>&,
    313                                         const std::vector<size_t>&) const;
     313    const MatrixLookup* validation_data(const utility::Index&,
     314                                        const utility::Index&) const;
    314315    ///
    315316    /// @return false
  • trunk/yat/classifier/MatrixLookupWeighted.cc

    r1121 r1134  
    4343    assert(data.rows()==weights.rows());
    4444    assert(data.columns()==weights.columns());
    45     for(size_t i=0;i<(*data_).rows();i++)
    46       row_index_.push_back(i);
    47     for(size_t i=0;i<(*data_).columns();i++)
    48       column_index_.push_back(i);
     45    row_index_ = utility::Index(data.rows());
     46    column_index_ = utility::Index(data.columns());
    4947  }
    5048
     
    5755    weights_= new utility::Matrix(weights);
    5856    ref_count_weights_=new u_int(1);
    59     for(size_t i=0;i<(*data_).rows();i++)
    60       row_index_.push_back(i);
    61     for(size_t i=0;i<(*data_).columns();i++)
    62       column_index_.push_back(i);
     57    row_index_ = utility::Index(data.rows());
     58    column_index_ = utility::Index(data.columns());
    6359  }
    6460
     
    7874  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    7975                                             const utility::Matrix& weights,
    80                                              const std::vector<size_t>& row,
    81                                              const std::vector<size_t>& col)
     76                                             const utility::Index& row,
     77                                             const utility::Index& col)
    8278    : DataLookup2D(row,col), data_(&data), weights_(&weights),
    8379      ref_count_weights_(NULL)
    8480  {
    85     // Checking that each row index is less than data.rows()
    86     assert(row.empty() ||
    87            *(std::max_element(row.begin(),row.end()))<data.rows());
    88     // Checking that each column index is less than data.column()
    89     assert(col.empty() ||
    90            *(std::max_element(col.begin(),col.end()))<data.columns());
    91     // Checking that each row index is less than weights.rows()
    92     assert(row.empty() ||
    93            *(std::max_element(row.begin(),row.end()))<weights.rows());
    94     // Checking that each column index is less than weights.column()
    95     assert(col.empty() ||
    96            *(std::max_element(col.begin(),col.end()))<weights.columns());
    9781  }
    9882 
     
    10185  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    10286                                             const utility::Matrix& weights,
    103                                              const std::vector<size_t>& index,
     87                                             const utility::Index& index,
    10488                                             const bool row)
    10589    : DataLookup2D(), data_(&data), weights_(&weights),
    10690      ref_count_weights_(NULL)
    10791  {
     92    assert(data.rows()==weights.rows());
     93    assert(data.columns()==weights.columns());
    10894    if (row){
    109       // Checking that each row index is less than data.rows()
    110       assert(index.empty() ||
    111              *(std::max_element(index.begin(),index.end()))<data.rows());
    112       // Checking that each row index is less than weights.rows()
    113       assert(index.empty() ||
    114              *(std::max_element(index.begin(),index.end()))<weights.rows());
    11595      row_index_=index;
    116       assert(column_index_.empty());
    117       column_index_.reserve(data.columns());
    118       for (size_t i=0; i<data.columns(); i++)
    119         column_index_.push_back(i);
     96      column_index_ = utility::Index(data.columns());
    12097    }
    12198    else{
    122       // Checking that each column index is less than data.column()
    123       assert(index.empty() ||
    124              *(std::max_element(index.begin(),index.end()))<data.columns());
    125       // Checking that each column index is less than weights.column()
    126       assert(index.empty() ||
    127              *(std::max_element(index.begin(),index.end()))<weights.columns());
    12899      column_index_=index;
    129       assert(row_index_.empty());
    130       column_index_.reserve(data.rows());
    131       for (size_t i=0; i<data.rows(); i++)
    132         row_index_.push_back(i);
     100      row_index_ = utility::Index(data.rows());
    133101    }
    134102  }
     
    159127
    160128  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other,
    161                                              const std::vector<size_t>& row,
    162                                              const std::vector<size_t>& col)
     129                                             const utility::Index& row,
     130                                             const utility::Index& col)
    163131    : DataLookup2D(other,row,col), data_(other.data_), weights_(other.weights_)
    164132  {
     
    174142
    175143  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other,
    176                                              const std::vector<size_t>& index,
     144                                             const utility::Index& index,
    177145                                             bool row)
    178146    : DataLookup2D(other,index,row), data_(other.data_),
     
    186154      ++(*ref_count_weights_);
    187155
    188     // Checking that no index is out of range
    189     assert(row_index_.empty() ||
    190            *(max_element(row_index_.begin(), row_index_.end()))<data_->rows());
    191     assert(column_index_.empty() ||
    192            *(max_element(column_index_.begin(), column_index_.end()))<
    193            data_->columns());
    194     // Checking that no index is out of range
    195     assert(row_index_.empty() ||
    196            *(max_element(row_index_.begin(), row_index_.end()))<
    197            weights_->rows());
    198     assert(column_index_.empty() ||
    199            *(max_element(column_index_.begin(), column_index_.end()))<
    200            weights_->columns());
    201156  }
    202157 
     
    221176    data_ = new utility::Matrix(is,sep);
    222177    ref_count_=new u_int(1);
    223     for(size_t i=0;i<(*data_).rows();i++)
    224       row_index_.push_back(i);
    225     for(size_t i=0;i<(*data_).columns();i++)
    226       column_index_.push_back(i);
     178    row_index_ = utility::Index(data_->rows());
     179    column_index_ = utility::Index(data_->columns());
    227180    utility::Matrix weights;
    228181    utility::nan(*data_,weights);
     
    296249
    297250  const MatrixLookupWeighted*
    298   MatrixLookupWeighted::selected(const std::vector<size_t>& i) const
     251  MatrixLookupWeighted::selected(const utility::Index& i) const
    299252  {
    300253    return new MatrixLookupWeighted(*this,i, true);
     
    304257
    305258  const MatrixLookupWeighted*
    306   MatrixLookupWeighted::training_data(const std::vector<size_t>& i) const
     259  MatrixLookupWeighted::training_data(const utility::Index& i) const
    307260  {
    308261    return new MatrixLookupWeighted(*this,i, false);
     
    312265
    313266  const MatrixLookupWeighted*
    314   MatrixLookupWeighted::validation_data(const std::vector<size_t>& train,
    315                                         const std::vector<size_t>& val) const
     267  MatrixLookupWeighted::validation_data(const utility::Index& train,
     268                                        const utility::Index& val) const
    316269  {
    317270    return new MatrixLookupWeighted(*this,val, false);
  • trunk/yat/classifier/MatrixLookupWeighted.h

    r1125 r1134  
    149149    MatrixLookupWeighted(const utility::Matrix& matrix,
    150150                         const utility::Matrix& weights,
    151                          const std::vector<size_t>& row,
    152                          const std::vector<size_t>& column);
     151                         const utility::Index& row,
     152                         const utility::Index& column);
    153153
    154154    ///
     
    172172    MatrixLookupWeighted(const utility::Matrix& matrix,
    173173                         const utility::Matrix& weights,
    174                          const std::vector<size_t>& index,
     174                         const utility::Index& index,
    175175                         const bool row_vectors);
    176176
     
    207207    ///
    208208    MatrixLookupWeighted(const MatrixLookupWeighted& ml,
    209                          const std::vector<size_t>& row,
    210                          const std::vector<size_t>& column);
     209                         const utility::Index& row,
     210                         const utility::Index& column);
    211211
    212212    ///
     
    233233    ///
    234234    MatrixLookupWeighted(const MatrixLookupWeighted& ml,
    235                          const std::vector<size_t>&,
     235                         const utility::Index&,
    236236                         const bool row_vectors);
    237237
     
    315315   
    316316    */
    317     const MatrixLookupWeighted* selected(const std::vector<size_t>& i) const;
     317    const MatrixLookupWeighted* selected(const utility::Index& i) const;
    318318
    319319    ///
     
    330330    ///
    331331    const MatrixLookupWeighted*
    332     training_data(const std::vector<size_t>& index) const;
     332    training_data(const utility::Index& index) const;
    333333   
    334334    ///
     
    344344    /// undefined.
    345345    ///
    346     const MatrixLookupWeighted* validation_data(const std::vector<size_t>&,
    347                                         const std::vector<size_t>&) const;
     346    const MatrixLookupWeighted* validation_data(const utility::Index&,
     347                                        const utility::Index&) const;
    348348
    349349    ///
  • trunk/yat/classifier/Sampler.cc

    r1000 r1134  
    5353  }
    5454
    55   const std::vector<size_t>&
     55  const utility::Index&
    5656  Sampler::training_index(std::vector<size_t>::size_type i) const
    5757  {
     
    6565  }
    6666
    67   const std::vector<size_t>&
     67  const utility::Index&
    6868  Sampler::validation_index(std::vector<size_t>::size_type i) const
    6969  {
  • trunk/yat/classifier/Sampler.h

    r1000 r1134  
    2727
    2828#include "Target.h"
     29#include "yat/utility/Index.h"
    2930
    3031#include <vector>
     
    6970    /// @return training indices
    7071    ///
    71     const std::vector<size_t>&
     72    const utility::Index&
    7273    training_index(std::vector<size_t>::size_type i) const;
    7374
     
    8586    /// @note if state is invalid the result is undefined
    8687    ///
    87     const std::vector<size_t>&
     88    const utility::Index&
    8889    validation_index(std::vector<size_t>::size_type i) const;
    8990
     
    99100    Target target_;
    100101    /// index of training sets for the partitions
    101     std::vector<std::vector<size_t> > training_index_;
     102    std::vector<utility::Index> training_index_;
    102103    /// Targets for training sets for the partitions
    103104    std::vector<Target> training_target_;
    104105    /// index of validation sets for the partitions
    105     std::vector<std::vector<size_t> > validation_index_;
     106    std::vector<utility::Index> validation_index_;
    106107    /// Targets for validation sets for the partitions
    107108    std::vector<Target> validation_target_;
  • trunk/yat/classifier/SubsetGenerator.h

    r1125 r1134  
    3333#include "Target.h"
    3434#include "Sampler.h"
     35#include "yat/utility/Index.h"
    3536#include "yat/utility/yat_assert.h"
    3637
     
    103104    /// @return training features
    104105    ///
    105     const std::vector<size_t>&
     106    const utility::Index&
    106107    training_features(std::vector<size_t>::size_type i) const;
    107108
     
    109110    /// @return training index
    110111    ///
    111     const std::vector<size_t>&
     112    const utility::Index&
    112113    training_index(std::vector<size_t>::size_type i) const;
    113114
     
    125126    /// @return validation index
    126127    ///
    127     const std::vector<size_t>&
     128    const utility::Index&
    128129    validation_index(std::vector<size_t>::size_type i) const;
    129130
     
    147148
    148149    FeatureSelector* f_selector_;
    149     std::vector<std::vector<size_t> > features_;
     150    std::vector<utility::Index > features_;
    150151    const Sampler& sampler_;
    151152    std::vector<const T*> training_data_;
     
    185186    // No feature selection, hence features same for all partitions
    186187    // and can be stored in features_[0]
    187     features_.resize(1);
    188     features_[0].reserve(data.rows());
    189     for (size_t i=0; i<data.rows(); ++i)
    190       features_[0].push_back(i);
     188    features_.push_back(utility::Index(data.rows()));
    191189
    192190    utility::yat_assert<std::runtime_error>(training_data_.size()==size());
     
    298296                            training_target(k));
    299297      }
    300       std::vector<size_t> dummie=f_selector_->features();
     298      utility::Index dummie=f_selector_->features();
    301299      features_.push_back(dummie);
    302300      //features_.push_back(f_selector_->features());
     
    338336
    339337  template<typename T>
    340   const std::vector<size_t>&
    341   SubsetGenerator<T>::training_features(typename std::vector<size_t>::size_type i) const
     338  const utility::Index&
     339  SubsetGenerator<T>::training_features(size_t i) const
    342340  {
    343341    return f_selector_ ? features_[i] : features_[0];
     
    346344
    347345  template<typename T>
    348   const std::vector<size_t>&
    349   SubsetGenerator<T>::training_index(std::vector<size_t>::size_type i) const
     346  const utility::Index&
     347  SubsetGenerator<T>::training_index(size_t i) const
    350348  {
    351349    return sampler_.training_index(i);
     
    370368
    371369  template<typename T>
    372   const std::vector<size_t>&
     370  const utility::Index&
    373371  SubsetGenerator<T>::validation_index(std::vector<size_t>::size_type i) const
    374372  {
  • trunk/yat/classifier/Target.cc

    r1100 r1134  
    2626#include "Target.h"
    2727#include "yat/random/random.h"
     28#include "yat/utility/Index.h"
    2829#include "yat/utility/stl_utility.h"
    2930#include "yat/utility/utility.h"
     
    102103
    103104  Target::Target(const Target& t,
    104                  const std::vector<size_t>& index)
     105                 const utility::Index& index)
    105106    : class_map_(t.class_map_)
    106107  {
  • trunk/yat/classifier/Target.h

    r1100 r1134  
    3737namespace theplu {
    3838namespace yat {
     39  namespace utility {
     40    class Index;
     41  }
    3942namespace classifier { 
    4043
     
    7477    /// for the Target as the original Target.
    7578    ///
    76     Target(const Target& org, const std::vector<size_t>& vec);
     79    Target(const Target& org, const utility::Index& vec);
    7780
    7881    ///
  • trunk/yat/utility/Index.cc

    r1127 r1134  
    4747
    4848
     49  Index::Index(const std::vector<size_t>& vec)
     50    : index_(utility::SmartPtr<const std::vector<size_t> >
     51             (new std::vector<size_t>(vec)))
     52  {}
     53
     54
    4955  Index::Index(const Index& a, const Index& b)
    5056  {
     
    6369
    6470
     71  const std::vector<size_t>& Index::vector(void) const
     72  {
     73    return *index_;
     74  }
     75
     76
    6577  const size_t& Index::operator[](size_t i) const
    6678  {
  • trunk/yat/utility/Index.h

    r1127 r1134  
    6363
    6464    /**
     65       \brief Constructor
     66
     67       vec is copied
     68     */
     69    explicit Index(const std::vector<size_t>& vec);
     70
     71    /**
    6572       \brief access operator
    6673    */
     
    7178    */
    7279    size_t size(void) const;
     80
     81    /**
     82       \return underlying vector
     83    */
     84    const std::vector<size_t>& vector(void) const;
    7385
    7486  private:
Note: See TracChangeset for help on using the changeset viewer.