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
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