美文网首页
Template Programming Language

Template Programming Language

作者: 骰子大鳄 | 来源:发表于2017-12-23 16:37 被阅读0次
    //ControlStructure.h
    #pragma once
    template<bool Cond, class Then, class Else>
    struct make_if {
        using result = typename Then;
    };
    template<class Then, class Else>
    struct make_if<false, Then, Else> {
        using result = typename Else;
    };
    template<bool Cond, class Then, class Else>
    using _If = typename make_if<Cond, Then, Else>::result;         //延迟求值
    template<bool Cond, class Then, class Else>
    using If = typename _If<Cond, Then, Else>::result; //立即求值
    
    
    //DataType.h
    #pragma once
    template<int N>
    struct Int { static int const result = N; };
    
    //list.h
    //下面开头带_为延迟求值, 否则是立即求值
    #pragma once
    #include "ControlStruct.h"
    struct Null { using result = Null; };
    
    //-------------定义序对--------------
    template<class left, class right>
    struct Cons {
        using result = Cons<left, right>;
        using car = left;
        using cdr = right;
        enum { length = cdr::length + 1 };
    };
    template<class left>
    struct Cons<left, Null> {
        using result = Cons<left, Null>;
        using car = left;
        using cdr = Null;
        enum { length = 1 };
    };
    
    //-----------------------------------
    
    //-------------定义链表--------------
    template<class head, class ...tail>
    struct make_list {
        using result = Cons<head, typename make_list<tail...>::result>;
    };
    template<class head>
    struct make_list<head> {
        using result = Cons<head, Null>;
    };
    template<class ...Arg>
    using List = typename make_list<Arg...>::result;
    
    //-----------------------------------
    
    template<class cons>
    struct _Car {
        using result = typename cons::car;
    };
    template<class cons>
    using Car = typename _Car<cons>::result;
    
    template<class cons>
    struct _Cdr {
        using result = typename cons::cdr;
    };
    template<class cons>
    using Cdr = typename _Cdr<cons>::result;
    
    template<class cons>
    struct _Length {
        enum{ result = cons::length };
    };
    template<class cons>
    using Length = typename _Length<cons>::result;
    
    //-------------链表查找--------------
    template<class list, int n>
    struct _Find {
        using result = typename _Find<typename list::cdr, n - 1>::result;
    };
    template<class head>
    struct _Find<make_list<head>, 0> {
        using result = typename make_list<head>::car;
    };
    template<class list>
    struct _Find<list, 0> {
        using result = typename list::car;
    };
    template<class list, int n>
    using Find = typename _Find<list, n>::result;
    //-----------------------------------
    
    //-------------链表拷贝--------------
    template<class list, int start , int end>
    struct _Copy {
        using result = Cons<Find<list, start>,
                   typename _Copy<typename list::cdr, start, end - 1>::result>;
    };
    template<class list, int start>
    struct _Copy<list, start, start> {
        using result = Cons<Find<list, start>, Null>;
    };
    template<class list, int start, int end>
    using Copy = typename _Copy<list, start, end>::result;
    //-----------------------------------
    
    //-------------链表合并--------------
    
    template<class T, class ...Arg>
    struct _Append {
        template<class cons1, class cons2>
        struct make_append {
            using result = Cons<typename cons1::car, 
                                typename make_append<typename cons1::cdr, cons2>::result>;
        };
        template<class cons2>
        struct make_append<Null, cons2> {
            using result = Cons<typename cons2::car, 
                                typename make_append<Null, typename cons2::cdr>::result>;
        };
        template<>
        struct make_append<Null, Null> {
            using result = Null;
        };
    
        using result = typename make_append<T, typename _Append<Arg...>::result>::result;
    };
    template<class list>
    struct _Append<list> {
        using result = list;
    };
    template<class T, class ...Arg>
    using Append = typename _Append<T, Arg...>::result;
    //-----------------------------------
    
    
    
    //-------------链表修改--------------
    template<class list, int index, class val>
    struct _Change {
        using result = Append<Copy<list, 0, index - 1>,
                              Cons<val,
                                   If<(index == list::length - 1),
                                       Null,
                                      _Copy<list, index + 1, list::length - 1>>>>;//延迟求值
    };
    template<class list, class val>
    struct _Change<list, 0, val> {
        using result = Append<Null,
                              Cons<val, 
                                   Copy<list, 1, list::length - 1>>>;
    };
    template<class list, int index, class val>
    using Change = typename _Change<list, index, val>::result;
    //-----------------------------------*
    
    template<class list, int a, int b>
    struct _Swap {
        using result = Change<Change<list,a,Find<list, b>>,b, Find<list, a>>;
    };
    template<class list, int a, int b>
    using Swap = typename _Swap<list, a, b>::result;
    

    相关文章

      网友评论

          本文标题:Template Programming Language

          本文链接:https://www.haomeiwen.com/subject/spsqgxtx.html