mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 16:05:44 -07:00
Introduce a `shuffle' utility function.
[originally from svn r6090]
This commit is contained in:
16
misc.c
16
misc.c
@ -196,4 +196,20 @@ void game_mkhighlight(frontend *fe, float *ret,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void shuffle(void *array, int nelts, int eltsize, random_state *rs)
|
||||||
|
{
|
||||||
|
char *tmp = smalloc(eltsize);
|
||||||
|
char *carray = (char *)array;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = nelts; i-- > 1 ;) {
|
||||||
|
int j = random_upto(rs, i+1);
|
||||||
|
if (j != i) {
|
||||||
|
memcpy(tmp, carray + eltsize * i, eltsize);
|
||||||
|
memcpy(carray + eltsize * i, carray + eltsize * j, eltsize);
|
||||||
|
memcpy(carray + eltsize * j, tmp, eltsize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* vim: set shiftwidth=4 tabstop=8: */
|
/* vim: set shiftwidth=4 tabstop=8: */
|
||||||
|
@ -223,6 +223,9 @@ unsigned char *hex2bin(const char *in, int outlen);
|
|||||||
void game_mkhighlight(frontend *fe, float *ret,
|
void game_mkhighlight(frontend *fe, float *ret,
|
||||||
int background, int highlight, int lowlight);
|
int background, int highlight, int lowlight);
|
||||||
|
|
||||||
|
/* Randomly shuffles an array of items. */
|
||||||
|
void shuffle(void *array, int nelts, int eltsize, random_state *rs);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* version.c
|
* version.c
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user