00001 #ifndef STATICSIZEDDQUEUE_HH
00002 #define STATICSIZEDDQUEUE_HH
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <sys/types.h>
00017 template <class T> class sdqueue
00018 {
00019 public:
00020 sdqueue(int s);
00021 ~sdqueue();
00022 void clear() ;
00023 int insert(T* a) ;
00024 int prepend(T* a) ;
00025 int first(T* a) ;
00026 int get(T* a) ;
00027 u_int entries();
00028
00029 u_int free()
00030 {
00031 return(max - in) ;
00032 };
00033
00034 T* element(int i)
00035 {
00036 if(i < max) return(&store[i]) ;
00037 return(NULL) ;
00038 };
00039
00040 private:
00041 T *store ;
00042 u_int f ;
00043 u_int l ;
00044 u_int in ;
00045 u_int max ;
00046 int s;
00047 };
00048
00049
00050 template <class T> sdqueue<T>::~sdqueue()
00051 {
00052 f = l = 0 ;
00053 in = 0 ; max = 0 ;
00054 delete [] store;
00055 };
00056
00057 template <class T> sdqueue<T>::sdqueue(int size)
00058 {
00059 f = l = 0 ;
00060 in = 0 ; max = size ;
00061 s = size;
00062 store = new T[s];
00063 };
00064
00065 template <class T> void sdqueue<T>::clear()
00066 {
00067 in = 0 ;
00068 l = f = 0 ;
00069 };
00070
00071 template <class T> u_int sdqueue<T>::entries()
00072 {
00073 return(in) ;
00074 };
00075
00076 template <class T> int sdqueue<T>::insert(T* a)
00077 {
00078 if(in == max) return(-1) ;
00079 in++ ;
00080 store[l++] = *a ;
00081 if(l == max) l = 0 ;
00082 return(0) ;
00083 };
00084
00085 template <class T> int sdqueue<T>::get(T* a)
00086 {
00087 if(!in)return(-1) ;
00088 in-- ;
00089 *a = store[f++] ;
00090 if(f == max) f = 0 ;
00091 return(0) ;
00092 };
00093
00094 template <class T> int sdqueue<T>::first(T* a)
00095 {
00096 if(!in) return -1;
00097 *a = store[f];
00098 return 0;
00099 };
00100
00101 template <class T> int sdqueue<T>::prepend(T* a)
00102 {
00103 if(in == max) return(-1) ;
00104 in++ ;
00105
00106 if(in == 1)
00107 {
00108 store[f] = *a ;
00109 l++ ;
00110 if(l == max) l = 0 ;
00111 return(0) ;
00112 }
00113
00114 f-- ;
00115 if(f < 0) f = max - 1 ;
00116 store[f] = *a ;
00117 return(0) ;
00118 };
00119
00120 #endif
00121
00122
00123
00124