Changeset 259


Ignore:
Timestamp:
Mar 4, 2005, 2:09:55 AM (17 years ago)
Author:
Jari Häkkinen
Message:

Moved out shuffle from vecotr class.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gslapi_utility.cc

    r257 r259  
    44#include <utility>
    55
     6#include "random_singleton.h"
    67#include "vector.h"
    78
     
    2627  }
    2728
     29
     30
     31  void shuffle(vector& invec)
     32  {
     33    vector vec(invec);
     34    theplu::cpptools::random_singleton*
     35      rnd=theplu::cpptools::random_singleton::get_instance();
     36    for (size_t i=0; i<vec.size(); i++){
     37      size_t index = rnd->get_uniform_int(vec.size()-i);
     38      invec[i]=vec(index);
     39      vec(index)=vec(vec.size()-i-1);
     40    }
     41  }
     42
    2843}} // of namespace gslapi and namespace thep
  • trunk/src/gslapi_utility.h

    r257 r259  
    3434                                        const std::vector<size_t>& subset);
    3535
     36  ///
     37  /// Randomly shuffles the elements in vector \a invec
     38  ///
     39  void shuffle(vector& invec);
     40
    3641}} // of namespace gslapi and namespace theplu
    3742
  • trunk/src/vector.cc

    r257 r259  
    22
    33#include "vector.h"
    4 #include "random_singleton.h"
    54
    65#include <iostream>
     
    170169
    171170
    172   void vector::shuffle(void) const
    173   {
    174     vector vec(*this);
    175     theplu::cpptools::random_singleton*
    176       rnd=theplu::cpptools::random_singleton::get_instance();
    177     for (size_t i=0; i<vec.size(); i++){
    178       size_t index = rnd->get_uniform_int(vec.size()-i);
    179       gsl_vector_set(v_,i,vec(index));
    180       vec(index)=vec(vec.size()-i-1);
    181     }
    182   }
    183 
    184171  double vector::sum(void) const
    185172  {
  • trunk/src/vector.h

    r257 r259  
    243243
    244244    ///
    245     /// Randomly shuffles the elements in vector @return resulting
    246     /// vector
    247     ///
    248     void vector::shuffle(void) const;
    249 
    250     ///
    251245    /// @return the number of elements in the vector.
    252246    ///
  • trunk/test/test_vector.cc

    r257 r259  
    22
    33#include "vector.h"
     4#include "gslapi_utility.h"
    45
    56int main(const int argc,const char* argv[])
     
    1314  // checking that shuffle works
    1415  double sum_before = vec.sum();
    15   vec.shuffle();
     16  shuffle(vec);
    1617  double sum_after = vec.sum();
    1718  if (sum_after != sum_before)
Note: See TracChangeset for help on using the changeset viewer.