From c7bb4a434b25d3c918cc7908e0b527aa0101b73d Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 16 Jul 2024 00:25:23 -0400 Subject: [PATCH] i really should run a test --- CMakeLists.txt | 2 +- include/blt/gp/operations.h | 9 +++---- include/blt/gp/program.h | 2 +- include/blt/gp/selection.h | 4 ++-- include/blt/gp/stack.h | 47 +++++++++++++++++++------------------ 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 96aba59..0ea7bf4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/blt/gp/operations.h b/include/blt/gp/operations.h index f3602f3..32c43be 100644 --- a/include/blt/gp/operations.h +++ b/include/blt/gp/operations.h @@ -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() : 0)), ...); + ((offset += (current_index++ > index ? stack_allocator::aligned_size>() : 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)(std::forward(args)..., allocator.from(getByteOffset())...); + return std::forward(func)(std::forward(args)..., + allocator.from>(getByteOffset())...); } template @@ -95,8 +96,8 @@ namespace blt::gp { constexpr auto seq = std::make_integer_sequence(); Return ret = exec_sequence_to_indices(std::forward(func), read_allocator, seq, std::forward(args)...); - read_allocator.call_destructors(); - read_allocator.pop_bytes((stack_allocator::aligned_size() + ...)); + read_allocator.call_destructors...>(); + read_allocator.pop_bytes((stack_allocator::aligned_size>() + ...)); return ret; } }; diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 593e142..6362f20 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -114,7 +114,7 @@ namespace blt::gp if constexpr (sizeof...(Args) > 0) { - (add_non_context_argument(info.argument_types), ...); + (add_non_context_argument>(info.argument_types), ...); } info.argc.argc_context = info.argc.argc = sizeof...(Args); diff --git a/include/blt/gp/selection.h b/include/blt/gp/selection.h index 0518861..940172c 100644 --- a/include/blt/gp/selection.h +++ b/include/blt/gp/selection.h @@ -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; diff --git a/include/blt/gp/stack.h b/include/blt/gp/stack.h index e7bd161..90777fb 100644 --- a/include/blt/gp/stack.h +++ b/include/blt/gp/stack.h @@ -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 + using NO_REF_T = std::remove_cv_t>; public: struct size_data_t { @@ -160,30 +162,30 @@ namespace blt::gp template void push(T&& value) { - using NO_REF_T = std::remove_reference_t; + using NO_REF_T = std::remove_cv_t>; static_assert(std::is_trivially_copyable_v && "Type must be bitwise copyable!"); - auto ptr = allocate_bytes(); - head->metadata.offset = static_cast(ptr) + aligned_size(); + auto ptr = allocate_bytes(); + head->metadata.offset = static_cast(ptr) + aligned_size(); new(ptr) NO_REF_T(std::forward(value)); } template T pop() { - using NO_REF_T = std::remove_reference_t; + using NO_REF_T = std::remove_cv_t>; static_assert(std::is_trivially_copyable_v && "Type must be bitwise copyable!"); - constexpr static auto TYPE_SIZE = aligned_size(); + constexpr static auto TYPE_SIZE = aligned_size(); if (head == nullptr) throw std::runtime_error("Silly boi the stack is empty!"); if (head->used_bytes_in_block() < static_cast(aligned_size())) 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(head->metadata.offset - TYPE_SIZE); + NO_REF_T t = *reinterpret_cast(head->metadata.offset - TYPE_SIZE); // call destructor - reinterpret_cast(head->metadata.offset - TYPE_SIZE)->~T(); + reinterpret_cast(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 T& from(blt::size_t bytes) { - constexpr static auto TYPE_SIZE = aligned_size(); + using NO_REF_T = std::remove_cv_t>; + constexpr static auto TYPE_SIZE = aligned_size(); auto remaining_bytes = static_cast(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(aligned_size())) + if (blk->used_bytes_in_block() < static_cast(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((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((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(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(offset).~Args(), offset += stack_allocator::aligned_size()), ...); + + ((from>(offset).~NO_REF_T(), offset += stack_allocator::aligned_size>()), ...); } [[nodiscard]] bool empty() const @@ -374,7 +378,7 @@ namespace blt::gp template static inline constexpr blt::size_t aligned_size() noexcept { - return aligned_size(sizeof(T)); + return aligned_size(sizeof(NO_REF_T)); } static inline constexpr blt::size_t aligned_size(blt::size_t size) noexcept @@ -431,7 +435,7 @@ namespace blt::gp template void* allocate_bytes() { - return allocate_bytes(sizeof(T)); + return allocate_bytes(sizeof(NO_REF_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)