ring.inc

Go to the documentation of this file.
00001 
00029 #ifndef ABA_RING_INC
00030 #define ABA_RING_INC
00031 
00032   template <class Type>
00033   inline ABA_RING<Type>::ABA_RING(ABA_GLOBAL *glob, int size)
00034   :  
00035     glob_(glob),  
00036     ring_(glob, size),  
00037     head_(0),  
00038     filled_(false)
00039   { }
00040  
00041   template <class Type> 
00042   inline ABA_RING<Type>::~ABA_RING()
00043   {
00044   }
00045 
00046   template <class Type>
00047   ostream &operator<<(ostream &out, const ABA_RING<Type> &rhs)
00048   {
00049     
00050     if(rhs.filled_) {
00051       const int s = rhs.size();
00052 
00053       for(int i = rhs.head_; i < s; i++)
00054         out << rhs.ring_[i] << " ";
00055     }
00056 
00057     for (int i = 0; i < rhs.head_; i++)
00058         out << rhs.ring_[i] << " ";
00059 
00060     out << endl;
00061 
00062     return out;
00063   }
00064 
00065   template <class Type>
00066   inline Type& ABA_RING<Type>::operator[](int i)
00067   {
00068     return ring_[i];
00069   }
00070 
00071   template <class Type>
00072   inline const Type& ABA_RING<Type>::operator[](int i) const
00073   {
00074     return ring_[i];
00075   }
00076 
00077   template <class Type>
00078   void ABA_RING<Type>::insert(Type elem)
00079   {
00080     ring_[head_] = elem;
00081 
00082     if (++head_ == size()) {
00083       if (!filled_) filled_ = true;
00084       head_ = 0;
00085     }
00086   }
00087 
00088   template <class Type>
00089   inline void ABA_RING<Type>::clear()
00090   {
00091     head_ = 0;
00092     filled_ = false;
00093   }
00094 
00095   template <class Type>
00096   inline int ABA_RING<Type>::size() const
00097   {
00098     return ring_.size();
00099   }
00100 
00101   template <class Type>
00102   inline int ABA_RING<Type>::number() const
00103   {
00104     if (filled_) 
00105       return size();
00106     else
00107       return head_;
00108   }
00109 
00110   template <class Type>
00111   inline Type ABA_RING<Type>::oldest() const
00112   {
00113     if(filled_) return ring_[head_];
00114     else return ring_[0];
00115   }
00116 
00117   template <class Type>
00118   inline int ABA_RING<Type>::oldestIndex() const
00119   {
00120     if(filled_) return head_;
00121     else return 0;
00122   }
00123 
00124   template <class Type>
00125   inline Type ABA_RING<Type>::newest() const
00126   {
00127     if (head_)  return ring_[head_ - 1];
00128     else return ring_[size() - 1];
00129   }
00130 
00131   template <class Type>
00132   inline int ABA_RING<Type>::newestIndex() const
00133   {
00134     if (head_)  return head_ - 1;
00135     else return size() - 1;
00136   }
00137 
00138   template <class Type>
00139   int ABA_RING<Type>::previous(int i, Type &p) const
00140   {
00141     int j = head_ - 1 - i;
00142 
00143     if (j >= 0) {
00144       p = ring_[j];
00145       return 0;
00146     }
00147     else if (filled_) {
00148       p = ring_[size() + j];
00149       return 0;
00150     }
00151     else return 1;
00152   }
00153 
00154   template <class Type>
00155   inline bool ABA_RING<Type>::empty() const
00156   {
00157 #ifdef ABACUS_NO_BOOL
00158     if (head_ || filled_) return false;
00159     else                  return true;
00160 #else
00161     return !(head_ || filled_);
00162 #endif
00163   }
00164 
00165   template <class Type>
00166   inline bool ABA_RING<Type>::filled() const
00167   {
00168     return filled_;
00169   }
00170 
00171   template <class Type>
00172   void ABA_RING<Type>::realloc(int newSize)
00173   {
00174     ABA_ARRAY<Type> tmp = ring_;
00175     int oldSize = size();
00176     int oldHead = head_;
00177     int i;
00178 
00179     
00180     ring_.realloc(newSize);
00181 
00182     if(newSize > oldSize) {
00183       // increase ring
00184       /* If the ring is increased yet has not been filled, nothing has to be
00185        * done. Otherwise, the elements of the old ring are copied in the correct
00186        * order.
00187        */ 
00188       if (filled_) {
00189         head_ = 0;
00190         for(i = oldHead; i < oldSize; i++) ring_[head_++] = tmp[i];
00191         for(i = 0; i < oldHead; i++) ring_[head_++] = tmp[i];
00192         filled_ = false;
00193       }
00194 
00195     }
00196     else {
00197       // decrease ring
00198       /* If the ring is decreased and it is filled, then we copy the elements of the
00199        * old ring in the correct order. If the ring is not filled we only have
00200        * to copy the elements if by decreasing the ring elements are removed.
00201        */
00202       if (filled_) {
00203         for (head_ = size() - 1, i = oldHead - 1; head_ >= 0; head_--, i--) {
00204           if (i < 0) i = oldSize - 1;
00205           ring_[head_] = tmp[i];
00206         }
00207         head_ = 0;
00208       }
00209       else if (oldHead > size()) {
00210         for (head_ = size() - 1, i = oldHead - 1; head_ >= 0; --head_, --i)
00211           ring_[head_] = tmp[i];
00212         head_ = 0;
00213         filled_ = true;
00214       }
00215 
00216     
00217     }
00218   }
00219 
00220 
00221 #endif   // ABA_RING_INC

Generated on Tue Aug 14 18:09:54 2007 for ABACUS by  doxygen 1.5.1