Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/openvic-simulation/types/FixedVector.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <memory>
Expand Down Expand Up @@ -33,19 +34,30 @@ namespace OpenVic::_detail {
constexpr T const* data() const { return _data_start_ptr; }
constexpr bool empty() const { return _size == 0; }

/**
* @brief Creates an uninitialised vector with fixed capacity
*/
explicit FixedVector(const size_t capacity)
: _max_size(capacity),
_size(0),
_allocator(),
_data_start_ptr(allocator_traits::allocate(_allocator, capacity)) {}

FixedVector(const size_t size, T const& value_for_all_indices)
: _max_size(size),
_size(size),
_allocator(),
_data_start_ptr(allocator_traits::allocate(_allocator, size)) {
std::fill(_data_start_ptr, _data_start_ptr + size, value_for_all_indices);
}

//Generator (size_t i) -> U (where T is constructable from U)
template<typename GeneratorTemplateType>
// The generator must NOT return a tuple
requires (!specialization_of<std::remove_cvref_t<std::invoke_result_t<GeneratorTemplateType, size_t>>, std::tuple>)
// The type must be constructible from the generator's single return value
&& std::constructible_from<T, decltype(std::declval<GeneratorTemplateType>()(std::declval<size_t>()))>
FixedVector(size_t size, GeneratorTemplateType&& generator)
FixedVector(const size_t size, GeneratorTemplateType&& generator)
: _max_size(size),
_size(size),
_allocator(),
Expand Down Expand Up @@ -74,7 +86,7 @@ namespace OpenVic::_detail {
)
};
}
FixedVector(size_t size, GeneratorTemplateType&& generator)
FixedVector(const size_t size, GeneratorTemplateType&& generator)
: _max_size(size),
_size(size),
_allocator(),
Expand Down Expand Up @@ -125,12 +137,12 @@ namespace OpenVic::_detail {
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }

T& operator[](size_t index) {
assert(index < _size && "Index out of bounds.");
T& operator[](const size_t index) {
assert(index < _size);
return _data_start_ptr[index];
}
const T& operator[](size_t index) const {
assert(index < _size && "Index out of bounds.");
const T& operator[](const size_t index) const {
assert(index < _size);
return _data_start_ptr[index];
}

Expand Down
6 changes: 3 additions & 3 deletions src/openvic-simulation/types/IndexedFlatMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ namespace OpenVic {
) : keys(new_keys),
min_index { new_keys.front().index },
max_index { new_keys.back().index },
values(new_keys.size()) {
values { new_keys.size() } {
static_assert(has_index<ForwardedKeyType>);
if (!validate_new_keys(new_keys)) {
return;
Expand Down Expand Up @@ -363,7 +363,7 @@ namespace OpenVic {
) : keys(new_keys),
min_index { type_safe::get(new_keys.front().index) },
max_index { type_safe::get(new_keys.back().index) },
values(new_keys.size()) {
values { new_keys.size() } {
static_assert(has_index<ForwardedKeyType>);
if (!validate_new_keys(new_keys)) {
return;
Expand Down Expand Up @@ -428,7 +428,7 @@ namespace OpenVic {
: keys(new_keys),
min_index { type_safe::get(new_keys.front().index) },
max_index { type_safe::get(new_keys.back().index) },
values(new_keys.size()) {
values { new_keys.size() } {
static_assert(has_index<ForwardedKeyType>);
if (!validate_new_keys(new_keys)) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/openvic-simulation/types/TypedSpan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace OpenVic {
}

constexpr forwardable_span<ValueType, _Extent>::reference operator[](const IndexType _Off) const {
assert(_Off < size());
return forwardable_span<ValueType, _Extent>::operator[](static_cast<std::size_t>(type_safe::get(_Off)));
}

Expand Down
4 changes: 2 additions & 2 deletions src/openvic-simulation/utility/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void ThreadPool::loop_until_cancelled(
) {
IndexedFlatMap<GoodDefinition, char> reusable_goods_mask { good_keys };

memory::FixedVector<fixed_point_t> reusable_country_map_0 { country_keys.size() };
memory::FixedVector<fixed_point_t> reusable_country_map_1 { country_keys.size() };
memory::FixedVector<fixed_point_t> reusable_country_map_0 { country_keys.size(), fixed_point_t::_0 };
memory::FixedVector<fixed_point_t> reusable_country_map_1 { country_keys.size(), fixed_point_t::_0 };
TypedSpan<country_index_t, fixed_point_t> reusable_country_map_0_span { reusable_country_map_0 };
TypedSpan<country_index_t, fixed_point_t> reusable_country_map_1_span { reusable_country_map_1 };

Expand Down
10 changes: 5 additions & 5 deletions tests/src/types/FixedVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int DestructionCounter::destructor_count = 0;
// A simple test case to check the basic constructor and accessors with a simple type.
TEST_CASE("FixedVector Construction and basic accessors","[FixedVector]") {
constexpr size_t capacity = 5;
FixedVector<int> vec(capacity);
FixedVector<int> vec { capacity };

// Initial state check
CHECK(vec.size() == 0);
Expand Down Expand Up @@ -111,7 +111,7 @@ TEST_CASE("FixedVector Generator constructor (tuple)","[FixedVector]") {
// Test emplace_back, pop_back, and clear with a complex type.
TEST_CASE("FixedVector Manipulation (emplace_back, pop_back, clear)","[FixedVector]") {
constexpr size_t capacity = 4;
FixedVector<ComplexType> vec(capacity);
FixedVector<ComplexType> vec { capacity };

// Emplace elements with multiple arguments
vec.emplace_back(1, "hello");
Expand Down Expand Up @@ -153,7 +153,7 @@ TEST_CASE("FixedVector Manipulation (emplace_back, pop_back, clear)","[FixedVect
// Test that accessor methods return references to the same memory location
TEST_CASE("FixedVector Accessor reference consistency","[FixedVector]") {
constexpr size_t capacity = 3;
FixedVector<int> vec(capacity);
FixedVector<int> vec { capacity };

vec.emplace_back(1);
CHECK(&vec.front() == &vec.back());
Expand All @@ -171,7 +171,7 @@ TEST_CASE("FixedVector Accessor reference consistency","[FixedVector]") {
// Test with a non-copyable and non-movable type to ensure in-place construction.
TEST_CASE("FixedVector Non-copyable, non-movable type","[FixedVector]") {
constexpr size_t capacity = 2;
FixedVector<NonCopyableNonMovable> vec(capacity);
FixedVector<NonCopyableNonMovable> vec { capacity };

// This should work because emplace_back constructs in place
const int value0 = 1;
Expand All @@ -196,7 +196,7 @@ TEST_CASE("FixedVector Destruction, Clear, and Refill","[FixedVector]") {

{
// Use a scope to test the FixedVector's destructor
FixedVector<DestructionCounter> vec(capacity);
FixedVector<DestructionCounter> vec { capacity };

// Emplace a few objects
vec.emplace_back();
Expand Down