-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQBfr.cpp
More file actions
executable file
·203 lines (172 loc) · 5.69 KB
/
QBfr.cpp
File metadata and controls
executable file
·203 lines (172 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
///////////////////////////////////////////////////////////////////////////////
// :mode=c:
/* QBfr.cpp */
///////////////////////////////////////////////////////////////////////////////
#include <inttypes.h> // data types, e.g. uint16_t
#include "QBfr.h"
#include "QTrace.h"
/**************************************************************************************/
// QBfr
/**************************************************************************************/
template<typename T> QBfr<T>::QBfr(int Size)
{
Init();
_Size= Size;
_Bfr= new T[_Size];
for (int i= 0 ; i < _Size ; i++)
{
_Bfr[i]= 0;
}
} // QBfr
/**************************************************************************************/
template<typename T> void QBfr<T>::Init()
{
_Index= 0;
_Wraparound= false;
} // Init
/**************************************************************************************/
template<typename T> void QBfr<T>::Clear()
{
_Index= 0;
_Wraparound= false;
for (int i= 0 ; i < _Size ; i++)
_Bfr[i]= 0;
} // Clear
/**************************************************************************************/
template<typename T> int QBfr<T>::Count()
{
int Result= (_Wraparound)?(_Size):(_Index);
return Result;
} // Count
/**************************************************************************************/
template<typename T> void QBfr<T>::Set(T Element)
{
_Bfr[_Index]= Element;
} // Set
/**************************************************************************************/
template<typename T> void QBfr<T>::Next()
{
if (_Index == (_Size-1))
{
_Wraparound= true;
_Index= 0;
}
else
_Index++;
Set(0); // Initialize head
} // Next
/**************************************************************************************/
template<typename T> void QBfr<T>::Put(T Element)
{
Set (Element);
Next();
} // Put
/**************************************************************************************/
template<typename T> T QBfr<T>::Get(int LookbackOffset)
{
ASSERT(LookbackOffset < _Size);
LookbackOffset= _min(LookbackOffset, _Size-1);
int i= _Index - LookbackOffset;
if (i < 0)
i+= _Size;
T Result= _Bfr[i];
return Result;
} // Get
/**************************************************************************************/
/* Explicit instantiation of all the supported types. Must be done here, after the
implementation code above, to avoid putting all implementation code in header files.
*/
/**************************************************************************************/
template class QBfr<float>;
template class QBfr<uint8_t>;
template class QBfr<uint16_t>;
template class QBfr<uint32_t>;
/**************************************************************************************/
// QQueue
/**************************************************************************************/
template<typename T> QQueue<T>::QQueue(int Size)
{
Init();
_Size= Size;
_Bfr= new T[_Size];
for (int i= 0 ; i < _Size ; i++)
_Bfr[i]= 0;
} // QQueue
/**************************************************************************************/
template<typename T> void QQueue<T>::Init()
{
_HeadIndex= _TailIndex= 0;
_Enabled= true;
} // Init
/**************************************************************************************/
template<typename T> void QQueue<T>::Clear()
{
_HeadIndex= _TailIndex= 0;
for (int i= 0 ; i < _Size ; i++)
_Bfr[i]= 0;
} // Clear
/**************************************************************************************/
template<typename T> int QQueue<T>::Count()
{
int Cnt;
if (_TailIndex >= _HeadIndex)
Cnt= _TailIndex - _HeadIndex;
else
Cnt= (_Size - _HeadIndex) + _TailIndex;
return Cnt;
} // Count
/**************************************************************************************/
template<typename T> bool QQueue<T>::IsFull()
{
return (Count() >= (_Size - 1));
} // IsFull
/**************************************************************************************/
template<typename T> bool QQueue<T>::IsEmpty()
{
return (Count() == 0);
} // IsEmpty
/**************************************************************************************/
template<typename T> int QQueue<T>::Put(T Element)
{
int Result= 0;
if (_Enabled && !IsFull())
{
_Bfr[_TailIndex]= Element;
_TailIndex= (_TailIndex + 1) % _Size;
}
else
Result= -1;
return Result;
} // Put
/**************************************************************************************/
template<typename T> T QQueue<T>::Peek()
{
T Result= 0;
ASSERT(Count() > 0);
if (Count() > 0)
{
Result= _Bfr[_HeadIndex];
}
return Result;
} // Peek
/**************************************************************************************/
template<typename T> T QQueue<T>::Get()
{
T Result= 0;
ASSERT(Count() > 0);
if (Count() > 0)
{
Result= _Bfr[_HeadIndex];
_HeadIndex= (_HeadIndex + 1) % _Size;
}
return Result;
} // Get
/**************************************************************************************/
/* Explicit instantiation of all the supported types. Must be done here, after the
implementation code above, to avoid putting all implementation code in header files.
*/
/**************************************************************************************/
template class QQueue<float>;
template class QQueue<uint8_t>;
template class QQueue<uint16_t>;
template class QQueue<uint32_t>;