-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicUsage.ino
More file actions
77 lines (67 loc) · 1.68 KB
/
Copy pathBasicUsage.ino
File metadata and controls
77 lines (67 loc) · 1.68 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
// LionArray — basic usage
//
// Demonstrates Array<T>, Queue<T> and Stack<T> with a trivially-copyable
// element type (int). Open the Serial Monitor at 115200 baud.
#include <Array.h>
static void printArray(const char *label, const Array<int> &a)
{
Serial.print(label);
Serial.print(" (len=");
Serial.print(a.Length());
Serial.print("): ");
for (int i = 0; i < a.Length(); i++)
{
Serial.print(a[i]);
Serial.print(' ');
}
Serial.println();
}
void setup()
{
Serial.begin(115200);
delay(300);
Serial.println();
Serial.println("=== LionArray demo ===");
// ---- Array ----
Array<int> a; // default capacity 8, length 0
a += 30;
a += 10;
a += 20; // append
a.Insert(15, 1); // -> 30 15 10 20
a.Remove(0); // -> 15 10 20
printArray("array", a);
// Sort with a lambda comparator (no std::function, no allocation)
a.Sort([](const int &x, const int &y) { return x - y; });
printArray("sorted", a);
Serial.print("Find(10) -> index ");
Serial.println(a.Find(10));
// ---- Queue (FIFO) ----
Queue<int> q;
q.Push(1);
q.Push(2);
q.Push(3);
int v;
Serial.print("queue pop order: ");
while (q.TryPop(v)) // TryPop: false when empty (unambiguous)
{
Serial.print(v);
Serial.print(' ');
}
Serial.println();
// ---- Stack (LIFO) ----
Stack<int> s;
s.Push(1);
s.Push(2);
s.Push(3);
Serial.print("stack pop order: ");
while (s.TryPop(v))
{
Serial.print(v);
Serial.print(' ');
}
Serial.println();
Serial.println("=== done ===");
}
void loop()
{
}