From: Svetlana Shmidt Date: Mon, 27 Jul 2020 14:32:30 +0000 (+0500) Subject: add distribution_update() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=33ed94a7aecacfda5f17c04b4be1cec55e99b2c5;p=thirdparty%2Fcollectd.git add distribution_update() --- diff --git a/src/daemon/distribution.c b/src/daemon/distribution.c index 55faf1cdf..6183f95a7 100644 --- a/src/daemon/distribution.c +++ b/src/daemon/distribution.c @@ -3,9 +3,10 @@ // #include "distribution.h" -#include + #include #include +#include #include typedef struct bucket_s { @@ -160,11 +161,38 @@ distribution_t* distribution_clone(distribution_t *dist) { return new_distribution; } +void update_tree(distribution_t *dist, size_t node_index, size_t left, size_t right, double gauge) { + if (left > right) + return; + if (left == right) { + dist->tree[node_index].bucket_counter++; + return; + } + size_t mid = (left + right) / 2; + size_t left_child = left_child_index(node_index, left, right); + size_t right_child = right_child_index(node_index, left, right); + if (dist->tree[left_child].maximum > gauge) + update_tree(dist, left_child, left, mid, gauge); + else + update_tree(dist, right_child, mid + 1, right, gauge); + dist->tree[node_index] = merge_buckets(dist->tree[left_child], dist->tree[right_child]); +} + +void distribution_update(distribution_t *dist, double gauge) { + if (dist == NULL) + return; + update_tree(dist, 0, 0, dist->num_buckets - 1, gauge); +} + int main() { double a[] = {3.0, 2.7, 1.0}; distribution_t *p = distribution_new_custom(3, a); + distribution_update(p, 2); + distribution_update(p, 5); + distribution_update(p, 7.5); + distribution_update(p, 3.1); for (size_t i = 0; i < 7; i++) { - printf("%f %f\n", p->tree[i].minimum, p->tree[i].maximum); + printf("%f %f %llu\n", p->tree[i].minimum, p->tree[i].maximum, p->tree[i].bucket_counter); } distribution_destroy(p); } \ No newline at end of file