C++的模板堆排序

normal版的
堆排序

//dataStructrue.h
#pragma once
template<int item> struct num { static int const value = item; };//包裝整數(shù)
struct null {};     //TODO, 空是否需要內(nèi)容
//-------------定義序?qū)?-------------
template<class left, class right>   
struct cons {
    using car = left;
    using cdr = right;
    static int const length = cdr::length + 1;
};
template<class left>
struct cons<left, null> {
    using car = left;
    using cdr = null;
    static int const length = 1;
};
//-----------------------------------

//-------------定義鏈表--------------
template<class head, class ...tail>
struct list_item {
    using type = cons<head, typename list_item<tail...>::type>;
    /*using car = head;
    using cdr = list_item<tail...>;
    static int const length = cdr::length + 1;*/
};
template<class head>
struct list_item<head> {
    using type = cons<head, null>;
    /*using car = head;
    using cdr = null;
    static int const length = 1;*/
};
template<>
struct list_item<null> {
    using type = null;
};
template<class T, class ...tail>
using list = typename list_item<T, tail...>::type;
//-----------------------------------


//-------------鏈表查找--------------
template<int n, class _list>        //_list可傳入諸如cons<num<1>, cons<num<2>, null>>
struct find_list {
    using result = typename find_list<n - 1, typename _list::cdr>::result;
};
template<class head>
struct find_list<0, list_item<head>> {
    using result = typename list_item<head>::car;
};
template<class _list>
struct find_list<0, _list> {
    using result = typename _list::car;
};
//-----------------------------------

//-------------鏈表拷貝--------------
template<int start, int end, class _list>
struct copy_list {
    using result = cons<typename find_list<start, _list>::result,
                        typename copy_list<start, end - 1, typename _list::cdr>::result>;
};
template<int start, class _list>
struct copy_list<start, start, _list> {
    using result = cons<typename find_list<start, _list>::result, null>;
};//TODO 如何簡便解決start>end的情形
//-----------------------------------

//-------------鏈表合并--------------   //TODO,可以寫可變參數(shù)
template<class cons1, class cons2>
struct append {
    using result = cons<typename cons1::car, typename append<typename cons1::cdr, cons2>::result>;
};
template<class cons2>
struct append<null, cons2> {
    using result = cons<typename cons2::car, typename append<null, typename cons2::cdr>::result>;
};
template<>
struct append<null, null> {
    using result = null;
};
//----------------------------------

//-------------鏈表修改--------------
template<int index, int val, class _list>
struct change_list {
    template<bool cond, int index, int length, class _list>
    struct if_need_null {};
    template<int index, int length, class _list>
    struct if_need_null<true, index, length, _list> { using type = null; };
    template<int index, int length, class _list>
    struct if_need_null<false, index, length, _list> { using type = typename copy_list<index + 1, length - 1, _list>::result; };
    using result = typename append<typename copy_list<0, index - 1, _list>::result,
        cons<num<val>, typename if_need_null<index == _list::length - 1, index, _list::length, _list>::type>>::result;
};
template<int val, class _list>
struct change_list<0, val, _list> {
    using result = typename append<null,
        cons<num<val>, typename copy_list<1, _list::length - 1, _list>::result>>::result;
};
//-----------------------------------
//heapSort.h
#pragma once
#include "dataStructure.h"
template<int a, int b, class _list>
struct swap {
    using temp = typename change_list<a, find_list<b, _list>::result::value, _list>::result;
    using result = typename change_list<b, find_list<a, _list>::result::value, temp>::result;
};
template<class _list, int index, int heapSize>
struct maxHeapify {
    static const int left = 2 * index + 1;
    static const int right = left + 1;
    template<bool cond, int index, int num1>
    struct if_temp {
        static const int max = index;
    };
    template<int index, int num1>
    struct if_temp<true, index, num1> {
        template<bool cond, int index, int num1>
        struct if_temp2 {
            static const int max = index;
        };
        template<int index, int num1>
        struct if_temp2<true, index, num1> {
            static const int max = num1;
        };
        static const int max = if_temp2 < ((find_list<index, _list>::result::value) < (find_list<num1, _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<bool cond1, int maxIndex, int index, int heapSize>
    struct if_temp3 {};
    template<int maxIndex, int index, int heapSize>
    struct if_temp3<false, maxIndex, index, heapSize> {
        using result = _list;
    };
    template<int maxIndex, int index, int heapSize>
    struct if_temp3<true, maxIndex, index, heapSize> {
        using temp_list = typename swap<maxIndex, index, _list>::result;
        using result = typename maxHeapify<temp_list, maxIndex, heapSize>::result;
    };
    using result = typename if_temp3<(maxIndex != index), maxIndex, index, heapSize>::result;
};
template<class _list, int heapSize>
struct build {
    template<int i, class _list>
    struct loop {
        using result = typename loop<i - 1, typename maxHeapify<_list, i, heapSize>::result>::result;
    };
    template<class _list>
    struct loop<0, _list> {
        using result = typename maxHeapify<_list, 0, heapSize>::result;
    };
    using result = typename loop<(heapSize / 2), _list>::result;
};
template<class _list, int heapSize>
struct sort {
    using temp = typename build<_list, heapSize>::result;
    template<int i, class _list>
    struct loop {
        using temp = typename swap<0, i, _list>::result;
        using temp2 = typename maxHeapify<temp, 0, i>::result;
        using result = typename loop<i - 1, temp2>::result;
    };
    template<class _list>
    struct loop<1, _list> {
        using temp = typename swap<0, 1, _list>::result;
        using result = typename maxHeapify<temp, 0, 1>::result;
    };
    using result = typename loop<(heapSize - 1), _list>::result;
};

// newHeapSort.cpp: 定義控制臺應(yīng)用程序的入口點宝泵。
//

#include "stdafx.h"


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

    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件降传,死亡現(xiàn)場離奇詭異,居然都是意外死亡勾怒,警方通過查閱死者的電腦和手機婆排,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來笔链,“玉大人段只,你說我怎么就攤上這事〖ǎ” “怎么了赞枕?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長坪创。 經(jīng)常有香客問我炕婶,道長,這世上最難降的妖魔是什么莱预? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任柠掂,我火速辦了婚禮,結(jié)果婚禮上依沮,老公的妹妹穿的比我還像新娘涯贞。我一直安慰自己,他們只是感情好危喉,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布宋渔。 她就那樣靜靜地躺著,像睡著了一般姥饰。 火紅的嫁衣襯著肌膚如雪傻谁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天列粪,我揣著相機與錄音,去河邊找鬼谈飒。 笑死岂座,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的杭措。 我是一名探鬼主播费什,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼手素!你這毒婦竟也來了鸳址?” 一聲冷哼從身側(cè)響起瘩蚪,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎稿黍,沒想到半個月后疹瘦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡巡球,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年言沐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酣栈。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡险胰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出矿筝,到底是詐尸還是另有隱情起便,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布窖维,位于F島的核電站缨睡,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏陈辱。R本人自食惡果不足惜奖年,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望沛贪。 院中可真熱鬧陋守,春花似錦、人聲如沸利赋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽媚送。三九已至中燥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間塘偎,已是汗流浹背疗涉。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留吟秩,地道東北人咱扣。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像涵防,于是被迫代替她去往敵國和親闹伪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容