Voxeloop  0.1.0
Musical Loop Generation in Voxel World
Stack.hpp
Go to the documentation of this file.
1 #ifndef CORE_INCLUDE_STACK_HPP
2 #define CORE_INCLUDE_STACK_HPP
3 
4 #include "Error.hpp"
5 
6 #include <climits>
7 #include <deque>
8 #include <iostream>
9 
10 const auto MAX_SIZE = 100;
11 
16 template <class T, class Container = std::deque<T>> class Stack {
17  size_t _top;
19 
20 public:
21  // initialization
22  Stack() { _top = -1; }
23 
24  bool isEmpty() {
25  // check if the stack is empty
26  return (_top == -1);
27  }
28 
29  bool isFull() {
30  // check if the stack is full
31  return (_top >= MAX_SIZE - 1);
32  }
33 
34  T push(T element) {
35  if (isFull()) {
36  throw EmptyError();
37  // std::cerr << "Stack overflow! No more elements can be pushed." << std::endl;
38  // exit(EXIT_FAILURE);
39  }
40 
41  stack[++_top] = element;
42  return element;
43  }
44 
45  T pop() {
46  if (isEmpty()) {
47  throw EmptyError();
48  // std::cerr << "Stack underflow! No element to pop." << std::endl;
49  // exit(EXIT_FAILURE);
50  }
51 
52  T itemPopped = stack[_top--];
53  return itemPopped;
54  }
55 
56  void peek() {
57  // peek at the _top of the stack
58  if (isEmpty()) {
59  throw EmptyError();
60  // std::cerr << "No element in stack." << std::endl;
61  // exit(EXIT_FAILURE);
62  }
63 
64  std::cout << stack[_top] << std::endl;
65  }
66 
67  T top() {
68  // peek at the top of the stack
69  if (isEmpty()) {
70  std::cerr << "No element in stack." << std::endl;
71  exit(EXIT_FAILURE);
72  }
73 
74  return stack[_top];
75  }
76 };
77 
78 #endif // CORE_INCLUDE_STACK_HPP
const auto MAX_SIZE
Definition: Stack.hpp:10
A data structure that works in LIFO principle.
Definition: Stack.hpp:16
bool isEmpty()
Definition: Stack.hpp:24
size_t _top
Definition: Stack.hpp:17
T push(T element)
Definition: Stack.hpp:34
void peek()
Definition: Stack.hpp:56
T top()
Definition: Stack.hpp:67
bool isFull()
Definition: Stack.hpp:29
T stack[MAX_SIZE]
Definition: Stack.hpp:18
Stack()
Definition: Stack.hpp:22
T pop()
Definition: Stack.hpp:45