circular_queue.h
Go to the documentation of this file.
00001 
00023 #ifndef CIRCULAR_QUEUE_H_
00024 #define CIRCULAR_QUEUE_H_
00025    
00026 #include <iostream>   
00027 
00028 namespace micros_swarm{
00029     template <class Type>  
00030     class cqueue  
00031     {
00032         public:
00033             cqueue()
00034             {
00035                 tail_ = 0;
00036                 head_ = 0;
00037                 capacity_ = 0;
00038                 data_ = NULL;
00039             }
00040             
00041             cqueue(int capacity)
00042             {
00043                 tail_ = 0;
00044                 head_ = 0;
00045                 capacity_ = capacity;
00046                 data_ = new Type[capacity_];
00047             }
00048             
00049             cqueue(const cqueue& c)
00050             {
00051                 tail_ = c.tail_;
00052                 head_ = c.head_;
00053                 capacity_ = c.capacity_;
00054                 data_ = new Type[capacity_];
00055                 memcpy(data_, c.data_, sizeof(Type)*c.length());
00056             }
00057             
00058             cqueue& operator=(const cqueue& c)
00059             {
00060                 if(this == &c) {
00061                     return *this;
00062                 }
00063                 delete [] data_;
00064                 tail_ = c.tail_;
00065                 head_ = c.head_;
00066                 capacity_ = c.capacity_;
00067                 data_ = new Type[capacity_];
00068                 memcpy(data_, c.data_, sizeof(Type)*c.length());
00069                 return *this;
00070             }
00071             
00072             ~cqueue()
00073             {
00074                 delete []data_;
00075             }
00076             
00077             bool empty()
00078             {
00079                 if (head_ == tail_) {
00080                     return true;
00081                 }
00082                 else {
00083                     return false;
00084                 }
00085             }
00086             
00087             bool full()
00088             {
00089                  if ((tail_+1)%capacity_ == head_) {
00090                      return true;
00091                  }
00092                  else {
00093                      return false;
00094                  }
00095             }
00096             
00097             void push(Type x)
00098             {
00099                 if (!full()) {
00100                     data_[tail_] = x;
00101                     tail_ = (tail_+1)%capacity_;
00102                 }
00103                 else {
00104                     std::cout<<"cqueue is full."<<std::endl;
00105                 }
00106             }
00107             
00108             void pop()
00109             {
00110                 if (!empty()) {
00111                     head_ = (head_ + 1) % capacity_;
00112                 }
00113                 else {
00114                     std::cout<<"cqueue is empty."<<std::endl;
00115                 }
00116             }
00117             
00118             const Type& front()
00119             {
00120                 return data_[head_];
00121             }
00122             
00123             int size()
00124             {
00125                 return (tail_-head_+capacity_)%capacity_;
00126             }
00127             
00128             void print()
00129             {
00130                 int cnt = head_;
00131                 if (head_ > tail_) {
00132                     tail_ += capacity_;
00133                 }
00134                 while(cnt <= tail_-1) {
00135                     std::cout << data_[cnt] << " ";
00136                     cnt++;
00137                 }
00138                std::cout<<std::endl;
00139             }
00140         private:
00141             Type *data_;
00142             int capacity_;
00143             int tail_;
00144             int head_;
00145     };   
00146 };
00147    
00148 #endif 


micros_swarm
Author(s):
autogenerated on Thu Jun 6 2019 18:52:14