Changeset 1323


Ignore:
Timestamp:
May 24, 2008, 2:25:07 AM (13 years ago)
Author:
Peter
Message:

change template name to RandomAccessIterator? to clarify that percentile only works with random access iterators (sice std::sort is used)

Location:
trunk/yat/statistics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/statistics/Percentiler.cc

    r1317 r1323  
    2424#include "Percentiler.h"
    2525
     26#include <cassert>
     27
    2628namespace theplu {
    2729namespace yat {
     
    3032  Percentiler::Percentiler(double perc, bool sorted)
    3133    : perc_(perc), sorted_(sorted)
    32   {}
     34  {
     35    assert(perc_<=100.0);
     36    assert(perc_>=0.0);
     37  }
    3338
    3439}}} // of namespace statistics, yat, and theplu
  • trunk/yat/statistics/Percentiler.h

    r1318 r1323  
    6464       \return percentile of range
    6565     */
    66     template<typename ForwardIterator>
    67     inline double operator()(ForwardIterator first, ForwardIterator last) const
     66    template<typename RandomAccessIterator>
     67    double operator()(RandomAccessIterator first,
     68                      RandomAccessIterator last) const;
     69    /*
    6870    {
    6971      return calculate(first, last, sorted_,
    70             typename utility::weighted_iterator_traits<ForwardIterator>::type());
     72            typename utility::weighted_iterator_traits<RandomAccessIterator>::type());
    7173    }
     74    */
    7275  private:
    7376    double perc_;
    7477    bool sorted_;
    7578
    76     // unweighted version
    77     template<typename ForwardIterator>
    78     double calculate(ForwardIterator first, ForwardIterator last,
    79                      bool sorted, utility::unweighted_iterator_tag tag) const;
     79    // unweighted version - NOTE range must be sorted
     80    template<typename RandomAccessIterator>
     81    double calculate(RandomAccessIterator first, RandomAccessIterator last,
     82                     utility::unweighted_iterator_tag tag) const;
    8083
    81     // weighted version
    82     template<typename ForwardIterator>
    83     double calculate(ForwardIterator first, ForwardIterator last,
    84                      bool sorted, utility::weighted_iterator_tag tag) const;
     84    // weighted version - NOTE range must be sorted
     85    template<typename RandomAccessIterator>
     86    double calculate(RandomAccessIterator first, RandomAccessIterator last,
     87                     utility::weighted_iterator_tag tag) const;
    8588
    8689    // using compiler generated copy
     
    9497
    9598  // unweighted version
    96   template<typename ForwardIterator>
    97   double Percentiler::calculate(ForwardIterator first, ForwardIterator last,
    98                                 bool sorted,
    99                                 utility::unweighted_iterator_tag tag) const
     99  template<typename RandomAccessIterator>
     100  double Percentiler::operator()(RandomAccessIterator first,
     101                                 RandomAccessIterator last) const
    100102  {
    101     if (sorted){
     103    // range is one value only is a special case
     104    if (first+1 == last)
     105      return *first;
     106    // just for convenience
     107    typedef
     108      typename utility::weighted_iterator_traits<RandomAccessIterator>::type
     109      weighted_tag;
     110
     111    if (sorted_){
    102112      if (perc_>=100)
    103113        return *(--last);
    104       // range is one value only is a special case
    105       if (first+1 == last)
    106         return *first;
    107       double j = perc_/100 * (std::distance(first,last)-1);
    108       int i = static_cast<int>(j);
    109       return (1-j+floor(j))*first[i] + (j-floor(j))*first[i+1];
     114      return calculate(first, last, weighted_tag());
    110115    }
    111116
    112     std::vector<double> v_copy;
     117    std::vector<typename std::iterator_traits<RandomAccessIterator>::value_type>
     118      v_copy;
    113119    v_copy.reserve(std::distance(first,last));
    114120    std::copy(first, last, std::back_inserter(v_copy));
     
    119125    else
    120126      std::sort(v_copy.begin(), v_copy.end());
    121     return calculate(v_copy.begin(), v_copy.end(), true, tag);
     127    return calculate(v_copy.begin(), v_copy.end(), weighted_tag());
    122128  }
    123129 
     130  // unweighted version
     131  template<typename RandomAccessIterator>
     132  double Percentiler::calculate(RandomAccessIterator first,
     133                                RandomAccessIterator last,
     134                                utility::unweighted_iterator_tag tag) const
     135  {
     136    double j = perc_/100 * (std::distance(first,last)-1);
     137    int i = static_cast<int>(j);
     138    return (1-j+floor(j))*first[i] + (j-floor(j))*first[i+1];
     139  }
     140
     141
    124142  // weighted version
    125   template<typename ForwardIterator>
    126   double Percentiler::calculate(ForwardIterator first, ForwardIterator last,
    127                                 bool sorted,
     143  template<typename RandomAccessIterator>
     144  double Percentiler::calculate(RandomAccessIterator first,
     145                                RandomAccessIterator last,
    128146                                utility::weighted_iterator_tag tag) const
    129147  {
Note: See TracChangeset for help on using the changeset viewer.