Compare commits
No commits in common. "19f94b14dc0922a4f3394f2c198b7b93d38d9f82" and "3972a70bc52f08d20d1572fb377ac0d5d178b6a5" have entirely different histories.
19f94b14dc
...
3972a70bc5
|
@ -1,5 +1,5 @@
|
|||
cmake_minimum_required(VERSION 3.25)
|
||||
project(blt-gp VERSION 0.0.118)
|
||||
project(blt-gp VERSION 0.0.116)
|
||||
|
||||
include(CTest)
|
||||
|
||||
|
|
|
@ -143,11 +143,51 @@ namespace blt::gp
|
|||
BLT_WARN("This stack is empty, we will copy no bytes from it!");
|
||||
return;
|
||||
}
|
||||
auto [start_block, bytes_left, start_point] = get_start_from_bytes(stack, bytes);
|
||||
|
||||
auto start_block = stack.head;
|
||||
auto bytes_left = static_cast<blt::ptrdiff_t>(bytes);
|
||||
blt::u8* start_point = nullptr;
|
||||
while (bytes_left > 0)
|
||||
{
|
||||
if (start_block == nullptr)
|
||||
{
|
||||
BLT_WARN("This stack doesn't contain enough space to copy %ld bytes!", bytes);
|
||||
BLT_WARN_STREAM << "State: " << size() << "\n";
|
||||
BLT_ABORT("Stack doesn't contain enough data for this copy operation!");
|
||||
}
|
||||
if (start_block->used_bytes_in_block() < bytes_left)
|
||||
{
|
||||
bytes_left -= start_block->used_bytes_in_block();
|
||||
start_block = start_block->metadata.prev;
|
||||
} else if (start_block->used_bytes_in_block() == bytes_left)
|
||||
{
|
||||
start_point = start_block->buffer;
|
||||
break;
|
||||
} else
|
||||
{
|
||||
start_point = start_block->metadata.offset - bytes_left;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bytes_left > 0)
|
||||
{
|
||||
allocate_block_to_head_for_size(bytes_left);
|
||||
auto insert = head;
|
||||
while (insert != nullptr)
|
||||
{
|
||||
if (insert->remaining_bytes_in_block() >= bytes_left)
|
||||
break;
|
||||
insert = insert->metadata.next;
|
||||
}
|
||||
// can directly copy into a block. this stack's head is now the insert point
|
||||
if (insert != nullptr && insert->remaining_bytes_in_block() >= bytes_left)
|
||||
head = insert;
|
||||
else
|
||||
{
|
||||
// need to push a block to the end.
|
||||
// make sure head is at the end.
|
||||
while (head != nullptr && head->metadata.next != nullptr)
|
||||
head = head->metadata.next;
|
||||
push_block(bytes_left);
|
||||
}
|
||||
std::memcpy(head->metadata.offset, start_point, bytes_left);
|
||||
head->metadata.offset += bytes_left;
|
||||
start_block = start_block->metadata.next;
|
||||
|
@ -155,44 +195,27 @@ namespace blt::gp
|
|||
// we now copy whole blocks at a time.
|
||||
while (start_block != nullptr)
|
||||
{
|
||||
allocate_block_to_head_for_size(start_block->used_bytes_in_block());
|
||||
auto prev = head;
|
||||
auto insert = head;
|
||||
while (insert != nullptr)
|
||||
{
|
||||
if (insert->remaining_bytes_in_block() >= start_block->used_bytes_in_block())
|
||||
break;
|
||||
prev = insert;
|
||||
insert = insert->metadata.next;
|
||||
}
|
||||
if (insert == nullptr)
|
||||
{
|
||||
head = prev;
|
||||
push_block(start_block->used_bytes_in_block());
|
||||
} else
|
||||
head = insert;
|
||||
std::memcpy(head->metadata.offset, start_block->buffer, start_block->used_bytes_in_block());
|
||||
head->metadata.offset += start_block->used_bytes_in_block();
|
||||
start_block = start_block->metadata.next;
|
||||
}
|
||||
}
|
||||
|
||||
void copy_from(blt::u8* data, blt::size_t bytes)
|
||||
{
|
||||
if (bytes == 0 || data == nullptr)
|
||||
return;
|
||||
allocate_block_to_head_for_size(bytes);
|
||||
std::memcpy(head->metadata.offset, data, bytes);
|
||||
head->metadata.offset += bytes;
|
||||
}
|
||||
|
||||
void copy_to(blt::u8* data, blt::size_t bytes) const
|
||||
{
|
||||
if (bytes == 0 || data == nullptr)
|
||||
return;
|
||||
auto [start_block, bytes_left, start_point] = get_start_from_bytes(*this, bytes);
|
||||
|
||||
blt::size_t write_point = 0;
|
||||
if (bytes_left > 0)
|
||||
{
|
||||
std::memcpy(data + write_point, start_point, bytes_left);
|
||||
write_point += bytes_left;
|
||||
start_block = start_block->metadata.next;
|
||||
}
|
||||
// we now copy whole blocks at a time.
|
||||
while (start_block != nullptr)
|
||||
{
|
||||
std::memcpy(data + write_point, start_block->buffer, start_block->used_bytes_in_block());
|
||||
write_point += start_block->used_bytes_in_block();
|
||||
start_block = start_block->metadata.next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes an instance of an object on to the stack
|
||||
* @tparam T type to push
|
||||
|
@ -271,21 +294,8 @@ namespace blt::gp
|
|||
|
||||
void pop_bytes(blt::ptrdiff_t bytes)
|
||||
{
|
||||
if (bytes == 0)
|
||||
return;
|
||||
if (empty())
|
||||
{
|
||||
BLT_WARN("Cannot pop %ld bytes", bytes);
|
||||
BLT_ABORT("Stack is empty, we cannot pop!");
|
||||
}
|
||||
while (bytes > 0)
|
||||
{
|
||||
if (head == nullptr)
|
||||
{
|
||||
BLT_WARN("The head is null, this stack doesn't contain enough data inside to pop %ld bytes!", bytes);
|
||||
BLT_WARN_STREAM << "Stack State: " << size() << "\n";
|
||||
BLT_ABORT("Stack doesn't contain enough data to preform a pop!");
|
||||
}
|
||||
auto diff = head->used_bytes_in_block() - bytes;
|
||||
// if there is not enough room left to pop completely off the block, then move to the next previous block
|
||||
// and pop from it, update the amount of bytes to reflect the amount removed from the current block
|
||||
|
@ -341,7 +351,7 @@ namespace blt::gp
|
|||
()), ...);
|
||||
}
|
||||
|
||||
[[nodiscard]] bool empty() const noexcept
|
||||
[[nodiscard]] bool empty() const
|
||||
{
|
||||
if (head == nullptr)
|
||||
return true;
|
||||
|
@ -350,7 +360,7 @@ namespace blt::gp
|
|||
return head->used_bytes_in_block() == 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] blt::ptrdiff_t bytes_in_head() const noexcept
|
||||
[[nodiscard]] blt::ptrdiff_t bytes_in_head() const
|
||||
{
|
||||
if (head == nullptr)
|
||||
return 0;
|
||||
|
@ -361,7 +371,7 @@ namespace blt::gp
|
|||
* Warning this function is slow!
|
||||
* @return the size of the stack allocator in bytes
|
||||
*/
|
||||
[[nodiscard]] size_data_t size() const noexcept
|
||||
[[nodiscard]] size_data_t size() const
|
||||
{
|
||||
size_data_t size_data;
|
||||
auto* prev = head;
|
||||
|
@ -394,7 +404,7 @@ namespace blt::gp
|
|||
|
||||
// TODO: cleanup this allocator!
|
||||
// if you keep track of type size information you can memcpy between stack allocators as you already only allow trivially copyable types
|
||||
stack_allocator(const stack_allocator& copy) noexcept
|
||||
stack_allocator(const stack_allocator& copy)
|
||||
{
|
||||
if (copy.empty())
|
||||
return;
|
||||
|
@ -434,7 +444,7 @@ namespace blt::gp
|
|||
return *this;
|
||||
}
|
||||
|
||||
~stack_allocator() noexcept
|
||||
~stack_allocator()
|
||||
{
|
||||
if (head != nullptr)
|
||||
{
|
||||
|
@ -460,27 +470,27 @@ namespace blt::gp
|
|||
return (size + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1);
|
||||
}
|
||||
|
||||
inline static constexpr auto metadata_size() noexcept
|
||||
inline static constexpr auto metadata_size()
|
||||
{
|
||||
return sizeof(typename block::block_metadata_t);
|
||||
}
|
||||
|
||||
inline static constexpr auto block_size() noexcept
|
||||
inline static constexpr auto block_size()
|
||||
{
|
||||
return sizeof(block);
|
||||
}
|
||||
|
||||
inline static constexpr auto page_size() noexcept
|
||||
inline static constexpr auto page_size()
|
||||
{
|
||||
return PAGE_SIZE;
|
||||
}
|
||||
|
||||
inline static constexpr auto page_size_no_meta() noexcept
|
||||
inline static constexpr auto page_size_no_meta()
|
||||
{
|
||||
return page_size() - metadata_size();
|
||||
}
|
||||
|
||||
inline static constexpr auto page_size_no_block() noexcept
|
||||
inline static constexpr auto page_size_no_block()
|
||||
{
|
||||
return page_size() - block_size();
|
||||
}
|
||||
|
@ -497,7 +507,7 @@ namespace blt::gp
|
|||
} metadata;
|
||||
blt::u8 buffer[8]{};
|
||||
|
||||
explicit block(blt::size_t size) noexcept
|
||||
explicit block(blt::size_t size)
|
||||
{
|
||||
#if BLT_DEBUG_LEVEL > 0
|
||||
if (size < PAGE_SIZE)
|
||||
|
@ -510,34 +520,27 @@ namespace blt::gp
|
|||
metadata.offset = buffer;
|
||||
}
|
||||
|
||||
void reset() noexcept
|
||||
void reset()
|
||||
{
|
||||
metadata.offset = buffer;
|
||||
}
|
||||
|
||||
[[nodiscard]] blt::ptrdiff_t storage_size() const noexcept
|
||||
[[nodiscard]] blt::ptrdiff_t storage_size() const
|
||||
{
|
||||
return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block::block_metadata_t));
|
||||
}
|
||||
|
||||
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const noexcept
|
||||
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const
|
||||
{
|
||||
return static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
|
||||
}
|
||||
|
||||
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const noexcept
|
||||
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const
|
||||
{
|
||||
return storage_size() - used_bytes_in_block();
|
||||
}
|
||||
};
|
||||
|
||||
struct copy_start_point
|
||||
{
|
||||
block* start_block;
|
||||
blt::ptrdiff_t bytes_left;
|
||||
blt::u8* start_point;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void* allocate_bytes()
|
||||
{
|
||||
|
@ -548,35 +551,25 @@ namespace blt::gp
|
|||
{
|
||||
auto ptr = get_aligned_pointer(size);
|
||||
if (ptr == nullptr)
|
||||
allocate_block_to_head_for_size(aligned_size(size));
|
||||
{
|
||||
while (head != nullptr && head->metadata.next != nullptr)
|
||||
{
|
||||
head = head->metadata.next;
|
||||
if (head != nullptr)
|
||||
head->reset();
|
||||
if (head->remaining_bytes_in_block() >= static_cast<blt::ptrdiff_t>(size))
|
||||
break;
|
||||
}
|
||||
if (head == nullptr || head->remaining_bytes_in_block() < static_cast<blt::ptrdiff_t>(size))
|
||||
push_block(aligned_size(size) + sizeof(typename block::block_metadata_t));
|
||||
}
|
||||
ptr = get_aligned_pointer(size);
|
||||
if (ptr == nullptr)
|
||||
throw std::bad_alloc();
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves forward through the list of "deallocated" blocks, if none meet size requirements it'll allocate a new block.
|
||||
* This function will take into account the size of the block metadata, but requires the size input to be aligned.
|
||||
* It will perform no modification to the size value.
|
||||
*
|
||||
* The block which allows for size is now at head.
|
||||
*/
|
||||
void allocate_block_to_head_for_size(const blt::size_t size) noexcept
|
||||
{
|
||||
while (head != nullptr && head->metadata.next != nullptr)
|
||||
{
|
||||
head = head->metadata.next;
|
||||
if (head != nullptr)
|
||||
head->reset();
|
||||
if (head->remaining_bytes_in_block() >= static_cast<blt::ptrdiff_t>(size))
|
||||
break;
|
||||
}
|
||||
if (head == nullptr || head->remaining_bytes_in_block() < static_cast<blt::ptrdiff_t>(size))
|
||||
push_block(size + sizeof(typename block::block_metadata_t));
|
||||
}
|
||||
|
||||
void* get_aligned_pointer(blt::size_t bytes) noexcept
|
||||
void* get_aligned_pointer(blt::size_t bytes)
|
||||
{
|
||||
if (head == nullptr)
|
||||
return nullptr;
|
||||
|
@ -585,7 +578,7 @@ namespace blt::gp
|
|||
return std::align(MAX_ALIGNMENT, bytes, pointer, remaining_bytes);
|
||||
}
|
||||
|
||||
void push_block(blt::size_t size) noexcept
|
||||
void push_block(blt::size_t size)
|
||||
{
|
||||
auto blk = allocate_block(size);
|
||||
if (head == nullptr)
|
||||
|
@ -598,13 +591,13 @@ namespace blt::gp
|
|||
head = blk;
|
||||
}
|
||||
|
||||
static size_t to_nearest_page_size(blt::size_t bytes) noexcept
|
||||
static size_t to_nearest_page_size(blt::size_t bytes)
|
||||
{
|
||||
constexpr static blt::size_t MASK = ~(PAGE_SIZE - 1);
|
||||
return (bytes & MASK) + PAGE_SIZE;
|
||||
}
|
||||
|
||||
static block* allocate_block(blt::size_t bytes) noexcept
|
||||
static block* allocate_block(blt::size_t bytes)
|
||||
{
|
||||
auto size = to_nearest_page_size(bytes);
|
||||
auto* data = std::aligned_alloc(PAGE_SIZE, size);
|
||||
|
@ -613,7 +606,7 @@ namespace blt::gp
|
|||
return reinterpret_cast<block*>(data);
|
||||
}
|
||||
|
||||
static void free_chain(block* current) noexcept
|
||||
static void free_chain(block* current)
|
||||
{
|
||||
while (current != nullptr)
|
||||
{
|
||||
|
@ -624,12 +617,12 @@ namespace blt::gp
|
|||
}
|
||||
}
|
||||
|
||||
static void free_block(block* ptr) noexcept
|
||||
static void free_block(block* ptr)
|
||||
{
|
||||
std::free(ptr);
|
||||
}
|
||||
|
||||
inline bool move_back() noexcept
|
||||
inline bool move_back()
|
||||
{
|
||||
auto old = head;
|
||||
head = head->metadata.prev;
|
||||
|
@ -641,36 +634,6 @@ namespace blt::gp
|
|||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline static copy_start_point get_start_from_bytes(const stack_allocator& stack, blt::size_t bytes)
|
||||
{
|
||||
auto start_block = stack.head;
|
||||
auto bytes_left = static_cast<blt::ptrdiff_t>(bytes);
|
||||
blt::u8* start_point = nullptr;
|
||||
while (bytes_left > 0)
|
||||
{
|
||||
if (start_block == nullptr)
|
||||
{
|
||||
BLT_WARN("This stack doesn't contain enough space to copy %ld bytes!", bytes);
|
||||
BLT_WARN_STREAM << "State: " << stack.size() << "\n";
|
||||
BLT_ABORT("Stack doesn't contain enough data for this copy operation!");
|
||||
}
|
||||
if (start_block->used_bytes_in_block() < bytes_left)
|
||||
{
|
||||
bytes_left -= start_block->used_bytes_in_block();
|
||||
start_block = start_block->metadata.prev;
|
||||
} else if (start_block->used_bytes_in_block() == bytes_left)
|
||||
{
|
||||
start_point = start_block->buffer;
|
||||
break;
|
||||
} else
|
||||
{
|
||||
start_point = start_block->metadata.offset - bytes_left;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return copy_start_point{start_block, bytes_left, start_point};
|
||||
}
|
||||
|
||||
private:
|
||||
block* head = nullptr;
|
||||
};
|
||||
|
|
|
@ -34,6 +34,10 @@ namespace blt::gp
|
|||
|
||||
blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, const std::vector<blt::gp::op_container_t>& container, blt::ptrdiff_t start);
|
||||
|
||||
void transfer_forward(blt::gp::stack_allocator& from, blt::gp::stack_allocator& to, detail::op_iter begin, detail::op_iter end);
|
||||
|
||||
void transfer_backward(blt::gp::stack_allocator& from, blt::gp::stack_allocator& to, detail::op_iter begin, detail::op_iter end);
|
||||
|
||||
class crossover_t
|
||||
{
|
||||
public:
|
||||
|
|
2
lib/blt
2
lib/blt
|
@ -1 +1 @@
|
|||
Subproject commit f9938691ecb13b7b6acb2402235015bef9d58982
|
||||
Subproject commit 8535480ad57553e592640b98e9e005d7a4d27501
|
|
@ -19,39 +19,13 @@
|
|||
#include <blt/gp/program.h>
|
||||
#include <blt/std/ranges.h>
|
||||
#include <blt/std/utility.h>
|
||||
#include <algorithm>
|
||||
#include <blt/std/memory.h>
|
||||
#include <blt/profiling/profiler_v2.h>
|
||||
#include <random>
|
||||
|
||||
namespace blt::gp
|
||||
{
|
||||
|
||||
inline blt::size_t accumulate_type_sizes(detail::op_iter begin, detail::op_iter end)
|
||||
{
|
||||
blt::size_t total = 0;
|
||||
for (auto it = begin; it != end; ++it)
|
||||
{
|
||||
if (it->is_value)
|
||||
total += stack_allocator::aligned_size(it->type_size);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
template<typename>
|
||||
blt::u8* get_thread_pointer_for_size(blt::size_t bytes)
|
||||
{
|
||||
static thread_local blt::expanding_buffer<blt::u8> buffer;
|
||||
if (bytes > buffer.size())
|
||||
buffer.resize(bytes);
|
||||
return buffer.data();
|
||||
}
|
||||
|
||||
grow_generator_t grow_generator;
|
||||
|
||||
mutation_t::config_t::config_t(): generator(grow_generator)
|
||||
{}
|
||||
|
||||
blt::expected<crossover_t::result_t, crossover_t::error_t> crossover_t::apply(gp_program& program, const tree_t& p1, const tree_t& p2) // NOLINT
|
||||
{
|
||||
result_t result{p1, p2};
|
||||
|
@ -76,8 +50,8 @@ namespace blt::gp
|
|||
auto found_point_begin_itr = c2_ops.begin() + point->p2_crossover_point;
|
||||
auto found_point_end_itr = c2_ops.begin() + find_endpoint(program, c2_ops, point->p2_crossover_point);
|
||||
|
||||
stack_allocator& c1_stack = c1.get_values();
|
||||
stack_allocator& c2_stack = c2.get_values();
|
||||
stack_allocator& c1_stack_init = c1.get_values();
|
||||
stack_allocator& c2_stack_init = c2.get_values();
|
||||
|
||||
// we have to make a copy because we will modify the underlying storage.
|
||||
std::vector<op_container_t> c1_operators;
|
||||
|
@ -88,26 +62,24 @@ namespace blt::gp
|
|||
for (const auto& op : blt::iterate(found_point_begin_itr, found_point_end_itr))
|
||||
c2_operators.push_back(op);
|
||||
|
||||
blt::size_t c1_stack_after_bytes = accumulate_type_sizes(crossover_point_end_itr, c1_ops.end());
|
||||
blt::size_t c1_stack_for_bytes = accumulate_type_sizes(crossover_point_begin_itr, crossover_point_end_itr);
|
||||
blt::size_t c2_stack_after_bytes = accumulate_type_sizes(found_point_end_itr, c2_ops.end());
|
||||
blt::size_t c2_stack_for_bytes = accumulate_type_sizes(found_point_begin_itr, found_point_end_itr);
|
||||
auto c1_total = static_cast<blt::ptrdiff_t>(c1_stack_after_bytes + c1_stack_for_bytes);
|
||||
auto c2_total = static_cast<blt::ptrdiff_t>(c2_stack_after_bytes + c2_stack_for_bytes);
|
||||
auto copy_ptr_c1 = get_thread_pointer_for_size<struct c1>(c1_total);
|
||||
auto copy_ptr_c2 = get_thread_pointer_for_size<struct c2>(c2_total);
|
||||
stack_allocator c1_stack_after_copy;
|
||||
stack_allocator c1_stack_for_copy;
|
||||
stack_allocator c2_stack_after_copy;
|
||||
stack_allocator c2_stack_for_copy;
|
||||
|
||||
c1_stack.copy_to(copy_ptr_c1, c1_total);
|
||||
c1_stack.pop_bytes(c1_total);
|
||||
|
||||
c2_stack.copy_to(copy_ptr_c2, c2_total);
|
||||
c2_stack.pop_bytes(c2_total);
|
||||
|
||||
c2_stack.copy_from(copy_ptr_c1, c1_stack_for_bytes);
|
||||
c2_stack.copy_from(copy_ptr_c2 + c2_stack_for_bytes, c2_stack_after_bytes);
|
||||
|
||||
c1_stack.copy_from(copy_ptr_c2, c2_stack_for_bytes);
|
||||
c1_stack.copy_from(copy_ptr_c1 + c1_stack_for_bytes, c1_stack_after_bytes);
|
||||
// transfer all values after the crossover point. these will need to be transferred back to child2
|
||||
transfer_backward(c1_stack_init, c1_stack_after_copy, c1_ops.end() - 1, crossover_point_end_itr - 1);
|
||||
// transfer all values for the crossover point.
|
||||
transfer_backward(c1_stack_init, c1_stack_for_copy, crossover_point_end_itr - 1, crossover_point_begin_itr - 1);
|
||||
// transfer child2 values for copying back into c1
|
||||
transfer_backward(c2_stack_init, c2_stack_after_copy, c2_ops.end() - 1, found_point_end_itr - 1);
|
||||
transfer_backward(c2_stack_init, c2_stack_for_copy, found_point_end_itr - 1, found_point_begin_itr - 1);
|
||||
// now copy back into the respective children
|
||||
transfer_forward(c2_stack_for_copy, c1.get_values(), found_point_begin_itr, found_point_end_itr);
|
||||
transfer_forward(c1_stack_for_copy, c2.get_values(), crossover_point_begin_itr, crossover_point_end_itr);
|
||||
// now copy after the crossover point back to the correct children
|
||||
transfer_forward(c1_stack_after_copy, c1.get_values(), crossover_point_end_itr, c1_ops.end());
|
||||
transfer_forward(c2_stack_after_copy, c2.get_values(), found_point_end_itr, c2_ops.end());
|
||||
|
||||
// now swap the operators
|
||||
auto insert_point_c1 = crossover_point_begin_itr - 1;
|
||||
|
@ -223,18 +195,26 @@ namespace blt::gp
|
|||
auto& new_ops_r = new_tree.get_operations();
|
||||
auto& new_vals_r = new_tree.get_values();
|
||||
|
||||
blt::size_t total_bytes_after = accumulate_type_sizes(end_itr, ops_r.end());
|
||||
auto* stack_after_data = get_thread_pointer_for_size<struct mutation>(total_bytes_after);
|
||||
stack_allocator stack_after;
|
||||
blt::size_t total_bytes_after = 0;
|
||||
for (auto it = end_itr; it != ops_r.end(); it++)
|
||||
{
|
||||
if (it->is_value)
|
||||
total_bytes_after += stack_allocator::aligned_size(it->type_size);
|
||||
}
|
||||
// transfer_backward(vals_r, stack_after, ops_r.end() - 1, end_itr - 1);
|
||||
stack_after.copy_from(vals_r, total_bytes_after);
|
||||
vals_r.pop_bytes(static_cast<blt::ptrdiff_t>(total_bytes_after));
|
||||
for (auto it = end_itr - 1; it != begin_itr - 1; it--)
|
||||
{
|
||||
if (it->is_value)
|
||||
vals_r.pop_bytes(static_cast<blt::ptrdiff_t>(stack_allocator::aligned_size(it->type_size)));
|
||||
}
|
||||
|
||||
// make a copy of any stack data after the mutation point / children.
|
||||
vals_r.copy_to(stack_after_data, total_bytes_after);
|
||||
|
||||
// remove the bytes of the data after the mutation point and the data for the children of the mutation node.
|
||||
vals_r.pop_bytes(static_cast<blt::ptrdiff_t>(total_bytes_after + accumulate_type_sizes(begin_itr, end_itr)));
|
||||
|
||||
// insert the new tree then move back the data from after the original mutation point.
|
||||
vals_r.insert(std::move(new_vals_r));
|
||||
vals_r.copy_from(stack_after_data, total_bytes_after);
|
||||
//transfer_forward(stack_after, vals_r, end_itr, ops_r.end());
|
||||
vals_r.copy_from(stack_after, total_bytes_after);
|
||||
stack_after = {};
|
||||
|
||||
auto before = begin_itr - 1;
|
||||
ops_r.erase(begin_itr, end_itr);
|
||||
|
@ -243,7 +223,7 @@ namespace blt::gp
|
|||
// this will check to make sure that the tree is in a correct and executable state. it requires that the evaluation is context free!
|
||||
#if BLT_DEBUG_LEVEL >= 2
|
||||
BLT_ASSERT(new_vals_r.empty());
|
||||
//BLT_ASSERT(stack_after.empty());
|
||||
BLT_ASSERT(stack_after.empty());
|
||||
blt::size_t bytes_expected = 0;
|
||||
auto bytes_size = vals_r.size().total_used_bytes;
|
||||
|
||||
|
@ -287,6 +267,9 @@ namespace blt::gp
|
|||
return c;
|
||||
}
|
||||
|
||||
mutation_t::config_t::config_t(): generator(grow_generator)
|
||||
{}
|
||||
|
||||
blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, const std::vector<blt::gp::op_container_t>& container, blt::ptrdiff_t index)
|
||||
{
|
||||
blt::i64 children_left = 0;
|
||||
|
@ -304,4 +287,23 @@ namespace blt::gp
|
|||
|
||||
return index;
|
||||
}
|
||||
|
||||
void transfer_backward(stack_allocator& from, stack_allocator& to, detail::op_iter begin, detail::op_iter end)
|
||||
{
|
||||
for (auto it = begin; it != end; it--)
|
||||
{
|
||||
if (it->is_value)
|
||||
from.transfer_bytes(to, it->type_size);
|
||||
}
|
||||
}
|
||||
|
||||
void transfer_forward(stack_allocator& from, stack_allocator& to, detail::op_iter begin, detail::op_iter end)
|
||||
{
|
||||
// now copy back into the respective children
|
||||
for (auto it = begin; it != end; it++)
|
||||
{
|
||||
if (it->is_value)
|
||||
from.transfer_bytes(to, it->type_size);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue