summaryrefslogtreecommitdiffstats
path: root/src/mergesort.c
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-03-10 15:04:13 +0200
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-03-10 15:33:58 +0200
commit8788b9b33ec46e3f96170fb35a70a03addbf9671 (patch)
tree44b620061ca427f436e89c7beb80022febde1e58 /src/mergesort.c
parentd7e2af2582660e3ed4ce824c33a21ffbf9ed4c6f (diff)
downloadalgos-ld1-8788b9b33ec46e3f96170fb35a70a03addbf9671.tar.gz
algos-ld1-8788b9b33ec46e3f96170fb35a70a03addbf9671.tar.bz2
algos-ld1-8788b9b33ec46e3f96170fb35a70a03addbf9671.zip
refactor && improve.
Make code more reusable and generic while preparing to add uncached ops. Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/mergesort.c')
-rw-r--r--src/mergesort.c16
1 files changed, 9 insertions, 7 deletions
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);
}
}