mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
added fixed-size stack implementation
This commit is contained in:
parent
1ae40ccf3b
commit
77307d70f5
2 changed files with 67 additions and 0 deletions
|
|
@ -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).
|
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)
|
### [calculus.hh](calculus.hh)
|
||||||
|
|
||||||
Some [calculus](https://en.wikipedia.org/wiki/Calculus) functions:
|
Some [calculus](https://en.wikipedia.org/wiki/Calculus) functions:
|
||||||
|
|
|
||||||
63
stack.hh
Normal file
63
stack.hh
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue