#pragma once /**************************************************************************** Copyright (c) 2006, Radon Labs GmbH Copyright (c) 2011-2013,WebJet Business Division,CYOU http://www.genesis-3d.com.cn Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "core/types.h" #include "util/array.h" #include #include //------------------------------------------------------------------------------ namespace Util { template class Queue { public: /// constructor Queue(); /// copy constructor Queue(const Queue& rhs); /// assignment operator void operator=(const Queue& rhs); /// access element by index, 0 is the frontmost element (next to be dequeued) TYPE& operator[](IndexT index) const; /// equality operator bool operator==(const Queue& rhs) const; /// inequality operator bool operator!=(const Queue& rhs) const; /// increase capacity to fit N more elements into the queue void Reserve(SizeT num); /// returns number of elements in the queue SizeT Size() const; /// return true if queue is empty bool IsEmpty() const; /// remove all elements from the queue void Clear(); /// return true if queue contains element bool Contains(const TYPE& e) const; /// add element to the back of the queue void Enqueue(const TYPE& e); /// remove the element from the front of the queue TYPE Dequeue(); /// access to element at front of queue without removing it TYPE& Peek() const; protected: Array queueArray; }; // queue的性能较差。用Stl::deque 重新实现一份 template > class StlQueue { public: /// constructor StlQueue(); /// copy constructor StlQueue(const StlQueue& rhs); /// assignment operator void operator=(const StlQueue& rhs); /// access element by index, 0 is the frontmost element (next to be dequeued) const TYPE& operator[](IndexT index) const; TYPE& operator[](IndexT index); /// equality operator bool operator==(const StlQueue& rhs) const; /// inequality operator bool operator!=(const StlQueue& rhs) const; /// increase capacity to fit N more elements into the queue void Reserve(SizeT num); /// returns number of elements in the queue SizeT Size() const; /// return true if queue is empty bool IsEmpty() const; /// remove all elements from the queue void Clear(); /// return true if queue contains element bool Contains(const TYPE& e) const; /// add element to the back of the queue void Enqueue(const TYPE& e); /// remove the element from the front of the queue TYPE Dequeue(); /// access to element at front of queue without removing it const TYPE& Peek() const; TYPE& Peek(); protected: _Container queueArray; }; //------------------------------------------------------------------------------ /** */ template Queue::Queue() { // empty } template StlQueue::StlQueue() { // empty } //------------------------------------------------------------------------------ /** */ template Queue::Queue(const Queue& rhs) { this->queueArray = rhs.queueArray; } template StlQueue::StlQueue(const StlQueue& rhs) { this->queueArray = rhs.queueArray; } //------------------------------------------------------------------------------ /** */ template void Queue::operator=(const Queue& rhs) { this->queueArray = rhs.queueArray; } template void StlQueue::operator=(const StlQueue& rhs) { this->queueArray = rhs.queueArray; } //------------------------------------------------------------------------------ /** */ template TYPE& Queue::operator[](IndexT index) const { return this->queueArray[index]; } template const TYPE& StlQueue::operator[](IndexT index) const { #if NEBULA3_BOUNDSCHECKS return this->queueArray.at(index); #else return this->queueArray[index]; #endif } template TYPE& StlQueue::operator[](IndexT index) { #if NEBULA3_BOUNDSCHECKS return this->queueArray.at(index); #else return this->queueArray[index]; #endif } //------------------------------------------------------------------------------ /** */ template bool Queue::operator==(const Queue& rhs) const { return this->queueArray == rhs.queueArray; } template bool StlQueue::operator==(const StlQueue& rhs) const { return this->queueArray == rhs.queueArray; } //------------------------------------------------------------------------------ /** */ template bool Queue::operator!=(const Queue& rhs) const { return this->queueArray != rhs.queueArray; } template bool StlQueue::operator!=(const StlQueue& rhs) const { return this->queueArray != rhs.queueArray; } //------------------------------------------------------------------------------ /** */ template bool Queue::Contains(const TYPE& e) const { return (InvalidIndex != this->queueArray.FindIndex(e)); } template bool StlQueue::Contains(const TYPE& e) const { return std::find( this->queueArray.begin(), this->queueArray.end(), e ) != this->queueArray.end(); } //------------------------------------------------------------------------------ /** */ template void Queue::Clear() { this->queueArray.Clear(); } template void StlQueue::Clear() { this->queueArray.clear(); } //------------------------------------------------------------------------------ /** */ template void Queue::Reserve(SizeT num) { this->queueArray.Reserve(num); } template void StlQueue::Reserve(SizeT num) { // empty } //------------------------------------------------------------------------------ /** */ template SizeT Queue::Size() const { return this->queueArray.Size(); } template SizeT StlQueue::Size() const { return this->queueArray.size(); } //------------------------------------------------------------------------------ /** */ template bool Queue::IsEmpty() const { return this->queueArray.IsEmpty(); } template bool StlQueue::IsEmpty() const { return this->queueArray.empty(); } //------------------------------------------------------------------------------ /** */ template void Queue::Enqueue(const TYPE& e) { this->queueArray.Append(e); } template void StlQueue::Enqueue(const TYPE& e) { this->queueArray.push_back(e); } //------------------------------------------------------------------------------ /** */ template TYPE Queue::Dequeue() { TYPE e = this->queueArray.Front(); this->queueArray.EraseIndex(0); return e; } template TYPE StlQueue::Dequeue() { TYPE e = this->queueArray.front(); this->queueArray.pop_front(); return e; } //------------------------------------------------------------------------------ /** */ template TYPE& Queue::Peek() const { return this->queueArray.Front(); } template const TYPE& StlQueue::Peek() const { return this->queueArray.front(); } template TYPE& StlQueue::Peek() { return this->queueArray.front(); } } // namespace Util //------------------------------------------------------------------------------