forked from e36freak/awk-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuf.awk
146 lines (121 loc) · 4.3 KB
/
shuf.awk
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
#!/usr/bin/awk -f
# actual shuffle function
# shuffles the values in "array" in-place, from indices "left" to "right".
# required for all of the shuf() functions below
function __shuffle(array, left, right, r, i, tmp) {
# loop backwards over the elements
for (i=right; i>left; i--) {
# generate a random number between the start and current element
r = int(rand() * (i - left + 1)) + left;
# swap current element and randomly generated one
tmp = array[i];
array[i] = array[r];
array[r] = tmp;
}
}
## usage: shuf(s, d)
## shuffles the array "s", creating a new shuffled array "d" indexed with
## sequential integers starting with one. returns the length, or -1 if an error
## occurs. leaves the indices of the source array "s" unchanged. uses the knuth-
## fisher-yates algorithm. requires the __shuffle() function.
function shuf(array, out, count, i) {
# loop over each index, and generate a new array with the same values and
# sequential indices
count = 0;
for (i in array) {
out[++count] = array[i];
}
# seed the random number generator
srand();
# actually shuffle
__shuffle(out, 1, count);
# return the length
return count;
}
## usage: ishuf(s)
## the behavior is the same as that of shuf(), except the array "s" is sorted
## in-place. the original indices are destroyed and replaced with sequential
## integers. everything else is described in shuf() above.
function ishuf(array, tmp, count, i) {
# loop over each index, and generate a new array with the same values and
# sequential indices
count = 0;
for (i in array) {
tmp[++count] = array[i];
delete array[i];
}
# copy tmp back over array
for (i=1; i<=count; i++) {
array[i] = tmp[i];
delete tmp[i];
}
# seed the random number generator
srand();
# actually shuffle
__shuffle(array, 1, count);
# return the length
return count;
}
## usage: shufi(s, d)
## the bevavior is the same as that of shuf(), except that the array indices
## are shuffled, not the array values. when done, the new array is indexed
## numerically, and the values are those of the original indices. everything
## else is described in shuf() above.
function shufi(array, out, count, i) {
# loop over each index, and generate a new array with the original indices
# mapped to new numeric ones
count = 0;
for (i in array) {
out[++count] = i;
}
# seed the random number generator
srand();
# actually shuffle
__shuffle(out, 1, count);
# return the length
return count;
}
## usage: ishufi(s)
## the behavior is tha same as that of shufi(), except that the array "s" is
## sorted in-place. the original indices are destroyed and replaced with
## sequential integers. everything else is describmed in shuf() and shufi()
## above.
function ishufi(array, tmp, count, i) {
# loop over each index, and generate a new array with the original indices
# mapped to new numeric ones
count = 0;
for (i in array) {
tmp[++count] = i;
delete array[i];
}
# copy tmp back over the original array
for (i=1; i<=count; i++) {
array[i] = tmp[i];
delete tmp[i];
}
# seed the random number generator
srand();
# actually shuffle
__shuffle(array, 1, count);
# return the length
return count;
}
# Copyright Daniel Mills <dm@e36freak.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.