任何人都可以帮助我将此代码从 C++ 重写为 C...(它有模板...yeeee :)) 谢谢。
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>( test_file, 0, 255 )
其中 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 重写这个结构。
简单整洁...帮助 :)
谢谢!
最佳答案
假设 sh_qsort()
实际上是一个快速排序实现,您需要做的就是将 TEST::c()
重构为 所期望的比较函数来自
。stdlib.h
的 qsort()
从通过模板的编译时通用代码到通过 void *
的运行时通用代码通常会带来性能损失,您可以通过使用所谓的“X-macros”来摆脱这种损失用预处理器模拟模板。然而,可能没有必要麻烦:因为 TEST::c()
足够复杂,它可能不会被内联,你只需要为运行时通用的使用付费由 qsort()
执行的交换。
关于c - 为 C 重写 C++ 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5179371/