From 8788b9b33ec46e3f96170fb35a70a03addbf9671 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Wed, 10 Mar 2021 15:04:13 +0200 Subject: refactor && improve. Make code more reusable and generic while preparing to add uncached ops. Signed-off-by: Gediminas Jakutis --- src/mergesort.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/mergesort.c') diff --git a/src/mergesort.c b/src/mergesort.c index 053e702..aa05f8e 100644 --- a/src/mergesort.c +++ b/src/mergesort.c @@ -23,11 +23,11 @@ int merge_sort(struct stream * const src, struct stream * const dest) try_s((ret = stream_shallow_copy(src, dest)), err); if (src->n > 1) { - src->split(src, tmp, tmp + 2); /* split stream in half */ + split(src, tmp, tmp + 2); /* split stream in half */ merge_sort(tmp, tmp + 1); /* recurse-sort first half */ merge_sort(tmp + 2, tmp + 3); /* recurse-sort second half */ merge(dest, tmp + 1, tmp + 3); /* merge the two halves back */ - dest->rewind(dest); + stream_rewind(dest); /* you can't spell -funroll-loops without fun, wee~! */ stream_close(tmp); @@ -49,17 +49,19 @@ static int merge(struct stream * const dest, struct stream * const A, struct str int ret = 0; struct entry_l *a; struct entry_l *b; + struct entry_l a_store; + struct entry_l b_store; - a = A->get(A); - b = B->get(B); + a = A->get(A, &a_store); + b = B->get(B, &b_store); while (a || b) { - if (a && (!b || is_less_equal(a->val, b->val))) { + if (a && (!b || is_less_equal(a_store.val, b_store.val))) { dest->put(dest, a); - a = A->get(A); + a = A->get(A, &a_store); } else { dest->put(dest, b); - b = B->get(B); + b = B->get(B, &b_store); } } -- cgit v1.2.3