diff options
Diffstat (limited to 'src/mergesort.c')
-rw-r--r-- | src/mergesort.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/mergesort.c b/src/mergesort.c new file mode 100644 index 0000000..2562d9d --- /dev/null +++ b/src/mergesort.c @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1-only */ + +/* Copyright (C) 2020 Gediminas Jakutis */ + +#include <errno.h> +#include <stdlib.h> +#include "defs.h" +#include "mergesort.h" + +int merge(struct stream * const dest, struct stream * const A, struct stream * const B) +{ + int ret; + struct entry_l *a; + struct entry_l *b; + + try(A->parent != B->parent, err, EINVAL, "cannot merge blocks: uncommon parent!"); + + a = get(A); + b = get(B); + + while (a || b) { + if (a && (!b || a->val <= b->val)) { + put(dest, a); + a = get(A); + } else { + put(dest, b); + b = get(B); + } + } + +err: + return ret; +} + |