-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStack.cpp
More file actions
171 lines (152 loc) · 3.29 KB
/
Copy pathStack.cpp
File metadata and controls
171 lines (152 loc) · 3.29 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <memory>
#include <iostream>
#include <stdexcept>
namespace details
{
template<typename T>
class StackImpl
{
protected:
StackImpl(size_t size = 0u);
~StackImpl();
StackImpl(const StackImpl& other) = delete;
StackImpl& operator = (const StackImpl& other) = delete;
void swap(StackImpl& other) noexcept;
protected:
T* m_data;
size_t m_size;
size_t m_used;
};
template<typename T>
StackImpl<T>::StackImpl(size_t size)
: m_data(static_cast<T*>( (size == 0) ? (nullptr) : (operator new(sizeof(T) * size) ))),
m_size(size), m_used(0u)
{
}
template<typename T>
StackImpl<T>::~StackImpl()
{
std::destroy(m_data, m_data + m_size);
operator delete(m_data);
}
template<typename T>
void StackImpl<T>::swap(StackImpl& other) noexcept
{
std::swap(m_data, other.m_data);
std::swap(m_size, other.m_size);
std::swap(m_used, other.m_used);
}
}
template<typename T>
class Stack : public details::StackImpl<T>
{
using super = details::StackImpl<T>;
public:
Stack(size_t size = 0u);
Stack(const Stack& other);
Stack& operator = (Stack other);
constexpr size_t size() const noexcept;
constexpr bool empty() const noexcept;
T& top();
void push(const T& value);
void pop();
};
template<typename T>
Stack<T>::Stack(size_t size)
: details::StackImpl<T>(size)
{
super::m_used = 0;
}
template<typename T>
Stack<T>::Stack(const Stack& other)
: details::StackImpl<T>(other.m_used)
{
while (super::m_used < other.m_used)
{
new (super::m_data + super::m_used) T(other.m_data[super::m_used]);
++super::m_used;
}
}
template<typename T>
Stack<T>& Stack<T>::operator = (Stack other)
{
swap(other);
return *this;
}
template<typename T>
constexpr size_t Stack<T>::size() const noexcept
{
return super::m_used;
}
template<typename T>
constexpr bool Stack<T>::empty() const noexcept
{
return super::m_used == 0u;
}
template<typename T>
T& Stack<T>::top()
{
if (empty())
{
throw std::logic_error("Stack is empty.");
}
else
{
return super::m_data[super::m_used - 1];
}
}
template<typename T>
void Stack<T>::push(const T& value)
{
if (super::m_used == super::m_size)
{
Stack temp(super::m_size * 2 + 1);
while (temp.size() < super::m_used)
{
temp.push(super::m_data[temp.size()]);
}
temp.push(value);
super::swap(temp);
}
else
{
new (super::m_data + super::m_used) T(value);
++super::m_used;
}
}
template<typename T>
void Stack<T>::pop()
{
if (empty())
{
throw std::logic_error("Stack is empty.");
}
else
{
--super::m_used;
T* pointer = super::m_data + super::m_used;
pointer->~T();
}
}
int main()
{
try
{
Stack<int> stack{};
int data[] = { 7, 6, 5, 4, 3, 2, 1 };
for (int value : data)
{
stack.push(value);
}
while (!stack.empty())
{
stack.top();
stack.pop();
}
return EXIT_SUCCESS;
}
catch (const std::exception& e)
{
return EXIT_FAILURE;
}
}