2
2
// Created by robos on 30/06/2022.
3
3
//
4
4
5
- #include " dynamicFifo .h"
5
+ #include " DynamicFifo .h"
6
6
#include " Vector.h"
7
7
8
8
9
9
10
10
template <class T >
11
- Fifo <T>::Fifo (int s) {
11
+ DynamicFifo <T>::DynamicFifo (int s) {
12
12
elem = new T[s]; // allocates on the free store (the heap)
13
13
sz = s;
14
14
nextFree=0 ;
@@ -28,28 +28,28 @@ Fifo<T>::Fifo(std::initializer_list<T> lst) {
28
28
*/
29
29
30
30
template <class T >
31
- T &Fifo <T>::operator [](int i) {
31
+ T &DynamicFifo <T>::operator [](int i) {
32
32
return elem[i%sz]; // if the index is larger than the sz, it wraps around;
33
33
}
34
34
35
35
template <class T >
36
- T &Fifo <T>::operator [](int i) const {
36
+ T &DynamicFifo <T>::operator [](int i) const {
37
37
return elem[i%sz]; // if the index is larger than the sz, it wraps around;
38
38
}
39
39
40
40
template <class T >
41
- T& Fifo <T>::atFifoIndex(int i) {
41
+ T& DynamicFifo <T>::atFifoIndex(int i) {
42
42
return elem[(endPointer+i) % sz];
43
43
}
44
44
45
45
template <class T >
46
- T& Fifo <T>::atFifoIndex(int i) const {
46
+ T& DynamicFifo <T>::atFifoIndex(int i) const {
47
47
return elem[(endPointer+i) % sz];
48
48
}
49
49
50
50
51
51
template <class T >
52
- Fifo_STATUS Fifo <T>::push(const T& item) {
52
+ typename DynamicFifo<T>:: Fifo_STATUS DynamicFifo <T>::push(const T& item) {
53
53
if (fifo_status ()==Fifo_STATUS::Fifo_FULL) {
54
54
// throw std::length_error("NA"); // throw does not work with arduino :(
55
55
return Fifo_STATUS::Fifo_FULL; // status code
@@ -67,7 +67,7 @@ Fifo_STATUS Fifo<T>::push(const T& item) {
67
67
}
68
68
69
69
template <class T >
70
- T Fifo <T>::pop() { // / Note: You should check the status of the fifo before calling this function
70
+ T DynamicFifo <T>::pop() { // / Note: You should check the status of the fifo before calling this function
71
71
if (fifo_status ()==Fifo_STATUS::Fifo_EMPTY) {
72
72
return {0 }; // return 0
73
73
}
@@ -83,7 +83,7 @@ T Fifo<T>::pop() { /// Note: You should check the status of the fifo before call
83
83
84
84
85
85
template <class T >
86
- T Fifo <T>::peekBack() const { // GitHub copilot for the win
86
+ T DynamicFifo <T>::peekBack() const { // GitHub copilot for the win
87
87
if (fifo_status ()==Fifo_STATUS::Fifo_EMPTY) {
88
88
return {0 }; // return 0
89
89
}
@@ -92,7 +92,7 @@ T Fifo<T>::peekBack() const { // GitHub copilot for the win
92
92
}
93
93
94
94
template <class T >
95
- T Fifo <T>::peekBack(int i) const {
95
+ T DynamicFifo <T>::peekBack(int i) const {
96
96
if (fifo_status ()==Fifo_STATUS::Fifo_EMPTY) {
97
97
return {0 }; // return 0
98
98
}
@@ -103,7 +103,7 @@ T Fifo<T>::peekBack(int i) const {
103
103
104
104
105
105
template <class T >
106
- T Fifo <T>::peekFront() const {
106
+ T DynamicFifo <T>::peekFront() const {
107
107
if (fifo_status ()==Fifo_STATUS::Fifo_EMPTY) {
108
108
return {0 }; // return 0
109
109
}
@@ -112,7 +112,7 @@ T Fifo<T>::peekFront() const {
112
112
}
113
113
114
114
template <class T >
115
- T Fifo <T>::peekFront(int i) const {
115
+ T DynamicFifo <T>::peekFront(int i) const {
116
116
if (fifo_status ()==Fifo_STATUS::Fifo_EMPTY) {
117
117
return {0 }; // return 0
118
118
}
@@ -121,7 +121,7 @@ T Fifo<T>::peekFront(int i) const {
121
121
}
122
122
123
123
template <class T >
124
- Fifo_STATUS Fifo <T>::fifo_status() const {
124
+ typename DynamicFifo<T>:: Fifo_STATUS DynamicFifo <T>::fifo_status() const {
125
125
if (nextFree==endPointer) {
126
126
return Fifo_STATUS::Fifo_EMPTY; // fifo empty
127
127
}
@@ -133,12 +133,12 @@ Fifo_STATUS Fifo<T>::fifo_status() const {
133
133
}
134
134
135
135
template <class T >
136
- int Fifo <T>::size() const {
136
+ int DynamicFifo <T>::size() const {
137
137
return sz;
138
138
}
139
139
140
140
template <class T >
141
- int Fifo <T>::free_space() const {
141
+ int DynamicFifo <T>::free_space() const {
142
142
if (fifo_status ()==Fifo_STATUS::Fifo_FULL){
143
143
return 0 ;
144
144
}
@@ -152,8 +152,8 @@ int Fifo<T>::free_space() const{
152
152
153
153
template <class T >
154
154
template <typename D>
155
- Fifo <T>::operator Fifo <D>() const {
156
- Fifo <D> r (sz);
155
+ DynamicFifo <T>::operator DynamicFifo <D>() const {
156
+ DynamicFifo <D> r (sz);
157
157
for (int i=0 ;i<used_space ();i++) {
158
158
D item = static_cast <D>(elem[i]);
159
159
r.push (item);
@@ -163,7 +163,7 @@ Fifo<T>::operator Fifo<D>() const {
163
163
164
164
// copy constructor
165
165
template <class T >
166
- Fifo <T>::Fifo (const Fifo &a) {
166
+ DynamicFifo <T>::DynamicFifo (const DynamicFifo &a) {
167
167
elem = new T[a.sz ];
168
168
sz = a.sz ;
169
169
nextFree = a.nextFree ;
@@ -175,7 +175,7 @@ Fifo<T>::Fifo(const Fifo &a) {
175
175
176
176
// copy assignment operator
177
177
template <class T >
178
- Fifo <T> &Fifo <T>::operator =(const Fifo &a) {
178
+ DynamicFifo <T> &DynamicFifo <T>::operator =(const DynamicFifo &a) {
179
179
T* p = new T[a.sz ];
180
180
for (int i=0 ;i<a.sz ;i++) {
181
181
p[i] = a.elem [i];
@@ -190,7 +190,7 @@ Fifo<T> &Fifo<T>::operator=(const Fifo &a) {
190
190
191
191
// move constructor
192
192
template <class T >
193
- Fifo <T>::Fifo(Fifo &&a) noexcept {
193
+ DynamicFifo <T>::DynamicFifo(DynamicFifo &&a) noexcept {
194
194
elem = a.elem ;
195
195
sz = a.sz ;
196
196
nextFree = a.nextFree ;
@@ -200,7 +200,7 @@ Fifo<T>::Fifo(Fifo &&a) noexcept {
200
200
201
201
// move assignment operator
202
202
template <class T >
203
- Fifo <T> &Fifo <T>::operator =(Fifo &&a) noexcept {
203
+ DynamicFifo <T> &DynamicFifo <T>::operator =(DynamicFifo &&a) noexcept {
204
204
if (this != &a) {
205
205
delete[] elem;
206
206
elem = a.elem ;
@@ -213,6 +213,6 @@ Fifo<T> &Fifo<T>::operator=(Fifo &&a) noexcept {
213
213
}
214
214
215
215
template <class T >
216
- int Fifo <T>::used_space() const {
216
+ int DynamicFifo <T>::used_space() const {
217
217
return sz-free_space ();
218
218
}
0 commit comments