About a month ago, Heathfield posted the peudosource for random permuting
from TAOCP. It was all of maybe five lines. You needed to be able to do
two things: be able to get a random number in a range and swap. I
remembered that Dan Pop taught me to write the swap as a macro. With forum
improvements, this became:
#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
The random number comes, again with forum improvements:
int rand_in_range(i nt m, int n)
{
/*seed srand in main */
/* [m, n] is range */
int roll_again_thre shold, divisor, result, tmp, offset, num_results;
if (m>n) SWAP(m, n);
offset = m;
num_results = n - m + 1;
if (num_results == 1) {
return m;
}
roll_again_thre shold = RAND_MAX - RAND_MAX%num_re sults;
divisor = roll_again_thre shold/num_results;
do {
result = rand();
} while (result >= roll_again_thre shold);
result /= divisor;
return offset + result;
}
But then I starting thinking, and that is, of course, where the trouble
began. I posted elsewhere, and I don't think you need much of a language
background to get the gist of it:
Ich habe zwei Funktionen bei file scope erklaert:
void permute_string( char * m, int n);
void permute_int(int * m, int n);
Sie tun das genau Gleiche, allerdings erstere mit chars und letztere mit
ints. Wie werden diese Funktionen Eine?
Basically, how do I take 2 functions that differ only in the type they
operate on, and make them one? I was advised to use:
void permute (void *data, int n, size_t elsize);
that could be called for an array a with
permute(a, sizeof a / sizeof a[0], sizeof a[0]);
Is this going to work? If it isn't, then the idea is mine. If it will then
it's Erich Fruehstueck's. cheers, furunculus 46 2439
"Your Uncle" <in*****@crippl ed.net> wrote in message
news:44******** *************** @news.usenetmon ster.com... About a month ago, Heathfield posted the peudosource for random permuting from TAOCP. It was all of maybe five lines. You needed to be able to do two things: be able to get a random number in a range and swap. I remembered that Dan Pop taught me to write the swap as a macro. With forum improvements, this became: #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp) The random number comes, again with forum improvements: int rand_in_range(i nt m, int n) { /*seed srand in main */ /* [m, n] is range */ int roll_again_thre shold, divisor, result, tmp, offset, num_results;
if (m>n) SWAP(m, n); offset = m; num_results = n - m + 1;
if (num_results == 1) { return m; }
roll_again_thre shold = RAND_MAX - RAND_MAX%num_re sults; divisor = roll_again_thre shold/num_results;
do { result = rand(); } while (result >= roll_again_thre shold); result /= divisor; return offset + result; } But then I starting thinking, and that is, of course, where the trouble began. I posted elsewhere, and I don't think you need much of a language background to get the gist of it: Ich habe zwei Funktionen bei file scope erklaert: void permute_string( char * m, int n); void permute_int(int * m, int n); Sie tun das genau Gleiche, allerdings erstere mit chars und letztere mit ints. Wie werden diese Funktionen Eine? Basically, how do I take 2 functions that differ only in the type they operate on, and make them one? I was advised to use: void permute (void *data, int n, size_t elsize); that could be called for an array a with permute(a, sizeof a / sizeof a[0], sizeof a[0]); Is this going to work? If it isn't, then the idea is mine. If it will then it's Erich Fruehstueck's. cheers, furunculus
Use a template. Oh, wait. That's C++.
;-)
The problem with the void pointer thingy is that the function taking the
void pointer will not know what is being passed in. So you are stuck with a
whole bunch of callback functions.
If I understand what you want to do, that is.
Your Uncle said: About a month ago, Heathfield posted the peudosource for random permuting from TAOCP.
No, I didn't. I did, however, post some shuffling pseudocode and source,
just a few days ago, which was not taken from TAOCP.
It was all of maybe five lines. You needed to be able to do two things: be able to get a random number in a range and swap.
Yes.
I remembered that Dan Pop taught me to write the swap as a macro.
I find that surprising. I'd have thought Dan Pop would have more sense.
With forum improvements, this became: #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
That's fine as far as it goes, provided tmp exists and is of the appropriate
type.
The random number comes, again with forum improvements: int rand_in_range(i nt m, int n) { /*seed srand in main */ /* [m, n] is range */ int roll_again_thre shold, divisor, result, tmp, offset, num_results;
if (m>n) SWAP(m, n); offset = m; num_results = n - m + 1;
if (num_results == 1) { return m; }
roll_again_thre shold = RAND_MAX - RAND_MAX%num_re sults; divisor = roll_again_thre shold/num_results;
do { result = rand(); } while (result >= roll_again_thre shold); result /= divisor; return offset + result; }
That's ghastly, but I can see what you're doing. Presumably this bit works
fine, so let's move on.
But then I starting thinking, and that is, of course, where the trouble began. I posted elsewhere, and I don't think you need much of a language background to get the gist of it: Ich habe zwei Funktionen bei file scope erklaert:
"I explained two functions with file scope", according to Babelfish.
void permute_string( char * m, int n); void permute_int(int * m, int n); Sie tun das genau Gleiche, allerdings erstere mit chars und letztere mit ints. Wie werden diese Funktionen Eine?
"They do that exactly resemble, however first with chars and the latters
with ints. How do these functions become one?"
Basically, how do I take 2 functions that differ only in the type they operate on, and make them one? I was advised to use: void permute (void *data, int n, size_t elsize); that could be called for an array a with permute(a, sizeof a / sizeof a[0], sizeof a[0]); Is this going to work?
It can be made to work.
If it isn't, then the idea is mine. If it will then it's Erich Fruehstueck's.
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);
}
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
"Dann Corbit" <dc*****@connx. com> wrote in message
news:e7******** **@nntp.aioe.or g... "Your Uncle" <in*****@crippl ed.net> wrote in message news:44******** *************** @news.usenetmon ster.com... About a month ago, Heathfield posted the peudosource for random permuting from TAOCP. It was all of maybe five lines. You needed to be able to do two things: be able to get a random number in a range and swap. I remembered that Dan Pop taught me to write the swap as a macro. With forum improvements, this became: #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp) The random number comes, again with forum improvements: int rand_in_range(i nt m, int n) { /*seed srand in main */ /* [m, n] is range */ int roll_again_thre shold, divisor, result, tmp, offset, num_results;
if (m>n) SWAP(m, n); offset = m; num_results = n - m + 1;
if (num_results == 1) { return m; }
roll_again_thre shold = RAND_MAX - RAND_MAX%num_re sults; divisor = roll_again_thre shold/num_results;
do { result = rand(); } while (result >= roll_again_thre shold); result /= divisor; return offset + result; } But then I starting thinking, and that is, of course, where the trouble began. I posted elsewhere, and I don't think you need much of a language background to get the gist of it: Ich habe zwei Funktionen bei file scope erklaert: void permute_string( char * m, int n); void permute_int(int * m, int n); Sie tun das genau Gleiche, allerdings erstere mit chars und letztere mit ints. Wie werden diese Funktionen Eine? Basically, how do I take 2 functions that differ only in the type they operate on, and make them one? I was advised to use: void permute (void *data, int n, size_t elsize); that could be called for an array a with permute(a, sizeof a / sizeof a[0], sizeof a[0]); Is this going to work? If it isn't, then the idea is mine. If it will then it's Erich Fruehstueck's. cheers, furunculus Use a template. Oh, wait. That's C++. ;-)
I'm on thin ice with topicality, so I better condemn this outrageous
suggestion.
The problem with the void pointer thingy is that the function taking the void pointer will not know what is being passed in. So you are stuck with a whole bunch of callback functions.
I'm not sure what that means. Maybe we could make a better trivial example:
void print_a_char(ch ar);
void print_an_int(in t);
would be definitions and then
print_a_char(ch ar g){ printf(" %c\n", g) }
print_an_int(in t g){ printf(" %d\n", g) }
Does that clarify, obfuscate, or just bore? cheers, f
Your Uncle wrote: "Dann Corbit" <dc*****@connx. com> wrote in message news:e7******** **@nntp.aioe.or g...
The problem with the void pointer thingy is that the function taking the void pointer will not know what is being passed in. So you are stuck with a whole bunch of callback functions.
I'm not sure what that means. Maybe we could make a better trivial example:
void print_a_char(ch ar); void print_an_int(in t); would be definitions and then print_a_char(ch ar g){ printf(" %c\n", g) } print_an_int(in t g){ printf(" %d\n", g) } Does that clarify, obfuscate, or just bore? cheers, f
For a trivial example, the overhead would outweigh the benifit of a
generic function.
For a more complex example, you are stuck with the function not knowing
the type passed in and the compiler not being able to check whether the
passed types are supported.
--
Ian Collins.
"Richard Heathfield" <in*****@invali d.invalid> wrote in message
news:hv******** ************@bt .com... Your Uncle said:
About a month ago, Heathfield posted the peudosource for random permuting from TAOCP. 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'm experiencing a wonderful time-dilation while rehabbing an injury. Why
doesn't time drag when you're golfing? It was all of maybe five lines. You needed to be able to do two things: be able to get a random number in a range and swap.
Yes.
I remembered that Dan Pop taught me to write the swap as a macro.
I find that surprising. I'd have thought Dan Pop would have more sense.
He does, but you have to remember he was talking to me. With forum improvements, this became: #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
That's fine as far as it goes, provided tmp exists and is of the appropriate type.
I'm surprised at how often this has caused me trouble. The random number comes, again with forum improvements: int rand_in_range(i nt m, int n) { /*seed srand in main */ /* [m, n] is range */ int roll_again_thre shold, divisor, result, tmp, offset, num_results;
if (m>n) SWAP(m, n); offset = m; num_results = n - m + 1;
if (num_results == 1) { return m; }
roll_again_thre shold = RAND_MAX - RAND_MAX%num_re sults; divisor = roll_again_thre shold/num_results;
do { result = rand(); } while (result >= roll_again_thre shold); result /= divisor; return offset + result; }
That's ghastly, but I can see what you're doing. Presumably this bit works fine, so let's move on.
But then I starting thinking, and that is, of course, where the trouble began. I posted elsewhere, and I don't think you need much of a language background to get the gist of it: Ich habe zwei Funktionen bei file scope erklaert:
"I explained two functions with file scope", according to Babelfish.
void permute_string( char * m, int n); void permute_int(int * m, int n); Sie tun das genau Gleiche, allerdings erstere mit chars und letztere mit ints. Wie werden diese Funktionen Eine?
"They do that exactly resemble, however first with chars and the latters with ints. How do these functions become one?"
Basically, how do I take 2 functions that differ only in the type they operate on, and make them one? I was advised to use: void permute (void *data, int n, size_t elsize); that could be called for an array a with permute(a, sizeof a / sizeof a[0], sizeof a[0]); Is this going to work?
It can be made to work.
If I fail, will a possible reason for this be that it was ill-advised to do
so as opposed to just having two awfully similar functions? If it isn't, then the idea is mine. If it will then it's Erich Fruehstueck's.
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; } }
I'll just snipe this wholesale, thank you.
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); } }
I'll need to take a closer look at this. Thanks and cheers, f
Your Uncle said: "Richard Heathfield" <in*****@invali d.invalid> wrote in message news:hv******** ************@bt .com... Your Uncle said:
Is this going to work?
It can be made to work. If I fail, will a possible reason for this be that it was ill-advised to do so as opposed to just having two awfully similar functions?
No. If you fail, it will be because you didn't manage to copy and paste the
code I gave you. :-)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
"Your Uncle" <in*****@crippl ed.net> writes:
[snip]
I've recently seen (or thought I saw) evidence that "Your Uncle",
"Frederick Gotham", and "Joe Smith" are the same person, posting under
different aliases. I recently asked you about this in the "wit's end"
thread, and you never answered.
Are "Your Uncle", "Frederick Gotham", and "Joe Smith" (or any two of
them) in fact the same person? If so, I ask you to pick a single
handle and stick with it. I don't care whether it's your real name or
not; pseudonyms are perfectly acceptable. But if you keep changing
identities, it makes the discussion more difficult to follow, with no
benefit.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Your Uncle wrote: "Richard Heathfield" <in*****@invali d.invalid> wrote in message news:hv******** ************@bt .com... Your Uncle said:
I remembered that Dan Pop taught me to write the swap as a macro.
I find that surprising. I'd have thought Dan Pop would have more sense. He does, but you have to remember he was talking to me.
With forum improvements, this became: #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
That's fine as far as it goes, provided tmp exists and is of the appropriate type.
I'm surprised at how often this has caused me trouble.
This is what I use:
#define SWAP(A, B, T) \
((void)(*(T) = *(A), *(A) = *(B), *(B) = *(T)))
--
pete
Richard Heathfield schrieb: Your Uncle said:I remembered that Dan Pop taught me to write the swap as a macro.
I find that surprising. I'd have thought Dan Pop would have more sense.
It is a matter of context: http://groups.google.de/group/comp.l...c5271ccb9149a/
somewhere around message 70 in the tree view; starting at
<2u************ *@uni-berlin.de> may suffice.
"Merrill & Michele" was the name the OP went by back then.
Dan Pop IIRC mainly argued against my suggestions of how to
"fix" the macro and pointed out how they fell short -- in
the light of that, the original swap macro was a better
solution for him.
Disclaimer: It is easily possible that I misremember -- I
just tracked down the thread but did not read it.
<snip> #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
<snip>
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hokiegal99 |
last post by:
A few questions about the following code. How would I "wrap" this in a
function, and do I need to?
Also, how can I make the code smart enough to realize that when a file
has 2 or more bad charcters in it, that the code needs to run until all
bad characters are gone? For example, if a file has the name
"<bad*mac\file" the program has to run 3 times to get all three bad
chars out of the file name.
The passes look like this:
|
by: David MacQuigg |
last post by:
I'm not getting any feedback on the most important benefit in my
proposed "Ideas for Python 3" thread - the unification of methods and
functions. Perhaps it was buried among too many other less important
changes, so in this thread I would like to focus on that issue alone.
I have edited the Proposed Syntax example below to take out the
changes unecessary to this discussion. I left in the change of
"instance variable" syntax (...
|
by: Nolan Martin |
last post by:
is a static functions address constant?
ie..
static void func();
write_to_file(&func);
Restart program...
static void func();
void (*funcPtr) ();
|
by: Gibby Koldenhof |
last post by:
Hiya,
Terrible subject but I haven't got a better term at the moment.
I've been building up my own library of functionality (all nice conforming
ISO C) for over 6 years and decided to adopt a more OO approach to fit my
needs. Altough I used an OO approach previously for some subparts of the
library it became somewhat difficult to maintain all those parts since they
really are the same thing coded for each part (code duplication). So...
|
by: junky_fellow |
last post by:
what are reentrant functions? What are the characteristics of a reentrant code ?
what things should be kept in mind while writing a reentrant code ?
| |
by: bahadir.balban |
last post by:
Hi,
What's the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case statement:
void print_structs(void * struct_argument, enum struct_type stype)
{
switch(stype) {
case STRUCT_TYPE_1:
|
by: Timothy Madden |
last post by:
Hello all.
I program C++ since a lot of time now and I still don't know this simple
thing: what's the problem with local functions so they are not part of
C++ ?
There surely are many people who will find them very helpfull. gcc has
them as a non-standard option, but only when compiling C language code,
so I'm afraid there might be some obscure reason why local functions are
not so easy to be dealt with in C++, which I do not yet know.
|
by: Richard G. Riley |
last post by:
In another thread it was pointed out that I'd made a booboo with
strcpy : one that that I've, if I'm honest, made many times
before. Not out of badness, just because since I first programmed C
back in 1986 (and have done so for about 25 % of the time since then)
or so I never really looked at the manpage for strcpy :
this combined with K&Rs famous pointer lessons which lead to 2 or 3
versions of a linear start to finish strpy implementatin...
|
by: Edward Diener |
last post by:
By reuse, I mean a function in an assembly which is called in another
assembly.
By a mixed-mode function I mean a function whose signature has one or
more CLR types and one or more non-CLR types.
The problem:
I have a number of mixed-mode functions which I want reuse. These
functions revolve around converting a CLR String to a C++ std::string or
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |