00001 /* 00002 * CircularQueue.h : part of the Mace toolkit for building distributed systems 00003 * 00004 * Copyright (c) 2007, Charles Killian, Dejan Kostic, Ryan Braud, James W. Anderson, John Fisher-Ogden, Calvin Hubble, Duy Nguyen, Justin Burke, David Oppenheimer, Amin Vahdat, Adolfo Rodriguez, Sooraj Bhat 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the names of Duke University nor The University of 00017 * California, San Diego, nor the names of the authors or contributors 00018 * may be used to endorse or promote products derived from 00019 * this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00027 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00029 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00030 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 * 00032 * ----END-OF-LEGAL-STUFF---- */ 00033 #ifndef CIRCULAR_QUEUE_H 00034 #define CIRCULAR_QUEUE_H 00035 00058 template<class T> 00059 class CircularQueue { 00060 public: 00061 static const size_t DEFAULT_MAX_SIZE = 4096; 00062 00063 public: 00064 CircularQueue(size_t len = DEFAULT_MAX_SIZE) : 00065 MAX_SIZE(len), data(0), first(0), last(0) { 00066 data = new T*[MAX_SIZE]; 00067 } // CircularQueue 00068 00069 virtual ~CircularQueue() { 00070 while (!empty()) { 00071 pop(); 00072 } 00073 delete [] data; 00074 } // ~CircularQueue 00075 00077 virtual uint64_t size() const { 00078 if (last >= first) { 00079 return last - first; 00080 } 00081 else { 00082 return MAX_SIZE - first + last; 00083 } 00084 } // size 00085 00087 virtual bool isFull() const { 00088 return (((last + 1) % MAX_SIZE) == first); 00089 } // isFull 00090 00092 virtual bool empty() const { 00093 return (first == last); 00094 } // empty 00095 00097 virtual T& front() const { 00098 assert(!empty()); 00099 return *(data[first]); 00100 } // front 00101 00103 virtual void pop() { 00104 assert(!empty()); 00105 T* v = data[first]; 00106 first = (first + 1) % MAX_SIZE; 00107 delete v; 00108 } // pop 00109 00111 virtual void push(const T& v) { 00112 assert(!isFull()); 00113 data[last] = new T(v); 00114 last = (last + 1) % MAX_SIZE; 00115 } // push 00116 00118 virtual void clear() { 00119 while (!empty()) { 00120 pop(); 00121 } 00122 } // clear 00123 00124 public: 00125 const size_t MAX_SIZE; 00126 00127 protected: 00128 T** data; 00129 size_t first; 00130 size_t last; 00131 00132 }; // CircularQueue 00133 00136 #endif // CIRCULAR_QUEUE_H