• 不可变队列


        实现一个高效的不可变队列,队列有enqueue和dequeue,分别从队列push、pop一个元素,并返回新队列。原有队列保持不变。

    template<typename T>
    class const_queue
    {
    public:
        const_queue()
            : m_nFrontIdx(0),
            m_nEndIdx(0),
            m_pdqData(new deque<T>())
        {
     
        }
     
        const_queue<T> enqueue(T t) const
        {
            // Share data.
            if (m_pdqData->size() == m_nEndIdx)
            {
                const_queue q(*this);
     
                q.m_pdqData->push_back(t);
                q.m_nFrontIdx = m_nFrontIdx;
                q.m_nEndIdx = m_nEndIdx + 1;
     
                return q;
            }
            // Copy data, share failed.
            else
            {
                const_queue q;
                deque<T> dq(
                    m_pdqData->begin() + m_nFrontIdx, 
                    m_pdqData->begin() + m_nEndIdx);
     
                q.m_pdqData->swap(dq);
                q.m_pdqData->push_back(t);
                q.m_nFrontIdx = 0;
                q.m_nEndIdx = m_nEndIdx - m_nFrontIdx + 1;
     
                return q;
            }
        }
     
        const_queue<T> dequeue() const
        {
            // Share data.
            const_queue q(*this);
     
            q.m_nFrontIdx = m_nFrontIdx + 1;
            q.m_nEndIdx = m_nEndIdx;
     
            return q;
        }
     
        T peek() const
        {
            if (!isEmpty())
            {
                return m_pdqData->at(m_nFrontIdx);
            }
     
            throw "const_queue is empty";
        }
     
        size_t size() const
        {
            if (!isEmpty())
            {
                return m_nEndIdx - m_nFrontIdx;
            }
     
            return 0;
        }
     
        bool isEmpty() const
        {
            return m_nEndIdx <= m_nFrontIdx;
        }
     
    private:
        size_t m_nFrontIdx;
        size_t m_nEndIdx;
        shared_ptr<deque<T> > m_pdqData;
    };
     
    void test()
    {
        const_queue<int> q;
     
        const_queue<int> q1 = q.enqueue(1);
        cout << q1.size() << " " << q1.peek() << endl;
        const_queue<int> q2 = q.enqueue(2);
        cout << q2.size() << " " << q2.peek() << endl;
        const_queue<int> q3 = q1.enqueue(3);
        cout << q3.size() << " " << q3.peek() << endl;
        const_queue<int> q4 = q3.dequeue();
        cout << q4.size() << " " << q4.peek() << endl;
        const_queue<int> q5 = q2.enqueue(4);
        cout << q5.size() << " " << q5.peek() << endl;
     
        cout << q1.size() << " " << q1.peek() << endl;
        cout << q2.size() << " " << q2.peek() << endl;
        cout << q3.size() << " " << q3.peek() << endl;
        cout << q4.size() << " " << q4.peek() << endl;
        cout << q5.size() << " " << q5.peek() << endl;
    }
  • 相关阅读:
    mybatis与spring的整合(代码实现)
    使用maven构建一个web项目
    解决maven 找不到指定路径应该如何
    建一个maven项目
    spring xml的配置
    mybatis.xml和mapper.xml的配置
    mvc @helper 创建用户自定义html
    sqlserver CLR sqlserver使用C# dll
    索引碎片
    压缩数据库
  • 原文地址:https://www.cnblogs.com/codingmylife/p/2710898.html
Copyright © 2020-2023  润新知