return build_distribution_from_bucket_array(num_buckets, bucket_array);
}
-distribution_t* distribution_new_exponential(size_t num_buckets, double initial_size, double factor) {
- if (num_buckets == 0 || initial_size <= 0 || factor <= 1) {
+distribution_t* distribution_new_exponential(size_t num_buckets, double base, double factor) {
+ if (num_buckets == 0 || base <= 1 || factor <= 0) {
errno = EINVAL;
return NULL;
}
bucket_t bucket_array[num_buckets];
- bucket_array[0] = (bucket_t) {
- .bucket_counter = 0,
- .minimum = 0,
- .maximum = initial_size,
- };
- for (size_t i = 1; i < num_buckets; i++) {
- bucket_array[i].bucket_counter = 0;
- bucket_array[i].minimum = bucket_array[i - 1].maximum;
- if (i == num_buckets - 1)
- bucket_array[i].maximum = INFINITY;
- else
- bucket_array[i].maximum = bucket_array[i].minimum * factor;
+ for (size_t i = 0; i < num_buckets; i++) {
+ bucket_array[i] = (bucket_t) {
+ .bucket_counter = 0,
+ .minimum = (i == 0) ? 0 : bucket_array[i - 1].maximum,
+ .maximum = (i == num_buckets - 1) ? INFINITY : factor * pow(base, i), //check if it's slow
+ };
}
return build_distribution_from_bucket_array(num_buckets, bucket_array);
}
int main() {
double a[] = {3.0, 5.7, 6.7};
- distribution_t *p = distribution_new_custom(3, a);
+ distribution_t *p = distribution_new_exponential(4, 2, 3);
distribution_update(p, 2);
distribution_update(p, 5);
distribution_update(p, 7.5);
printf("%f %f %llu\n", p->tree[i].minimum, p->tree[i].maximum, p->tree[i].bucket_counter);
}
printf("%f\n", distribution_average(p));
- printf("%f\n", distribution_percentile(p, 90));
+ printf("%f\n", distribution_percentile(p, 70));
distribution_destroy(p);
}
\ No newline at end of file
/**
* function creates new distribution with the exponential buckets:
- * [0; initial_size) [initial_size; initial_size * factor) ... [initial_size * factor^{num_buckets - 2}; infinity)
+ * [0; factor) [factor; factor * base) ... [factor * base^{num_buckets - 2}; infinity)
* @param num_buckets - number of buckets. Should be greater than 0
- * @param initial_size - size of the first bucket. Should be greater than 0
- * @param factor - factor for buckets' upper bound. Should be greater than 1
+ * @param base - base of geometric progression. Should be greater than 1
+ * @param factor - size of the first bucket. Should be greater than 0
* @return - pointer to a new distribution or null pointer if parameters are wrong or memory allocation fails
*/
-distribution_t* distribution_new_exponential(size_t num_buckets, double initial_size, double factor);
+distribution_t* distribution_new_exponential(size_t num_buckets, double base, double factor);
/**
* function creates new distribution with the custom buckets: