summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2014-06-30 00:59:20 -0500
committerBobby Bingham <koorogi@koorogi.info>2014-06-30 08:36:08 -0500
commitbc91f97fe8186b43a2b876c65b9583ac18d9aab4 (patch)
treeba45d06765e505a576438600c1e8e44ee80b62ff
parent7c9343faae62800f6a4c39f7e127bcbde36cd584 (diff)
Move penda info into a struct array
-rw-r--r--wikisort.c49
1 files changed, 25 insertions, 24 deletions
diff --git a/wikisort.c b/wikisort.c
index 7e9c196..db74029 100644
--- a/wikisort.c
+++ b/wikisort.c
@@ -38,27 +38,27 @@ static void rotate(char *base, size_t nmel, size_t width, size_t shift)
reverse(base, nmel, width);
}
-/* buf must be large enough to hold a_count elements */
-static void merge_blocks(char *buf, char *base, size_t as, size_t bs, size_t width, cmpfun cmp)
+/* buf must be large enough to hold as->nmel elements */
+static void merge_blocks(char *buf, struct array *as, size_t bs_nmel, size_t width, cmpfun cmp)
{
char *a = buf;
- char *b = base + as * width;
- char *target = base;
+ char *b = as->base + as->nmel * width;
+ char *target = as->base;
- swap(a, base, as * width);
+ swap(a, as->base, as->nmel * width);
- while (as > 0 && bs > 0) {
+ while (as->nmel > 0 && bs_nmel > 0) {
if (cmp(a, b) <= 0) {
swap(target, a, width);
- a += width; as--;
+ a += width; as->nmel--;
} else {
swap(target, b, width);
- b += width; bs--;
+ b += width; bs_nmel--;
}
target += width;
}
- swap(target, a, as * width);
+ swap(target, a, as->nmel * width);
}
static char *find_min_block(char *base, size_t count, size_t bwidth, size_t width, cmpfun cmp)
@@ -188,8 +188,9 @@ void wikisort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
return;
}
- size_t nmel_pend_a = 0, nmel_pend_b = 0;
- char *mina = a.base, *penda;
+ struct array penda = { 0 };
+ size_t pendb_nmel = 0;
+ char *mina = a.base;
while (a.nmel) {
size_t rolled_blocks = b.nmel ? roll(&a, &b, bwidth, width, &mina, cmp) : 0;
@@ -197,22 +198,22 @@ void wikisort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
if (mina != a.base) swap(mina, a.base, bwidth);
/* rotate mina into correct spot in the last rolled b */
- nmel_pend_b += rolled_blocks * bnmel;
- if (nmel_pend_b) {
- size_t offset = nmel_pend_b - binary_search(a.base, a.base - nmel_pend_b * width, nmel_pend_b, width, cmp);
+ pendb_nmel += rolled_blocks * bnmel;
+ if (pendb_nmel) {
+ size_t offset = pendb_nmel - binary_search(a.base, a.base - pendb_nmel * width, pendb_nmel, width, cmp);
rotate(a.base - offset * width, bnmel + offset, width, offset);
- nmel_pend_b -= offset;
- if (nmel_pend_a && nmel_pend_b) {
- merge_blocks(buf.base, penda, nmel_pend_a, nmel_pend_b, width, cmp);
+ pendb_nmel -= offset;
+ if (penda.nmel && pendb_nmel) {
+ merge_blocks(buf.base, &penda, pendb_nmel, width, cmp);
}
- penda = a.base - offset * width;
- nmel_pend_a = bnmel;
- nmel_pend_b = offset;
+ penda.base = a.base - offset * width;
+ penda.nmel = bnmel;
+ pendb_nmel = offset;
} else {
- nmel_pend_a = 0;
- nmel_pend_b = 0;
+ penda.nmel = 0;
+ pendb_nmel = 0;
}
a.base += bwidth;
@@ -221,8 +222,8 @@ void wikisort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
}
}
- if (nmel_pend_a) {
- merge_blocks(buf.base, penda, nmel_pend_a, b.nmel * bnmel + nmel_pend_b, width, cmp);
+ if (penda.nmel) {
+ merge_blocks(buf.base, &penda, b.nmel * bnmel + pendb_nmel, width, cmp);
}
wikisort(buf.base, buf.nmel, width, cmp);