diff --git a/CMakeLists.txt b/CMakeLists.txt index 62d5355..f329385 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.136) +project(blt-gp VERSION 0.0.137) include(CTest) diff --git a/include/blt/gp/tree.h b/include/blt/gp/tree.h index 013d728..70644e8 100644 --- a/include/blt/gp/tree.h +++ b/include/blt/gp/tree.h @@ -58,6 +58,7 @@ namespace blt::gp class tree_t { + using iter_type = std::vector::const_iterator; public: [[nodiscard]] inline std::vector& get_operations() { @@ -111,11 +112,40 @@ namespace blt::gp return results.values.pop(); } - void print(gp_program& program, std::ostream& output, bool print_literals = true, bool pretty_indent = false, bool include_types = false) const; + void print(gp_program& program, std::ostream& output, bool print_literals = true, bool pretty_indent = false, + bool include_types = false) const; bool check(gp_program& program, void* context) const; blt::ptrdiff_t find_endpoint(blt::gp::gp_program& program, blt::ptrdiff_t start); + + // valid for [begin, end) + static blt::size_t total_value_bytes(iter_type begin, iter_type 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; + } + + [[nodiscard]] blt::size_t total_value_bytes(blt::size_t begin, blt::size_t end) const + { + return total_value_bytes(operations.begin() + static_cast(begin), + operations.begin() + static_cast(end)); + } + + [[nodiscard]] blt::size_t total_value_bytes(blt::size_t begin) const + { + return total_value_bytes(operations.begin() + static_cast(begin), operations.end()); + } + + [[nodiscard]] blt::size_t total_value_bytes() const + { + return total_value_bytes(operations.begin(), operations.end()); + } private: std::vector operations;