11#pragma once
22
3+ #include < algorithm>
34#include < cassert>
45#include < cstddef>
56#include < memory>
67#include < utility>
78
89#include " openvic-simulation/core/template/Concepts.hpp"
910#include " openvic-simulation/core/Typedefs.hpp"
11+ #include " openvic-simulation/utility/Logger.hpp"
1012
1113namespace OpenVic ::_detail {
1214 // fixed capacity + not movable + not copyable
@@ -19,6 +21,12 @@ namespace OpenVic::_detail {
1921 OV_NO_UNIQUE_ADDRESS Allocator _allocator;
2022 T* const _data_start_ptr;
2123
24+ explicit FixedVector (const size_t capacity)
25+ : _max_size(capacity),
26+ _size(0 ),
27+ _allocator(),
28+ _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {}
29+
2230 public:
2331 using const_reference = T const &;
2432 using difference_type = ptrdiff_t ;
@@ -33,19 +41,23 @@ namespace OpenVic::_detail {
3341 constexpr T const * data () const { return _data_start_ptr; }
3442 constexpr bool empty () const { return _size == 0 ; }
3543
36- explicit FixedVector (const size_t capacity)
37- : _max_size(capacity),
38- _size(0 ),
44+ FixedVector static create_empty (const size_t capacity) { return FixedVector (capacity); }
45+
46+ FixedVector (const size_t size, T const & value_for_all_indices)
47+ : _max_size(size),
48+ _size (size),
3949 _allocator(),
40- _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {}
50+ _data_start_ptr(allocator_traits::allocate(_allocator, size)) {
51+ std::fill (_data_start_ptr, _data_start_ptr + size, value_for_all_indices);
52+ }
4153
4254 // Generator (size_t i) -> U (where T is constructable from U)
4355 template <typename GeneratorTemplateType>
4456 // The generator must NOT return a tuple
4557 requires (!specialization_of<std::remove_cvref_t <std::invoke_result_t <GeneratorTemplateType, size_t >>, std::tuple>)
4658 // The type must be constructible from the generator's single return value
4759 && std::constructible_from<T, decltype(std::declval<GeneratorTemplateType>()(std::declval<size_t >()))>
48- FixedVector (size_t size, GeneratorTemplateType&& generator)
60+ FixedVector(const size_t size, GeneratorTemplateType&& generator)
4961 : _max_size(size),
5062 _size(size),
5163 _allocator(),
@@ -74,7 +86,7 @@ namespace OpenVic::_detail {
7486 )
7587 };
7688 }
77- FixedVector (size_t size, GeneratorTemplateType&& generator)
89+ FixedVector (const size_t size, GeneratorTemplateType&& generator)
7890 : _max_size(size),
7991 _size (size),
8092 _allocator(),
@@ -125,12 +137,22 @@ namespace OpenVic::_detail {
125137 const_reverse_iterator rend () const { return const_reverse_iterator (begin ()); }
126138 const_reverse_iterator crend () const { return const_reverse_iterator (begin ()); }
127139
128- T& operator [](size_t index) {
129- assert (index < _size && " Index out of bounds." );
140+ T& operator [](const size_t index) {
141+ if (OV_unlikely (_size <= index)) {
142+ spdlog::error_s (
143+ " Out of bound non-const indexing on FixedVector. index {} _size {}" ,
144+ index, _size
145+ );
146+ }
130147 return _data_start_ptr[index];
131148 }
132- const T& operator [](size_t index) const {
133- assert (index < _size && " Index out of bounds." );
149+ const T& operator [](const size_t index) const {
150+ if (OV_unlikely (_size <= index)) {
151+ spdlog::error_s (
152+ " Out of bound const indexing on FixedVector. index {} _size {}" ,
153+ index, _size
154+ );
155+ }
134156 return _data_start_ptr[index];
135157 }
136158
0 commit comments