Fleet  0.0.9
Inference in the LOT
Stack.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <assert.h>
5 
6 #include "Strings.h"
7 
21 template<typename T>
22 class Stack {
23 
24 
25 
26 public:
27 std::vector<T> value;
28  Stack() {
29 // reserve(100); // does nothing
30  }
31 
32  template<typename... Args>
33  void emplace_back(Args... args) {
34  value.emplace_back(args...);
35  }
36 
37  void reserve(size_t t) {
38  value.reserve(t);
39  }
40 
41  void clear() {
42  value.clear();
43  }
44 
49  void push(const T& val) {
50  value.push_back(val);
51  }
52  void push(const T&& val) {
53  value.push_back(std::move(val));
54  }
55 
59  void pop() {
63  assert(!empty());
64  value.pop_back();
65  }
66 
71  void popn(size_t n) {
72  for(size_t i=0;i<n;i++) {
73  this->pop();
74  }
75  }
76 
77  /* There is a little sublety here -- for integral types, it's a pain to return a reference
78  * so we want to just return T. But some optimizations of the virtual machine will do better
79  * with references, leaving the top value on the stack. So by default we don't return a reference
80  * with top() but we do with topref
81  */
82 
83  [[nodiscard]] T top() {
89  return value.back();
90  }
91 
92  [[nodiscard]] T& topref() {
98  return std::forward<T&>(value.back());
99  }
100 
101  [[nodiscard]] T toppop() {
106  auto o = value.back();
107  pop();
108  return o;
109  }
110 
111 
112 
113  const std::vector<T>& get_value() const {
114  return value;
115  }
116 
117  size_t size() const {
118  return value.size();
119  }
120 
121  bool empty() const {
122  return value.empty();
123  }
124 
125  std::string string() const {
126  return str(value);
127  }
128 
129 // void print_stack(std::string prefix) const {
130 // for(size_t i=0;i<value.size();i++) {
131 // print(prefix, "[i]=", value.at(i));
132 // }
133 // }
134 
138  auto begin() { return value.begin(); }
139  auto end() { return value.end(); }
140  auto rbegin() { return value.rbegin(); }
141  auto rend() { return value.rend(); }
142 };
143 
144 template<typename T>
145 std::string str(const Stack<T>& a ){
146  return str(a.get_value());
147 }
Definition: Stack.h:22
void push(const T &val)
Push val onto the stack.
Definition: Stack.h:49
auto rbegin()
Definition: Stack.h:140
void clear()
Definition: Stack.h:41
void reserve(size_t t)
Definition: Stack.h:37
const std::vector< T > & get_value() const
Definition: Stack.h:113
std::vector< T > value
Definition: Stack.h:27
Stack()
Definition: Stack.h:28
void emplace_back(Args... args)
Definition: Stack.h:33
void pop()
Remove top from the stack.
Definition: Stack.h:59
auto begin()
These are for iterating through the underlying vector.
Definition: Stack.h:138
T top()
Definition: Stack.h:83
void popn(size_t n)
Remove n from the stack.
Definition: Stack.h:71
bool empty() const
Definition: Stack.h:121
size_t size() const
Definition: Stack.h:117
void push(const T &&val)
Definition: Stack.h:52
T & topref()
Definition: Stack.h:92
std::string str(const Stack< T > &a)
Definition: Stack.h:145
auto end()
Definition: Stack.h:139
T toppop()
Definition: Stack.h:101
auto rend()
Definition: Stack.h:141
std::string string() const
Definition: Stack.h:125