-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshufflemodule.c
380 lines (327 loc) · 8.58 KB
/
shufflemodule.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
PyObject *do_pile(PyObject *origList, int num_piles, int num_shuffles)
{
double length = PyList_Size(origList);
int i;
// init empty list to return
PyObject * shuffledList = PyList_New(0);
// init piles
PyObject **pilesArray = malloc(num_piles * sizeof(PyObject *));
for(i = 0; i < num_piles; i++)
{
pilesArray[i] = PyList_New(0);
}
// deal items into respective piles
for(i = 0; i < length; i++)
{
int pile = i % num_piles;
// Retrieve Object
PyObject *temp = PyList_GetItem(origList, i);
if (temp == NULL)
{
Py_DECREF(shuffledList);
for(i = 0; i < num_piles; i++)
{
Py_DECREF(pilesArray[i]);
}
free(pilesArray);
return NULL;
}
// append the item
int test = PyList_Append(pilesArray[pile], temp);
if (test != 0)
{
Py_DECREF(shuffledList);
for(i = 0; i < num_piles; i++)
{
Py_DECREF(pilesArray[i]);
}
free(pilesArray);
return NULL;
}
Py_INCREF(temp);
}
// Reverse Piles
for(i = 0; i < num_piles; i++)
{
int test = PyList_Reverse(pilesArray[i]);
if (test != 0)
{
Py_DECREF(shuffledList);
for(i = 0; i < num_piles; i++)
{
Py_DECREF(pilesArray[i]);
}
free(pilesArray);
return NULL;
}
}
// Build final list
for(i = 0; i < num_piles; i++)
{
int pile_size = (int)PyList_Size(pilesArray[i]);
int j;
for(j = 0; j < pile_size; j++)
{
PyObject *temp = PyList_GetItem(pilesArray[i], j);
if (temp == NULL)
{
Py_DECREF(shuffledList);
for(i = 0; i < num_piles; i++)
{
Py_DECREF(pilesArray[i]);
}
free(pilesArray);
return NULL;
}
// append the item
int test = PyList_Append(shuffledList, temp);
if (test != 0)
{
Py_DECREF(shuffledList);
for(i = 0; i < num_piles; i++)
{
Py_DECREF(pilesArray[i]);
}
free(pilesArray);
return NULL;
}
Py_INCREF(temp);
}
}
// free uneeded memory
for(i = 0; i < num_piles; i++)
{
Py_DECREF(pilesArray[i]);
}
free(pilesArray);
// recursively shuffle the desired amount
num_shuffles--;
if (num_shuffles > 0)
{
shuffledList = do_pile(shuffledList, num_piles, num_shuffles);
}
return shuffledList;
}
PyObject *do_mongean(PyObject *origList, int num_shuffles)
{
double length = PyList_Size(origList);
PyObject * shuffledList = PyList_New((int)length);
int i, current;
for(i = 0, current = (int)length / 2; i < length; i++)
{
PyObject *temp = PyList_GetItem(origList, i);
if (temp == NULL)
{
Py_DECREF(shuffledList);
return NULL;
}
PyList_SET_ITEM(shuffledList, current , temp);
Py_INCREF(temp);
// Calc position of next item
if (i % 2) // even (back)
{
current += (i + 1);
}
else // odd (front)
{
current -= (i + 1);
}
}
// recursively shuffle the desired amount
num_shuffles--;
if (num_shuffles > 0)
{
shuffledList = do_mongean(shuffledList, num_shuffles);
}
return shuffledList;
}
PyObject *do_overhand(PyObject *origList, int num_shuffles)
{
srand((int)time(NULL));
double length = PyList_Size(origList);
PyObject * shuffledList = PyList_New((int)length);
// progress through shuffled list (backward)
int s = (int)length - 1;
// progress through original list (forward)
int o = 0;
// make sure we don't run off the end of the original list
while ( o < length )
{
// random item number ceiling
int ceiling = length > 4 ? (int)(.2 * length) : 1;
// get random number of items (min of 1), respecting the limit
int num_items = (rand() % ceiling) + 1;
int remaining = (int)length - o;
if (num_items > remaining)
{
num_items = remaining;
}
int i;
for (i = 0; i < num_items; i++)
{
// add item to new shuffledList from the the origList
// o + i = position in original list
PyObject *temp = PyList_GetItem(origList, o + i);
if (temp == NULL)
{
Py_DECREF(shuffledList);
return NULL;
}
// s - (num_items - (i + 1)) = new position in shuffledList
PyList_SET_ITEM(shuffledList, s - (num_items - (i + 1)) , temp);
Py_INCREF(temp);
}
// move progress identifiers to next position
s -= num_items;
o += num_items;
}
// recursively shuffle the desired amount
num_shuffles--;
if (num_shuffles > 0)
{
shuffledList = do_overhand(shuffledList, num_shuffles);
}
return shuffledList;
}
PyObject *do_riffle(PyObject *origList, int num_shuffles)
{
// how many times an item has been used consecutively from the same half
int const max_streak = 10;
srand((int)time(NULL));
double length = PyList_Size(origList);
PyObject * shuffledList = PyList_New((int)length);
/****
* Calculate the starting point for the second half of the list
* Add the half point to a random additional amount (maximum 10% of list)
*/
int last_half_start = (int)(length / 2) + (rand() % (length > 10 ? (int)(.1 * length) : 2)) + 1;
/**** Identifiers
* m = current total progress through shuffledList
* f = current progress across the first half
* l = current progress across the last half
* streak = count of how many times a card has been used consecutively from the same half
*
**** Conditions
* m < length = make sure we don't run off the end of shuffledList
* l < length = make sure we don't run off the end of the last half
* f < last_half_start = make sure we don't run into the last half with the first half identifier
*
**** Increments
* m++ = progress through shuffledList
* *current_ptr++ = increment the currently select half's identifier
*
*/
int m, f, l, streak;
// initialize the current pointer
int *current_ptr = (rand() % 2) ? &f : &l;
for(m = 0, f = 0, l = last_half_start, streak = 0; m < length && l < length && f < last_half_start; m++, *current_ptr += 1)
{
// calculate remaining percentage of the list to process
double remaining = 1 - m / length;
// generate random double
double test = rand() / (double)RAND_MAX;
// if the random double is benieth the continually lowering remaining percentage
// (lowering probability)
if (test < remaining || streak > max_streak)
{
// switch to the other half
current_ptr = (current_ptr == &f ? &l : &f);
// reset streak
streak = 0;
}
// add item to new shuffledList from the *current_ptr position in the origList
PyObject *temp = PyList_GetItem(origList, *current_ptr);
if (temp == NULL)
{
Py_DECREF(shuffledList);
return NULL;
}
PyList_SET_ITEM(shuffledList, m, temp);
Py_INCREF(temp);
// increment streak
streak += 1;
}
// change the pointer to the half that didn't cause the for to exit
current_ptr = (current_ptr == &f ? &l : &f);
// for the remaining half, add on to the end of the newly shuffledList
// (one hand ran out before the other)
while(m < (int)length)
{
PyObject *temp = PyList_GetItem(origList, *current_ptr);
if (temp == NULL)
{
Py_DECREF(shuffledList);
return NULL;
}
PyList_SET_ITEM(shuffledList, m, temp);
Py_INCREF(temp);
m++;
*current_ptr += 1;
}
// recursively shuffle the desired amount
num_shuffles--;
if (num_shuffles > 0)
{
shuffledList = do_riffle(shuffledList, num_shuffles);
}
return shuffledList;
}
static PyObject *shuffle_pile(PyObject *self, PyObject *args)
{
PyObject * origList;
int num_piles;
int num_shuffles = 1;
// parse args to list
if (! PyArg_ParseTuple( args, "Oi|i", &origList, &num_piles, &num_shuffles) )
{
return NULL;
}
return do_pile(origList, num_piles, num_shuffles);
}
static PyObject *shuffle_mongean(PyObject *self, PyObject *args)
{
PyObject * origList;
int num_shuffles = 1;
// parse args to list
if (! PyArg_ParseTuple( args, "O|i", &origList, &num_shuffles) )
{
return NULL;
}
return do_mongean(origList, num_shuffles);
}
static PyObject *shuffle_overhand(PyObject *self, PyObject *args)
{
PyObject * origList;
int num_shuffles = 1;
// parse args to list
if (! PyArg_ParseTuple( args, "O|i", &origList, &num_shuffles) )
{
return NULL;
}
return do_overhand(origList, num_shuffles);
}
static PyObject *shuffle_riffle(PyObject *self, PyObject *args)
{
PyObject * origList;
int num_shuffles = 1;
// parse args to list
if (! PyArg_ParseTuple( args, "O|i", &origList, &num_shuffles) )
{
return NULL;
}
return do_riffle(origList, num_shuffles);
}
static PyMethodDef ShuffleMethods[] = {
{"riffle", shuffle_riffle, METH_VARARGS, "Simulate a Riffle Shuffle on a List."},
{"overhand", shuffle_overhand, METH_VARARGS, "Simulate an Overhand Shuffle on a List."},
{"mongean", shuffle_mongean, METH_VARARGS, "Simulate a Mongean Shuffle on a List."},
{"pile", shuffle_pile, METH_VARARGS, "Simulate a Pile Shuffle on a List."},
{NULL, NULL, 0, NULL}
};
void initshuffle(void){
(void) Py_InitModule("shuffle", ShuffleMethods);
}