Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

bqueuet.cc

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------
00002  *
00003  * (C) Copyright Koninklijke Philips Electronics NV 2006. 
00004  * All rights reserved. This software is licensed under the terms of
00005  * version 2.1 of the GNU Lesser General Public License as published 
00006  * by the Free Software Foundation. For licensing and warranty
00007  * information, see the file COPYING in the main directory.
00008  *
00009  *------------------------------------------------------------------*/
00010 
00011 #ifndef BQUEUET_CC
00012 #define BQUEUET_CC
00013 
00014 #include <minmax.h>
00015 #include <assert.h>
00016 
00017 template<class T> inline
00018 bqueueT<T>::bqueueT(unsigned int sz)
00019 {
00020   begin = new T [sz];
00021   end   = begin + sz;
00022   r = w = begin;
00023   d = 0;
00024 }
00025 
00026 template<class T> inline
00027 bqueueT<T>::~bqueueT()
00028 {
00029   delete [] begin;
00030 }
00031 
00032 template<class T> inline
00033 unsigned int bqueueT<T>::size() const
00034 {
00035   return end - begin;
00036 }
00037 
00038 template<class T> inline
00039 unsigned int bqueueT<T>::data() const
00040 {
00041   return d;
00042 }
00043 
00044 template<class T> inline
00045 unsigned int bqueueT<T>::room() const
00046 {
00047   return size() - data();
00048 }
00049 
00050 
00051 template<class T> inline
00052 void bqueueT<T>::put(const T& value)
00053 {
00054   *w = value;
00055   if (++w == end) w = begin;
00056   d++;
00057 }
00058 
00059 template<class T> inline
00060 void bqueueT<T>::get(T& value)
00061 {
00062   value = *r;
00063   if (++r == end) r = begin;
00064   d--;
00065 }
00066 
00067 template<class T> inline
00068 void bqueueT<T>::put(const T* p, unsigned int n)
00069 {
00070   unsigned int a = end - w;
00071   if (n <= a) {
00072     copy(p, p+n, w);
00073     w += n;
00074     if (w == end) w = begin;
00075   } else {
00076     unsigned int b = n - a;
00077     copy(p,   p+a, w);
00078     copy(p+a, p+n, begin);
00079     w = begin + b;
00080   }
00081   d += n;
00082 }
00083 
00084 template<class T> inline
00085 void bqueueT<T>::get(T* p, unsigned int n)
00086 {
00087   unsigned int a = end - r;
00088   if (n <= a) {
00089     copy(r, r+n, p);
00090     r += n;
00091     if (r == end) r = begin;
00092   } else {
00093     unsigned int b = n - a;
00094     copy(r,     end,     p);
00095     copy(begin, begin+b, p+a);
00096     r = begin + b;
00097   }
00098   d -= n;
00099 }
00100 
00101 template<class T> inline
00102 void bqueueT<T>::resize(unsigned int sz)
00103 {
00104   assert(d <= sz);
00105   T* p = new T [sz];
00106   unsigned int a = end - r;
00107   if (d <= a) {
00108     copy(r, r+d, p);
00109   } else {
00110     unsigned int b = d - a;
00111     copy(r,     end,     p);
00112     copy(begin, begin+b, p+a);
00113   }
00114   delete [] begin;
00115   begin = p;
00116   r     = p;
00117   w     = p + d;
00118   end   = p + sz;
00119 }
00120 
00121 #endif

Generated on Wed Feb 15 14:52:38 2006 for yapi by doxygen 1.3.2