-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_reverse_polish_notation.hpp
232 lines (202 loc) · 6.84 KB
/
evaluate_reverse_polish_notation.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
/// Problem source:
/// https://leetcode.com/problems/evaluate-reverse-polish-notation/
#ifndef FORFUN_EVALUATE_REVERSE_POLISH_NOTATION_HPP_
#define FORFUN_EVALUATE_REVERSE_POLISH_NOTATION_HPP_
#include <cassert>
#include <charconv>
#include <iterator>
#include <memory>
#include <string_view>
#include <system_error>
#include <utility>
#include <vector>
namespace forfun::evaluate_reverse_polish_notation {
namespace hardened {
template <std::contiguous_iterator Iter, std::sentinel_for<Iter> Sentinel>
[[nodiscard]] auto eval_expression(Iter iter, Sentinel const end)
-> std::pair<int, std::errc>
{
if (iter == end) [[unlikely]]
{
return {0, std::errc{}};
}
auto const evaluation_stack_size{
static_cast<std::vector<int>::size_type>(end - iter)
};
std::vector<int> evaluation_stack;
evaluation_stack.reserve(evaluation_stack_size);
for (; iter != end; ++iter)
{
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int operand /*[[indeterminate]]*/;
if (std::from_chars_result const parse_result{std::from_chars(
iter->data(), iter->data() + iter->size(), operand
)};
parse_result.ec == std::errc{}
&& parse_result.ptr == iter->data() + iter->size())
{
evaluation_stack.push_back(operand);
}
// clang-format off
else if (
(iter->length() == std::string_view::size_type{1})
&& (evaluation_stack.size() >= std::vector<int>::size_type{2})
)
// clang-format on
{
int const operand_2{evaluation_stack.back()};
evaluation_stack.pop_back();
int& accumulator{evaluation_stack.back()};
switch (iter->front())
{
case '+':
accumulator += operand_2;
break;
case '-':
accumulator -= operand_2;
break;
case '*':
accumulator *= operand_2;
break;
case '/':
if (operand_2 == 0) [[unlikely]]
{
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto invalid_argument;
}
accumulator /= operand_2;
break;
default:
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto invalid_argument;
}
}
else
{
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto invalid_argument;
}
}
return {evaluation_stack.back(), std::errc{}};
invalid_argument:
return {0, std::errc::invalid_argument};
}
} // namespace hardened
namespace unhardened {
template <std::contiguous_iterator Iter, std::sentinel_for<Iter> Sentinel>
[[nodiscard]] auto eval_expression(Iter iter, Sentinel const end)
-> std::pair<int, std::errc>
{
if (iter == end) [[unlikely]]
{
return {0, std::errc{}};
}
auto const evaluation_stack_size{
static_cast<std::vector<int>::size_type>(end - iter)
};
std::vector<int> evaluation_stack;
evaluation_stack.reserve(evaluation_stack_size);
for (; iter != end; ++iter)
{
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int operand /*[[indeterminate]]*/;
if (std::from_chars_result const parse_result{std::from_chars(
iter->data(), iter->data() + iter->size(), operand
)};
parse_result.ec == std::errc{})
{
evaluation_stack.push_back(operand);
}
else
{
int const operand_2{evaluation_stack.back()};
evaluation_stack.pop_back();
int& accumulator{evaluation_stack.back()};
switch (iter->front())
{
case '+':
accumulator += operand_2;
break;
case '-':
accumulator -= operand_2;
break;
case '*':
accumulator *= operand_2;
break;
case '/':
assert(operand_2 != 0);
accumulator /= operand_2;
break;
default:
std::unreachable();
}
}
}
return {evaluation_stack.back(), std::errc{}};
}
} // namespace unhardened
namespace speed_optimized {
template <std::contiguous_iterator Iter, std::sentinel_for<Iter> Sentinel>
[[nodiscard]] auto eval_expression(Iter iter, Sentinel const end)
-> std::pair<int, std::errc>
{
auto const evaluation_stack_size{
static_cast<std::vector<int>::size_type>(end - iter)
};
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
std::unique_ptr<int[]> const evaluation_stack{
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
std::make_unique_for_overwrite<int[]>(evaluation_stack_size + 1U)
};
int* evaluation_stack_top{evaluation_stack.get()};
*evaluation_stack_top = 0;
for (; iter != end; ++iter)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
auto const sv_end{iter->data() + iter->size()};
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int operand /*[[indeterminate]]*/;
if (std::from_chars_result const parse_result{
std::from_chars(iter->data(), sv_end, operand)
};
parse_result.ec == std::errc{})
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
++evaluation_stack_top;
*evaluation_stack_top = operand;
}
else
{
int const operand_2{*evaluation_stack_top};
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
--evaluation_stack_top;
int& accumulator{*evaluation_stack_top};
switch (iter->front())
{
case '+':
accumulator += operand_2;
break;
case '-':
accumulator -= operand_2;
break;
case '*':
accumulator *= operand_2;
break;
case '/':
assert(operand_2 != 0);
accumulator /= operand_2;
break;
default:
std::unreachable();
}
}
}
return {*evaluation_stack_top, std::errc{}};
}
} // namespace speed_optimized
} // namespace forfun::evaluate_reverse_polish_notation
#endif // FORFUN_EVALUATE_REVERSE_POLISH_NOTATION_HPP_