i really should run a test

thread
Brett 2024-07-16 00:25:23 -04:00
parent 9ff86161bb
commit c7bb4a434b
5 changed files with 33 additions and 31 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.0.83)
project(blt-gp VERSION 0.0.84)
include(CTest)

View File

@ -78,7 +78,7 @@ namespace blt::gp
{
blt::size_t offset = 0;
blt::size_t current_index = 0;
((offset += (current_index++ > index ? stack_allocator::aligned_size<Args>() : 0)), ...);
((offset += (current_index++ > index ? stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() : 0)), ...);
return offset;
}
@ -87,7 +87,8 @@ namespace blt::gp
ExtraArgs&& ... args)
{
// expands Args and indices, providing each argument with its index calculating the current argument byte offset
return std::forward<Func>(func)(std::forward<ExtraArgs>(args)..., allocator.from<Args>(getByteOffset<indices>())...);
return std::forward<Func>(func)(std::forward<ExtraArgs>(args)...,
allocator.from<detail::remove_cv_ref<Args>>(getByteOffset<indices>())...);
}
template<typename Func, typename... ExtraArgs>
@ -95,8 +96,8 @@ namespace blt::gp
{
constexpr auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
Return ret = exec_sequence_to_indices(std::forward<Func>(func), read_allocator, seq, std::forward<ExtraArgs>(args)...);
read_allocator.call_destructors<Args...>();
read_allocator.pop_bytes((stack_allocator::aligned_size<Args>() + ...));
read_allocator.call_destructors<detail::remove_cv_ref<Args>...>();
read_allocator.pop_bytes((stack_allocator::aligned_size<detail::remove_cv_ref<Args>>() + ...));
return ret;
}
};

View File

@ -114,7 +114,7 @@ namespace blt::gp
if constexpr (sizeof...(Args) > 0)
{
(add_non_context_argument<Args>(info.argument_types), ...);
(add_non_context_argument<detail::remove_cv_ref<Args>>(info.argument_types), ...);
}
info.argc.argc_context = info.argc.argc = sizeof...(Args);

View File

@ -52,8 +52,8 @@ namespace blt::gp
{
for (blt::size_t i = 0; i < config.elites; i++)
{
BLT_INFO("%lf >= %lf? // %lf (indexes: %ld %ld)", ind.second.fitness.adjusted_fitness, values[i].second,
ind.second.fitness.raw_fitness, ind.first, values[i].first);
// BLT_INFO("%lf >= %lf? // %lf (indexes: %ld %ld)", ind.second.fitness.adjusted_fitness, values[i].second,
// ind.second.fitness.raw_fitness, ind.first, values[i].first);
if (ind.second.fitness.adjusted_fitness >= values[i].second)
{
bool doesnt_contain = true;

View File

@ -108,7 +108,7 @@ namespace blt::gp
void push_block()
{
block * block;
block* block;
if (deallocated_blocks.empty())
block = allocate_block();
else
@ -141,6 +141,8 @@ namespace blt::gp
{
constexpr static blt::size_t PAGE_SIZE = 0x1000;
constexpr static blt::size_t MAX_ALIGNMENT = 8;
template<typename T>
using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>;
public:
struct size_data_t
{
@ -160,30 +162,30 @@ namespace blt::gp
template<typename T>
void push(T&& value)
{
using NO_REF_T = std::remove_reference_t<T>;
using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>;
static_assert(std::is_trivially_copyable_v<NO_REF_T> && "Type must be bitwise copyable!");
auto ptr = allocate_bytes<T>();
head->metadata.offset = static_cast<blt::u8*>(ptr) + aligned_size<T>();
auto ptr = allocate_bytes<NO_REF_T>();
head->metadata.offset = static_cast<blt::u8*>(ptr) + aligned_size<NO_REF_T>();
new(ptr) NO_REF_T(std::forward<T>(value));
}
template<typename T>
T pop()
{
using NO_REF_T = std::remove_reference_t<T>;
using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>;
static_assert(std::is_trivially_copyable_v<NO_REF_T> && "Type must be bitwise copyable!");
constexpr static auto TYPE_SIZE = aligned_size<T>();
constexpr static auto TYPE_SIZE = aligned_size<NO_REF_T>();
if (head == nullptr)
throw std::runtime_error("Silly boi the stack is empty!");
if (head->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(aligned_size<T>()))
throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string(
head->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(T))).c_str());
head->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(NO_REF_T))).c_str());
if (head->used_bytes_in_block() == 0)
move_back();
// make copy
T t = *reinterpret_cast<T*>(head->metadata.offset - TYPE_SIZE);
NO_REF_T t = *reinterpret_cast<NO_REF_T*>(head->metadata.offset - TYPE_SIZE);
// call destructor
reinterpret_cast<T*>(head->metadata.offset - TYPE_SIZE)->~T();
reinterpret_cast<NO_REF_T*>(head->metadata.offset - TYPE_SIZE)->~NO_REF_T();
// move offset back
head->metadata.offset -= TYPE_SIZE;
return t;
@ -192,7 +194,8 @@ namespace blt::gp
template<typename T>
T& from(blt::size_t bytes)
{
constexpr static auto TYPE_SIZE = aligned_size<T>();
using NO_REF_T = std::remove_cv_t<std::remove_reference_t<T>>;
constexpr static auto TYPE_SIZE = aligned_size<NO_REF_T>();
auto remaining_bytes = static_cast<blt::i64>(bytes);
blt::i64 bytes_into_block = 0;
block* blk = head;
@ -211,10 +214,10 @@ namespace blt::gp
}
if (blk == nullptr)
throw std::runtime_error("Some nonsense is going on. This function already smells");
if (blk->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(aligned_size<T>()))
if (blk->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(TYPE_SIZE))
throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string(
blk->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(T))).c_str());
return *reinterpret_cast<T*>((blk->metadata.offset - bytes_into_block) - TYPE_SIZE);
blk->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(NO_REF_T))).c_str());
return *reinterpret_cast<NO_REF_T*>((blk->metadata.offset - bytes_into_block) - TYPE_SIZE);
}
void pop_bytes(blt::ptrdiff_t bytes)
@ -264,13 +267,13 @@ namespace blt::gp
*/
void transfer_bytes(stack_allocator& to, blt::size_t bytes)
{
while (!empty() && head->used_bytes_in_block() == 0)
move_back();
if (empty())
throw std::runtime_error("This stack is empty!");
if (head->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(bytes))
BLT_ABORT("This stack doesn't contain enough data for this type! This is an invalid runtime state!");
if (head->used_bytes_in_block() == 0)
move_back();
BLT_ABORT(("This stack doesn't contain enough data for this type! " + std::to_string(head->used_bytes_in_block()) + " / " +
std::to_string(bytes) + " This is an invalid runtime state!").c_str());
auto type_size = aligned_size(bytes);
auto ptr = to.allocate_bytes(bytes);
@ -283,7 +286,8 @@ namespace blt::gp
void call_destructors()
{
blt::size_t offset = 0;
((from<Args>(offset).~Args(), offset += stack_allocator::aligned_size<Args>()), ...);
((from<NO_REF_T<Args>>(offset).~NO_REF_T<Args>(), offset += stack_allocator::aligned_size<NO_REF_T<Args>>()), ...);
}
[[nodiscard]] bool empty() const
@ -374,7 +378,7 @@ namespace blt::gp
template<typename T>
static inline constexpr blt::size_t aligned_size() noexcept
{
return aligned_size(sizeof(T));
return aligned_size(sizeof(NO_REF_T<T>));
}
static inline constexpr blt::size_t aligned_size(blt::size_t size) noexcept
@ -431,7 +435,7 @@ namespace blt::gp
template<typename T>
void* allocate_bytes()
{
return allocate_bytes(sizeof(T));
return allocate_bytes(sizeof(NO_REF_T<T>));
}
void* allocate_bytes(blt::size_t size)
@ -506,10 +510,7 @@ namespace blt::gp
auto old = head;
head = head->metadata.prev;
if (head == nullptr)
{
head = old;
head->reset();
}
//free_chain(old);
// required to prevent silly memory :3
// if (head != nullptr)