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

ArRingQueue.h

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
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 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 
00027 
00028 #ifndef _AR_RING_QUEUE_H_
00029 #define _AR_RING_QUEUE_H_
00030 
00031 #include <iostream>
00032 #include <list>
00033 #include <ariaTypedefs.h>
00034 
00054 template<class T> 
00055 class ArRingQueue {
00056 public:
00060   ArRingQueue(int capacity, T init_value)
00061     : ring(capacity, init_value), curSize(0), initval(init_value)
00062   {
00063     back_it = ring.begin(); 
00064     front_it = ring.end();// signals empty state
00065   }
00066 
00067 
00076   typename std::list<T>::iterator front() {
00077     if(empty())
00078       return nil();
00079     return front_it;
00080   }
00081 
00093   typename std::list<T>::iterator back() {
00094     if(front_it == back_it)
00095     {
00096       std::cerr << "ArRingQueue: back(): 0-capacity or full, returning nil.\n";
00097       return nil();
00098     }
00099     return back_it;
00100   }
00101 
00103   void advance_front() {
00104     if(front_it == ring.end())  // initial or  empty state.
00105       front_it = ring.begin();
00106     else if(++front_it == ring.end()) 
00107       front_it = ring.begin();
00108     if(front_it == back_it) { // it's now empty (not full)
00109       front_it = ring.end();
00110       back_it = ring.begin();
00111     }
00112     curSize--;
00113   }
00114 
00115 
00117   void advance_back() {
00118     if(front_it == back_it) // full or 0-capacity
00119     {
00120       // debugging:
00121       /*
00122       if(empty()) {
00123         std::cerr << "ArRingQueue: advance_back(): queue is *empty*, can't advance back.\n";
00124         return;
00125       }
00126       std::cerr << "ArRingQueue: advance_back(): queue is full, can't advance back.\n";
00127       */
00128       return;
00129     }
00130     if(++back_it == ring.end())
00131       back_it = ring.begin();
00132     if(front_it == ring.end())
00133       front_it = ring.begin();  // no longer empty.
00134     curSize++;
00135   }
00136 
00140   void push(const T& item) {
00141     if(full()) {
00142       back_it = ring.insert(back_it, item);
00143     } else {
00144       *back_it = item;
00145     }
00146     advance_back();
00147   }
00148 
00150   void push_back(const T& item) { push(item); }
00151 
00155   void push_without_expanding(const T& item) {
00156     if(full())
00157       advance_front();
00158     push(item);
00159   }
00160 
00163   T pop() {
00164     typename std::list<T>::iterator thing = front();
00165     if(front() != nil())
00166       advance_front();
00167     return (*thing);
00168   }
00169 
00171   T pop_front() { return pop(); }
00172 
00176   void print() {
00177     for(typename std::list<T>::const_iterator i = ring.begin(); i != ring.end(); i++) {
00178       if(i == back_it)
00179         std::cerr << "]";
00180       if(i == front_it || (i == ring.begin() && front_it == ring.end()) )
00181         std::cerr << "[";
00182       std::cerr << (*i) << "," ;
00183     }
00184     std::cerr << std::endl;
00185   }
00186 
00188   size_t size() {
00189     return curSize;
00190   }
00191 
00193   size_t capacity() {
00194     return ring.size();
00195   }
00196 
00198   bool empty() {
00199     return (front_it == ring.end());
00200   }
00201 
00205   void reset() {
00206     front_it = ring.end();
00207     back_it = ring.begin();
00208     curSize = 0;
00209   }
00210 
00212   bool full() {
00213     return (back_it == front_it);
00214   }
00215 
00218   typename std::list<T>::iterator nil() {
00219     return ring.end();
00220   }
00221 
00222 protected:
00223   std::list<T> ring;
00224   typename std::list<T>::iterator front_it, back_it;   
00225   // push to back, pop from front; front will point to first item, 
00226   // back to one past last. 
00227 
00228   size_t curSize;
00229   T initval;
00230 
00231 };
00232 
00233 
00234 
00235 #endif

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0