summaryrefslogtreecommitdiff
path: root/generators.c
blob: 2e31b72a421a6c90af2a908ac3cc1499220ce23b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>

#include "generators.h"

static void generate_random(int *buffer, size_t size)
{
	srandom(1);
	for (size_t i = 0; i < size; i++) buffer[i] = random();

	/* compress the values to nice small numbers to make debugging nicer */
#if 0
	int maxcompressed = -1;
	for (size_t compressed = 0; compressed < size; compressed++) {
		size_t minidx = -1;
		for (size_t i = 0; i < size; i++) {
			if (maxcompressed < buffer[i] && (minidx >= size || buffer[i] < buffer[minidx]))
				minidx = i;
		}
		buffer[minidx] = ++maxcompressed;
	}
#endif
}

static void generate_sorted(int *buffer, size_t size)
{
	for (size_t i = 0; i < size; i++) buffer[i] = i;
}

static void generate_reverse(int *buffer, size_t size)
{
	for (size_t i = 0; i < size; i++) buffer[i] = size - i - 1;
}

static void generate_constant(int *buffer, size_t size)
{
	for (size_t i = 0; i < size; i++) buffer[i] = 42;
}

static void generate_sorted_noise(int *buffer, size_t size)
{
	int noisemax = size / 4;
	int noiseoff = size / 8;

	srandom(1);
	for (size_t i = 0; i < size; i++) {
		int noise = 0;
		if (random() < 0.2 * RAND_MAX) {
			noise = random() % noisemax - noiseoff;
		}
		buffer[i] = i + noise;
	}
}

const struct generator generators[] = {
	{ .name = "random",       .func = generate_random       },
	{ .name = "sorted",       .func = generate_sorted       },
	{ .name = "reverse",      .func = generate_reverse      },
	{ .name = "constant",     .func = generate_constant     },
	{ .name = "sorted+noise", .func = generate_sorted_noise },
	{ 0 }
};