diff --git a/README.md b/README.md index 789b9e0..4fbcdcc 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/stack.hh b/stack.hh new file mode 100644 index 0000000..8325cc1 --- /dev/null +++ b/stack.hh @@ -0,0 +1,63 @@ +/* +Fixed-size stack + +Copyright 2020 Ahmet Inan +*/ + +#pragma once + +namespace DSP { + +template +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; + } +}; + +} +