Mozzi  version v2.0
sound synthesis library for Arduino
Stack.h
1 /*
2  * Stack.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2013-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12  /** A simple stack, used internally for keeping track of analog input channels as they are read.
13  This stack is really just an array with a pointer to the most recent item, and memory is allocated at compile time.
14 @tparam T the kind of numbers (or other things) to store.
15 @tparam NUM_ITEMS the maximum number of items the stack will need to hold.
16  */
17 template <class T, int NUM_ITEMS>
18 class Stack
19 {
20 private:
21  T _array[NUM_ITEMS];
22  int top;
23 
24 public:
25  /** Constructor
26  */
27  Stack(): top(-1)
28  {
29  }
30 
31 /** Put an item on the stack.
32 @param item the thing you want to put on the stack.
33 */
34  void push(T item)
35  {
36  if (top< (NUM_ITEMS-1)){
37  top++;
38  _array[top]=item;
39  }
40  }
41 
42 /** Get the item on top of the stack.
43 @return T the item
44 */
45  T pop()
46  {
47  if(top==-1) return -1;
48  T item =_array[top];
49  top--;
50  return item;
51  }
52 
53 };