-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.hpp
152 lines (111 loc) · 3.66 KB
/
stack.hpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariahi <ariahi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/29 19:43:46 by ariahi #+# #+# */
/* Updated: 2023/01/29 20:16:02 by ariahi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef STACK_HPP
#define STACK_HPP
#include "vector.hpp"
#include <stack>
namespace ft
{
template <class T, class Container = ft::vector<T> >
class stack
{
public:
// It's defining the type of the container, the type of the value and the type of the size.
typedef Container container_type;
typedef typename container_type::value_type value_type;
typedef typename container_type::size_type size_type;
protected:
// It's a container that will be used to store the elements of the stack.
container_type c;
public:
// It's a copy constructor.
stack(const stack &other)
{
c = other.c;
}
// It's a constructor that takes a container_type as a parameter.
explicit stack(const container_type &_cont = container_type())
{
c = _cont;
}
// It's a empty destructor bc vector destructor gonna take care of it.
~stack() {}
// It's an operator that is assigning the stack to the other stack.
stack &operator=(const stack &rhs)
{
c = rhs.c;
return (*this);
}
// It's returning true if the stack is empty and false if it's not.
bool empty() const
{
return (c.empty());
}
// It's returning the size of the stack.
size_type size() const
{
return (c.size());
}
// It's returning the last element of the stack.
value_type &top()
{
return (c.back());
}
// It's returning the last element of the stack.
value_type &top() const
{
return (c.back());
}
// It's pushing a value at the end of the stack.
void push(const value_type &value)
{
return (c.push_back(value));
}
// It's removing the last element of the stack.
void pop()
{
return (c.pop_back());
}
// It's swapping the content of the stack with the content of the other stack.
void swap(stack &other)
{
c.swap(other);
}
// It's comparing the content of the stack with the content of the other stack.
// and we use the friend keyword bc we compare a private(protacted) member.
friend bool operator==(const stack<T, Container> &lhs, const stack<T, Container> &rhs)
{
return (lhs.c == rhs.c);
}
friend bool operator<=(const stack<T, Container> &lhs, const stack<T, Container> &rhs)
{
return (lhs.c <= rhs.c);
}
friend bool operator>=(const stack<T, Container> &lhs, const stack<T, Container> &rhs)
{
return (lhs.c >= rhs.c);
}
friend bool operator!=(const stack<T, Container> &lhs, const stack<T, Container> &rhs)
{
return (lhs.c != rhs.c);
}
friend bool operator<(const stack<T, Container> &lhs, const stack<T, Container> &rhs)
{
return (lhs.c < rhs.c);
}
friend bool operator>(const stack<T, Container> &lhs, const stack<T, Container> &rhs)
{
return (lhs.c > rhs.c);
}
};
} // namespace ft
#endif