StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StaticSizedDQueue.hh
1 #ifndef STATICSIZEDDQUEUE_HH
2 #define STATICSIZEDDQUEUE_HH
3 // a static allocated dqueue
4 // values are copied into the list
5 // T should have a copy constructor.
6 // insertion at end of queue is via insert.
7 // at front of queue via prepend.
8 // get copies next element into a and removes the first element
9 // first returns pointer to first element NO REMOVAL
10 // clear is clear
11 // entries returns the number of entries in the list
12 // free e returns the number of free elements
13 
14 // Modified JML 3/11/99
15 // Removed size from template, added to constructor
16 #include <sys/types.h>
17 template <class T> class sdqueue
18 {
19 public:
20  sdqueue(int s);
21  ~sdqueue();
22  void clear() ;
23  int insert(T* a) ; // returns 0 on success -1 if list is full (end of list)
24  int prepend(T* a) ; // beginning of list
25  int first(T* a) ; // returns (but doesn't remove) first element
26  int get(T* a) ; // like first, but removes element
27  u_int entries(); // number of entries in List
28 
29  u_int free() // returns number of free entries
30  {
31  return(max - in) ;
32  };
33 
34  T* element(int i)
35  {
36  if(i < max) return(&store[i]) ;
37  return(NULL) ;
38  };
39 
40 private:
41  T *store ; // array of allocated buffers
42  u_int f ; // index to first
43  u_int l ;
44  u_int in ;
45  u_int max ;
46  int s;
47 };
48 
49 //***********************************************
50 template <class T> sdqueue<T>::~sdqueue()
51 {
52  f = l = 0 ;
53  in = 0 ; max = 0 ;
54  delete [] store;
55 };
56 
57 template <class T> sdqueue<T>::sdqueue(int size)
58 {
59  f = l = 0 ;
60  in = 0 ; max = size ;
61  s = size;
62  store = new T[s];
63 };
64 
65 template <class T> void sdqueue<T>::clear()
66 {
67  in = 0 ;
68  l = f = 0 ;
69 };
70 
71 template <class T> u_int sdqueue<T>::entries()
72 {
73  return(in) ;
74 };
75 
76 template <class T> int sdqueue<T>::insert(T* a) // inserts at end
77 {
78  if(in == max) return(-1) ;
79  in++ ;
80  store[l++] = *a ;
81  if(l == max) l = 0 ;
82  return(0) ;
83 };
84 
85 template <class T> int sdqueue<T>::get(T* a) // removes first element of list
86 {
87  if(!in)return(-1) ; // empty
88  in-- ;
89  *a = store[f++] ;
90  if(f == max) f = 0 ;
91  return(0) ;
92 };
93 
94 template <class T> int sdqueue<T>::first(T* a) // returns first or null
95 {
96  if(!in) return -1;
97  *a = store[f];
98  return 0;
99 };
100 
101 template <class T> int sdqueue<T>::prepend(T* a) // adds element at the front
102 {
103  if(in == max) return(-1) ;
104  in++ ;
105 
106  if(in == 1)
107  { // empty list
108  store[f] = *a ;
109  l++ ;
110  if(l == max) l = 0 ;
111  return(0) ;
112  }//end empty list
113  // list is not empty
114  f-- ;
115  if(f < 0) f = max - 1 ;
116  store[f] = *a ;
117  return(0) ;
118 };
119 
120 #endif
121 
122 
123 
124