numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
static_stack.h
Go to the documentation of this file.
1/*
2 * Copyright 2024 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstddef>
23#include <new>
24
28
29namespace num_collect::util {
30
31// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic,*-avoid-c-arrays): This class is a container.
32
39template <typename T, std::size_t StaticArraySize>
41public:
46 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
47 : begin_(std::launder(reinterpret_cast<T*>(buffer_))),
48 end_(begin_),
49 storage_end_(begin_ + StaticArraySize) {}
50
51 static_stack(const static_stack&) = delete;
52 static_stack(static_stack&&) = delete;
53 auto operator=(const static_stack&) -> static_stack& = delete;
54 auto operator=(static_stack&&) -> static_stack& = delete;
55
60 while (!empty()) {
61 pop();
62 }
63 }
64
70 void push(const T& value) {
71 NUM_COLLECT_PRECONDITION(end_ != storage_end_, "Stack is full.");
72 new (end_) T(value);
73 ++end_;
74 }
75
79 void pop() noexcept {
80 --end_;
81 end_->~T();
82 }
83
89 [[nodiscard]] auto top() const noexcept -> const T& { return *(end_ - 1); }
90
96 [[nodiscard]] auto top() noexcept -> T& { return *(end_ - 1); }
97
104 [[nodiscard]] auto empty() const noexcept -> bool { return begin_ == end_; }
105
111 [[nodiscard]] auto size() const noexcept -> std::size_t {
112 return static_cast<std::size_t>(end_ - begin_);
113 }
114
115private:
117 alignas(alignof(T)) char buffer_[sizeof(T[StaticArraySize])]{};
118
121
124
127};
128
129// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic,*-avoid-c-arrays)
130
131} // namespace num_collect::util
Class of stacks using static arrays.
char buffer_[sizeof(T[StaticArraySize])]
Buffer for stack.
auto top() noexcept -> T &
Get the top value.
void pop() noexcept
Pop a value.
void push(const T &value)
Push a value.
auto size() const noexcept -> std::size_t
Get the current number of values.
T * end_
Pointer to the past-the-end element in the buffer.
T * storage_end_
Pointer to the past-the-end storage in the buffer.
T * begin_
Pointer to the first element in the buffer.
auto top() const noexcept -> const T &
Get the top value.
auto empty() const noexcept -> bool
Check whether this stack is empty.
Definition of exceptions.
Definition of macros for logging.
Namespace of utilities.
Definition assert.h:30
STL namespace.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.