Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
economizing with functions that do the same thing
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Richard Heathfield, post: 2470759"] Your Uncle said: No, I didn't. I did, however, post some shuffling pseudocode and source, just a few days ago, which was not taken from TAOCP. I find that surprising. I'd have thought Dan Pop would have more sense. That's fine as far as it goes, provided tmp exists and is of the appropriate type. That's ghastly, but I can see what you're doing. Presumably this bit works fine, so let's move on. "I explained two functions with file scope", according to Babelfish. "They do that exactly resemble, however first with chars and the latters with ints. How do these functions become one?" It can be made to work. I doubt whether it's either your idea or Erich Fruehstueck's. Incidentally, this isn't really permuting. It's shuffling. Here is a generic swapping function: void swap(void *s, void *t, size_t len) { unsigned char *u = s; unsigned char *v = t; unsigned char tmp; while(len--) { tmp = *u; *u++ = *v; *v++ = tmp; } } Here is a generic shuffling function: void shuffle(void *s, size_t size, size_t len) { unsigned char *t = s; unsigned char *u = s; size_t i = 0; size_t r = 0; for(i = 0; i < len; i++) { r = (len - i) * rand() / (RAND_MAX + 1.0); swap(t + size * i, u + size * r, size); } } [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
economizing with functions that do the same thing
Top