From 611852c273b3cc20315b2e04a9b3d955693aa892 Mon Sep 17 00:00:00 2001 From: nogitsune-youkai Date: Mon, 28 Sep 2026 00:38:38 +0300 Subject: [PATCH] Refactor: Use size_type for sizes and indices --- src/util/stack_vector.hpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/util/stack_vector.hpp b/src/util/stack_vector.hpp index e6565a811..87749396b 100644 --- a/src/util/stack_vector.hpp +++ b/src/util/stack_vector.hpp @@ -1,9 +1,12 @@ #pragma once +#include +#include #include +#include namespace util { - template + template class stack_vector { struct buffer { alignas(alignof(T)) char data[sizeof(T) * capacity]; @@ -16,18 +19,19 @@ namespace util { } }; public: + using size_type = std::size_t; stack_vector() : size_(0) {} stack_vector(const stack_vector& other) : size_(other.size_) { - for (int i = 0; i < size_; ++i) { + for (size_type i = 0; i < size_; ++i) { new (&data_.ptr()[i]) T(other.data_.ptr()[i]); } } stack_vector(stack_vector&& other) noexcept : size_(other.size_) { - for (int i = 0; i < size_; ++i) { + for (size_type i = 0; i < size_; ++i) { new (&data_.ptr()[i]) T(std::move(other.data_.ptr()[i])); } other.size_ = 0; @@ -77,29 +81,29 @@ namespace util { } void clear() noexcept { - for (int i = 0; i < size_; ++i) { + for (size_type i = 0; i < size_; ++i) { data_.ptr()[i].~T(); } size_ = 0; } - T& operator[](int index) { + T& operator[](size_type index) { return data_.ptr()[index]; } - const T& operator[](int index) const { + const T& operator[](size_type index) const { return data_.ptr()[index]; } - T& at(int index) { - if (index < 0 || index >= size_) { + T& at(size_type index) { + if (index >= size_) { throw std::out_of_range("index out of range"); } return data_.ptr()[index]; } - const T& at(int index) const { - if (index < 0 || index >= size_) { + const T& at(size_type index) const { + if (index >= size_) { throw std::out_of_range("index out of range"); } return data_.ptr()[index]; @@ -121,7 +125,7 @@ namespace util { return data_.ptr()[size_ - 1]; } - int size() const { + size_type size() const { return size_; } @@ -150,6 +154,6 @@ namespace util { } private: buffer data_; - int size_; + size_type size_; }; }