C++的模板堆排序

normal版的
堆排序

//dataStructrue.h
#pragma once
template struct num { static int const value = item; };//包装整数
struct null {};     //TODO, 空是否需要内容
//-------------定义序对--------------
template   
struct cons {
    using car = left;
    using cdr = right;
    static int const length = cdr::length + 1;
};
template
struct cons {
    using car = left;
    using cdr = null;
    static int const length = 1;
};
//-----------------------------------

//-------------定义链表--------------
template
struct list_item {
    using type = cons::type>;
    /*using car = head;
    using cdr = list_item;
    static int const length = cdr::length + 1;*/
};
template
struct list_item {
    using type = cons;
    /*using car = head;
    using cdr = null;
    static int const length = 1;*/
};
template<>
struct list_item {
    using type = null;
};
template
using list = typename list_item::type;
//-----------------------------------


//-------------链表查找--------------
template        //_list可传入诸如cons, cons, null>>
struct find_list {
    using result = typename find_list::result;
};
template
struct find_list<0, list_item> {
    using result = typename list_item::car;
};
template
struct find_list<0, _list> {
    using result = typename _list::car;
};
//-----------------------------------

//-------------链表拷贝--------------
template
struct copy_list {
    using result = cons::result,
                        typename copy_list::result>;
};
template
struct copy_list {
    using result = cons::result, null>;
};//TODO 如何简便解决start>end的情形
//-----------------------------------

//-------------链表合并--------------   //TODO,可以写可变参数
template
struct append {
    using result = cons::result>;
};
template
struct append {
    using result = cons::result>;
};
template<>
struct append {
    using result = null;
};
//----------------------------------

//-------------链表修改--------------
template
struct change_list {
    template
    struct if_need_null {};
    template
    struct if_need_null { using type = null; };
    template
    struct if_need_null { using type = typename copy_list::result; };
    using result = typename append::result,
        cons, typename if_need_null::type>>::result;
};
template
struct change_list<0, val, _list> {
    using result = typename append, typename copy_list<1, _list::length - 1, _list>::result>>::result;
};
//-----------------------------------
//heapSort.h
#pragma once
#include "dataStructure.h"
template
struct swap {
    using temp = typename change_list::result::value, _list>::result;
    using result = typename change_list::result::value, temp>::result;
};
template
struct maxHeapify {
    static const int left = 2 * index + 1;
    static const int right = left + 1;
    template
    struct if_temp {
        static const int max = index;
    };
    template
    struct if_temp {
        template
        struct if_temp2 {
            static const int max = index;
        };
        template
        struct if_temp2 {
            static const int max = num1;
        };
        static const int max = if_temp2 < ((find_list::result::value) < (find_list::result::value)), index, num1>::max;
    };
    static const int temp_max = if_temp<(left < heapSize), index, left>::max;
    static const int maxIndex = if_temp<(right < heapSize), temp_max, right>::max;

    template
    struct if_temp3 {};
    template
    struct if_temp3 {
        using result = _list;
    };
    template
    struct if_temp3 {
        using temp_list = typename swap::result;
        using result = typename maxHeapify::result;
    };
    using result = typename if_temp3<(maxIndex != index), maxIndex, index, heapSize>::result;
};
template
struct build {
    template
    struct loop {
        using result = typename loop::result>::result;
    };
    template
    struct loop<0, _list> {
        using result = typename maxHeapify<_list, 0, heapSize>::result;
    };
    using result = typename loop<(heapSize / 2), _list>::result;
};
template
struct sort {
    using temp = typename build<_list, heapSize>::result;
    template
    struct loop {
        using temp = typename swap<0, i, _list>::result;
        using temp2 = typename maxHeapify::result;
        using result = typename loop::result;
    };
    template
    struct loop<1, _list> {
        using temp = typename swap<0, 1, _list>::result;
        using result = typename maxHeapify::result;
    };
    using result = typename loop<(heapSize - 1), _list>::result;
};

// newHeapSort.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"


int main()
{
    using mylist = list, num<5>, num<4>, num<3>, num<2>, num<1>>;
    using a = sort::result;
    std::cout << "sort " << std::endl << typeid(a).name() << std::endl;

    return 0;
}

你可能感兴趣的:(C++的模板堆排序)