节约功能,做同样的事情 [英] economizing with functions that do the same thing

查看:61
本文介绍了节约功能,做同样的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约一个月前,Heathfield发布了来自TAOCP的随机排列

的peudosource。这可能是五行。你需要能够做两件事:能够获得一个范围内的随机数并交换。我记得Dan Pop教我把交换写成宏。随着论坛

的改进,这成了:

#define SWAP(m,n)(tmp =(m),(m)=(n),(n)= tmp)

随机数字随着论坛的改进而来:

int rand_in_range(int m,int n)

{

/ *种子srand in main * /

/ * [m,n]是范围* /

int roll_again_threshold,divisor,result,tmp,offset, num_results;


if(m> n)SWAP(m,n);

offset = m;

num_results = n - m + 1;


if(num_results == 1){

返回m;

}

roll_again_threshold = RAND_MAX - RAND_MAX%num_results;

divisor = roll_again_threshold / num_results;


do {

result = rand ();

} while(结果> = roll_again_threshold);

结果/ = divisor;

返回偏移+结果;

}

然后我开始思考,当然,那就是麻烦

开始了。我发布在其他地方,我认为你不需要太多的语言

背景来获得它的要点:

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,过敏症,精神病和/或
整数。 Wie werden diese Funktionen Eine?

基本上,我如何选择两种功能,这些功能只在它们运行的​​类型上有所不同,并使它们成为一个?我被建议使用:

void permute(void * data,int n,size_t elsize);

可以使用
$调用数组a b $ b permute(a,sizeof a / sizeof a [0],sizeof a [0]);

这会起作用吗?如果不是,那么这个想法就是我的。如果那么它将是'b $ B $',那就是Erich Fruehstueck'。欢呼,毛茛

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(int m, int n)
{
/*seed srand in main */
/* [m, n] is range */
int roll_again_threshold, 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_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold/num_results;

do {
result = rand();
} while (result >= roll_again_threshold);
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

推荐答案

"你的叔叔 <在***** @ crippled.net>在消息中写道

news:44 *********************** @ news.usenetmonster。 com ...
"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...
大约一个月前,Heathfield从TAOCP发布了随机排列的peudosource。这可能是五行。你需要能够做两件事:能够获得一个范围内的随机数并交换。我记得Dan Pop教我把交换写成宏。随着论坛的改进,这成了:
#define SWAP(m,n)(tmp =(m),(m)=(n),(n)= tmp)
随机数字来了,再次进行论坛改进:
int rand_in_range(int m,int n)
{
/ *种子srand in main * /
/ * [m,n]是范围* /
int roll_again_threshold,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_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold / num_results;

执行{
result = rand();
} while(结果> = roll_again_threshold);
结果/ =除数;
返回偏移+结果;
}
然后我开始思考,当然,那就是麻烦开始的地方。我发布在其他地方,我认为你不需要太多的语言背景来获得它的要点:
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
整数。 Wie werden diese Funktionen Eine?
基本上,我如何才能使用两种功能,这些功能只在它们运行的​​类型上有所不同,并使它们成为一体?我被建议使用:
void permute(void * data,int n,size_t elsize);
可以调用一个带有
置换的数组a(a,sizeof a / sizeof a [0],sizeof a [0]);
这会起作用吗?如果不是,那么这个想法就是我的。如果它将
那么它是'Erich Fruehstueck'的。欢呼,furunculus
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(int m, int n)
{
/*seed srand in main */
/* [m, n] is range */
int roll_again_threshold, 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_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold/num_results;

do {
result = rand();
} while (result >= roll_again_threshold);
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




使用模板。等一下。这是'C ++。

;-)


void指针的问题在于函数取

void指针不知道传入了什么。所以你被一堆

的回调函数困住了。


如果我明白你想要什么做,就是这样。



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:
大约一个月前,希思菲尔德发布了随机排列的peudosource来自TAOCP的。


不,我没有。然而,我确实在几天前发布了一些改组伪代码和来源,

,这不是从TAOCP中获取的。

这可能是五行。你需要能够做两件事:能够获得一个范围内的随机数并交换。


是的。

我记得Dan Pop教我把交换写成宏。


我觉得这很令人惊讶。我以为Dan Pop会更有意义。

随着论坛的改进,这成了:
#define SWAP(m,n)(tmp =(m) ,(m)=(n),(n)= tmp)


只要tmp存在并且是合适的,那就没关系。

类型。

随机数字随着论坛的改进而来:
int rand_in_range(int m,int n)
{
/ * seed srand在main * /
/ * [m,n]是范围* /
int roll_again_threshold,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_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold / num_results;

做{
result = rand() ;
} while(结果> = roll_again_threshold);
结果/ = divisor;
返回偏移+结果;
}


那是可怕的,但我可以看到你在做什么。大概这个位有点好了,所以让我们继续前进。

然后我开始思考,那当然是麻烦的地方
开始了。我发布在其他地方,我认为你不需要太多的语言背景来获得它的要点:
Ich habe zwei Funktionen bei file scope erklaert:

根据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
整体。 Wie werden diese Funktionen Eine?


他们这样做完全相似,但首先是字符和后者

with int。这些功能如何成为一体?"

基本上,我如何才能使用两种功能,这些功能只在它们运行的​​类型上有所不同,并使它们成为一种?我被建议使用:
void permute(void * data,int n,size_t elsize);
可以调用一个带有
置换的数组a(a,sizeof a / sizeof a [0],sizeof a [0]);
这会起作用吗?


它可以工作。

如果不是,那么这个想法就是我的。如果它会那么'那就是Erich Fruehstueck'。
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(int m, int n)
{
/*seed srand in main */
/* [m, n] is range */
int roll_again_threshold, 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_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold/num_results;

do {
result = rand();
} while (result >= roll_again_threshold);
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.




我怀疑它是你的想法还是Erich Fruehstueck'的。


顺便说一句,这并不是真的在进行。它正在改变。


这是一个通用的交换功能:


void swap(void * s,void * t,size_t len )

{

unsigned char * u = s;

unsigned char * v = t;

unsigned char tmp;

而(len--)

{

tmp = * u;

* u ++ = * v;

* v ++ = tmp;

}

}


这是一个通用的改组功能:


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是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



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 <直流***** @ connx.com>在消息中写道

news:e7 ********** @ nntp.aioe.org ...

"Dann Corbit" <dc*****@connx.com> wrote in message
news:e7**********@nntp.aioe.org...
你的叔叔 <在***** @ crippled.net>在消息中写道
新闻:44 *********************** @ news.usenetmonster。 com ...
"Your Uncle" <in*****@crippled.net> wrote in message
news:44***********************@news.usenetmonster. com...
大约一个月前,Heathfield从TAOCP发布了随机排列的peudosource。这可能是五行。你需要能够做两件事:能够获得一个范围内的随机数并交换。我记得Dan Pop教我把交换写成宏。随着论坛的改进,这成了:
#define SWAP(m,n)(tmp =(m),(m)=(n),(n)= tmp)
随机数字来了,再次进行论坛改进:
int rand_in_range(int m,int n)
{
/ *种子srand in main * /
/ * [m,n]是范围* /
int roll_again_threshold,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_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold / num_results;

执行{
result = rand();
} while(结果> = roll_again_threshold);
结果/ =除数;
返回偏移+结果;
}
然后我开始思考,当然,那就是麻烦开始的地方。我发布在其他地方,我认为你不需要太多的语言背景来获得它的要点:
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
整数。 Wie werden diese Funktionen Eine?
基本上,我如何才能使用两种功能,这些功能只在它们运行的​​类型上有所不同,并使它们成为一体?我被建议使用:
void permute(void * data,int n,size_t elsize);
可以调用一个带有
置换的数组a(a,sizeof a / sizeof a [0],sizeof a [0]);
这会起作用吗?如果不是,那么这个想法就是我的。如果它将
那么它是'Erich Fruehstueck'的。欢呼,furunculus
使用模板。等一下。那是'C ++。
; - )
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(int m, int n)
{
/*seed srand in main */
/* [m, n] is range */
int roll_again_threshold, 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_threshold = RAND_MAX - RAND_MAX%num_results;
divisor = roll_again_threshold/num_results;

do {
result = rand();
} while (result >= roll_again_threshold);
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++.
;-)



我正处于薄薄的局面,所以我最好谴责这个令人发指的

建议。


void指针的问题在于,使用
void指针的函数不会知道传入的是什么。所以你被困在了
一大堆回调函数。


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.



我不确定这意味着什么。也许我们可以做一个更好的例子:


void print_a_char(char);

void print_an_int(int);

会是定义然后

print_a_char(char g){printf("%c\ n,g)}

print_an_int(int g){printf(" %d \ n",g)}

这是澄清,混淆,还是只是钻孔?欢呼,f


I''m not sure what that means. Maybe we could make a better trivial example:

void print_a_char(char);
void print_an_int(int);
would be definitions and then
print_a_char(char g){ printf(" %c\n", g) }
print_an_int(int g){ printf(" %d\n", g) }
Does that clarify, obfuscate, or just bore? cheers, f


这篇关于节约功能,做同样的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆