Changeset 257


Ignore:
Timestamp:
Mar 4, 2005, 1:53:34 AM (18 years ago)
Author:
Jari Häkkinen
Message:

Added vector views and made vector member functions to be non-member functions.

Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r225 r257  
    1010  Alignment.cc Averager.cc AveragerPair.cc ConsensusInputRanker.cc  \
    1111  CrossValidation.cc FileIO.cc Fisher.cc GaussianKernelFunction.cc  \
     12  gslapi_utility.cc \
    1213  Histogram.cc InputRanker.cc Kernel.cc kNNI.cc matrix.cc Merge.cc  \
    1314  NNI.cc PCA.cc PolynomialKernelFunction.cc random_singleton.cc     \
     
    2223  Alignment.h Averager.h AveragerPair.h ConsensusInputRanker.h        \
    2324  CrossValidation.h FileIO.h Fisher.h GaussianKernelFunction.h        \
     25  gslapi_utility.h \
    2426  Histogram.h InputRanker.h Kernel.h KernelFunction.h kNNI.h matrix.h \
    2527  Merge.h NNI.h PCA.h Pearson.h PolynomialKernelFunction.h            \
  • trunk/src/vector.cc

    r227 r257  
    3939  {
    4040    v_ = other.TEMP_gsl_vector_copy();
     41  }
     42
     43
     44
     45  vector::vector(vector& v, size_t offset, size_t n, size_t stride)
     46  {
     47    view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset,
     48                                                                 stride,n));
     49    v_ = &(view_->vector);
    4150  }
    4251
     
    125134  vector::~vector(void)
    126135  {
    127     if (v_) {
     136    if (view_)
     137      delete view_;
     138    else if (v_)
    128139      gsl_vector_free(v_);
    129       v_=NULL;
    130     }
    131     delete view_;
     140    v_=NULL;
    132141  }
    133142
     
    160169
    161170
    162 
    163 /* Jari, remove this section
    164   std::pair<size_t,size_t> vector::minmax_index(const std::vector<size_t>& subset ) const
    165   {
    166     size_t min_index = subset[0];
    167     size_t max_index = subset[0];
    168     for (unsigned int i=1; i<subset.size(); i++) {
    169       if (gsl_vector_get( v_, subset[i]) < gsl_vector_get( v_, min_index))
    170         min_index = subset[i];
    171       else if (gsl_vector_get( v_, subset[i]) > gsl_vector_get( v_, max_index))
    172         max_index = subset[i];
    173     }
    174     return std::pair<size_t,size_t>(min_index, max_index);
    175   }
    176 */
    177171
    178172  void vector::shuffle(void) const
  • trunk/src/vector.h

    r227 r257  
    6868
    6969    ///
     70    /// The vector view constructor.
     71    ///
     72    /// Create a view of vector \a v, with starting index \a offset,
     73    /// size \a n, and an optional \a stride.
     74    ///
     75    /// A vector view can be used as any vector with the difference
     76    /// that changes made to the view will also change the object that
     77    /// is viewed. Also, using the copy constructor will create a new
     78    /// vector object that is a copy of whatever is viewed. If a copy
     79    /// of the view is needed the you should use this consturctor to
     80    /// get one.
     81    ///
     82    /// @note If the object viewed by the view goes out of scope or is
     83    /// deleted, the view becomes invalid.
     84    ///
     85    vector(vector& v, size_t offset, size_t n, size_t stride=1);
     86
     87    ///
    7088    /// Constructor that imports a GSL vector. The GSL object is owned
    7189    /// by the created object.
     
    178196    // Jari, doxygen group as Finding maximum and minimum elements
    179197    std::pair<size_t,size_t> vector::minmax_index(void) const;
    180 
    181     ///This function returns the indices of the minimum and maximum
    182     ///values in the sub-vector (defined by \a subset), storing them
    183     ///in imin and imax. When there are several equal minimum or
    184     ///maximum elements then the lowest indices are returned. The
    185     ///returned index is the index from the complete vector (not the
    186     ///sub-vector)
    187     ///
    188     /// @return Index corresponding to the smallest and largest value.
    189     ///
    190     std::pair<size_t,size_t>
    191     vector::TEMP_minmax_index(const std::vector<size_t>& subset ) const;
    192198
    193199    ///
  • trunk/test/test_vector.cc

    r127 r257  
    11// $Id$
    22
    3 // C++ tools include
    4 ////////////////////
    53#include "vector.h"
    64
    7 using namespace std;
    8 
    9 int main()
    10 
     5int main(const int argc,const char* argv[])
    116
    127  bool ok = true;
    13   theplu::gslapi::vector vec(10);
     8
     9  theplu::gslapi::vector vec(12);
    1410  for (unsigned int i=0; i<vec.size(); i++)
    1511    vec(i)=i;
     12
     13  // checking that shuffle works
    1614  double sum_before = vec.sum();
    1715  vec.shuffle();
     
    1917  if (sum_after != sum_before)
    2018    ok = false;
    21  
    22   if (ok)
     19
     20  // checking that view works
     21  sum_before=0;
     22  for (unsigned int i=0; i<vec.size(); i+=2)
     23    sum_before+=vec[i];
     24  theplu::gslapi::vector vec_view(vec,0,6,2);
     25  sum_after=vec_view.sum();
     26  if (sum_after != sum_before)
     27    ok = false;
     28  vec[0]=0;
     29  vec_view[0]=24;
     30  if (vec[0]!=vec_view[0])
     31    ok=false;
     32
     33  // checking that copy contrutor creates an independent object
     34  theplu::gslapi::vector vec2(vec);
     35  if (vec.size()!=vec2.size())
     36    ok=false;
     37  if (&vec2 == &vec)
     38    ok=false;
     39  if (vec2.isview())
     40    ok=false;
     41
     42  // checking that copy contrutor creates an independent object when
     43  // copying a view
     44  vec2=vec_view;
     45  if (vec_view.size()!=vec2.size())
     46    ok=false;
     47  if ((&vec2 == &vec_view) || (&vec2 == &vec))
     48    ok=false;
     49  if (vec2.isview())
     50    ok=false;
     51  vec2[0]=0;
     52  vec_view[0]=24;
     53  if (vec2[0]==vec_view[0])
     54    ok=false;
     55
     56  if (ok) {
     57    std::cout << "test_vector: SUCCESS\n";
    2358    return 0;
     59  }
     60  std::cerr << "test_vector: FAILED\n";
    2461  return -1;
    2562}
Note: See TracChangeset for help on using the changeset viewer.