重写 C++ C 的模板
任何人都可以帮我将这段代码从 C++ 重写为 C...(它有模板...是的:)) 谢谢。
template <class T>
void SWAP( T& t1, T& t2 )
{
T tmp=t1;
t1=t2;
t2=tmp;
}
这个
template <class CMP, class Element>
void sh_qsort( Element* array, uint lo, uint hi )
{
some code...
}
和 sh_qsort 的调用方式类似于 sh_qsort
其中 TEST
是
struct TEST {
static int c( uint* A, int x, int y )
{
uint px = A[x];
uint py = A[y];
int r,s;
s = (px<py) ? SWAP(px,py),0x80000000 : 0;
byte* p = &f_buf[px];
byte* th = &f_buf[blocksize];
byte* q = &f_buf[py];
for(; (r=1,p<th) && ((r=(*p)-(*q))==0); p++,q++ );
return r^s;
}
static void s( uint* A, int x, int y )
{
SWAP( A[x], A[y] );
}
};
以及如何在 c 中重写此结构。
简单又整洁...帮助:)
谢谢!
Can anyone help me in rewriting this code from C++ to C... (it has templates... yeeee :))
Thank you.
template <class T>
void SWAP( T& t1, T& t2 )
{
T tmp=t1;
t1=t2;
t2=tmp;
}
and this one
template <class CMP, class Element>
void sh_qsort( Element* array, uint lo, uint hi )
{
some code...
}
and the sh_qsort is called like sh_qsort<TEST>( test_file, 0, 255 )
where TEST
is
struct TEST {
static int c( uint* A, int x, int y )
{
uint px = A[x];
uint py = A[y];
int r,s;
s = (px<py) ? SWAP(px,py),0x80000000 : 0;
byte* p = &f_buf[px];
byte* th = &f_buf[blocksize];
byte* q = &f_buf[py];
for(; (r=1,p<th) && ((r=(*p)-(*q))==0); p++,q++ );
return r^s;
}
static void s( uint* A, int x, int y )
{
SWAP( A[x], A[y] );
}
};
and how to rewrite this struct in c.
Simple and neat... HELP :)
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,大家,这是 http://encode 的结果。 ru/threads/1230-压缩一和零序列。
我编写了他试图翻译的程序。
无论如何,http://ctxmodel.net/files/BWT.cpp
Sorry guys, its the result of http://encode.ru/threads/1230-On-compressing-series-of-ones-and-zeroes.
and I written the program which he's trying to translate.
Anyway there's a more C-like implementation in http://ctxmodel.net/files/BWT.cpp
假设
sh_qsort()
实际上是一个快速排序实现,您所需要做的就是将TEST::c()
重构为qsort() 所期望的比较函数
来自stdlib.h
。从通过模板的编译时通用代码到通过
void *
的运行时通用代码通常会带来性能损失,您可以通过使用所谓的“X-macros”来模拟模板来消除性能损失与预处理器。然而,可能没有必要费心:由于TEST::c()
足够复杂,它可能不会被内联,并且您只需为运行时通用的使用付费交换由qsort()
执行。Assuming
sh_qsort()
actually is a quicksort implementation, all you need to do is refactorTEST::c()
into a comparison function as expected byqsort()
fromstdlib.h
.Going from compile-time generic code via templates to runtime-generic code via
void *
normally carries a performance penalty, of which you could get rid of by using so-called 'X-macros' to simulate templates with the preprocessor. However, there's probably no need to bother: AsTEST::c()
is sufficiently complex, it probably won't be inlined anyway, and you only have to pay for the use of the runtime-generic swaps performed byqsort()
.