From d4a02ec833f2e608930065c82b1a7308b60ff9c2 Mon Sep 17 00:00:00 2001 From: Chris Blume Date: Wed, 19 Nov 2025 17:51:35 +0900 Subject: [PATCH 1/5] Add NodeIndexType --- Code/max/Containers/StateMachine/Node.hpp | 22 ++++++++++--------- .../Containers/StateMachine/StateMachine.hpp | 12 +++++----- .../Containers/StateMachine/Transition.hpp | 4 ++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Code/max/Containers/StateMachine/Node.hpp b/Code/max/Containers/StateMachine/Node.hpp index 12bc53d..f5377c1 100644 --- a/Code/max/Containers/StateMachine/Node.hpp +++ b/Code/max/Containers/StateMachine/Node.hpp @@ -16,25 +16,26 @@ namespace max { namespace Containers { namespace StateMachine { - template + template class Node { public: - constexpr explicit Node(std::tuple outbound_transitions) noexcept - : outbound_transitions_(std::move(outbound_transitions)) + constexpr explicit Node(NodeIndexType this_node, std::tuple outbound_transitions) noexcept + : this_node_(std::move(this_node)) + , outbound_transitions_(std::move(outbound_transitions)) {} - template - constexpr std::optional AttemptTransition(const T& input) noexcept { + template + constexpr std::optional AttemptTransition(const T& input) noexcept { auto transition_happened = false; - auto new_node_index = std::optional{std::nullopt}; + auto new_node_index = std::optional{std::nullopt}; auto attempt_transition = [&transition_happened, &new_node_index, &input](auto&& arg) { // TODO: I added .value_ to RangeMatcher because I couldn't get this line to work //if constexpr (std::is_same_v) { if constexpr (std::is_same_v) { if (!transition_happened) { - auto possible_new_node_index = arg.AttemptTransition(input); + auto possible_new_node_index = arg.AttemptTransition(input); if (possible_new_node_index) { transition_happened = true; new_node_index = std::move(possible_new_node_index); @@ -50,13 +51,14 @@ namespace StateMachine { return new_node_index; } + NodeIndexType this_node_; std::tuple outbound_transitions_; }; - template - constexpr Node MakeNode(TransitionTypes... outbound_transitions) noexcept { - return Node{std::make_tuple(std::move(outbound_transitions) ...)}; + template + constexpr Node MakeNode(NodeIndexType this_node, TransitionTypes... outbound_transitions) noexcept { + return Node{std::move(this_node), std::make_tuple(std::move(outbound_transitions) ...)}; } } // namespace StateMachine diff --git a/Code/max/Containers/StateMachine/StateMachine.hpp b/Code/max/Containers/StateMachine/StateMachine.hpp index f698a22..c20faf5 100644 --- a/Code/max/Containers/StateMachine/StateMachine.hpp +++ b/Code/max/Containers/StateMachine/StateMachine.hpp @@ -12,13 +12,13 @@ namespace max { namespace Containers { namespace StateMachine { - template + template class StateMachine { public: - constexpr explicit StateMachine(std::tuple nodes) noexcept + constexpr explicit StateMachine(NodeIndexType starting_node, std::tuple nodes) noexcept : nodes_(std::move(nodes)) - , current_node_index_(0) + , current_node_index_(std::move(starting_node)) {} template @@ -26,8 +26,8 @@ namespace StateMachine { auto transition_happened = false; auto i = size_t{0}; auto attempt_transition = [this, &transition_happened, &i, &input](auto&& arg) { - if (!transition_happened && current_node_index_ == i++) { - auto possible_new_node_index = arg.AttemptTransition(input); + if (!transition_happened && current_node_index_ == arg.this_node_) { + auto possible_new_node_index = arg.AttemptTransition(input); if (possible_new_node_index) { transition_happened = true; current_node_index_ = std::move(possible_new_node_index.value()); @@ -41,7 +41,7 @@ namespace StateMachine { } std::tuple nodes_; - size_t current_node_index_; + NodeIndexType current_node_index_; }; diff --git a/Code/max/Containers/StateMachine/Transition.hpp b/Code/max/Containers/StateMachine/Transition.hpp index 5c88251..3c3af28 100644 --- a/Code/max/Containers/StateMachine/Transition.hpp +++ b/Code/max/Containers/StateMachine/Transition.hpp @@ -21,8 +21,8 @@ namespace StateMachine { , callback_(std::move(callback)) {} - template - constexpr std::optional AttemptTransition(const T& input) noexcept { + template + constexpr std::optional AttemptTransition(const T& input) noexcept { if (matcher_.DoesMatch(input)) { return callback_(std::move(input)); } From 5e9dbbd691d82aad94b5633519e7c478b3f0f405 Mon Sep 17 00:00:00 2001 From: Chris Blume Date: Wed, 19 Nov 2025 22:44:05 +0900 Subject: [PATCH 2/5] Foo --- Code/max/Containers/StateMachine/Node.hpp | 4 +-- Code/max/Containers/StateMachine/NodeTest.cpp | 30 +++++++++++++------ .../Containers/StateMachine/StateMachine.hpp | 8 ++--- .../StateMachine/StateMachineTest.cpp | 14 +++++---- .../StateMachine/TransitionTest.cpp | 12 +++++--- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/Code/max/Containers/StateMachine/Node.hpp b/Code/max/Containers/StateMachine/Node.hpp index f5377c1..99b0c1e 100644 --- a/Code/max/Containers/StateMachine/Node.hpp +++ b/Code/max/Containers/StateMachine/Node.hpp @@ -25,8 +25,8 @@ namespace StateMachine { , outbound_transitions_(std::move(outbound_transitions)) {} - template - constexpr std::optional AttemptTransition(const T& input) noexcept { + template + constexpr std::optional AttemptTransition(const T& input) noexcept { auto transition_happened = false; auto new_node_index = std::optional{std::nullopt}; diff --git a/Code/max/Containers/StateMachine/NodeTest.cpp b/Code/max/Containers/StateMachine/NodeTest.cpp index a12e2fd..2a0d3b2 100644 --- a/Code/max/Containers/StateMachine/NodeTest.cpp +++ b/Code/max/Containers/StateMachine/NodeTest.cpp @@ -32,7 +32,9 @@ namespace StateMachine { return size_t{2}; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { @@ -40,9 +42,11 @@ namespace StateMachine { return size_t{3}; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; - auto node = max::Containers::StateMachine::MakeNode(std::move(range_transition), std::move(string_transition)); + auto node = max::Containers::StateMachine::MakeNode(size_t{0}, std::move(range_transition), std::move(string_transition)); auto new_node_index = node.AttemptTransition(uint32_t{1}); @@ -59,7 +63,9 @@ namespace StateMachine { return 2; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { @@ -67,9 +73,11 @@ namespace StateMachine { return 3; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; - auto node = max::Containers::StateMachine::MakeNode(std::move(range_transition), std::move(string_transition)); + auto node = max::Containers::StateMachine::MakeNode(0, std::move(range_transition), std::move(string_transition)); auto new_node_index = node.AttemptTransition(uint32_t{2}); @@ -86,7 +94,9 @@ namespace StateMachine { return 2; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { @@ -94,9 +104,11 @@ namespace StateMachine { return 3; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; - auto node = max::Containers::StateMachine::MakeNode(std::move(range_transition), std::move(string_transition)); + auto node = max::Containers::StateMachine::MakeNode(0, std::move(range_transition), std::move(string_transition)); auto new_node_index = node.AttemptTransition(std::string_view{"test"}); diff --git a/Code/max/Containers/StateMachine/StateMachine.hpp b/Code/max/Containers/StateMachine/StateMachine.hpp index c20faf5..5e15bb2 100644 --- a/Code/max/Containers/StateMachine/StateMachine.hpp +++ b/Code/max/Containers/StateMachine/StateMachine.hpp @@ -17,8 +17,8 @@ namespace StateMachine { public: constexpr explicit StateMachine(NodeIndexType starting_node, std::tuple nodes) noexcept - : nodes_(std::move(nodes)) - , current_node_index_(std::move(starting_node)) + : current_node_index_(std::move(starting_node)) + , nodes_(std::move(nodes)) {} template @@ -27,7 +27,7 @@ namespace StateMachine { auto i = size_t{0}; auto attempt_transition = [this, &transition_happened, &i, &input](auto&& arg) { if (!transition_happened && current_node_index_ == arg.this_node_) { - auto possible_new_node_index = arg.AttemptTransition(input); + auto possible_new_node_index = arg.AttemptTransition(input); if (possible_new_node_index) { transition_happened = true; current_node_index_ = std::move(possible_new_node_index.value()); @@ -40,8 +40,8 @@ namespace StateMachine { }, nodes_); } - std::tuple nodes_; NodeIndexType current_node_index_; + std::tuple nodes_; }; diff --git a/Code/max/Containers/StateMachine/StateMachineTest.cpp b/Code/max/Containers/StateMachine/StateMachineTest.cpp index 9df8178..c259401 100644 --- a/Code/max/Containers/StateMachine/StateMachineTest.cpp +++ b/Code/max/Containers/StateMachine/StateMachineTest.cpp @@ -32,18 +32,22 @@ namespace StateMachine { range_callback_called = true; return size_t{1}; }; - auto range_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback)}; - auto node_0 = max::Containers::StateMachine::MakeNode(std::move(range_transition)); + auto range_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(range_callback) + }; + auto node_0 = max::Containers::StateMachine::MakeNode(size_t{0}, std::move(range_transition)); bool string_callback_called = false; auto string_callback = [&string_callback_called](const std::string_view& /*input*/) { string_callback_called = true; return size_t{0}; }; - auto string_transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback)}; - auto node_1 = max::Containers::StateMachine::MakeNode(std::move(string_transition)); + auto string_transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::StringMatcher{std::string_view{"test"}}, std::move(string_callback) + }; + auto node_1 = max::Containers::StateMachine::MakeNode(size_t{1}, std::move(string_transition)); - auto state_machine = max::Containers::StateMachine::StateMachine{std::make_tuple(std::move(node_0), std::move(node_1))}; + auto state_machine = max::Containers::StateMachine::StateMachine{size_t{0}, std::make_tuple(std::move(node_0), std::move(node_1))}; state_machine.AttemptTransition(uint32_t{1}); diff --git a/Code/max/Containers/StateMachine/TransitionTest.cpp b/Code/max/Containers/StateMachine/TransitionTest.cpp index f9b1aae..ff16703 100644 --- a/Code/max/Containers/StateMachine/TransitionTest.cpp +++ b/Code/max/Containers/StateMachine/TransitionTest.cpp @@ -30,9 +30,11 @@ namespace StateMachine { return size_t{2}; }; - auto transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback)}; + auto transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback) + }; - auto new_node_index = transition.AttemptTransition(uint32_t{2}); + auto new_node_index = transition.AttemptTransition(uint32_t{2}); // TODO: This should be constexpr, right?? //static_assert( !callback_called, "" ); @@ -48,9 +50,11 @@ namespace StateMachine { return size_t{2}; }; - auto transition = max::Containers::StateMachine::Transition{max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback)}; + auto transition = max::Containers::StateMachine::Transition{ + max::Containers::StateMachine::RangeMatcher{0, 1}, std::move(callback) + }; - auto new_node_index = transition.AttemptTransition(uint32_t{1}); + auto new_node_index = transition.AttemptTransition(uint32_t{1}); CurrentTest.MAX_TESTING_ASSERT( callback_called ); CurrentTest.MAX_TESTING_ASSERT( new_node_index == size_t{2} ); From db246fe19740c19b9ddc122da1f18cf841d5992f Mon Sep 17 00:00:00 2001 From: Chris Blume Date: Wed, 19 Nov 2025 22:50:57 +0900 Subject: [PATCH 3/5] Foo --- Code/max/Containers/StateMachine/NodeTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/max/Containers/StateMachine/NodeTest.cpp b/Code/max/Containers/StateMachine/NodeTest.cpp index 2a0d3b2..330c621 100644 --- a/Code/max/Containers/StateMachine/NodeTest.cpp +++ b/Code/max/Containers/StateMachine/NodeTest.cpp @@ -112,7 +112,7 @@ namespace StateMachine { auto new_node_index = node.AttemptTransition(std::string_view{"test"}); - CurrentTest.MAX_TESTING_ASSERT( new_node_index == size_t{3} ); + CurrentTest.MAX_TESTING_ASSERT( new_node_index == 3 ); CurrentTest.MAX_TESTING_ASSERT( !range_callback_called ); CurrentTest.MAX_TESTING_ASSERT( string_callback_called ); } From 15b8727af8f985cc9ac466d119a9ddffa27cad35 Mon Sep 17 00:00:00 2001 From: Chris Blume Date: Wed, 19 Nov 2025 22:54:26 +0900 Subject: [PATCH 4/5] Foo --- Code/max/Containers/StateMachine/Node.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/max/Containers/StateMachine/Node.hpp b/Code/max/Containers/StateMachine/Node.hpp index 99b0c1e..61b735e 100644 --- a/Code/max/Containers/StateMachine/Node.hpp +++ b/Code/max/Containers/StateMachine/Node.hpp @@ -35,7 +35,7 @@ namespace StateMachine { //if constexpr (std::is_same_v) { if constexpr (std::is_same_v) { if (!transition_happened) { - auto possible_new_node_index = arg.AttemptTransition(input); + auto possible_new_node_index = arg.template AttemptTransition(input); if (possible_new_node_index) { transition_happened = true; new_node_index = std::move(possible_new_node_index); From 9e02ef65bc2cb7bbebd88c581292107f468bae75 Mon Sep 17 00:00:00 2001 From: Chris Blume Date: Wed, 19 Nov 2025 23:01:47 +0900 Subject: [PATCH 5/5] Foo --- Code/max/Containers/StateMachine/StateMachine.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Code/max/Containers/StateMachine/StateMachine.hpp b/Code/max/Containers/StateMachine/StateMachine.hpp index 5e15bb2..50b382a 100644 --- a/Code/max/Containers/StateMachine/StateMachine.hpp +++ b/Code/max/Containers/StateMachine/StateMachine.hpp @@ -24,8 +24,7 @@ namespace StateMachine { template constexpr void AttemptTransition(T input) noexcept { auto transition_happened = false; - auto i = size_t{0}; - auto attempt_transition = [this, &transition_happened, &i, &input](auto&& arg) { + auto attempt_transition = [this, &transition_happened, &input](auto&& arg) { if (!transition_happened && current_node_index_ == arg.this_node_) { auto possible_new_node_index = arg.AttemptTransition(input); if (possible_new_node_index) {