53template <
typename Rep,
typename Period>
79 return std::numeric_limits<T>::max();
91 return std::numeric_limits<T>::lowest();
99T
divide(
const T& value,
size_t count) {
104 return value /
static_cast<typename T::rep
>(count);
106 return value /
static_cast<T
>(count);
130T
percentile(
const std::vector<T>& sorted_values,
double percentile_value) {
131 if (sorted_values.empty()) {
135 if (percentile_value <= 0.0) {
136 return sorted_values.front();
139 if (percentile_value >= 100.0) {
140 return sorted_values.back();
143 double rank = (percentile_value / 100.0) * (sorted_values.size() - 1);
144 size_t lower_idx =
static_cast<size_t>(rank);
145 size_t upper_idx = lower_idx + 1;
147 if (upper_idx >= sorted_values.size()) {
148 return sorted_values[lower_idx];
155 return sorted_values[
static_cast<size_t>(std::round(rank))];
157 double fraction = rank -
static_cast<double>(lower_idx);
158 return sorted_values[lower_idx] +
159 static_cast<T
>(fraction * (sorted_values[upper_idx] - sorted_values[lower_idx]));
176 if (sorted_values.empty()) {
188 result.
count = sorted_values.size();
189 result.min = sorted_values.front();
190 result.max = sorted_values.back();
193 result.total = std::accumulate(sorted_values.begin(), sorted_values.end(),
200 result.median =
percentile(sorted_values, 50.0);
229 if (values.empty()) {
233 std::vector<T> sorted = values;
234 std::sort(sorted.begin(), sorted.end());
249 if (values.empty()) {
253 std::sort(values.begin(), values.end());
Internal implementation details - not part of public API.
constexpr bool is_chrono_duration_v
constexpr T zero_value()
Get zero value for a type.
constexpr T min_value()
Get minimum (lowest) value for a type.
T divide(const T &value, size_t count)
Divide value by count.
constexpr T max_value()
Get maximum value for a type.
statistics< T > compute_sorted(const std::vector< T > &sorted_values)
Compute statistics from sorted values.
statistics< T > compute_inplace(std::vector< T > &values)
Compute statistics in place (modifies input)
T percentile(const std::vector< T > &sorted_values, double percentile_value)
statistics< T > compute(const std::vector< T > &values)
Type trait to detect std::chrono::duration types.
Statistical summary for a collection of values.