码迷,mamicode.com
首页 > 其他好文 > 详细

每日一题22:Deque与Trait

时间:2015-04-15 11:23:15      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:模板   deque   c++   

虽然之前写过一个Deque,但是那个属于C与C++混合的产品,近来在学习C++模板,所以写了一个C++版,同时第一次尝试使用Trait技术。
本来已经写好的文章,发表之后丢失了,好郁闷,没心情再写了,贴贴代码吧。

这个Trait是用来决定传递参数时是使用值传递还是引用传递

#ifndef _TYPE_TRAITS_H_
#define _TYPE_TRAITS_H_

namespace MyDataStructure
{
    template<typename T>
    class ParameterTrait
    {
    private:
        template<typename U>
        struct __ParaTrait
        {
            typedef U& ParameterType;
        };

        template<typename U>
        struct __ParaTrait<U*>
        {
            typedef U ParameterType;
        };

        template<typename U>
        struct __ParaTrait<U&>
        {
            typedef typename __ParaTrait<U>::ParameterType ParameterType;
        };

        template<>
        struct __ParaTrait<char>
        {
            typedef char ParameterType;
        };

        template<>
        struct __ParaTrait<unsigned char>
        {
            typedef unsigned char ParameterType;
        };

        template<>
        struct __ParaTrait<int>
        {
            typedef int ParameterType;
        };

        template<>
        struct __ParaTrait<unsigned int>
        {
            typedef unsigned int ParameterType;
        };

    public:
        typedef typename __ParaTrait<T>::ParameterType ParameterType;
    };
}

#endif

模板情况下实现的双端队列(其实这个只是双向链表,真正的双端队列比这个复杂太多了):

#ifndef _DEQUE_H_
#define _DEQUE_H_

#include "..\Utilites\type_traits.h"

namespace MyDataStructure
{
    template<typename T>
    class BiDirectionNode
    {
    private:
        typedef typename ParameterTrait<T>::ParameterType ParameterType;
    public:
        BiDirectionNode(const ParameterType value) : value(value),next(nullptr),pre(nullptr) {}
        BiDirectionNode(const BiDirectionNode& n)
        {
            _copy(n);
        }

        BiDirectionNode& operator = (const BiDirectionNode& n)
        {
            _copy(n);
            return *this;
        }
        T Value() const { return value; }
        T& Value() { return value; }
        T* Next() { return next; }
    public:
        void _copy(const BiDirectionNode& n)
        {
            value = n.value;
            next = n.next;
            pre = n.pre;
        }
        T value;
        BiDirectionNode* pre;
        BiDirectionNode* next;
    };

    template<typename T>
    class Deque
    {
    private:
        typedef typename BiDirectionNode<T> NodeType;
        typedef typename ParameterTrait<T>::ParameterType ParameterType;
    public:
        Deque() : size(0), head(nullptr),tail(nullptr){}
        Deque(const Deque& d){ _copy(d); }
        Deque& operator=(const Deque& d){ _destroy(); _copy(d); return *this; }
        ~Deque(){ _destroy(); }
        void Clear() { _destroy(); }
        const int Size() { return size; }
        bool Empty(){ return size == 0; }
        void Push(const ParameterType value)
        {
            NodeType* n = new NodeType(value);
            _push(n);
        }
        void Insert(const ParameterType value)
        {
            NodeType* n = new NodeType(value);
            _insert(n);
        }
        bool Pop(T& value)
        {
            if (size == 0)
                return false;
            value = head->value;
            NodeType* p = head;
            if (size == 1)
            {
                head = tail = nullptr;
            }
            else
            {
                head = head->next;
                head->pre = nullptr;
            }
            --size;
            delete p;
            return true;
        }
        bool Eject(T& value)
        {
            if (size == 0)
                return false;
            value = tail->value;
            NodeType* p = tail;
            if (size == 1)
            {
                head = tail = nullptr;
            }
            else
            {
                tail = tail->pre;
                tail->next = nullptr;
            }

            --size;
            delete p;
            return true;
        }
    private:
        void _push( NodeType* n)
        {
            if (size == 0)
            {
                head = n;
                head->next = nullptr;
                head->pre = nullptr;
                tail = head;
            }
            else
            {
                n->next = head;
                n->pre = nullptr;
                head->pre = n;
                head = n;
            }
            ++size;
        }
        void _insert( NodeType* n)
        {
            if (size == 0)
            {
                head = n;
                head->next = nullptr;
                head->pre = nullptr;
                tail = head;
            }
            else
            {
                tail->next = n;
                n->pre = tail;
                n->next = nullptr;
                tail = n;
            }
            ++size;
        }
        void _copy(const Deque& d)
        {
            NodeType* node = d.head;
            while (node)
            {
                NodeType* newNode = new NodeType(*node);
                _insert(newNode);
                node = node->next;
            }
        }
        void _destroy()
        {
            NodeType* node = head;
            while (node)
            {
                NodeType* p = node->next;
                delete node;
                node = p;
            }
            head = tail = nullptr;
            size = 0;
        }
    private:
        int size;
        NodeType* head;
        NodeType* tail;
    };
}

#endif

测试代码:

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

#include "stdafx.h"
#include "Deque.h"
#include <iostream>
#include <deque>

using namespace std;

using namespace MyDataStructure;

int _tmain(int argc, _TCHAR* argv[])
{
    Deque<int> d;
    for (int i = 0; i < 10; ++i)
    {
        d.Insert(i);
    }
    Deque<int> d2;
    Deque<int> d1 = d2 = d;

    int value;
    for (int i = 0; i < 10; ++i)
    {
        if (d1.Eject(value))
            cout << value << ‘ ‘;
    }
    cout << endl;
    for (int i = 0; i < 10; ++i)
    {
        if (d2.Eject(value))
            cout << value << ‘ ‘;
    }
    cout << endl;
    for (int i = 0; i < 10; ++i)
    {
        if (d.Eject(value))
            cout << value << ‘ ‘;
    }
    cout << endl;
    cout << d.Size() << endl;
    d.Clear();
    d1.Clear();
    d2.Clear();
    return 0;
}

运行结果:
技术分享
本次写博客的经验是:1)以后注释直接写在源文件里,不要到了写博客的时候才写在博文里。2)如果你是写了一半保存在线上,下次写完发表时,请先保存一下再发表,不然如果马上再来修改已经发表的文章的时候,只有之前的一半内容。已被坑死!

每日一题22:Deque与Trait

标签:模板   deque   c++   

原文地址:http://blog.csdn.net/liao_jian/article/details/45055887

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!