Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
FixedStack.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
28#include "shambase/assert.hpp"
29#include <type_traits>
30
31namespace shambase {
32
87 template<class T, u32 stack_size>
88 struct FixedStack {
89
90 static_assert(
91 std::is_trivially_destructible_v<T>,
92 "FixedStack only supports trivially destructible types to avoid resource leaks.");
93 static_assert(stack_size > 0, "FixedStack must have a size greater than 0.");
94
99
100 // Note that the stack itself is voluntarily not initialized
101 // do not add it to the constructor otherwise we may have to pay for zero initialization
102
104 inline constexpr FixedStack() : stack_cursor{stack_size} {}
105
107 inline constexpr FixedStack(const T &val) : stack_cursor{stack_size - 1} {
108 id_stack[stack_cursor] = val;
109 }
110
112 inline constexpr bool is_not_empty() const { return stack_cursor < stack_size; }
113
115 inline constexpr bool empty() const { return stack_cursor == stack_size; }
116
118 inline constexpr u32 size() const { return stack_size - stack_cursor; }
119
121 inline void push(const T &val) {
122
123 // FixedStack overflow: cannot push to a full stack.
125
126 stack_cursor--;
127 id_stack[stack_cursor] = val;
128 }
129
131 inline T &top() {
133 return id_stack[stack_cursor];
134 }
135
137 inline constexpr const T &top() const {
139 return id_stack[stack_cursor];
140 }
141
143 inline constexpr void pop() {
144
145 // FixedStack underflow: cannot pop from an empty stack.
147
148 stack_cursor++;
149 }
150
152 [[nodiscard]]
153 inline T pop_ret() {
154 T val = top();
155 pop();
156 return val;
157 }
158 };
159
160} // namespace shambase
std::uint32_t u32
32 bit unsigned integer
Shamrock assertion utility.
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
Definition assert.hpp:67
namespace for basic c++ utilities
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
Fixed-capacity stack container with compile-time size determination.
constexpr u32 size() const
Returns the number of elements in the stack.
T & top()
Access the top element.
constexpr void pop()
Remove the top element from the stack.
constexpr bool empty() const
Check if the stack is empty.
u32 stack_cursor
Cursor pointing to the next available slot (stack_size = empty, 0 = full)
void push(const T &val)
Push an element onto the top of the stack.
T pop_ret()
Remove and return the top element from the stack.
constexpr const T & top() const
Access the top element (const version)
constexpr FixedStack()
Default constructor creating an empty stack.
constexpr FixedStack(const T &val)
Constructor creating a stack with one initial element.
T id_stack[stack_size]
Storage array for stack elements.
constexpr bool is_not_empty() const
Check if the stack contains any elements.