C 语言实现的 stl-like 算法

使用类似BOOST.PP技巧,自动生成代码,效率上小胜stl,主要抽象出一般的(sort/heap/search)算法中的compare,按成员类型、偏移、类尺寸分派至不同函数;性能比stl相应算法还略高,用法更简单:

#include <febird/c/algorithm.h>

using namespace std;

struct A { int x, y; }; // x, y can be all base type: char/float/double/ptr etc...
struct Compare_A_x // only needed by std algorithm, maybe cause code explosion
{
	bool operator()(const A& x, const A& y) const { return x.x < y.x; }
	bool operator()(const A* x, const A* y) const { return x->x < y->x; }
};
void foo(std::vector<A>& va, std::vector<A*>& vpa)
{
	// febird_sort_xxx is macro, called a C function, will not cause code explosion
	// more fast 15% than std::sort
	febird_sort_field(&*va.begin(), va.size(), x); // in cpp, auto deduce type of x
	febird_sort_field_c(&*va.begin(), va.size(), x, tev_int); // in C, can not deduce type of x
	std::sort(va.begin(), va.end(), Compare_A_x());

	febird_sort_field_p(&*vpa.begin(), vpa.size(), x); // in cpp, auto deduce type of x
	febird_sort_field_pc(&*vpa.begin(), vpa.size(), x, tev_int); // in C, can not deduce type of x
	std::sort(vpa.begin(), vpa.end(), Compare_A_x());
}

 

 

 项目地址:http://code.google.com/p/febird

你可能感兴趣的:(C++,c,算法,C#,Google)