Loading...
Searching...
No Matches
containers.hpp
Go to the documentation of this file.
1
11
12#pragma once
13
14#include <array>
15#include <cstddef>
16#include <stdexcept>
17#include <string>
18#include <variant>
19#include <vector>
20
33
34namespace containers {
35
36// ─── Exceptions ──────────────────────────────────────────────────────────────
37
46class ContainerError : public std::runtime_error {
47public:
48 using std::runtime_error::runtime_error;
49};
50
57public:
58 BufferOverflow() : ContainerError("buffer overflow: container is full") {}
59};
60
67public:
68 BufferUnderflow() : ContainerError("buffer underflow: container is empty") {}
69};
70
71// ─── Stack ───────────────────────────────────────────────────────────────────
72
84template <typename T>
85class Stack {
86public:
92 void push(const T& value) {
93 data_.push_back(value);
94 }
95
102 T pop() {
103 if (data_.empty()) throw BufferUnderflow();
104 T val = data_.back();
105 data_.pop_back();
106 return val;
107 }
108
114 [[nodiscard]] const T& top() const {
115 if (data_.empty()) throw BufferUnderflow();
116 return data_.back();
117 }
118
120 [[nodiscard]] bool empty() const { return data_.empty(); }
121
123 [[nodiscard]] std::size_t size() const { return data_.size(); }
124
126 void clear() { data_.clear(); }
127
128private:
129 std::vector<T> data_;
130};
131
132// ─── RingBuffer ──────────────────────────────────────────────────────────────
133
150template <typename T, std::size_t N>
152public:
153 RingBuffer() = default;
154
160 void write(const T& value) {
161 if (full()) throw BufferOverflow();
162 buf_[tail_] = value;
163 tail_ = (tail_ + 1) % N;
164 ++size_;
165 }
166
172 T read() {
173 if (empty()) throw BufferUnderflow();
174 T val = buf_[head_];
175 head_ = (head_ + 1) % N;
176 --size_;
177 return val;
178 }
179
181 [[nodiscard]] bool empty() const { return size_ == 0; }
182
184 [[nodiscard]] bool full() const { return size_ == N; }
185
187 [[nodiscard]] std::size_t size() const { return size_; }
188
190 [[nodiscard]] constexpr std::size_t capacity() const { return N; }
191
192private:
193 std::array<T, N> buf_{};
194 std::size_t head_ = 0;
195 std::size_t tail_ = 0;
196 std::size_t size_ = 0;
197};
198
199// ─── Result ──────────────────────────────────────────────────────────────────
200
219template <typename T, typename E = std::string>
220class Result {
221public:
227 static Result ok(T value) { return Result(std::move(value)); }
228
234 static Result err(E error) { return Result(ErrTag{}, std::move(error)); }
235
237 [[nodiscard]] bool is_ok() const {
238 return std::holds_alternative<T>(data_);
239 }
240
242 [[nodiscard]] bool is_err() const { return !is_ok(); }
243
249 [[nodiscard]] const T& value() const { return std::get<T>(data_); }
250
256 [[nodiscard]] const E& error() const { return std::get<E>(data_); }
257
258private:
259 struct ErrTag {};
260 explicit Result(T value) : data_(std::move(value)) {}
261 Result(ErrTag, E error) : data_(std::move(error)) {}
262
263 std::variant<T, E> data_;
264};
265
266} // namespace containers
267 // end of containers group
Thrown when a fixed-capacity container is full.
Thrown when popping from an empty container.
Base exception for container errors.
bool is_err() const
Check if this result is an error.
bool is_ok() const
Check if this result is a success.
static Result ok(T value)
Create a success result.
const E & error() const
Unwrap the error.
const T & value() const
Unwrap the success value.
static Result err(E error)
Create an error result.
constexpr std::size_t capacity() const
Get the maximum capacity.
T read()
Read and remove the oldest value.
std::size_t size() const
Get the number of elements currently stored.
bool empty() const
Check if the buffer is empty.
bool full() const
Check if the buffer is at capacity.
void write(const T &value)
Write a value to the buffer.
A simple LIFO stack backed by std::vector.
void clear()
Remove all elements.
void push(const T &value)
Push a value onto the top of the stack.
const T & top() const
Peek at the top element without removing it.
bool empty() const
Check if the stack is empty.
T pop()
Remove and return the top element.
std::size_t size() const
Get the number of elements.