#include #include #include #include #include #include "sorters.h" #include "common.h" #include "counts.h" static inline void *tail(char *base, size_t nmel, size_t width) { return base + (nmel-1) * width; } /* get index of last block whose head is less than the previous block's tail */ static size_t last_overlap(char *base, size_t bcount, size_t bwidth, size_t width, cmpfun cmp) { struct counts snapshot = counts[CURRENT]; for (char *cur = tail(base, bcount, bwidth); --bcount; cur -= bwidth) if (cmp(cur - width, cur) > 0) break; add_counts(counts + LAST_OVERLAP, &snapshot); return bcount; } size_t merge(char *buf, char *a, char *b, size_t bufnmel, size_t anmel, size_t bnmel, size_t width, cmpfun cmp) { struct counts snapshot = counts[CURRENT]; char *a_end = a + width * anmel; char *b_end = b + width * bnmel; char *buf_end = buf + width * bufnmel; size_t count; while (bnmel) { for (count = 0; cmp(a_end-width, b_end-width) > 0; count++) { anmel -= 1; bufnmel -= 1; a_end -= width; buf_end -= width; } swap(a_end, buf_end, count*width); do { for (count = 0; bnmel && count < bufnmel && cmp(a_end-width, b_end-width) <= 0; count++) { bnmel -= 1; b_end -= width; buf_end -= width; } swap(b_end, buf_end, count*width); } while (count == bufnmel); } add_counts(counts + MERGE, &snapshot); return anmel; } void grailsort(void *unsorted, size_t nmel, size_t width, cmpfun cmp) { char *base = unsorted; if (nmel <= MAX_SORTNET) { sorting_network(base, nmel, width, cmp); return; } size_t blknmel = sqrt(nmel); /* elements in a block */ size_t bufnmel = blknmel + nmel % blknmel; /* elements in the buffer */ size_t bwidth = blknmel * width; /* size of a block in bytes */ size_t blocks = nmel / blknmel - 1; /* number of blocks in a + b */ size_t acount = blocks / 2; size_t bcount = blocks - acount; char *a = base; char *b = a + acount * bwidth; char *buf = b + bcount * bwidth; grailsort(a, acount * blknmel, width, cmp); grailsort(b, bcount * blknmel, width, cmp); /* sort all the a and b blocks together by their head elements */ grailsort(base, blocks, bwidth, cmp); /* merge, starting from the end and working towards the beginning */ size_t last_nmel = blknmel; while (blocks > 1) { size_t overlap = last_overlap(base, blocks, bwidth, width, cmp); if (!overlap) break; char *block_a = base + bwidth * (overlap-1); char *block_b = base + bwidth * overlap; size_t bnmel = (blocks - overlap - 1) * blknmel + last_nmel; last_nmel = merge(buf, block_a, block_b, bufnmel, blknmel, bnmel, width, cmp); buf = block_a + last_nmel * width; blocks = overlap; } struct counts snapshot = counts[CURRENT]; rotate(base, buf-base + bufnmel*width, buf-base); add_counts(counts + MOVE_BUFFER, &snapshot); grailsort(base, bufnmel, width, cmp); distribute_buffer(base, bufnmel, nmel - bufnmel, width, cmp); #if 0 assert_sorted(base, nmel, width, cmp); #endif }