11#pragma once
22
3+ #include < algorithm>
34#include < cassert>
45#include < cstddef>
56#include < memory>
@@ -19,6 +20,12 @@ namespace OpenVic::_detail {
1920 OV_NO_UNIQUE_ADDRESS Allocator _allocator;
2021 T* const _data_start_ptr;
2122
23+ explicit FixedVector (const size_t capacity)
24+ : _max_size(capacity),
25+ _size(0 ),
26+ _allocator(),
27+ _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {}
28+
2229 public:
2330 using const_reference = T const &;
2431 using difference_type = ptrdiff_t ;
@@ -33,19 +40,23 @@ namespace OpenVic::_detail {
3340 constexpr T const * data () const { return _data_start_ptr; }
3441 constexpr bool empty () const { return _size == 0 ; }
3542
36- explicit FixedVector (const size_t capacity)
37- : _max_size(capacity),
38- _size(0 ),
43+ static FixedVector create_empty (const size_t capacity) { return FixedVector (capacity); }
44+
45+ FixedVector (const size_t size, T const & value_for_all_indices)
46+ : _max_size(size),
47+ _size (size),
3948 _allocator(),
40- _data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {}
49+ _data_start_ptr(allocator_traits::allocate(_allocator, size)) {
50+ std::fill (_data_start_ptr, _data_start_ptr + size, value_for_all_indices);
51+ }
4152
4253 // Generator (size_t i) -> U (where T is constructable from U)
4354 template <typename GeneratorTemplateType>
4455 // The generator must NOT return a tuple
4556 requires (!specialization_of<std::remove_cvref_t <std::invoke_result_t <GeneratorTemplateType, size_t >>, std::tuple>)
4657 // The type must be constructible from the generator's single return value
4758 && std::constructible_from<T, decltype(std::declval<GeneratorTemplateType>()(std::declval<size_t >()))>
48- FixedVector (size_t size, GeneratorTemplateType&& generator)
59+ FixedVector(const size_t size, GeneratorTemplateType&& generator)
4960 : _max_size(size),
5061 _size(size),
5162 _allocator(),
@@ -74,7 +85,7 @@ namespace OpenVic::_detail {
7485 )
7586 };
7687 }
77- FixedVector (size_t size, GeneratorTemplateType&& generator)
88+ FixedVector (const size_t size, GeneratorTemplateType&& generator)
7889 : _max_size(size),
7990 _size (size),
8091 _allocator(),
@@ -125,12 +136,12 @@ namespace OpenVic::_detail {
125136 const_reverse_iterator rend () const { return const_reverse_iterator (begin ()); }
126137 const_reverse_iterator crend () const { return const_reverse_iterator (begin ()); }
127138
128- T& operator [](size_t index) {
129- assert (index < _size && " Index out of bounds. " );
139+ T& operator [](const size_t index) {
140+ assert (index < _size);
130141 return _data_start_ptr[index];
131142 }
132- const T& operator [](size_t index) const {
133- assert (index < _size && " Index out of bounds. " );
143+ const T& operator [](const size_t index) const {
144+ assert (index < _size);
134145 return _data_start_ptr[index];
135146 }
136147
0 commit comments