Move the code in `interval_map_decompose` which adds a new closed interval to the set into a separate function. In addition to the moving of the code, there is one other change: `compound_expr_add` is called once, after the main conditional, instead of being called in each branch. Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx> --- src/segtree.c | 71 +++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/src/segtree.c b/src/segtree.c index c36497ce6253..d15c39f31f3a 100644 --- a/src/segtree.c +++ b/src/segtree.c @@ -493,12 +493,48 @@ static struct expr *interval_to_range(struct expr *low, struct expr *i, mpz_t ra return __expr_to_set_elem(low, tmp); } +static void +add_interval(struct expr *set, struct expr *low, struct expr *i) +{ + struct expr *expr; + mpz_t range, p; + + mpz_init(range); + mpz_init(p); + + mpz_sub(range, expr_value(i)->value, expr_value(low)->value); + mpz_sub_ui(range, range, 1); + + mpz_and(p, expr_value(low)->value, range); + + if (!mpz_cmp_ui(range, 0)) { + if (expr_basetype(low)->type == TYPE_STRING) + mpz_switch_byteorder(expr_value(low)->value, + expr_value(low)->len / BITS_PER_BYTE); + low->flags |= EXPR_F_KERNEL; + expr = expr_get(low); + } else if (range_is_prefix(range) && !mpz_cmp_ui(p, 0)) { + + if (i->dtype->flags & DTYPE_F_PREFIX) + expr = interval_to_prefix(low, i, range); + else if (expr_basetype(i)->type == TYPE_STRING) + expr = interval_to_string(low, i, range); + else + expr = interval_to_range(low, i, range); + } else + expr = interval_to_range(low, i, range); + + compound_expr_add(set, expr); + + mpz_clear(range); + mpz_clear(p); +} + void interval_map_decompose(struct expr *set) { struct expr *i, *next, *low = NULL, *end, *catchall = NULL, *key; struct expr **elements, **ranges; unsigned int n, m, size; - mpz_t range, p; bool interval; if (set->size == 0) @@ -507,9 +543,6 @@ void interval_map_decompose(struct expr *set) elements = xmalloc_array(set->size, sizeof(struct expr *)); ranges = xmalloc_array(set->size * 2, sizeof(struct expr *)); - mpz_init(range); - mpz_init(p); - /* Sort elements */ n = 0; list_for_each_entry_safe(i, next, &set->expressions, list) { @@ -568,32 +601,7 @@ void interval_map_decompose(struct expr *set) } } - mpz_sub(range, expr_value(i)->value, expr_value(low)->value); - mpz_sub_ui(range, range, 1); - - mpz_and(p, expr_value(low)->value, range); - - if (!mpz_cmp_ui(range, 0)) { - if (expr_basetype(low)->type == TYPE_STRING) - mpz_switch_byteorder(expr_value(low)->value, expr_value(low)->len / BITS_PER_BYTE); - low->flags |= EXPR_F_KERNEL; - compound_expr_add(set, expr_get(low)); - } else if (range_is_prefix(range) && !mpz_cmp_ui(p, 0)) { - struct expr *expr; - - if (i->dtype->flags & DTYPE_F_PREFIX) - expr = interval_to_prefix(low, i, range); - else if (expr_basetype(i)->type == TYPE_STRING) - expr = interval_to_string(low, i, range); - else - expr = interval_to_range(low, i, range); - - compound_expr_add(set, expr); - } else { - struct expr *expr = interval_to_range(low, i, range); - - compound_expr_add(set, expr); - } + add_interval(set, low, i); if (i->flags & EXPR_F_INTERVAL_END) { expr_free(low); @@ -633,9 +641,6 @@ out: if (catchall) compound_expr_add(set, catchall); - mpz_clear(range); - mpz_clear(p); - xfree(ranges); xfree(elements); } -- 2.35.1