This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
3
(Code review) Type agnostic shuffle function
Post Body
#include <stddef.h>
/* Byte-wise swap two items of size SIZE. */
/* This code is borrowed straight from the GCC qsort implementation of SWAP. */
#define SWAP(a, b, size) \
do \
{ \
size_t __size = (size); \
char *__a = (a), *__b = (b); \
do \
{ \
char __tmp = *__a; \
*__a = *__b; \
*__b = __tmp; \
} while (--__size > 0); \
} while (0) \
/**
* Performs a Fisher-Yates shuffle shuffle on an arbitrarily typed array.
* @param base [in,out] A pointer to the beginning of the array.
* @param nmbers [in] The number of items in the array.
* @param size [in] The size of the individual elements of the array.
* @param r_func [in] The random number generator for shuffling the data.
*
* Example usage:
* @code{.c}
* shuffle(array, sizeof(array)/sizeof(array[0]), sizeof(array[0]), rand_wrapper)
* @endcode
* @see <a href="https://en.wikipedia.org/wiki/Fisher?Yates_shuffle">
* Fisher-Yates shuffle | Wikipedia</a>
* @copyright GNU Public License.
*/
void
shuffle(void* base, size_t nmbers, size_t size, size_t (*r_func)(void))
{
register char* right_ptr = (char*)base (nmbers-1) * size;
register char* rand_ptr;
register size_t randint;
while (nmbers > 0)
{
randint = r_func() % nmbers;
rand_ptr = &((char*)base)[randint*size];
SWAP(rand_ptr, right_ptr, size);
// decrements the right pointer and number of members
right_ptr -= size;
--nmbers;
}
}
Author
Account Strength
100%
Account Age
10 years
Verified Email
Yes
Verified Flair
No
Total Karma
45,629
Link Karma
14,091
Comment Karma
31,419
Profile updated: 5 days ago
Posts updated: 9 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 7 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/c_language/...