StRoot
1
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
RTS
include
UNIX
simpleQ2.hh
1
#ifndef SIMPLEQ2_HH
2
#define SIMPLEQ2_HH
3
// Same as simpleQ, except the size of the queue is not part of the template
4
// but rather set in the constructor!
5
#include <stdlib.h>
6
#include <sys/types.h>
7
8
template
<
class
T>
class
simpleQ2
9
{
10
public
:
11
simpleQ2
(
int
sz);
12
~
simpleQ2
();
13
14
void
clear();
15
int
put(T* a) ;
// returns 0 on success -1 if list is full (end of list)
16
int
prepend(T* a) ;
// beginning of list
17
int
get
(T* a) ;
// like first, but removes element
18
int
query(T* a) ;
19
20
int
entries()
// number of entries in List
21
{
22
return
in;
23
};
24
25
int
free()
// returns number of free entries
26
{
27
return
(S - in) ;
28
};
29
30
T* element(
int
i)
31
{
32
if
(i < S)
return
(&store[i]) ;
33
return
(NULL) ;
34
};
35
36
//private:
37
int
S;
38
T *store ;
// array of allocated buffers
39
int
f ;
// index to first
40
int
l;
41
int
in;
42
};
43
44
//***********************************************
45
template
<
class
T>
simpleQ2<T>::simpleQ2
(
int
sz)
46
{
47
f = l = 0 ;
48
in = 0 ;
49
S = sz;
50
store = NULL;
51
store = (T *)malloc(
sizeof
(T) * S);
52
};
53
54
template
<
class
T>
simpleQ2<T>::~simpleQ2
()
55
{
56
if
(store) ::free(store);
57
}
58
59
template
<
class
T>
void
simpleQ2<T>::clear
()
60
{
61
in = 0 ;
62
l = f = 0 ;
63
};
64
65
template
<
class
T>
int
simpleQ2<T>::put
(T* a)
// inserts at end
66
{
67
if
(in == S)
return
(-1) ;
68
in++ ;
69
store[l++] = *a ;
70
if
(l == S) l = 0 ;
71
return
(0) ;
72
};
73
74
template
<
class
T>
int
simpleQ2<T>::get
(T* a)
// removes first element of list
75
{
76
if
(!in)
return
(-1) ;
// empty
77
in-- ;
78
*a = store[f++] ;
79
if
(f == S) f = 0 ;
80
return
(0) ;
81
};
82
83
template
<
class
T>
int
simpleQ2<T>::prepend
(T* a)
// adds element at the front
84
{
85
if
(in == S)
return
(-1) ;
86
in++ ;
87
88
f--;
89
if
(f<0) f = S - 1;
90
store[f] = *a;
91
92
return
0;
93
};
94
95
template
<
class
T>
int
simpleQ2<T>::query
(T* a)
96
{
97
int
i=f;
98
99
while
(i != l)
100
{
101
if
(memcmp(a,&store[i],
sizeof
(T)) == 0)
return
1;
102
i++;
103
if
(i==S) i=0;
104
}
105
106
return
0;
107
};
108
109
#endif
110
111
112
113
114
115
116
simpleQ2
Definition:
simpleQ2.hh:8
Generated by
1.8.5