Returns the requested percentile from the sorted values. Note that we need at least two values to compute percentiles! TODO(dhamon): Use a 'Percentage' abstraction.
| 130 | // |
| 131 | // TODO(dhamon): Use a 'Percentage' abstraction. |
| 132 | static T percentile(const std::vector<T>& values, double percentile) |
| 133 | { |
| 134 | CHECK_GE(values.size(), 2u); |
| 135 | |
| 136 | if (percentile <= 0.0) { |
| 137 | return values.front(); |
| 138 | } |
| 139 | |
| 140 | if (percentile >= 1.0) { |
| 141 | return values.back(); |
| 142 | } |
| 143 | |
| 144 | // Use linear interpolation. |
| 145 | const double position = percentile * (values.size() - 1); |
| 146 | const size_t index = static_cast<size_t>(floor(position)); |
| 147 | const double delta = position - index; |
| 148 | |
| 149 | CHECK_GE(index, 0u); |
| 150 | CHECK_LT(index, values.size() - 1); |
| 151 | |
| 152 | return values[index] + (values[index + 1] - values[index]) * delta; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | } // namespace process { |