//author:flysnowxg
#include "stdio.h"
//用模板表示类型,模板的实例化表示对象(如pait_t<1,2> 表示(1,2)这样的两个值的对象)
template<int _first,int _second>
struct pair_t{
static const int first=_first;
static const int second=_second;
static void print(){printf("%d %d",first,second);}
};
//计算两个不同的pair_t类型的实例的小于关系
template <typename T1,typename T2>
struct less_t{
static const int first_le=T1::first<T2::first;
static const int first_eq=T1::first==T2::first;
static const int second_le=(first_eq&&(T1::second<T2::second));
static const int result=first_le || second_le;
};
//计算两个不同的pair_t类型的实例的大于关系
template <typename T1,typename T2>
struct greate_t{
static const int result=!less_t<T1,T2>::result;
};
struct null_t;
//类型列表
template <typename T1,typename T2> struct list_t;
template <typename T>
struct list_t<T,null_t>{
typedef T value;
typedef null_t next;
};
template <typename T1,typename T2,typename T3>
struct list_t<T1,list_t<T2,T3> >
{
typedef T1 value;
typedef list_t<T2,T3> next;
};
#define list_t1(e1) list_t<e1,null_t>
#define list_t2(e1,e2) list_t<e1,list_t1(e2)>
#define list_t3(e1,e2,e3) list_t<e1,list_t2(e2,e3)>
#define list_t4(e1,e2,e3,e4) list_t<e1,list_t3(e2,e3,e4)>
#define list_t5(e1,e2,e3,e4,e5) list_t<e1,list_t4(e2,e3,e4,e5)>
#define list_t6(e1,e2,e3,e4,e5,e6) list_t<e1,list_t5(e2,e3,e4,e5,e6)>
#define list_t7(e1,e2,e3,e4,e5,e6,e7) list_t<e1,list_t6(e2,e3,e4,e5,e6,e7)>
#define list_t8(e1,e2,e3,e4,e5,e6,e7,e8) list_t<e1,list_t7(e2,e3,e4,e5,e6,e7,e8)>
#define list_t9(e1,e2,e3,e4,e5,e6,e7,e8,e9) list_t<e1,list_t8(e2,e3,e4,e5,e6,e7,e8,e9)>
#define list_t10(e1,e2,e3,e4,e5,e6,e7,e8,e9,e10) list_t<e1,list_t9(e2,e3,e4,e5,e6,e7,e8,e9,e10)>
//递归打印类型列表中每个类型的值
template<typename T> struct print_t;
template<typename T>
struct print_t<list_t<T,null_t>>
{
typedef typename T result;
static void print(){
printf("\nelem::");
result::print();
}
};
template<typename T1,typename T2>
struct print_t<list_t<T1,T2>>
{
typedef typename T1 result;
static void print(){
printf("\nelem::");
result::print();
print_t<T2>::print();
}
};
//冒泡排序算法
template<typename T,template <typename,typename> class CompareT> struct sort_t;
template<typename T,template <typename,typename> class CompareT>
struct sort_t<list_t<T,null_t> ,CompareT>{
typedef list_t<T,null_t> sort_head;
typedef T least_elem;
typedef null_t remainder;
typedef list_t<T,null_t> result;
};
template<typename T1,typename T2,template <typename,typename> class CompareT>
struct sort_t<list_t<T1,T2>,CompareT>{
template<bool _b_swap> struct swap_t{
typedef list_t<T1,T2> result;
};
template<> struct swap_t<false>{
typedef list_t<typename T2::value,list_t<T1,typename T2::next> > result;
};
static const int order=!CompareT<T1,T2::value>::result;
typedef typename swap_t<order>::result sort_head;//假如CompareT是less_t,将开头两个元素中大的放前面,小的放后面
typedef typename sort_t<typename sort_head::next,CompareT>::least_elem least_elem;//假如CompareT是less_t,获取列表中最小的元素
typedef list_t<typename sort_head::value,typename sort_t<typename sort_head::next,CompareT>::remainder> remainder;//去掉末尾那个最小元素
typedef list_t<least_elem,typename sort_t<remainder,CompareT>::result> result;//将最小元素和剩余已经排好序的元素链表组成一个新链表
};