实现一个高效的不可变队列,队列有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;
}