00001 #ifndef SIMPLEQ_HH
00002 #define SIMPLEQ_HH
00003
00004 #include <sys/types.h>
00005
00006 template <class T, int S> class simpleQ
00007 {
00008 public:
00009 simpleQ();
00010
00011 void clear();
00012 int put(T* a) ;
00013 int prepend(T* a) ;
00014 int get(T* a) ;
00015 int query(T* a) ;
00016
00017 int entries()
00018 {
00019 return in;
00020 };
00021
00022 int free()
00023 {
00024 return(S - in) ;
00025 };
00026
00027 T* element(int i)
00028 {
00029 if(i < S) return(&store[i]) ;
00030 return(NULL) ;
00031 };
00032
00033
00034 T store[S] ;
00035 int f ;
00036 int l;
00037 int in;
00038 };
00039
00040
00041 template <class T, int S> simpleQ<T,S>::simpleQ()
00042 {
00043 f = l = 0 ;
00044 in = 0 ;
00045 };
00046
00047 template <class T, int S> void simpleQ<T,S>::clear()
00048 {
00049 in = 0 ;
00050 l = f = 0 ;
00051 };
00052
00053 template <class T, int S> int simpleQ<T,S>::put(T* a)
00054 {
00055 if(in == S) return(-1) ;
00056 in++ ;
00057 store[l++] = *a ;
00058 if(l == S) l = 0 ;
00059 return(0) ;
00060 };
00061
00062 template <class T, int S> int simpleQ<T,S>::get(T* a)
00063 {
00064 if(!in)return(-1) ;
00065 in-- ;
00066 *a = store[f++] ;
00067 if(f == S) f = 0 ;
00068 return(0) ;
00069 };
00070
00071 template <class T, int S> int simpleQ<T,S>::prepend(T* a)
00072 {
00073 if(in == S) return(-1) ;
00074 in++ ;
00075
00076 f--;
00077 if(f<0) f = S - 1;
00078 store[f] = *a;
00079
00080 return 0;
00081 };
00082
00083 template <class T, int S> int simpleQ<T,S>::query(T* a)
00084 {
00085 int i=f;
00086
00087 while(i != l)
00088 {
00089 if(memcmp(a,&store[i],sizeof(T)) == 0) return 1;
00090 i++;
00091 if(i==S) i=0;
00092 }
00093
00094 return 0;
00095 };
00096
00097 #endif
00098
00099
00100
00101
00102
00103
00104