Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "openvic-simulation/DefinitionManager.hpp"
#include "openvic-simulation/console/ConsoleInstance.hpp"
#include "openvic-simulation/core/stl/containers/TypedSpan.hpp"
#include "openvic-simulation/misc/GameAction.hpp"
#include "openvic-simulation/types/TypedSpan.hpp"
#include "openvic-simulation/utility/Logger.hpp"

using namespace OpenVic;
Expand Down
9 changes: 3 additions & 6 deletions src/openvic-simulation/core/memory/CowPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
#include <foonathan/memory/std_allocator.hpp>

#include "openvic-simulation/core/memory/MemoryTracker.hpp"
#include "openvic-simulation/types/CowPtr.hpp"
#include "openvic-simulation/core/stl/containers/CowPtr.hpp"

namespace OpenVic::memory {
template<typename T, class RawAllocator = foonathan::memory::default_allocator>
using cow_ptr = ::OpenVic::cow_ptr<
T,
foonathan::memory::std_allocator<T, tracker<RawAllocator>>
>;
}
using cow_ptr = ::OpenVic::stl::cow_ptr<T, foonathan::memory::std_allocator<T, tracker<RawAllocator>>>;
}
9 changes: 3 additions & 6 deletions src/openvic-simulation/core/memory/CowVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
#include <foonathan/memory/std_allocator.hpp>

#include "openvic-simulation/core/memory/MemoryTracker.hpp"
#include "openvic-simulation/types/CowVector.hpp"
#include "openvic-simulation/core/stl/containers/CowVector.hpp"

namespace OpenVic::memory {
template<typename T, class RawAllocator = foonathan::memory::default_allocator>
using cow_vector = ::OpenVic::cow_vector<
T,
foonathan::memory::std_allocator<T, tracker<RawAllocator>>
>;
}
using cow_vector = ::OpenVic::stl::cow_vector<T, foonathan::memory::std_allocator<T, tracker<RawAllocator>>>;
}
12 changes: 12 additions & 0 deletions src/openvic-simulation/core/memory/FixedVector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <foonathan/memory/default_allocator.hpp>
#include <foonathan/memory/std_allocator.hpp>
Comment thread
Spartan322 marked this conversation as resolved.

#include "openvic-simulation/core/memory/MemoryTracker.hpp"
#include "openvic-simulation/core/stl/containers/FixedVector.hpp"

namespace OpenVic::memory {
template<typename T, typename SizeT = std::size_t, class RawAllocator = foonathan::memory::default_allocator>
using FixedVector = ::OpenVic::stl::FixedVector<T, SizeT, foonathan::memory::std_allocator<T, tracker<RawAllocator>>>;
}
12 changes: 12 additions & 0 deletions src/openvic-simulation/core/memory/RingBuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <foonathan/memory/default_allocator.hpp>
#include <foonathan/memory/std_allocator.hpp>
Comment thread
Spartan322 marked this conversation as resolved.

#include "openvic-simulation/core/memory/MemoryTracker.hpp"
#include "openvic-simulation/core/stl/containers/RingBuffer.hpp"

namespace OpenVic::memory {
template<typename T, class RawAllocator = foonathan::memory::default_allocator>
using RingBuffer = ::OpenVic::stl::RingBuffer<T, foonathan::memory::std_allocator<T, tracker<RawAllocator>>>;
}
2 changes: 1 addition & 1 deletion src/openvic-simulation/core/portable/ForwardableSpan.hpp
Comment thread
wvpm marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <type_traits>

#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/types/BasicIterator.hpp"
#include "openvic-simulation/core/stl/BasicIterator.hpp"

namespace OpenVic::_detail::forwardable_span {
static constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <concepts>
#include <iterator>

#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/core/Compare.hpp"
#include "openvic-simulation/core/Typedefs.hpp"

namespace OpenVic {
template<typename Pointer, typename ContainerTag>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "openvic-simulation/core/template/Concepts.hpp"
#include "openvic-simulation/core/Typedefs.hpp"

namespace OpenVic {
namespace OpenVic::stl {
/**
* A Copy-On-Write pointer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
#include <memory>
#include <span>
#include <type_traits>
#include <vector>
Comment thread
Spartan322 marked this conversation as resolved.

#include "openvic-simulation/core/Assert.hpp"
#include "openvic-simulation/types/BasicIterator.hpp"
#include "openvic-simulation/utility/Allocator.hpp"
#include "openvic-simulation/core/Compare.hpp"
#include "openvic-simulation/core/stl/BasicIterator.hpp"
#include "openvic-simulation/core/template/Concepts.hpp"
#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/utility/Allocator.hpp"

namespace OpenVic {
namespace OpenVic::stl {
/**
* A Copy-On-Write vector (replicating std::vector)
*
Expand Down Expand Up @@ -97,8 +98,7 @@ namespace OpenVic {
swap(other, *this);
} else if (!other.empty()) {
_data = _allocate_payload(other.size());
_data->array_end =
uninitialized_move(other._data->array, other._data->array_end, _data->array, alloc);
_data->array_end = uninitialized_move(other._data->array, other._data->array_end, _data->array, alloc);
destroy(_data->array, _data->array_end, alloc);
_data->array_end = _data->array;
}
Expand Down Expand Up @@ -449,8 +449,7 @@ namespace OpenVic {
if constexpr (move_insertable_allocator<allocator_type>) {
_relocate(_data->array, _data->array_end, new_data->array, alloc);
} else {
new_data->array_end =
uninitialized_move(_data->array, _data->array_end, new_data->array, alloc);
new_data->array_end = uninitialized_move(_data->array, _data->array_end, new_data->array, alloc);
destroy(_data->array, _data->array_end, alloc);
}
_deallocate_payload(_data);
Expand Down Expand Up @@ -1115,37 +1114,36 @@ namespace OpenVic {
return std::lexicographical_compare_three_way(x.begin(), x.end(), y.begin(), y.end(), three_way_compare);
}

namespace cow {
template<specialization_of<cow_vector> T>
T const& read(T& v) {
return v;
}

template<specialization_of<cow_vector> T>
typename T::writer& write(T& v) {
return v.write();
}
}

static_assert(
sizeof(cow_vector<int>) == sizeof(cow_vector<int>::writer), "cow_vector must always be the same size as it's writer"
);
}

namespace OpenVic::cow {
template<specialization_of<::OpenVic::stl::cow_vector> T>
T const& read(T& v) {
return v;
}

template<specialization_of<::OpenVic::stl::cow_vector> T>
typename T::writer& write(T& v) {
return v.write();
}
}

namespace std {
template<typename T, typename Allocator>
inline void swap( //
typename OpenVic::cow_vector<T, Allocator>::writer& x, typename OpenVic::cow_vector<T, Allocator>::writer& y
typename ::OpenVic::stl::cow_vector<T, Allocator>::writer& x, typename ::OpenVic::stl::cow_vector<T, Allocator>::writer& y
) {
x.swap(y);
}

template<typename T, typename Allocator, typename Predicate>
inline typename OpenVic::cow_vector<T, Allocator>::size_type erase_if( //
typename OpenVic::cow_vector<T, Allocator>::writer& cont, Predicate pred
inline typename ::OpenVic::stl::cow_vector<T, Allocator>::size_type erase_if( //
typename ::OpenVic::stl::cow_vector<T, Allocator>::writer& cont, Predicate pred
) {
using namespace OpenVic;
typename cow_vector<T, Allocator>::writer& ucont = cont;
typename ::OpenVic::stl::cow_vector<T, Allocator>::writer& ucont = cont;
const auto orig_size = cont.size();
const auto end = ucont.end();
decltype(end) removed = std::remove_if(ucont.begin(), end, std::ref(pred));
Expand All @@ -1158,11 +1156,10 @@ namespace std {
}

template<typename T, typename Allocator, typename U>
inline typename OpenVic::cow_vector<T, Allocator>::size_type erase( //
typename OpenVic::cow_vector<T, Allocator>::writer& cont, U const& value
inline typename ::OpenVic::stl::cow_vector<T, Allocator>::size_type erase( //
typename ::OpenVic::stl::cow_vector<T, Allocator>::writer& cont, U const& value
) {
using namespace OpenVic;
typename cow_vector<T, Allocator>::writer& ucont = cont;
typename ::OpenVic::stl::cow_vector<T, Allocator>::writer& ucont = cont;
const auto orig_size = cont.size();
const auto end = ucont.end();
decltype(end) removed = std::remove_if(ucont.begin(), end, [&value](auto it) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "openvic-simulation/core/Typedefs.hpp"
#include "openvic-simulation/types/ConstructorTags.hpp"

namespace OpenVic::_detail {
namespace OpenVic::stl {
// fixed capacity vector
// supports immovable + uncopyable value types
template <typename T, typename SizeT = std::size_t, typename Allocator = std::allocator<T>>
Expand All @@ -23,6 +23,7 @@ namespace OpenVic::_detail {
using reference = T&;
using size_type = SizeT;
using value_type = T;
using allocator_type = Allocator;
private:
using allocator_traits = std::allocator_traits<Allocator>;
size_type _max_size;
Expand All @@ -45,24 +46,30 @@ namespace OpenVic::_detail {
constexpr bool empty() const { return _size == size_type{}; }

constexpr explicit FixedVector(const create_empty_t t) noexcept : FixedVector(t, size_type{}) {}
constexpr explicit FixedVector(const create_empty_t, const size_type capacity, std::type_identity_t<Allocator> const& alloc)
: _max_size(capacity),
_size(size_type{}),
_allocator(alloc),
_data_start_ptr(allocate(_allocator, capacity)) {}

/**
* @brief Creates an uninitialised vector with fixed capacity
*/
constexpr explicit FixedVector(const create_empty_t, const size_type capacity)
: _max_size(capacity),
_size(size_type{}),
_allocator(),
_data_start_ptr(allocate(_allocator, capacity)) {}
: FixedVector(create_empty, capacity, Allocator()) {}

constexpr FixedVector(const size_type size, T const& value_for_all_indices)
: FixedVector(size, value_for_all_indices, Allocator()) {}

constexpr FixedVector(const size_type size, T const& value_for_all_indices, std::type_identity_t<Allocator> const& alloc)
: _max_size(size),
_size(size),
_allocator(),
_allocator(alloc),
_data_start_ptr(allocate(_allocator, size)) {
std::fill(_data_start_ptr, _data_start_ptr + get_index_as_size_t(size), value_for_all_indices);
}
constexpr explicit FixedVector(const generate_values_t, const size_type capacity)

constexpr FixedVector(const generate_values_t, const size_type capacity, std::type_identity_t<Allocator> const& alloc)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const size_type size, not capacity my bad...
Could you fix it in this PR?

requires (
std::constructible_from<value_type, size_type>
|| std::is_default_constructible_v<value_type>
Expand All @@ -77,16 +84,32 @@ namespace OpenVic::_detail {
}
) {}

constexpr FixedVector(const generate_values_t, const size_type capacity)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size, not capacity

requires (
std::constructible_from<value_type, size_type>
|| std::is_default_constructible_v<value_type>
) : FixedVector(generate_values, capacity, Allocator()) {}

//Generator (size_type 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_type>>, 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_type>()))>
constexpr FixedVector(const size_type size, GeneratorTemplateType&& generator)
: FixedVector(size, std::forward<GeneratorTemplateType>(generator), Allocator()) {
}

//Generator (size_type 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_type>>, 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_type>()))>
constexpr FixedVector(const size_type size, GeneratorTemplateType&& generator, std::type_identity_t<Allocator> const& alloc)
: _max_size(size),
_size(size_type{}),
_allocator(),
_allocator(alloc),
_data_start_ptr(allocate(_allocator, size)) {
for (size_type i {}; i < size; ++i) {
allocator_traits::construct(
Expand Down Expand Up @@ -114,9 +137,28 @@ namespace OpenVic::_detail {
};
}
constexpr FixedVector(const size_type size, GeneratorTemplateType&& generator)
: FixedVector(size, std::forward<GeneratorTemplateType>(generator), Allocator()) {
}

//Generator (size_type i) -> std::tuple<Args...> (where T is constructable from Args)
template<typename GeneratorTemplateType>
// The generator must return a tuple
requires specialization_of<std::remove_cvref_t<std::invoke_result_t<GeneratorTemplateType, size_type>>, std::tuple>
// The tuple must be constructible into a T
&& requires(GeneratorTemplateType&& generator) {
{
std::apply(
[](auto&&... args) {
T obj{std::forward<decltype(args)>(args)...};
},
generator(std::declval<size_type>())
)
};
}
constexpr FixedVector(const size_type size, GeneratorTemplateType&& generator, std::type_identity_t<Allocator> const& alloc)
: _max_size(size),
_size(size_type{}),
_allocator(),
_allocator(alloc),
_data_start_ptr(allocate(_allocator, size)) {
for (size_type i {}; i < size; ++i) {
std::apply(
Expand Down Expand Up @@ -156,7 +198,7 @@ namespace OpenVic::_detail {
clear();
allocator_traits::deallocate(_allocator, _data_start_ptr, get_index_as_size_t(_max_size));
}

using iterator = T*;
using const_iterator = T const*;

Expand All @@ -167,7 +209,7 @@ namespace OpenVic::_detail {
constexpr iterator end() { return begin() + get_index_as_size_t(_size); }
constexpr const_iterator end() const { return begin() + get_index_as_size_t(_size); }
constexpr const_iterator cend() const { return cbegin() + get_index_as_size_t(_size); }

using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

Expand Down Expand Up @@ -239,14 +281,9 @@ namespace OpenVic::_detail {
}
_size = {};
}

constexpr allocator_type get_allocator() const {
return _allocator;
}
};
}

#include <foonathan/memory/std_allocator.hpp>

#include "openvic-simulation/core/memory/MemoryTracker.hpp"

namespace OpenVic::memory {
template<typename T, typename SizeT = std::size_t, class RawAllocator = foonathan::memory::default_allocator>
using FixedVector = _detail::FixedVector<T, SizeT, foonathan::memory::std_allocator<T, tracker<RawAllocator>>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
#include "openvic-simulation/core/Assert.hpp"
#include "openvic-simulation/core/Typedefs.hpp"

namespace OpenVic {

namespace OpenVic::stl {
/// RingBuffer datatype
/// Generally most of the interface should be a drop-in replacement for std::vector
template<typename T, typename Allocator = std::allocator<T>>
Expand Down Expand Up @@ -799,6 +798,6 @@ namespace OpenVic {

// The allocator is used to allocate memory, and to construct and destroy
// elements.
[[no_unique_address]] allocator_type _allocator {};
OV_NO_UNIQUE_ADDRESS allocator_type _allocator {};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <type_safe/strong_typedef.hpp>

#include "openvic-simulation/core/Assert.hpp"
#include "openvic-simulation/core/portable/ForwardableSpan.hpp"
#include "openvic-simulation/core/template/Concepts.hpp"

Expand All @@ -29,7 +30,7 @@ namespace OpenVic {
}

[[nodiscard]] constexpr forwardable_span<ValueType, _Extent>::reference operator[](const IndexType index) const {
assert(index < size());
OV_HARDEN_ASSERT_ACCESS(index, "operator[]");
Comment thread
Spartan322 marked this conversation as resolved.
return forwardable_span<ValueType, _Extent>::operator[](static_cast<std::size_t>(type_safe::get(index)));
}

Expand Down
Loading
Loading