libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
stack.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstddef>
11#include <stack>
13
15
26template <typename T>
27class Stack {
28 public:
30 Stack() = default;
31
33 Stack(const Stack &other) : d(other.d) {}
34
36 Stack(Stack &&other) noexcept : d(std::move(other.d)) {}
37
39 ~Stack() = default;
40
43 d = other.d;
44 return *this;
45 }
46
48 Stack &operator=(Stack &&other) noexcept {
49 d = std::move(other.d);
50 return *this;
51 }
52
53 // -- Capacity --
54
56 bool isEmpty() const { return d.empty(); }
57
59 size_t size() const { return d.size(); }
60
61 // -- Access --
62
64 T &top() { return d.top(); }
65
67 const T &top() const { return d.top(); }
68
70 const T &constTop() const { return d.top(); }
71
72 // -- Modifiers --
73
78 void push(const T &value) {
79 d.push(value);
80 return;
81 }
82
87 void push(T &&value) {
88 d.push(std::move(value));
89 return;
90 }
91
96 T pop() {
97 T val = std::move(d.top());
98 d.pop();
99 return val;
100 }
101
103 void clear() {
104 while(!d.empty()) d.pop();
105 return;
106 }
107
112 void swap(Stack &other) noexcept {
113 d.swap(other.d);
114 return;
115 }
116
117 // -- Comparison --
118
120 friend bool operator==(const Stack &lhs, const Stack &rhs) { return lhs.d == rhs.d; }
121
123 friend bool operator!=(const Stack &lhs, const Stack &rhs) { return lhs.d != rhs.d; }
124
125 private:
126 std::stack<T> d;
127};
128
Dynamic array container wrapping std::vector.
Definition list.h:40
LIFO stack container wrapping std::stack.
Definition stack.h:27
Stack & operator=(Stack &&other) noexcept
Move assignment operator.
Definition stack.h:48
bool isEmpty() const
Returns true if the stack has no elements.
Definition stack.h:56
void clear()
Removes all elements.
Definition stack.h:103
~Stack()=default
Destructor.
Stack()=default
Default constructor. Creates an empty stack.
friend bool operator!=(const Stack &lhs, const Stack &rhs)
Returns true if the stacks differ.
Definition stack.h:123
friend bool operator==(const Stack &lhs, const Stack &rhs)
Returns true if both stacks have identical contents.
Definition stack.h:120
Stack(Stack &&other) noexcept
Move constructor.
Definition stack.h:36
void push(const T &value)
Pushes a value onto the top of the stack.
Definition stack.h:78
Stack & operator=(const Stack &other)
Copy assignment operator.
Definition stack.h:42
Stack(const Stack &other)
Copy constructor.
Definition stack.h:33
T & top()
Returns a mutable reference to the top element.
Definition stack.h:64
void swap(Stack &other) noexcept
Swaps contents with another stack.
Definition stack.h:112
T pop()
Removes and returns the top element.
Definition stack.h:96
size_t size() const
Returns the number of elements.
Definition stack.h:59
const T & top() const
Returns a const reference to the top element.
Definition stack.h:67
void push(T &&value)
Pushes a value onto the top of the stack (move overload).
Definition stack.h:87
const T & constTop() const
Returns a const reference to the top element.
Definition stack.h:70
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56