forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
75 lines (58 loc) · 2.12 KB
/
stack.h
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
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_EXPLORER_INTERPRETER_STACK_H_
#define CARBON_EXPLORER_INTERPRETER_STACK_H_
#include <cstddef>
#include <iterator>
#include <vector>
#include "common/check.h"
namespace Carbon {
// A stack data structure.
template <class T>
struct Stack {
// NOLINTNEXTLINE(readability-identifier-naming)
using const_iterator = typename std::vector<T>::const_reverse_iterator;
// Creates an empty instance.
Stack() = default;
// Creates an instance containing just `x`.
explicit Stack(T x) : Stack() { Push(std::move(x)); }
// Pushes `x` onto the top of the stack.
void Push(T x) { elements_.push_back(std::move(x)); }
// Removes and returns the top element of the stack.
//
// - Requires: !this->IsEmpty()
auto Pop() -> T {
CARBON_CHECK(!IsEmpty()) << "Can't pop from empty stack.";
auto r = std::move(elements_.back());
elements_.pop_back();
return r;
}
// Removes the top `n` elements of the stack.
//
// - Requires: n >= 0 && n <= Count()
void Pop(int n) {
CARBON_CHECK(n >= 0) << "Negative pop count disallowed.";
CARBON_CHECK(static_cast<size_t>(n) <= elements_.size())
<< "Can only pop as many elements as stack has.";
elements_.erase(elements_.end() - n, elements_.end());
}
// Returns the top element of the stack.
//
// - Requires: !this->IsEmpty()
auto Top() const -> const T& {
CARBON_CHECK(!IsEmpty()) << "Empty stack has no Top().";
return elements_.back();
}
// Returns `true` iff `Count() > 0`.
auto IsEmpty() const -> bool { return elements_.empty(); }
// Returns the number of elements in `*this`.
auto Count() const -> int { return elements_.size(); }
// Iterates over the Stack from top to bottom.
auto begin() const -> const_iterator { return elements_.crbegin(); }
auto end() const -> const_iterator { return elements_.crend(); }
private:
std::vector<T> elements_;
};
} // namespace Carbon
#endif // CARBON_EXPLORER_INTERPRETER_STACK_H_