Files
carbon-lang/explorer/interpreter/stack.h
T
Jon Meow 3825f97e43 Add automatic header guard checks/fixes (#1260)
My intent is to add CARBON_ bas a prefix, and this makes that easier by creating a tool for auto-fixing guards in general.

string_literal is a manual fix -- it had no guard and I didn't automate that (technically I think I could, especially by enforcing the file header/footer, but it didn't feel quite worth it to me).
2022-05-12 15:54:45 -07:00

76 lines
2.1 KiB
C++

// 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 EXPLORER_INTERPRETER_STACK_H_
#define 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 // EXPLORER_INTERPRETER_STACK_H_