-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChimeArray.cpp
64 lines (51 loc) · 1.21 KB
/
ChimeArray.cpp
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
#include "ChimeArray.h"
#include "string.h"
ChimeArray::ChimeArray() {
chimes = 0;
count = 0;
debugStream = 0;
}
ChimeArray::~ChimeArray() {
delete[] chimes;
count = 0;
}
void ChimeArray::setDebugStream(Stream *pStream) {
debugStream = pStream;
}
void ChimeArray::debug(String msg) {
if (debugStream) {
debugStream->println(msg);
}
}
void ChimeArray::push(chime_struct *chime) {
count++;
chime_struct *new_chimes = new chime_struct[count];
unsigned short offset = sizeof(chime_struct)*(count - 1);
if (count > 1) {
memcpy(new_chimes, chimes, offset);
delete[] chimes;
}
new_chimes[count - 1].pitch = chime->pitch;
new_chimes[count - 1].duration = chime->duration;
chimes = new_chimes;
}
bool ChimeArray::shift(chime_struct *chime) {
if (count == 0) {
return false;
}
chime->pitch = chimes[0].pitch;
chime->duration = chimes[0].duration;
count--;
if (count > 0) {
chime_struct *new_chimes = new chime_struct[count];
memcpy(new_chimes, (void *)chimes + sizeof(chime_struct), sizeof(chime_struct) * count);
delete[] chimes;
chimes = new_chimes;
} else {
delete[] chimes;
}
return true;
}
bool ChimeArray::isEmpty() {
return (count == 0);
}