Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArRingQueue.h

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 
00028 
00029 #ifndef _AR_RING_QUEUE_H_
00030 #define _AR_RING_QUEUE_H_
00031 
00032 #include <iostream>
00033 #include <list>
00034 #include <ariaTypedefs.h>
00035 
00051 template<class T> 
00052 class ArRingQueue {
00053 public:
00057   ArRingQueue(int capacity, T init_value)
00058     : ring(capacity, init_value), curSize(0), initval(init_value)
00059   {
00060     back_it = ring.begin(); 
00061     front_it = ring.end();// signals empty state
00062   }
00063 
00064 
00073   typename std::list<T>::iterator front() {
00074     if(empty())
00075       return nil();
00076     return front_it;
00077   }
00078 
00090   typename std::list<T>::iterator back() {
00091     if(front_it == back_it)
00092     {
00093       std::cerr << "ArRingQueue: back(): 0-capacity or full, returning nil.\n";
00094       return nil();
00095     }
00096     return back_it;
00097   }
00098 
00100   void advance_front() {
00101     if(front_it == ring.end())  // initial or  empty state.
00102       front_it = ring.begin();
00103     else if(++front_it == ring.end()) 
00104       front_it = ring.begin();
00105     if(front_it == back_it) { // it's now empty (not full)
00106       front_it = ring.end();
00107       back_it = ring.begin();
00108     }
00109     curSize--;
00110   }
00111 
00112 
00114   void advance_back() {
00115     if(front_it == back_it) // full or 0-capacity
00116     {
00117       std::cerr << "ArRingQueue: advance_back(): queue is full, can't advance back.\n";
00118       return;
00119     }
00120     if(++back_it == ring.end())
00121       back_it = ring.begin();
00122     if(front_it == ring.end())
00123       front_it = ring.begin();  // no longer empty.
00124     curSize++;
00125   }
00126 
00130   void push(const T& item) {
00131     if(full()) {
00132       back_it = ring.insert(back_it, item);
00133     } else {
00134       *back_it = item;
00135     }
00136     advance_back();
00137   }
00138 
00140   void push_back(const T& item) { push(item); }
00141 
00145   void push_without_expanding(const T& item) {
00146     if(full())
00147       advance_front();
00148     push(item);
00149   }
00150 
00153   T pop() {
00154     typename std::list<T>::iterator thing = front();
00155     if(front() != nil())
00156       advance_front();
00157     return (*thing);
00158   }
00159 
00161   T pop_front() { return pop(); }
00162 
00164   void print() {
00165     for(typename std::list<T>::const_iterator i = ring.begin(); i != ring.end(); i++) {
00166       if(i == back_it)
00167         std::cerr << "]";
00168       if(i == front_it || (i == ring.begin() && front_it == ring.end()) )
00169         std::cerr << "[";
00170       std::cerr << (*i) << "," ;
00171     }
00172     std::cerr << std::endl;
00173   }
00174 
00176   size_t size() {
00177     return curSize;
00178   }
00179 
00181   size_t capacity() {
00182     return ring.size();
00183   }
00184 
00186   bool empty() {
00187     return (front_it == ring.end());
00188   }
00189 
00192   void reset() {
00193     front_it = ring.end();
00194     back_it = ring.begin();
00195     curSize = 0;
00196   }
00197 
00199   bool full() {
00200     return (back_it == front_it);
00201   }
00202 
00205   typename std::list<T>::iterator nil() {
00206     return ring.end();
00207   }
00208 
00209 protected:
00210   std::list<T> ring;
00211   typename std::list<T>::iterator front_it, back_it;   
00212   // push to back, pop from front; front will point to first item, 
00213   // back to one past last. 
00214 
00215   size_t curSize;
00216   T initval;
00217 
00218 };
00219 
00220 
00221 
00222 #endif

Generated on Wed Oct 19 12:56:36 2005 for Aria by  doxygen 1.4.0