Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5d366c0

Browse files
DataStrcuturs/Queue
1 parent adda7f3 commit 5d366c0

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

‎examples/DataStructures/NdQueueExample/NdQueueExample.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <ArduinoUnit.h>
2-
#include <eloquentarduino.h>
2+
#include <EloquentArduino.h>
33
#include <eloquentarduino/data_structures/NdQueue.h>
44

55

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <ArduinoUnit.h>
2+
#include <EloquentArduino.h>
3+
#include <eloquentarduino/data_structures/Queue.h>
4+
5+
6+
using namespace Eloquent::DataStructures;
7+
8+
9+
test(append_one) {
10+
float x = 1;
11+
Queue<5> queue;
12+
13+
queue.append(x);
14+
15+
assertEqual(queue.X[0], x);
16+
}
17+
18+
test(append_two) {
19+
float x1 = 1;
20+
float x2 = 2;
21+
Queue<5> queue;
22+
23+
queue.append(x1);
24+
queue.append(x2);
25+
26+
assertEqual(queue.X[0], x1);
27+
assertEqual(queue.X[1], x2);
28+
}
29+
30+
test(append_overflow) {
31+
float x1 = 1;
32+
float x2 = 2;
33+
float x3 = 3;
34+
35+
Queue<2> queue;
36+
37+
queue.append(x1);
38+
queue.append(x2);
39+
queue.append(x3);
40+
41+
assertEqual(queue.X[0], x2);
42+
assertEqual(queue.X[1], x3);
43+
}
44+
45+
46+
void setup() {
47+
Serial.begin(115200);
48+
}
49+
50+
51+
void loop() {
52+
Test::run();
53+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
6+
namespace Eloquent {
7+
namespace DataStructures {
8+
/**
9+
* Circular queue
10+
* @tparam depth
11+
*/
12+
template<uint16_t depth>
13+
class Queue {
14+
public:
15+
float X[depth];
16+
17+
/**
18+
* Constructor
19+
*/
20+
Queue() {
21+
clear();
22+
}
23+
24+
/**
25+
* Set all elements to 0
26+
*/
27+
void clear() {
28+
_idx = 0;
29+
30+
for (uint16_t k = 0; k < depth; k++) {
31+
X[k] = 0;
32+
}
33+
}
34+
35+
/**
36+
* Get total number of items pushed to the queue.
37+
* If more than UINT32_MAX - depth have been pushed, this number becomes meaningless
38+
* @return
39+
*/
40+
inline size_t length() {
41+
return _idx;
42+
}
43+
44+
/**
45+
* Test if queue is full
46+
*/
47+
inline bool isFull() {
48+
return length() >= depth;
49+
}
50+
51+
/**
52+
* Add sample to queue.
53+
*/
54+
void append(float x) {
55+
uint16_t i = isFull() ? 0 : _idx;
56+
57+
X[i] = x;
58+
_idx += 1;
59+
60+
if (length() > depth) {
61+
shift();
62+
}
63+
}
64+
65+
protected:
66+
size_t _idx;
67+
68+
/**
69+
* Shift queue by one position to the left
70+
*/
71+
void shift() {
72+
float first = X[0];
73+
74+
for (uint16_t k = 1; k < depth; k++) {
75+
X[k - 1] = X[k];
76+
}
77+
78+
X[depth - 1] = first;
79+
}
80+
};
81+
}
82+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /