Changeset 879 for trunk


Ignore:
Timestamp:
Sep 21, 2007, 4:10:07 PM (16 years ago)
Author:
Markus Ringnér
Message:

Added support to utility::vector for gsl-functions that finds indices to the k largest/smallest elements in a vector

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/vector_test.cc

    r877 r879  
    242242  } catch (utility::IO_error& err) {
    243243    *message << err.what() << std::endl;
    244     this_ok=true; // good, exceoption thrown, test passed
     244    this_ok=true; // good, exception thrown, test passed
    245245  }
    246246  try {
     
    267267  ok &= this_ok;
    268268
    269   std::string data3("data/vector3.data");
    270   std::ifstream data_stream3(data3.c_str());
    271   utility::vector vec3(data_stream3);
    272   std::vector<size_t> dummy;
    273   utility::sort_index(dummy,vec3);
    274   for(size_t i=0;i<dummy.size();i++) {
    275     *message << "dummy: " << dummy[i] << std::endl;
     269
     270  // Test sort algorithms
     271  {
     272    std::string data3("data/vector3.data");
     273    std::ifstream data_stream3(data3.c_str());
     274    utility::vector vec3(data_stream3);
     275    std::vector<size_t> dummy;
     276    dummy.push_back(100); // To make sure it works starting with a non-empty vector
     277
     278    utility::sort_index(dummy,vec3);
     279    if(dummy.size()!=vec3.size()) {
     280      ok=false;
     281      *message << "Vector of sorted indices has incorrect size" << std::endl;
     282    }
     283    if(dummy[0]!=11 || dummy[dummy.size()-1]!=8) {
     284      ok=false;
     285      *message << "Vector of sorted indices incorrect" << std::endl;
     286    }
     287    size_t k=3;
     288
     289    utility::sort_smallest_index(dummy,k,vec3);
     290    if(dummy.size()!=k) {
     291      ok=false;
     292      *message << "Vector of sorted smallest indices has incorrect size" << std::endl;
     293    }
     294    if(dummy[0]!=11 || dummy[dummy.size()-1]!=9) {
     295      ok=false;
     296      *message << "Vector of sorted smallest indices incorrect" << std::endl;
     297    }
     298
     299    k=4;
     300    utility::sort_largest_index(dummy,k,vec3);
     301    if(dummy.size()!=k) {
     302      ok=false;
     303      *message << "Vector of sorted largest indices has incorrect size" << std::endl;
     304    }
     305    if(dummy[0]!=8 || dummy[dummy.size()-1]!=5) {
     306      ok=false;
     307      *message << "Vector of sorted largest indices incorrect" << std::endl;
     308    }
    276309  }
    277310
  • trunk/yat/utility/vector.cc

    r877 r879  
    522522      throw utility::GSL_error(std::string("sort_index(vector&,const vector&)",status));     
    523523    }
    524     for(size_t i=0;i<p->size;i++) {
    525       sort_index.resize(0);
    526       sort_index.push_back(gsl_permutation_get(p,i));
    527     }
     524    sort_index=std::vector<size_t>(p->data,p->data+p->size);
    528525    gsl_permutation_free(p);
    529526  }
    530527
     528
     529  void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,
     530                            const vector& invec)
     531  {
     532    assert(invec.gsl_vector_p());
     533    assert(k<=invec.size());
     534    sort_index.resize(k);
     535    gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p());
     536  }
     537 
     538  void sort_largest_index(std::vector<size_t>& sort_index, size_t k,
     539                            const vector& invec)
     540  {
     541    assert(invec.gsl_vector_p());
     542    assert(k<=invec.size());
     543    sort_index.resize(k);
     544    gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p());
     545  }
    531546
    532547
  • trunk/yat/utility/vector.h

    r878 r879  
    547547  void sort_index(std::vector<size_t>& sort_index, const vector& invec);
    548548
     549  /** Similar to sort_index but creates a vector with indices to the \a k
     550  smallest elements in \a invec. 
     551  */
     552  void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const
     553  vector& invec);
     554
     555  /** Similar to sort_index but creates a vector with indices to the \a k
     556  largest elements in \a invec. 
     557  */
     558  void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const
     559  vector& invec);
     560
     561 
    549562
    550563  /**
Note: See TracChangeset for help on using the changeset viewer.