summaryrefslogtreecommitdiff
path: root/distribute.c
blob: 7c2b49f9d8beb865b85c3d12f0a17fe7c14fd4de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stddef.h>

#include "common.h"
#include "counts.h"

void distribute_buffer(char *base, size_t bufnmel, size_t sortnmel, size_t width, cmpfun cmp)
{
	struct counts snapshot = counts[CURRENT];

	while (bufnmel && sortnmel) {
		char  *sorted    = base + bufnmel * width;
		size_t insertpos = binary_search(base, sorted, sortnmel, width, cmp);
		if (insertpos > 0) {
			rotate(base, (bufnmel + insertpos) * width, bufnmel * width);
		}

		base     += (insertpos + 1) * width;
		bufnmel  -= 1;
		sortnmel -= insertpos;
	}

	add_counts(counts + DISTRIBUTE, &snapshot);
}