added fixed-size stack implementation

This commit is contained in:
Ahmet Inan 2020-10-13 17:10:59 +02:00
commit 77307d70f5
2 changed files with 67 additions and 0 deletions

View file

@ -142,6 +142,10 @@ A [Digital delay line](https://en.wikipedia.org/wiki/Digital_delay_line) can be
A [ring buffer](https://en.wikipedia.org/wiki/Circular_buffer) based, fixed-size [double-ended queue](https://en.wikipedia.org/wiki/Double-ended_queue).
### [stack.hh](stack.hh)
A fixed-size [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) with access to the first element.
### [calculus.hh](calculus.hh)
Some [calculus](https://en.wikipedia.org/wiki/Calculus) functions:

63
stack.hh Normal file
View file

@ -0,0 +1,63 @@
/*
Fixed-size stack
Copyright 2020 Ahmet Inan <inan@aicodix.de>
*/
#pragma once
namespace DSP {
template <typename TYPE, int SIZE>
class Stack
{
TYPE buf[SIZE];
int pos;
public:
Stack() : pos(-1)
{
}
void clear()
{
pos = -1;
}
void push(TYPE input)
{
assert(pos < SIZE-1);
buf[++pos] = input;
}
void pop()
{
assert(pos >= 0);
--pos;
}
TYPE top()
{
assert(pos >= 0);
return buf[pos];
}
TYPE first()
{
assert(pos >= 0);
return buf[0];
}
bool empty()
{
return pos == -1;
}
bool full()
{
return pos == SIZE-1;
}
int size()
{
return pos+1;
}
int max_size()
{
return SIZE;
}
};
}