Sequoia
Loading...
Searching...
No Matches
StaticStack.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2018. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
17
18namespace sequoia::data_structures
19{
25 template<class T, std::size_t MaxDepth>
27 {
28 public:
29 constexpr static_stack(std::initializer_list<T> l)
30 : m_Stack{utilities::to_array<T, MaxDepth>(l)}
31 , m_End{l.size()}
32 {
33 }
34
35 constexpr static_stack(const static_stack&) = default;
36 constexpr static_stack(static_stack&) noexcept = default;
37 ~static_stack() = default;
38
39 constexpr static_stack& operator=(const static_stack&) = default;
40 constexpr static_stack& operator=(static_stack&) noexcept = default;
41
42 constexpr void push(const T& val)
43 {
44 if(m_End == MaxDepth)
45 throw std::logic_error("Attempting to exceed max stack depth");
46
47 m_Stack[m_End] = val;
48 ++m_End;
49 }
50
51 [[nodiscard]]
52 constexpr const T& top() const noexcept
53 {
54 return m_Stack[m_End - 1];
55 }
56
57 constexpr void pop() noexcept
58 {
59 --m_End;
60 }
61
62 [[nodiscard]]
63 constexpr bool empty() const noexcept
64 {
65 return m_End == 0u;
66 }
67
68 [[nodiscard]]
69 constexpr std::size_t size() const noexcept
70 {
71 return m_End;
72 }
73
74 [[nodiscard]]
75 friend constexpr bool operator==(const static_stack& lhs, const static_stack& rhs) noexcept
76 {
77 return (lhs.m_End == rhs.m_End)
78 && std::ranges::equal(lhs.m_Stack.begin(), lhs.m_Stack.begin() + lhs.m_End, rhs.m_Stack.begin(), rhs.m_Stack.begin() + rhs.m_End);
79 }
80 private:
81 std::array<T, MaxDepth> m_Stack{};
82
83 std::size_t m_End{};
84 };
85}
A collection of constexpr algorithms.
Utility to convert an initializer_list into an array, potentially transforming the initializer_list i...
A stack suitable for constexpr contexts.
Definition: StaticStack.hpp:27