From 6d5e75e46de4d0e588a02bc2871a976dd5889621 Mon Sep 17 00:00:00 2001 From: Igor Putovny Date: Tue, 19 Sep 2023 17:00:16 +0200 Subject: [PATCH] Refactor and fix aggregator_bucket_unionize() --- proto/aggregator/aggregator.c | 67 +++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/proto/aggregator/aggregator.c b/proto/aggregator/aggregator.c index 8272ea0e..ac290adf 100644 --- a/proto/aggregator/aggregator.c +++ b/proto/aggregator/aggregator.c @@ -275,32 +275,55 @@ aggregator_bucket_unionize(struct trie_node *node, const struct trie_node *left, int res = aggregator_bucket_compare(left->potential_buckets[i], right->potential_buckets[j]); - if (res == 0) + switch (res) { - node->potential_buckets[node->potential_buckets_count++] = left->potential_buckets[i]; + case 0: + if (node->potential_buckets_count == 0 || node->potential_buckets[node->potential_buckets_count - 1] != left->potential_buckets[i]) + node->potential_buckets[node->potential_buckets_count++] = left->potential_buckets[i]; + + i++; + j++; + break; + + case -1: + if (node->potential_buckets_count == 0 || node->potential_buckets[node->potential_buckets_count - 1] != left->potential_buckets[i]) + node->potential_buckets[node->potential_buckets_count++] = left->potential_buckets[i]; + + i++; + break; + + case 1: + if (node->potential_buckets_count == 0 || node->potential_buckets[node->potential_buckets_count - 1] != right->potential_buckets[j]) + node->potential_buckets[node->potential_buckets_count++] = right->potential_buckets[j]; + + j++; + break; + + default: + bug("Impossible"); + } + + while (i < left->potential_buckets_count) + { + if (node->potential_buckets_count >= MAX_POTENTIAL_NEXTHOP_COUNT) + return; + + if (node->potential_buckets_count == 0 || node->potential_buckets[node->potential_buckets_count - 1] != left->potential_buckets[i]) + node->potential_buckets[node->potential_buckets_count++] = left->potential_buckets[i]; + i++; + } + + while (j < right->potential_buckets_count) + { + if (node->potential_buckets_count >= MAX_POTENTIAL_NEXTHOP_COUNT) + return; + + if (node->potential_buckets_count == 0 || node->potential_buckets[node->potential_buckets_count - 1] != right->potential_buckets[j]) + node->potential_buckets[node->potential_buckets_count++] = right->potential_buckets[j]; + j++; } - else if (res == -1) - node->potential_buckets[node->potential_buckets_count++] = left->potential_buckets[i++]; - else if (res == 1) - node->potential_buckets[node->potential_buckets_count++] = right->potential_buckets[j++]; - } - - while (i < left->potential_buckets_count) - { - if (node->potential_buckets_count >= MAX_POTENTIAL_NEXTHOP_COUNT) - return; - - node->potential_buckets[node->potential_buckets_count++] = left->potential_buckets[i++]; - } - - while (j < right->potential_buckets_count) - { - if (node->potential_buckets_count >= MAX_POTENTIAL_NEXTHOP_COUNT) - return; - - node->potential_buckets[node->potential_buckets_count++] = right->potential_buckets[j++]; } }