Changeset 1323 for trunk/yat/statistics/Percentiler.h
 Timestamp:
 May 24, 2008, 2:25:07 AM (13 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/statistics/Percentiler.h
r1318 r1323 64 64 \return percentile of range 65 65 */ 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 /* 68 70 { 69 71 return calculate(first, last, sorted_, 70 typename utility::weighted_iterator_traits< ForwardIterator>::type());72 typename utility::weighted_iterator_traits<RandomAccessIterator>::type()); 71 73 } 74 */ 72 75 private: 73 76 double perc_; 74 77 bool sorted_; 75 78 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; 80 83 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; 85 88 86 89 // using compiler generated copy … … 94 97 95 98 // 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 100 102 { 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_){ 102 112 if (perc_>=100) 103 113 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 (1j+floor(j))*first[i] + (jfloor(j))*first[i+1]; 114 return calculate(first, last, weighted_tag()); 110 115 } 111 116 112 std::vector<double> v_copy; 117 std::vector<typename std::iterator_traits<RandomAccessIterator>::value_type> 118 v_copy; 113 119 v_copy.reserve(std::distance(first,last)); 114 120 std::copy(first, last, std::back_inserter(v_copy)); … … 119 125 else 120 126 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()); 122 128 } 123 129 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 (1j+floor(j))*first[i] + (jfloor(j))*first[i+1]; 139 } 140 141 124 142 // 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, 128 146 utility::weighted_iterator_tag tag) const 129 147 {
Note: See TracChangeset
for help on using the changeset viewer.