summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2014-06-29 23:53:54 -0500
committerBobby Bingham <koorogi@koorogi.info>2014-06-29 23:53:54 -0500
commit342e801b02f7d8c0ac70b18f74d2686e61f0a6ce (patch)
tree31fdfa0cfae9966894f225bfe40f12102ece50b4
parent426e724002780cf36e882e664c7e340c63a2942e (diff)
Rename stract blockarray to more generic "array"
-rw-r--r--wikisort.c30
1 files changed, 15 insertions, 15 deletions
diff --git a/wikisort.c b/wikisort.c
index 4d69582..4b01e9b 100644
--- a/wikisort.c
+++ b/wikisort.c
@@ -5,9 +5,9 @@
#include "sorters.h"
-struct blockarray {
+struct array {
char *base;
- size_t blocks;
+ size_t nmel;
};
static void swap(char *a, char *b, size_t width)
@@ -92,16 +92,16 @@ static size_t binary_search(const char *needle, char *haystack,
return baseidx;
}
-static size_t roll(struct blockarray *a, struct blockarray *b,
+static size_t roll(struct array *a, struct array *b,
size_t bwidth, size_t width, char **mina, cmpfun cmp)
{
size_t iters = 0;
- while (b->blocks && cmp(b->base , *mina + bwidth - width) < 0) {
+ while (b->nmel && cmp(b->base , *mina + bwidth - width) < 0) {
swap(a->base, b->base, bwidth);
if (*mina == a->base) *mina = b->base;
a->base += bwidth;
b->base += bwidth;
- b->blocks--;
+ b->nmel--;
iters++;
}
return iters;
@@ -171,26 +171,26 @@ void wikisort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
/* temporary buffer is at base. carve remainder of input into a and b subarrays.
* the nmels for these subarrays is specified in terms of blocks. */
size_t bufnmel = bnmel + nmel % bnmel;
- struct blockarray a = { .base = base + width * bufnmel, .blocks = (bcount - 1) / 2 };
- struct blockarray b = { .base = a.base + bwidth * a.blocks, .blocks = bcount - 1 - a.blocks };
+ struct array a = { .base = base + width * bufnmel, .nmel = (bcount - 1) / 2 };
+ struct array b = { .base = a.base + bwidth * a.nmel, .nmel = bcount - 1 - a.nmel };
/* sort the buffer as if it were part of the a array */
- wikisort( base, nmel - b.blocks * bnmel, width, cmp);
- wikisort(b.base, b.blocks * bnmel, width, cmp);
+ wikisort( base, nmel - b.nmel * bnmel, width, cmp);
+ wikisort(b.base, b.nmel * bnmel, width, cmp);
/* if already sorted, nothing to do */
if (cmp(b.base - width, b.base) <= 0) return;
/* if blocks in b are all less than those in a, just need a rotate */
if (cmp(base + (nmel - 1) * width, base) <= 0) {
- rotate(base, nmel, width, a.blocks * bnmel + bufnmel);
+ rotate(base, nmel, width, a.nmel * bnmel + bufnmel);
return;
}
size_t nmel_pend_a = 0, nmel_pend_b = 0;
char *mina = a.base, *penda;
- while (a.blocks) {
- size_t rolled_blocks = b.blocks ? roll(&a, &b, bwidth, width, &mina, cmp) : 0;
+ while (a.nmel) {
+ size_t rolled_blocks = b.nmel ? roll(&a, &b, bwidth, width, &mina, cmp) : 0;
size_t offset;
/* move mina block to immediately after rolled b blocks */
@@ -218,13 +218,13 @@ void wikisort(void *unsorted, size_t nmel, size_t width, cmpfun cmp)
penda = a.base - offset * width;
a.base += bwidth;
- if (--a.blocks) {
- mina = find_min_block(a.base, a.blocks, bwidth, width, cmp);
+ if (--a.nmel) {
+ mina = find_min_block(a.base, a.nmel, bwidth, width, cmp);
}
}
if (nmel_pend_a) {
- merge_blocks(base, penda, nmel_pend_a, b.blocks * bnmel + nmel_pend_b, width, cmp);
+ merge_blocks(base, penda, nmel_pend_a, b.nmel * bnmel + nmel_pend_b, width, cmp);
}
wikisort(base, bufnmel, width, cmp);