-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfstack.hpp
More file actions
35 lines (27 loc) · 896 Bytes
/
fstack.hpp
File metadata and controls
35 lines (27 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once
#include <memory>
#include <cassert>
template <class T>
class FStack {
public: // idk how to get rid of this but still have empty_node initialized
using SELF = FStack<T>;
std::shared_ptr<FStack> prev;
T val;
FStack(std::shared_ptr<FStack> prev, T val) : prev(prev), val(val) {
}
inline static std::shared_ptr<FStack> empty_node = std::make_shared<FStack>(nullptr, T{});
public:
static std::shared_ptr<FStack> empty() {
return empty_node;
}
static std::shared_ptr<FStack> push(const std::shared_ptr<SELF> &stack, T new_val) {
return std::make_shared<FStack>(stack, new_val);
}
static std::shared_ptr<FStack> pop(const std::shared_ptr<SELF> &stack) {
assert(stack != empty());
return stack->prev;
}
static T top(const std::shared_ptr<SELF> &stack) {
return stack->val;
}
};