MCQueue.h

Go to the documentation of this file.
00001 #ifndef MCQUEUE_H
00002 #define MCQUEUE_H
00003 
00004 #include "MercuryVector.h"
00005 
00006 template<typename T>
00007 class MCQueue
00008 {
00009 public:
00010     MCQueue(unsigned long size)
00011         :m_tail(0), m_head(0)
00012     {
00013         m_queue.resize(size);
00014     };
00015 
00016     bool push_back(const T& x)
00017     {
00018         if (!isFull())
00019         {
00020             //write then move
00021             m_queue[m_tail].data = x;
00022             m_queue[m_tail].valid = true;
00023             m_tail = nextItem(m_tail);
00024             return true;
00025         }
00026         return false;
00027     }
00028 
00029     T& front()
00030     {
00031         return m_queue[m_head].data;
00032     }
00033 
00034     bool pop_front()
00035     {
00036         if (!isEmpty())
00037         {
00038             m_queue[nextItem(m_head)].valid = false;
00039             m_head = nextItem(m_head);
00040         }
00041         return false;
00042     }
00043 
00044     inline bool isFull() { return (m_tail == m_head)&&m_queue[nextItem(m_head)].valid; }
00045     inline bool isEmpty() { return (m_tail == m_head)&&(!m_queue[nextItem(m_head)].valid); }
00046 private:
00047     struct status
00048     {
00049         status()
00050             :valid(false)
00051         {}
00052         T data;
00053         bool valid;
00054     };
00055     inline unsigned long nextItem(unsigned long i) { return ++i%m_queue.size(); }
00056     unsigned long m_tail;
00057     unsigned long m_head;
00058     MVector< status > m_queue;
00059 };
00060 
00061 #endif
00062 
00063 /* 
00064  * Copyright (c) 2007 Joshua Allen
00065  * All rights reserved.
00066  *
00067  * Redistribution and use in source and binary forms, with or
00068  * without modification, are permitted provided that the following
00069  * conditions are met:
00070  *  -   Redistributions of source code must retain the above
00071  *      copyright notice, this list of conditions and the following disclaimer.
00072  *  -   Redistributions in binary form must reproduce the above copyright
00073  *      notice, this list of conditions and the following disclaimer in
00074  *      the documentation and/or other materials provided with the distribution.
00075  *  -   Neither the name of the Mercury Engine nor the names of its
00076  *      contributors may be used to endorse or promote products derived from
00077  *      this software without specific prior written permission.
00078  *
00079  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00080  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00081  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00082  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00083  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00084  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00085  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00086  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00087  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00088  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00089  */

Hosted by SourceForge.net Logo