-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircstack.h
83 lines (69 loc) · 1.53 KB
/
circstack.h
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
//
// Created by ariadne lewis-towbes on 6/21/23.
//
#ifndef WALDORF_WAVE_CONTROL_CIRCSTACK_H
#define WALDORF_WAVE_CONTROL_CIRCSTACK_H
namespace ParameterMapper
{
/**
* a stack which works within a circular buffer of memory, implementing the following operations:
*
*
* @tparam T type to store in the stack
* @tparam N size of circular buffer (number of items that can be stored in the stack without overwriting previous items)
*/
template <typename T, size_t N = 128>
struct circstack
{
circstack(T def) : write(1)
{
buffer.fill(def);
}
std::array<T, N> buffer;
size_t write = 1;
void push(T t)
{
buffer[write++] = t;
if (write == N)
{
write = 0;
}
}
//! pushes an array of Ts
template <size_t n>
void push(std::array<T, n> ts)
{
push(ts.data(), ts.size());
}
void push(std::vector<T> ts)
{
push(ts.data(), ts.size());
}
//! pushes from the end of the array to the beginning, which makes more sense imho
void push(T* tps, size_t num)
{
for (size_t i = num; i > 0; --i)
{
push(tps[i]);
}
}
T pop()
{
if (write == 0)
{
write = N - 1;
return buffer[N - 1];
}
else
{
return buffer[--write];
}
}
T top()
{
auto tmp = write == 0 ? N - 1 : write - 1;
return buffer[tmp];
}
};
} // namespace ParameterMapper
#endif //WALDORF_WAVE_CONTROL_CIRCSTACK_H