File: shuffle.c

package info (click to toggle)
unsort 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 532 kB
  • ctags: 134
  • sloc: ansic: 1,401; sh: 1,042; makefile: 5
file content (178 lines) | stat: -rw-r--r-- 3,751 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*****************************************************************************

	unsort - reorder files semi-randomly
	Copyright (C) 2007, 2008  Wessel Dankers <wsl@fruit.je>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*****************************************************************************/

#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>

#include "mt19937ar.h"
#include "msort.h"
#include "shuffle.h"

static uint32_t u32reverse(uint32_t i) {
	i = (i & 0xAAAAAAAA) >> 1 | (i & 0x55555555) << 1;
	i = (i & 0xCCCCCCCC) >> 2 | (i & 0x33333333) << 2;
	i = (i & 0xF0F0F0F0) >> 4 | (i & 0x0F0F0F0F) << 4;
	i = (i & 0xFF00FF00) >> 8 | (i & 0x00FF00FF) << 8;
	return (i << 16) | (i >> 16);
}

static void u32swap(uint32_t *a, uint32_t *b) {
	uint32_t u;
	u = *a;
	*a = *b;
	*b = u;
}

static uint32_t sqrt32(uint32_t u) {
	uint32_t r, p;

	if(u < 2)
		return u;

	r = p = u / 2;

	do {
		p = r;
		r = (p + u / p) / 2;
	} while(r < p);

	return p;
}

static uint32_t seed = 0;
static uint32_t *tmp = NULL;

void shuffle_seed(uint32_t s) {
	seed = s;
}

void shuffle_tmp(uint32_t *t) {
	tmp = t;
}

void shuffle_init(uint32_t *a, uint32_t count) {
	uint32_t u;
	for(u = 0; u < count; u++)
		a[u] = u;
}

void shuffle_none(uint32_t *src, uint32_t *dst, uint32_t count) {
	if(!count)
		return;

	if(src) {
		if(src != dst)
			memcpy(dst, src, count * sizeof *src);
	} else {
		shuffle_init(dst, count);
	}
}

void shuffle_random(uint32_t *src, uint32_t *dst, uint32_t count) {
	uint32_t u;

	if(!count)
		return;

	if(src != dst) {
		if(src)
			memcpy(dst, src, count * sizeof *src);
		else
			shuffle_init(dst, count);
	}
	for(u = count - 1; u > 0; u--)
		u32swap(dst + mt_genrand32_bounded(0, u + 1), dst + u);
}

void shuffle_bitreverse(uint32_t *src, uint32_t *dst, uint32_t count) {
	uint32_t u;

	if(!count)
		return;

	if(src == dst) {
		for(u = 0; u < count; u++)
			dst[u] = u32reverse(dst[u] ^ seed);
		src = NULL;
	} else {
		for(u = 0; u < count; u++)
			dst[u] = u32reverse(u ^ seed);
	}
	msort32(dst, count);
	if(src)
		for(u = 0; u < count; u++)
			dst[u] = src[u32reverse(dst[u]) ^ seed];
	else
		for(u = 0; u < count; u++)
			dst[u] = u32reverse(dst[u]) ^ seed;
}

static uint32_t rev(uint32_t r, uint32_t *a, uint32_t u) {
	return (u % r) * r + a[u / r];
}

static uint32_t fwd(uint32_t r, uint32_t *a, uint32_t u) {
	return a[u % r] * r + u / r;
}

static void shuffle_invert(uint32_t *src, uint32_t *dst, uint32_t count) {
	uint32_t u;
	for(u = 0; u < count; u++)
		dst[src[u]] = u;
}

void shuffle_sqrtbase(uint32_t *src, uint32_t *dst, uint32_t count) {
	uint32_t u, r;
	uint32_t *a, *b;

	if(!count)
		return;

	if(src == dst || !tmp) {
		shuffle_bitreverse(src, dst, count);
		return;
	}

	r = sqrt32(count);
	if(r * r < count)
		r++;

	a = tmp;
	b = a + r;

	shuffle_random(NULL, a, r);

	for(u = 0; u < count; u++)
			dst[u] = fwd(r, a, u);

	msort32(dst, count);
	shuffle_invert(a, b, r);
	if(src)
		for(u = 0; u < count; u++)
			dst[u] = src[rev(r, b, dst[u])];
	else
		for(u = 0; u < count; u++)
			dst[u] = rev(r, b, dst[u]);
}

const shuffle_algo_t shuffle_heuristic = shuffle_sqrtbase;