From ecd97bd701ed20da6c463fcea3a2763d34d56f0d Mon Sep 17 00:00:00 2001 From: kaoru Date: Sun, 30 Oct 2022 15:14:19 +0900 Subject: [PATCH 1/5] Add a parameter to tetengo::lattice::node #265 --- .../c/src/tetengo_lattice_constraint.cpp | 1 + .../src/tetengo_lattice_constraintElement.cpp | 2 ++ .../c/src/tetengo_lattice_nBestIterator.cpp | 1 + .../lattice/c/src/tetengo_lattice_node.cpp | 9 ++++--- .../lattice/c/src/tetengo_lattice_path.cpp | 1 + .../c/src/tetengo_lattice_vocabulary.cpp | 1 + .../cpp/include/tetengo/lattice/node.hpp | 20 ++++++++++++++-- .../cpp/src/tetengo.lattice.lattice.cpp | 1 + .../lattice/cpp/src/tetengo.lattice.node.cpp | 3 ++- .../src/test_tetengo.lattice.constraint.cpp | 22 ++++++++--------- ...est_tetengo.lattice.constraint_element.cpp | 6 ++--- .../test/src/test_tetengo.lattice.node.cpp | 24 +++++++++---------- ...etengo.lattice.node_constraint_element.cpp | 8 +++---- .../test/src/test_tetengo.lattice.path.cpp | 6 ++--- ...tengo.lattice.unordered_map_vocabulary.cpp | 1 + .../src/test_tetengo.lattice.vocabulary.cpp | 1 + ...go.lattice.wildcard_constraint_element.cpp | 8 +++---- 17 files changed, 72 insertions(+), 43 deletions(-) diff --git a/library/lattice/c/src/tetengo_lattice_constraint.cpp b/library/lattice/c/src/tetengo_lattice_constraint.cpp index 9e633eea..3ad5c73c 100644 --- a/library/lattice/c/src/tetengo_lattice_constraint.cpp +++ b/library/lattice/c/src/tetengo_lattice_constraint.cpp @@ -53,6 +53,7 @@ namespace cpp_path.emplace_back( p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), + 0, // TODO: p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_cost_lists.back(), p_node->best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_constraintElement.cpp b/library/lattice/c/src/tetengo_lattice_constraintElement.cpp index c4f06e5d..cd17f94c 100644 --- a/library/lattice/c/src/tetengo_lattice_constraintElement.cpp +++ b/library/lattice/c/src/tetengo_lattice_constraintElement.cpp @@ -49,6 +49,7 @@ tetengo_lattice_constraintElement_createNodeConstraintElement(const tetengo_latt BOOST_SCOPE_EXIT_END; tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), + 0, // TODO: p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_costs, p_node->best_preceding_node, @@ -119,6 +120,7 @@ int tetengo_lattice_constraintElement_matches( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), + 0, // TODO: p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_costs, p_node->best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp b/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp index a0ad0eea..fc950ec8 100644 --- a/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp +++ b/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp @@ -84,6 +84,7 @@ tetengo_lattice_nBestIterator_t* tetengo_lattice_nBestIterator_create( p_eos_node->p_preceding_edge_costs + p_eos_node->preceding_edge_cost_count); tetengo::lattice::node cpp_eos_node{ p_cpp_node_key ? &p_cpp_node_key->cpp_input() : nullptr, reinterpret_cast(p_eos_node->value_handle), + 0, // TODO: p_eos_node->index_in_step, p_eos_node->preceding_step, std::to_address(p_cpp_preceding_edge_costs), p_eos_node->best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_node.cpp b/library/lattice/c/src/tetengo_lattice_node.cpp index 0f0b7760..df948e47 100644 --- a/library/lattice/c/src/tetengo_lattice_node.cpp +++ b/library/lattice/c/src/tetengo_lattice_node.cpp @@ -138,9 +138,9 @@ bool tetengo_lattice_node_toNode( reinterpret_cast(p_entry->value_handle), p_entry->cost }; const std::vector cpp_preceding_edge_costs{}; - const tetengo::lattice::node cpp_node{ - cpp_entry, preceding_step, &cpp_preceding_edge_costs, best_preceding_node, path_cost - }; + const tetengo::lattice::node cpp_node{ cpp_entry, 0, // TODO: index_in_step + preceding_step, &cpp_preceding_edge_costs, + best_preceding_node, path_cost }; p_node->key_handle = reinterpret_cast(cpp_node.p_key()); p_node->value_handle = reinterpret_cast(&cpp_node.value()); @@ -185,6 +185,7 @@ bool tetengo_lattice_node_equal( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_one{ p_one_key ? &p_one_key->cpp_input() : nullptr, reinterpret_cast(p_one->value_handle), + 0, // TODO: p_one->index_in_step, p_one->preceding_step, &cpp_preceding_edge_costs_one, p_one->best_preceding_node, @@ -201,6 +202,7 @@ bool tetengo_lattice_node_equal( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_another{ p_another_key ? &p_another_key->cpp_input() : nullptr, reinterpret_cast(p_another->value_handle), + 0, // TODO: p_another->index_in_step, p_another->preceding_step, &cpp_preceding_edge_costs_another, p_another->best_preceding_node, @@ -235,6 +237,7 @@ bool tetengo_lattice_node_isBos(const tetengo_lattice_node_t* const p_node) BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), + 0, // TODO: p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_costs, p_node->best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_path.cpp b/library/lattice/c/src/tetengo_lattice_path.cpp index e24378dc..62d63069 100644 --- a/library/lattice/c/src/tetengo_lattice_path.cpp +++ b/library/lattice/c/src/tetengo_lattice_path.cpp @@ -98,6 +98,7 @@ tetengo_lattice_path_create(const tetengo_lattice_node_t* const p_nodes, const s cpp_nodes.emplace_back( p_cpp_node_key ? &p_cpp_node_key->cpp_input() : nullptr, reinterpret_cast(node.value_handle), + 0, // TODO: node.index_in_step, node.preceding_step, &cpp_preceding_edge_cost_list, node.best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_vocabulary.cpp b/library/lattice/c/src/tetengo_lattice_vocabulary.cpp index b98e6b7f..5bbc6149 100644 --- a/library/lattice/c/src/tetengo_lattice_vocabulary.cpp +++ b/library/lattice/c/src/tetengo_lattice_vocabulary.cpp @@ -349,6 +349,7 @@ bool tetengo_lattice_vocabulary_findConnection( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_from{ p_cpp_from_key ? &p_cpp_from_key->cpp_input() : nullptr, reinterpret_cast(p_from->value_handle), + 0, // TODO: p_from->index_in_step, p_from->preceding_step, &cpp_preceding_edge_costs, p_from->best_preceding_node, diff --git a/library/lattice/cpp/include/tetengo/lattice/node.hpp b/library/lattice/cpp/include/tetengo/lattice/node.hpp index d4963ba1..f3f5d53f 100644 --- a/library/lattice/cpp/include/tetengo/lattice/node.hpp +++ b/library/lattice/cpp/include/tetengo/lattice/node.hpp @@ -65,6 +65,7 @@ namespace tetengo::lattice \param p_key A pointer to a key. \param p_value A pointer to a value. + \param index_in_step An index in the step. \param preceding_step An index of a preceding step. \param p_preceding_edge_costs A pointer to preceding edge costs. \param best_preceding_node An index of a best preceding node. @@ -74,6 +75,7 @@ namespace tetengo::lattice constexpr node( const input* p_key, const std::any* p_value, + std::size_t index_in_step, std::size_t preceding_step, const std::vector* p_preceding_edge_costs, std::size_t best_preceding_node, @@ -81,6 +83,7 @@ namespace tetengo::lattice int path_cost) : m_p_key{ p_key }, m_p_value{ p_value }, + m_index_in_step{ index_in_step }, m_preceding_step{ preceding_step }, m_p_preceding_edge_costs{ p_preceding_edge_costs }, m_best_preceding_node{ best_preceding_node }, @@ -92,6 +95,7 @@ namespace tetengo::lattice \brief Creates a node from a vocabulary entry. \param entry An entry. + \param index_in_step An index in the step. \param preceding_step An index of a preceding step. \param p_preceding_edge_costs A pointer to preceding edge costs. \param best_preceding_node An index of a best preceding node. @@ -101,12 +105,13 @@ namespace tetengo::lattice */ constexpr node( const entry_view& entry, + std::size_t index_in_step, std::size_t preceding_step, const std::vector* p_preceding_edge_costs, std::size_t best_preceding_node, int path_cost) : - node{ entry.p_key(), entry.value(), preceding_step, p_preceding_edge_costs, - best_preceding_node, entry.cost(), path_cost } + node{ entry.p_key(), entry.value(), index_in_step, preceding_step, + p_preceding_edge_costs, best_preceding_node, entry.cost(), path_cost } {} @@ -151,6 +156,15 @@ namespace tetengo::lattice return *m_p_value; } + /*! + \brief Returns the index in the step. + + \return The index in the step. + */ + [[nodiscard]] constexpr std::size_t index_in_step() const + { + return m_index_in_step; + } /*! \brief Returns the index of the preceding step. @@ -218,6 +232,8 @@ namespace tetengo::lattice const std::any* m_p_value; + std::size_t m_index_in_step; + std::size_t m_preceding_step; const std::vector* m_p_preceding_edge_costs; diff --git a/library/lattice/cpp/src/tetengo.lattice.lattice.cpp b/library/lattice/cpp/src/tetengo.lattice.lattice.cpp index 300727a6..49621cf9 100644 --- a/library/lattice/cpp/src/tetengo.lattice.lattice.cpp +++ b/library/lattice/cpp/src/tetengo.lattice.lattice.cpp @@ -143,6 +143,7 @@ namespace tetengo::lattice nodes.emplace_back( entry, + j, i, &preceding_edge_costs, best_preceding_node_index_, diff --git a/library/lattice/cpp/src/tetengo.lattice.node.cpp b/library/lattice/cpp/src/tetengo.lattice.node.cpp index 73235237..f726d122 100644 --- a/library/lattice/cpp/src/tetengo.lattice.node.cpp +++ b/library/lattice/cpp/src/tetengo.lattice.node.cpp @@ -17,6 +17,7 @@ namespace tetengo::lattice node node::bos(const std::vector* const p_preceding_edge_costs) { return node{ entry_view::bos_eos(), + 0, std::numeric_limits::max(), p_preceding_edge_costs, std::numeric_limits::max(), @@ -29,7 +30,7 @@ namespace tetengo::lattice const std::size_t best_preceding_node, const int path_cost) { - return node{ entry_view::bos_eos(), preceding_step, p_preceding_edge_costs, best_preceding_node, path_cost }; + return node{ entry_view::bos_eos(), 0, preceding_step, p_preceding_edge_costs, best_preceding_node, path_cost }; } bool node::is_bos() const diff --git a/library/lattice/test/src/test_tetengo.lattice.constraint.cpp b/library/lattice/test/src/test_tetengo.lattice.constraint.cpp index d92cb9a6..f63749a1 100644 --- a/library/lattice/test/src/test_tetengo.lattice.constraint.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.constraint.cpp @@ -66,9 +66,9 @@ namespace static const tetengo::lattice::string_input key_tsubame{ "tsubame" }; static const std::vector singleton{ tetengo::lattice::node::bos(&bos_preceding_edge_costs()), - tetengo::lattice::node{ &key_mizuho, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_sakura, &node_value(), 1, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_tsubame, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_mizuho, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_sakura, &node_value(), 0, 1, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_tsubame, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 }, tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0) }; return singleton; @@ -81,9 +81,9 @@ namespace static const tetengo::lattice::string_input key_tsubame{ "tsubame" }; static const std::vector singleton{ tetengo::lattice::node::bos(&bos_preceding_edge_costs()), - tetengo::lattice::node{ &key_mizuho, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_ariake, &node_value(), 1, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_tsubame, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_mizuho, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_ariake, &node_value(), 0, 1, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_tsubame, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 }, tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0) }; return singleton; @@ -95,8 +95,8 @@ namespace static const tetengo::lattice::string_input key_tsubame{ "tsubame" }; static const std::vector singleton{ tetengo::lattice::node::bos(&bos_preceding_edge_costs()), - tetengo::lattice::node{ &key_hinokuni, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_tsubame, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_hinokuni, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_tsubame, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 }, tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0) }; return singleton; @@ -109,9 +109,9 @@ namespace static const tetengo::lattice::string_input key_kumagawa{ "kumagawa" }; static const std::vector singleton{ tetengo::lattice::node::bos(&bos_preceding_edge_costs()), - tetengo::lattice::node{ &key_kamome, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_sakura, &node_value(), 1, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_kumagawa, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_kamome, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_sakura, &node_value(), 0, 1, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_kumagawa, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 }, tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0) }; return singleton; diff --git a/library/lattice/test/src/test_tetengo.lattice.constraint_element.cpp b/library/lattice/test/src/test_tetengo.lattice.constraint_element.cpp index 5d28c026..fa10aa1f 100644 --- a/library/lattice/test/src/test_tetengo.lattice.constraint_element.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.constraint_element.cpp @@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) < 0); } @@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "sakura" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 3, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 3, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) == 0); } @@ -81,7 +81,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "tsubame" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 5, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 5, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) > 0); } diff --git a/library/lattice/test/src/test_tetengo.lattice.node.cpp b/library/lattice/test/src/test_tetengo.lattice.node.cpp index 4b210e00..cf786d76 100644 --- a/library/lattice/test/src/test_tetengo.lattice.node.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.node.cpp @@ -157,14 +157,14 @@ BOOST_AUTO_TEST_CASE(construction) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; } { const tetengo::lattice::string_input entry_key{ "mizuho" }; const std::any entry_value{ 42 }; const tetengo::lattice::entry_view entry{ &entry_key, &entry_value, 24 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ entry, 1, &preceding_edge_costs, 5, 2424 }; + const tetengo::lattice::node node_{ entry, 53, 1, &preceding_edge_costs, 5, 2424 }; BOOST_TEST(node_.p_key() == &entry_key); BOOST_TEST(std::any_cast(node_.value()) == 42); @@ -324,11 +324,11 @@ BOOST_AUTO_TEST_CASE(operator_equal) const std::any value1{ 42 }; const std::vector preceding_edge_costs1{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node1{ &key, &value1, 1, &preceding_edge_costs1, 5, 24, 2424 }; + const tetengo::lattice::node node1{ &key, &value1, 53, 1, &preceding_edge_costs1, 5, 24, 2424 }; const std::any value2{ 42 }; const std::vector preceding_edge_costs2{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node2{ &key, &value2, 1, &preceding_edge_costs2, 5, 24, 2424 }; + const tetengo::lattice::node node2{ &key, &value2, 53, 1, &preceding_edge_costs2, 5, 24, 2424 }; BOOST_CHECK(bos == bos); BOOST_CHECK(bos != eos); @@ -401,7 +401,7 @@ BOOST_AUTO_TEST_CASE(key) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST_REQUIRE(node_.p_key()); BOOST_TEST_REQUIRE(node_.p_key()->is()); @@ -445,7 +445,7 @@ BOOST_AUTO_TEST_CASE(value) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(std::any_cast(node_.value()) == 42); } @@ -480,7 +480,7 @@ BOOST_AUTO_TEST_CASE(preceding_step) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(node_.preceding_step() == 1U); } @@ -515,7 +515,7 @@ BOOST_AUTO_TEST_CASE(preceding_edge_costs) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(&node_.preceding_edge_costs() == &preceding_edge_costs); } @@ -551,7 +551,7 @@ BOOST_AUTO_TEST_CASE(best_preceding_node) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(node_.best_preceding_node() == 5U); } @@ -586,7 +586,7 @@ BOOST_AUTO_TEST_CASE(node_cost) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(node_.node_cost() == 24); } @@ -621,7 +621,7 @@ BOOST_AUTO_TEST_CASE(path_cost) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(node_.path_cost() == 2424); } @@ -664,7 +664,7 @@ BOOST_AUTO_TEST_CASE(is_bos) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - BOOST_TEST((!tetengo::lattice::node{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }.is_bos())); + BOOST_TEST((!tetengo::lattice::node{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }.is_bos())); } { diff --git a/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp b/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp index 21e24879..b78dc6aa 100644 --- a/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp @@ -37,7 +37,7 @@ BOOST_AUTO_TEST_CASE(construction) const std::any element_node_value{ 42 }; const std::vector element_node_preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; tetengo::lattice::node element_node{ - &element_node_key, &element_node_value, 1, &element_node_preceding_edge_costs, 5, 24, 2424 + &element_node_key, &element_node_value, 0, 1, &element_node_preceding_edge_costs, 5, 24, 2424 }; const tetengo::lattice::node_constraint_element element{ std::move(element_node) }; } @@ -84,7 +84,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::any element_node_value{ 42 }; const std::vector element_node_preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; tetengo::lattice::node element_node{ - &element_node_key, &element_node_value, 1, &element_node_preceding_edge_costs, 5, 24, 2424 + &element_node_key, &element_node_value, 0, 1, &element_node_preceding_edge_costs, 5, 24, 2424 }; const tetengo::lattice::node_constraint_element element{ std::move(element_node) }; @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) == 0); } @@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "sakura" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) < 0); } diff --git a/library/lattice/test/src/test_tetengo.lattice.path.cpp b/library/lattice/test/src/test_tetengo.lattice.path.cpp index 60dc5125..83c4371d 100644 --- a/library/lattice/test/src/test_tetengo.lattice.path.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.path.cpp @@ -50,9 +50,9 @@ namespace static const tetengo::lattice::string_input key_tsubame{ "tsubame" }; static const std::vector singleton{ tetengo::lattice::node::bos(&bos_preceding_edge_costs()), - tetengo::lattice::node{ &key_mizuho, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_sakura, &node_value(), 1, &preceding_edge_costs(), 0, 0, 0 }, - tetengo::lattice::node{ &key_tsubame, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_mizuho, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_sakura, &node_value(), 0, 1, &preceding_edge_costs(), 0, 0, 0 }, + tetengo::lattice::node{ &key_tsubame, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 }, tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0) }; return singleton; diff --git a/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp b/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp index 30517fdf..b8c014a6 100644 --- a/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp @@ -59,6 +59,7 @@ namespace { static const std::vector preceding_edge_costs{}; return tetengo::lattice::node{ entry, + 0, std::numeric_limits::max(), &preceding_edge_costs, std::numeric_limits::max(), diff --git a/library/lattice/test/src/test_tetengo.lattice.vocabulary.cpp b/library/lattice/test/src/test_tetengo.lattice.vocabulary.cpp index 8abd91e1..398fce00 100644 --- a/library/lattice/test/src/test_tetengo.lattice.vocabulary.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.vocabulary.cpp @@ -102,6 +102,7 @@ namespace static const std::vector preceding_edge_costs{}; return tetengo::lattice::node{ entry, std::numeric_limits::max(), + 0, &preceding_edge_costs, std::numeric_limits::max(), std::numeric_limits::max() }; diff --git a/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp b/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp index 88e41ad5..c59e84bb 100644 --- a/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp @@ -59,7 +59,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) < 0); } @@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "sakura" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 3, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 3, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) == 0); } @@ -75,7 +75,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "tsubame" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 5, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 5, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) > 0); } @@ -93,7 +93,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo::lattice::string_input key{ "mizuho" }; const std::any value{ 42 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; - const tetengo::lattice::node node_{ &key, &value, 1, &preceding_edge_costs, 5, 24, 2424 }; + const tetengo::lattice::node node_{ &key, &value, 0, 1, &preceding_edge_costs, 5, 24, 2424 }; BOOST_TEST(element.matches(node_) > 0); } From e3b3d87eb8cd44221725337c7adbc0c50025705e Mon Sep 17 00:00:00 2001 From: kaoru Date: Thu, 3 Nov 2022 14:39:34 +0900 Subject: [PATCH 2/5] Implement the C interface of tetengo::lattice::node #265 --- .../lattice/c/include/tetengo/lattice/node.h | 5 ++++ .../c/src/tetengo_lattice_constraint.cpp | 2 +- .../src/tetengo_lattice_constraintElement.cpp | 4 ++-- .../c/src/tetengo_lattice_nBestIterator.cpp | 3 ++- .../lattice/c/src/tetengo_lattice_node.cpp | 10 ++++---- .../lattice/c/src/tetengo_lattice_path.cpp | 2 +- .../c/src/tetengo_lattice_vocabulary.cpp | 3 ++- ...test_tetengo.lattice.custom_vocabulary.cpp | 1 + .../test/src/test_tetengo.lattice.node.cpp | 23 ++++++++++++++----- ...etengo.lattice.node_constraint_element.cpp | 6 +++++ ...tengo.lattice.unordered_map_vocabulary.cpp | 5 ++++ ...go.lattice.wildcard_constraint_element.cpp | 5 ++++ 12 files changed, 53 insertions(+), 16 deletions(-) diff --git a/library/lattice/c/include/tetengo/lattice/node.h b/library/lattice/c/include/tetengo/lattice/node.h index eec8ff13..14f2dcb1 100644 --- a/library/lattice/c/include/tetengo/lattice/node.h +++ b/library/lattice/c/include/tetengo/lattice/node.h @@ -29,6 +29,9 @@ typedef struct tetengo_lattice_node_tag /*! A value handle. */ tetengo_lattice_entryView_valueHandle_t value_handle; + /*! An index in the step. */ + size_t index_in_step; + /*! An index of a preceding step. */ size_t preceding_step; @@ -90,6 +93,7 @@ bool tetengo_lattice_node_eos( \brief Makes a node from an entry. \param p_entry A pointer to an entry. + \param index_in_step An index of the step. \param preceding_step An index of a preceding step. \param p_preceding_edge_costs A pointer to preceding edge costs. \param preceding_edge_count A preceding edge count. @@ -102,6 +106,7 @@ bool tetengo_lattice_node_eos( */ bool tetengo_lattice_node_toNode( const tetengo_lattice_entryView_t* p_entry, + size_t index_in_step, size_t preceding_step, const int* p_preceding_edge_costs, size_t preceding_edge_count, diff --git a/library/lattice/c/src/tetengo_lattice_constraint.cpp b/library/lattice/c/src/tetengo_lattice_constraint.cpp index 3ad5c73c..3d9b7a9e 100644 --- a/library/lattice/c/src/tetengo_lattice_constraint.cpp +++ b/library/lattice/c/src/tetengo_lattice_constraint.cpp @@ -53,7 +53,7 @@ namespace cpp_path.emplace_back( p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), - 0, // TODO: p_node->index_in_step, + p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_cost_lists.back(), p_node->best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_constraintElement.cpp b/library/lattice/c/src/tetengo_lattice_constraintElement.cpp index cd17f94c..1e8c0083 100644 --- a/library/lattice/c/src/tetengo_lattice_constraintElement.cpp +++ b/library/lattice/c/src/tetengo_lattice_constraintElement.cpp @@ -49,7 +49,7 @@ tetengo_lattice_constraintElement_createNodeConstraintElement(const tetengo_latt BOOST_SCOPE_EXIT_END; tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), - 0, // TODO: p_node->index_in_step, + p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_costs, p_node->best_preceding_node, @@ -120,7 +120,7 @@ int tetengo_lattice_constraintElement_matches( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), - 0, // TODO: p_node->index_in_step, + p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_costs, p_node->best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp b/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp index fc950ec8..0827067e 100644 --- a/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp +++ b/library/lattice/c/src/tetengo_lattice_nBestIterator.cpp @@ -84,7 +84,7 @@ tetengo_lattice_nBestIterator_t* tetengo_lattice_nBestIterator_create( p_eos_node->p_preceding_edge_costs + p_eos_node->preceding_edge_cost_count); tetengo::lattice::node cpp_eos_node{ p_cpp_node_key ? &p_cpp_node_key->cpp_input() : nullptr, reinterpret_cast(p_eos_node->value_handle), - 0, // TODO: p_eos_node->index_in_step, + p_eos_node->index_in_step, p_eos_node->preceding_step, std::to_address(p_cpp_preceding_edge_costs), p_eos_node->best_preceding_node, @@ -133,6 +133,7 @@ tetengo_lattice_nBestIterator_createPath(const tetengo_lattice_nBestIterator_t* { nodes.push_back({ reinterpret_cast(cpp_node.p_key()), reinterpret_cast(&cpp_node.value()), + cpp_node.index_in_step(), cpp_node.preceding_step(), std::data(cpp_node.preceding_edge_costs()), std::size(cpp_node.preceding_edge_costs()), diff --git a/library/lattice/c/src/tetengo_lattice_node.cpp b/library/lattice/c/src/tetengo_lattice_node.cpp index df948e47..22c254c3 100644 --- a/library/lattice/c/src/tetengo_lattice_node.cpp +++ b/library/lattice/c/src/tetengo_lattice_node.cpp @@ -104,6 +104,7 @@ bool tetengo_lattice_node_eos( bool tetengo_lattice_node_toNode( const tetengo_lattice_entryView_t* const p_entry, + size_t index_in_step, const size_t preceding_step, const int* const p_preceding_edge_costs, const size_t preceding_edge_count, @@ -138,12 +139,13 @@ bool tetengo_lattice_node_toNode( reinterpret_cast(p_entry->value_handle), p_entry->cost }; const std::vector cpp_preceding_edge_costs{}; - const tetengo::lattice::node cpp_node{ cpp_entry, 0, // TODO: index_in_step + const tetengo::lattice::node cpp_node{ cpp_entry, index_in_step, preceding_step, &cpp_preceding_edge_costs, best_preceding_node, path_cost }; p_node->key_handle = reinterpret_cast(cpp_node.p_key()); p_node->value_handle = reinterpret_cast(&cpp_node.value()); + p_node->index_in_step = cpp_node.index_in_step(); p_node->preceding_step = cpp_node.preceding_step(); p_node->p_preceding_edge_costs = p_preceding_edge_costs; p_node->preceding_edge_cost_count = preceding_edge_count; @@ -185,7 +187,7 @@ bool tetengo_lattice_node_equal( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_one{ p_one_key ? &p_one_key->cpp_input() : nullptr, reinterpret_cast(p_one->value_handle), - 0, // TODO: p_one->index_in_step, + p_one->index_in_step, p_one->preceding_step, &cpp_preceding_edge_costs_one, p_one->best_preceding_node, @@ -202,7 +204,7 @@ bool tetengo_lattice_node_equal( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_another{ p_another_key ? &p_another_key->cpp_input() : nullptr, reinterpret_cast(p_another->value_handle), - 0, // TODO: p_another->index_in_step, + p_another->index_in_step, p_another->preceding_step, &cpp_preceding_edge_costs_another, p_another->best_preceding_node, @@ -237,7 +239,7 @@ bool tetengo_lattice_node_isBos(const tetengo_lattice_node_t* const p_node) BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr, reinterpret_cast(p_node->value_handle), - 0, // TODO: p_node->index_in_step, + p_node->index_in_step, p_node->preceding_step, &cpp_preceding_edge_costs, p_node->best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_path.cpp b/library/lattice/c/src/tetengo_lattice_path.cpp index 62d63069..7ea604bc 100644 --- a/library/lattice/c/src/tetengo_lattice_path.cpp +++ b/library/lattice/c/src/tetengo_lattice_path.cpp @@ -98,7 +98,7 @@ tetengo_lattice_path_create(const tetengo_lattice_node_t* const p_nodes, const s cpp_nodes.emplace_back( p_cpp_node_key ? &p_cpp_node_key->cpp_input() : nullptr, reinterpret_cast(node.value_handle), - 0, // TODO: node.index_in_step, + node.index_in_step, node.preceding_step, &cpp_preceding_edge_cost_list, node.best_preceding_node, diff --git a/library/lattice/c/src/tetengo_lattice_vocabulary.cpp b/library/lattice/c/src/tetengo_lattice_vocabulary.cpp index 5bbc6149..0b027d06 100644 --- a/library/lattice/c/src/tetengo_lattice_vocabulary.cpp +++ b/library/lattice/c/src/tetengo_lattice_vocabulary.cpp @@ -222,6 +222,7 @@ namespace const tetengo_lattice_node_t c_from{ reinterpret_cast(from.p_key()), reinterpret_cast( &from.value()), + from.index_in_step(), from.preceding_step(), std::data(from.preceding_edge_costs()), std::size(from.preceding_edge_costs()), @@ -349,7 +350,7 @@ bool tetengo_lattice_vocabulary_findConnection( BOOST_SCOPE_EXIT_END; const tetengo::lattice::node cpp_from{ p_cpp_from_key ? &p_cpp_from_key->cpp_input() : nullptr, reinterpret_cast(p_from->value_handle), - 0, // TODO: p_from->index_in_step, + p_from->index_in_step, p_from->preceding_step, &cpp_preceding_edge_costs, p_from->best_preceding_node, diff --git a/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp b/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp index 6c08cc7f..ccda718d 100644 --- a/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp @@ -177,6 +177,7 @@ BOOST_AUTO_TEST_CASE(find_connection) const tetengo_lattice_node_t from{ tetengo_lattice_entryView_toKeyHandle(p_key_from), reinterpret_cast(&value_from), 0, + 0, nullptr, 0, 0, diff --git a/library/lattice/test/src/test_tetengo.lattice.node.cpp b/library/lattice/test/src/test_tetengo.lattice.node.cpp index cf786d76..d89b9879 100644 --- a/library/lattice/test/src/test_tetengo.lattice.node.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.node.cpp @@ -187,6 +187,7 @@ BOOST_AUTO_TEST_CASE(construction) [[maybe_unused]] const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast( &value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -208,7 +209,7 @@ BOOST_AUTO_TEST_CASE(construction) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; tetengo_lattice_node_t node{}; const auto result = tetengo_lattice_node_toNode( - &entry, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, &node); + &entry, 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, &node); BOOST_TEST_REQUIRE(result); BOOST_TEST(node.key_handle == entry.key_handle); @@ -234,7 +235,7 @@ BOOST_AUTO_TEST_CASE(construction) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; tetengo_lattice_node_t node{}; const auto result = tetengo_lattice_node_toNode( - &entry, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, &node); + &entry, 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, &node); BOOST_TEST_REQUIRE(result); BOOST_TEST(node.key_handle == entry.key_handle); @@ -250,7 +251,7 @@ BOOST_AUTO_TEST_CASE(construction) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; tetengo_lattice_node_t node{}; const auto result = tetengo_lattice_node_toNode( - nullptr, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, &node); + nullptr, 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, &node); BOOST_TEST(!result); } { @@ -265,7 +266,7 @@ BOOST_AUTO_TEST_CASE(construction) reinterpret_cast(&value), 24 }; tetengo_lattice_node_t node{}; - const auto result = tetengo_lattice_node_toNode(&entry, 1, nullptr, 0, 5, 2424, &node); + const auto result = tetengo_lattice_node_toNode(&entry, 53, 1, nullptr, 0, 5, 2424, &node); BOOST_TEST_REQUIRE(result); BOOST_TEST(node.key_handle == entry.key_handle); @@ -288,7 +289,7 @@ BOOST_AUTO_TEST_CASE(construction) reinterpret_cast(&value), 24 }; tetengo_lattice_node_t node{}; - const auto result = tetengo_lattice_node_toNode(&entry, 1, nullptr, 3, 5, 2424, &node); + const auto result = tetengo_lattice_node_toNode(&entry, 53, 1, nullptr, 3, 5, 2424, &node); BOOST_TEST(!result); } { @@ -304,7 +305,7 @@ BOOST_AUTO_TEST_CASE(construction) 24 }; const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const auto result = tetengo_lattice_node_toNode( - &entry, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, nullptr); + &entry, 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), 5, 2424, nullptr); BOOST_TEST(!result); } } @@ -359,6 +360,7 @@ BOOST_AUTO_TEST_CASE(operator_equal) const std::vector preceding_edge_costs1{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node1{ tetengo_lattice_entryView_toKeyHandle(p_key1), reinterpret_cast(&value1), + 53, 1, std::data(preceding_edge_costs1), std::size(preceding_edge_costs1), @@ -376,6 +378,7 @@ BOOST_AUTO_TEST_CASE(operator_equal) const std::vector preceding_edge_costs2{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node2{ tetengo_lattice_entryView_toKeyHandle(p_key2), reinterpret_cast(&value2), + 53, 1, std::data(preceding_edge_costs2), std::size(preceding_edge_costs2), @@ -419,6 +422,7 @@ BOOST_AUTO_TEST_CASE(key) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -461,6 +465,7 @@ BOOST_AUTO_TEST_CASE(value) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -496,6 +501,7 @@ BOOST_AUTO_TEST_CASE(preceding_step) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -531,6 +537,7 @@ BOOST_AUTO_TEST_CASE(preceding_edge_costs) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -567,6 +574,7 @@ BOOST_AUTO_TEST_CASE(best_preceding_node) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -602,6 +610,7 @@ BOOST_AUTO_TEST_CASE(node_cost) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -637,6 +646,7 @@ BOOST_AUTO_TEST_CASE(path_cost) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -692,6 +702,7 @@ BOOST_AUTO_TEST_CASE(is_bos) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 53, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), diff --git a/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp b/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp index b78dc6aa..9303e203 100644 --- a/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.node_constraint_element.cpp @@ -54,6 +54,7 @@ BOOST_AUTO_TEST_CASE(construction) const tetengo_lattice_node_t element_node{ tetengo_lattice_entryView_toKeyHandle(p_element_key), reinterpret_cast( &element_value), + 0, 1, std::data(element_preceding_edge_costs), std::size(element_preceding_edge_costs), @@ -118,6 +119,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo_lattice_node_t element_node{ tetengo_lattice_entryView_toKeyHandle(p_element_key), reinterpret_cast( &element_value), + 0, 1, std::data(element_preceding_edge_costs), std::size(element_preceding_edge_costs), @@ -144,6 +146,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -164,6 +167,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -185,6 +189,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -206,6 +211,7 @@ BOOST_AUTO_TEST_CASE(matches) const tetengo_lattice_node_t element_node{ tetengo_lattice_entryView_toKeyHandle(p_element_key), reinterpret_cast( &element_value), + 0, 1, std::data(element_preceding_edge_costs), std::size(element_preceding_edge_costs), diff --git a/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp b/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp index b8c014a6..684cffc7 100644 --- a/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.unordered_map_vocabulary.cpp @@ -637,6 +637,7 @@ BOOST_AUTO_TEST_CASE(find_connection) tetengo_lattice_node_t node_mizuho{}; BOOST_TEST_REQUIRE(tetengo_lattice_node_toNode( &entry_mizuho, + 0, std::numeric_limits::max(), std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -673,6 +674,7 @@ BOOST_AUTO_TEST_CASE(find_connection) tetengo_lattice_node_t node_mizuho{}; BOOST_TEST_REQUIRE(tetengo_lattice_node_toNode( &entry_mizuho, + 0, std::numeric_limits::max(), std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -711,6 +713,7 @@ BOOST_AUTO_TEST_CASE(find_connection) tetengo_lattice_node_t node_mizuho{}; BOOST_TEST_REQUIRE(tetengo_lattice_node_toNode( &entry_mizuho, + 0, std::numeric_limits::max(), std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -764,6 +767,7 @@ BOOST_AUTO_TEST_CASE(find_connection) tetengo_lattice_node_t node_mizuho{}; BOOST_TEST_REQUIRE(tetengo_lattice_node_toNode( &entry_mizuho, + 0, std::numeric_limits::max(), std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -800,6 +804,7 @@ BOOST_AUTO_TEST_CASE(find_connection) tetengo_lattice_node_t node_mizuho{}; BOOST_TEST_REQUIRE(tetengo_lattice_node_toNode( &entry_mizuho, + 0, std::numeric_limits::max(), std::data(preceding_edge_costs), std::size(preceding_edge_costs), diff --git a/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp b/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp index c59e84bb..38db85b6 100644 --- a/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.wildcard_constraint_element.cpp @@ -119,6 +119,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -139,6 +140,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 3, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -159,6 +161,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 5, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -197,6 +200,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), @@ -218,6 +222,7 @@ BOOST_AUTO_TEST_CASE(matches) const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; const tetengo_lattice_node_t node{ tetengo_lattice_entryView_toKeyHandle(p_key), reinterpret_cast(&value), + 0, 1, std::data(preceding_edge_costs), std::size(preceding_edge_costs), From 3f6bcda1e1dbbc1b89b0b481ca0694b2c68d69ae Mon Sep 17 00:00:00 2001 From: kaoru Date: Thu, 3 Nov 2022 17:05:32 +0900 Subject: [PATCH 3/5] Implement tetengo::lattice::node::index_in_step() #265 --- .../lattice/c/src/tetengo_lattice_lattice.cpp | 1 + .../lattice/c/src/tetengo_lattice_path.cpp | 1 + .../cpp/src/tetengo.lattice.lattice.cpp | 2 +- .../src/test_tetengo.lattice.constraint.cpp | 1 + .../test/src/test_tetengo.lattice.lattice.cpp | 32 ++++++++++++++++ .../test/src/test_tetengo.lattice.node.cpp | 38 +++++++++++++++++++ .../test/src/test_tetengo.lattice.path.cpp | 1 + 7 files changed, 75 insertions(+), 1 deletion(-) diff --git a/library/lattice/c/src/tetengo_lattice_lattice.cpp b/library/lattice/c/src/tetengo_lattice_lattice.cpp index 54147dbc..5f3ad108 100644 --- a/library/lattice/c/src/tetengo_lattice_lattice.cpp +++ b/library/lattice/c/src/tetengo_lattice_lattice.cpp @@ -103,6 +103,7 @@ size_t tetengo_lattice_lattice_nodesAt( p_nodes[i].key_handle = reinterpret_cast(cpp_nodes[i].p_key()); p_nodes[i].value_handle = reinterpret_cast(&cpp_nodes[i].value()); + p_nodes[i].index_in_step = cpp_nodes[i].index_in_step(); p_nodes[i].preceding_step = cpp_nodes[i].preceding_step(); p_nodes[i].p_preceding_edge_costs = std::data(cpp_nodes[i].preceding_edge_costs()); p_nodes[i].preceding_edge_cost_count = std::size(cpp_nodes[i].preceding_edge_costs()); diff --git a/library/lattice/c/src/tetengo_lattice_path.cpp b/library/lattice/c/src/tetengo_lattice_path.cpp index 7ea604bc..64a4a697 100644 --- a/library/lattice/c/src/tetengo_lattice_path.cpp +++ b/library/lattice/c/src/tetengo_lattice_path.cpp @@ -164,6 +164,7 @@ size_t tetengo_lattice_path_pNodes(const tetengo_lattice_path_t* const p_path, t tetengo_lattice_node_t c_node{}; c_node.key_handle = reinterpret_cast(cpp_node.p_key()); c_node.value_handle = reinterpret_cast(&cpp_node.value()); + c_node.index_in_step = cpp_node.index_in_step(); c_node.preceding_step = cpp_node.preceding_step(); c_node.p_preceding_edge_costs = std::data(cpp_node.preceding_edge_costs()); c_node.preceding_edge_cost_count = std::size(cpp_node.preceding_edge_costs()); diff --git a/library/lattice/cpp/src/tetengo.lattice.lattice.cpp b/library/lattice/cpp/src/tetengo.lattice.lattice.cpp index 49621cf9..da27ac95 100644 --- a/library/lattice/cpp/src/tetengo.lattice.lattice.cpp +++ b/library/lattice/cpp/src/tetengo.lattice.lattice.cpp @@ -143,7 +143,7 @@ namespace tetengo::lattice nodes.emplace_back( entry, - j, + nodes.size(), i, &preceding_edge_costs, best_preceding_node_index_, diff --git a/library/lattice/test/src/test_tetengo.lattice.constraint.cpp b/library/lattice/test/src/test_tetengo.lattice.constraint.cpp index f63749a1..55b3c4d8 100644 --- a/library/lattice/test/src/test_tetengo.lattice.constraint.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.constraint.cpp @@ -123,6 +123,7 @@ namespace c_node.key_handle = reinterpret_cast(cpp_node.p_key()); c_node.value_handle = reinterpret_cast(&cpp_node.value()); + c_node.index_in_step = cpp_node.index_in_step(); c_node.preceding_step = cpp_node.preceding_step(); c_node.p_preceding_edge_costs = std::data(cpp_node.preceding_edge_costs()); c_node.preceding_edge_cost_count = std::size(cpp_node.preceding_edge_costs()); diff --git a/library/lattice/test/src/test_tetengo.lattice.lattice.cpp b/library/lattice/test/src/test_tetengo.lattice.lattice.cpp index 491d67b5..f7eebf17 100644 --- a/library/lattice/test/src/test_tetengo.lattice.lattice.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.lattice.cpp @@ -380,6 +380,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) const std::vector preceding_edge_costs{}; BOOST_TEST( nodes[0].value().has_value() == tetengo::lattice::node::bos(&preceding_edge_costs).value().has_value()); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step() == i); + } } { const auto& nodes = lattice_.nodes_at(1); @@ -387,6 +391,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) BOOST_TEST_REQUIRE(std::size(nodes) == 2U); BOOST_TEST(std::any_cast(nodes[0].value()) == "kamome"); BOOST_TEST(std::any_cast(nodes[1].value()) == "local415"); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step() == i); + } } { const auto& nodes = lattice_.nodes_at(2); @@ -395,6 +403,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) BOOST_TEST(std::any_cast(nodes[0].value()) == "ariake"); BOOST_TEST(std::any_cast(nodes[1].value()) == "rapid811"); BOOST_TEST(std::any_cast(nodes[2].value()) == "local813"); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step() == i); + } } { const auto& nodes = lattice_.nodes_at(3); @@ -405,6 +417,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) BOOST_TEST(std::any_cast(nodes[2].value()) == "tsubame"); BOOST_TEST(std::any_cast(nodes[3].value()) == "local815"); BOOST_TEST(std::any_cast(nodes[4].value()) == "local817"); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step() == i); + } } { BOOST_CHECK_THROW([[maybe_unused]] const auto& nodes = lattice_.nodes_at(4), std::out_of_range); @@ -440,6 +456,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) tetengo_lattice_node_t bos{}; tetengo_lattice_node_bos(std::data(preceding_edge_costs), std::size(preceding_edge_costs), &bos); BOOST_TEST(nodes[0].value_handle == bos.value_handle); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step == i); + } } { const auto node_count = tetengo_lattice_lattice_nodesAt(p_lattice, 1, nullptr); @@ -455,6 +475,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "local415"); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step == i); + } } { const auto node_count = tetengo_lattice_lattice_nodesAt(p_lattice, 2, nullptr); @@ -473,6 +497,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[2].value_handle)) == "local813"); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step == i); + } } { const auto node_count = tetengo_lattice_lattice_nodesAt(p_lattice, 3, nullptr); @@ -497,6 +525,10 @@ BOOST_AUTO_TEST_CASE(nodes_at) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[4].value_handle)) == "local817"); + for (std::size_t i = 0; i < std::size(nodes); ++i) + { + BOOST_TEST(nodes[i].index_in_step == i); + } } { const auto node_count = tetengo_lattice_lattice_nodesAt(p_lattice, 4, nullptr); diff --git a/library/lattice/test/src/test_tetengo.lattice.node.cpp b/library/lattice/test/src/test_tetengo.lattice.node.cpp index d89b9879..b78693a5 100644 --- a/library/lattice/test/src/test_tetengo.lattice.node.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.node.cpp @@ -214,6 +214,7 @@ BOOST_AUTO_TEST_CASE(construction) BOOST_TEST(node.key_handle == entry.key_handle); BOOST_TEST(node.value_handle == entry.value_handle); + BOOST_TEST(node.index_in_step == 53U); BOOST_TEST(node.preceding_step == 1U); BOOST_TEST(node.p_preceding_edge_costs == std::data(preceding_edge_costs)); BOOST_TEST(node.preceding_edge_cost_count == std::size(preceding_edge_costs)); @@ -271,6 +272,7 @@ BOOST_AUTO_TEST_CASE(construction) BOOST_TEST(node.key_handle == entry.key_handle); BOOST_TEST(node.value_handle == entry.value_handle); + BOOST_TEST(node.index_in_step == 53U); BOOST_TEST(node.preceding_step == 1U); BOOST_TEST(node.preceding_edge_cost_count == 0U); BOOST_TEST(node.best_preceding_node == 5U); @@ -477,6 +479,42 @@ BOOST_AUTO_TEST_CASE(value) } } +BOOST_AUTO_TEST_CASE(index_in_step) +{ + BOOST_TEST_PASSPOINT(); + + { + const tetengo::lattice::string_input key{ "mizuho" }; + const std::any value{ 42 }; + const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; + const tetengo::lattice::node node_{ &key, &value, 53, 1, &preceding_edge_costs, 5, 24, 2424 }; + + BOOST_TEST(node_.index_in_step() == 53U); + } + + { + const auto* const p_key = tetengo_lattice_input_createStringInput("mizuho"); + BOOST_SCOPE_EXIT(p_key) + { + tetengo_lattice_input_destroy(p_key); + } + BOOST_SCOPE_EXIT_END; + const std::any value{ reinterpret_cast("MIZUHO") }; + const std::vector preceding_edge_costs{ 3, 1, 4, 1, 5, 9, 2, 6 }; + const tetengo_lattice_node_t node_{ tetengo_lattice_entryView_toKeyHandle(p_key), + reinterpret_cast(&value), + 53, + 1, + std::data(preceding_edge_costs), + std::size(preceding_edge_costs), + 5, + 24, + 2424 }; + + BOOST_TEST(node_.index_in_step == 53U); + } +} + BOOST_AUTO_TEST_CASE(preceding_step) { BOOST_TEST_PASSPOINT(); diff --git a/library/lattice/test/src/test_tetengo.lattice.path.cpp b/library/lattice/test/src/test_tetengo.lattice.path.cpp index 83c4371d..f1112f73 100644 --- a/library/lattice/test/src/test_tetengo.lattice.path.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.path.cpp @@ -64,6 +64,7 @@ namespace c_node.key_handle = reinterpret_cast(cpp_node.p_key()); c_node.value_handle = reinterpret_cast(&cpp_node.value()); + c_node.index_in_step = cpp_node.index_in_step(); c_node.preceding_step = cpp_node.preceding_step(); c_node.p_preceding_edge_costs = std::data(cpp_node.preceding_edge_costs()); c_node.preceding_edge_cost_count = std::size(cpp_node.preceding_edge_costs()); From 9af2821f5cf51cab519508066cd5b5f4d6273a73 Mon Sep 17 00:00:00 2001 From: kaoru Date: Thu, 3 Nov 2022 23:31:58 +0900 Subject: [PATCH 4/5] Implement tetengo::lattice::node::index_in_step() #265 --- .../cpp/src/tetengo.lattice.lattice.cpp | 2 +- .../test_tetengo.lattice.n_best_iterator.cpp | 113 ++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/library/lattice/cpp/src/tetengo.lattice.lattice.cpp b/library/lattice/cpp/src/tetengo.lattice.lattice.cpp index da27ac95..0700fd03 100644 --- a/library/lattice/cpp/src/tetengo.lattice.lattice.cpp +++ b/library/lattice/cpp/src/tetengo.lattice.lattice.cpp @@ -143,7 +143,7 @@ namespace tetengo::lattice nodes.emplace_back( entry, - nodes.size(), + std::size(nodes), i, &preceding_edge_costs, best_preceding_node_index_, diff --git a/library/lattice/test/src/test_tetengo.lattice.n_best_iterator.cpp b/library/lattice/test/src/test_tetengo.lattice.n_best_iterator.cpp index 94007263..f9fe7dc4 100644 --- a/library/lattice/test/src/test_tetengo.lattice.n_best_iterator.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.n_best_iterator.cpp @@ -151,6 +151,27 @@ namespace entries, connections, cpp_entry_hash, cpp_entry_equal_to); } + int preceding_edge_cost(const tetengo::lattice::path& path_, const std::size_t node_index) + { + const auto& nodes = path_.nodes(); + assert(!std::empty(nodes)); + assert(0 < node_index && node_index < std::size(nodes)); + return nodes[node_index].preceding_edge_costs()[nodes[node_index - 1].index_in_step()]; + } + + int recalc_path_cost(const tetengo::lattice::path& path_) + { + const auto& nodes = path_.nodes(); + assert(!std::empty(nodes)); + auto cost = nodes[0].node_cost(); + for (std::size_t i = 1; i < std::size(nodes); ++i) + { + cost += preceding_edge_cost(path_, i); + cost += nodes[i].node_cost(); + } + return cost; + } + size_t c_entry_hash(const tetengo_lattice_entryView_t* const p_entry) { if (p_entry) @@ -258,6 +279,28 @@ namespace c_entry_hash, c_entry_equal_to); } + + int c_preceding_edge_cost(const std::vector& nodes, const std::size_t node_index) + { + assert(!std::empty(nodes)); + assert(0 < node_index && node_index < std::size(nodes)); + const auto preceding_index_in_step = nodes[node_index - 1].index_in_step; + assert(preceding_index_in_step < nodes[node_index].preceding_edge_cost_count); + return nodes[node_index].p_preceding_edge_costs[preceding_index_in_step]; + } + + int recalc_c_path_cost(const std::vector& nodes) + { + assert(!std::empty(nodes)); + auto cost = nodes[0].node_cost; + for (std::size_t i = 1; i < std::size(nodes); ++i) + { + cost += c_preceding_edge_cost(nodes, i); + cost += nodes[i].node_cost; + } + return cost; + } + } @@ -690,7 +733,10 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 3U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "tsubame"); + BOOST_TEST(preceding_edge_cost(path, 1) == 600); BOOST_TEST(!path.nodes()[2].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 2) == 400); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } ++iterator; @@ -699,7 +745,10 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 3U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "sakura"); + BOOST_TEST(preceding_edge_cost(path, 1) == 600); BOOST_TEST(!path.nodes()[2].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 2) == 400); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } iterator++; @@ -708,8 +757,12 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 4U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "rapid811"); + BOOST_TEST(preceding_edge_cost(path, 1) == 700); BOOST_TEST(std::any_cast(path.nodes()[2].value()) == "local817"); + BOOST_TEST(preceding_edge_cost(path, 2) == 200); BOOST_TEST(!path.nodes()[3].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 3) == 600); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } ++iterator; @@ -718,8 +771,12 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 4U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "local415"); + BOOST_TEST(preceding_edge_cost(path, 1) == 800); BOOST_TEST(std::any_cast(path.nodes()[2].value()) == "local815"); + BOOST_TEST(preceding_edge_cost(path, 2) == 500); BOOST_TEST(!path.nodes()[3].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 3) == 500); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } iterator++; @@ -728,8 +785,12 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 4U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "kamome"); + BOOST_TEST(preceding_edge_cost(path, 1) == 800); BOOST_TEST(std::any_cast(path.nodes()[2].value()) == "local815"); + BOOST_TEST(preceding_edge_cost(path, 2) == 500); BOOST_TEST(!path.nodes()[3].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 3) == 500); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } ++iterator; @@ -738,8 +799,12 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 4U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "ariake"); + BOOST_TEST(preceding_edge_cost(path, 1) == 700); BOOST_TEST(std::any_cast(path.nodes()[2].value()) == "local817"); + BOOST_TEST(preceding_edge_cost(path, 2) == 200); BOOST_TEST(!path.nodes()[3].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 3) == 600); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } iterator++; @@ -748,7 +813,10 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 3U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "mizuho"); + BOOST_TEST(preceding_edge_cost(path, 1) == 600); BOOST_TEST(!path.nodes()[2].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 2) == 400); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } ++iterator; @@ -757,9 +825,14 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 5U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "local415"); + BOOST_TEST(preceding_edge_cost(path, 1) == 800); BOOST_TEST(std::any_cast(path.nodes()[2].value()) == "local813"); + BOOST_TEST(preceding_edge_cost(path, 2) == 600); BOOST_TEST(std::any_cast(path.nodes()[3].value()) == "local817"); + BOOST_TEST(preceding_edge_cost(path, 3) == 300); BOOST_TEST(!path.nodes()[4].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 4) == 600); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } iterator++; @@ -768,9 +841,14 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST_REQUIRE(std::size(path.nodes()) == 5U); BOOST_TEST(!path.nodes()[0].value().has_value()); BOOST_TEST(std::any_cast(path.nodes()[1].value()) == "kamome"); + BOOST_TEST(preceding_edge_cost(path, 1) == 800); BOOST_TEST(std::any_cast(path.nodes()[2].value()) == "local813"); + BOOST_TEST(preceding_edge_cost(path, 2) == 600); BOOST_TEST(std::any_cast(path.nodes()[3].value()) == "local817"); + BOOST_TEST(preceding_edge_cost(path, 3) == 300); BOOST_TEST(!path.nodes()[4].value().has_value()); + BOOST_TEST(preceding_edge_cost(path, 4) == 600); + BOOST_TEST(recalc_path_cost(path) == path.cost()); } ++iterator; @@ -986,7 +1064,10 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "tsubame"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 600); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[2].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 400); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1008,7 +1089,10 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "sakura"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 600); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[2].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 400); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1030,10 +1114,14 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "rapid811"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 700); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[2].value_handle)) == "local817"); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 200); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[3].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 3) == 600); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1055,10 +1143,14 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "local415"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 800); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[2].value_handle)) == "local815"); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 500); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[3].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 3) == 500); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1080,10 +1172,14 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "kamome"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 800); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[2].value_handle)) == "local815"); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 500); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[3].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 3) == 500); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1105,10 +1201,14 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "ariake"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 700); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[2].value_handle)) == "local817"); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 200); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[3].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 3) == 600); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1130,7 +1230,10 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "mizuho"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 600); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[2].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 400); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1152,13 +1255,18 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "local415"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 800); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[2].value_handle)) == "local813"); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 600); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[3].value_handle)) == "local817"); + BOOST_TEST(c_preceding_edge_cost(nodes, 3) == 300); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[4].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 4) == 600); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); @@ -1180,13 +1288,18 @@ BOOST_AUTO_TEST_CASE(operator_increment) BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[1].value_handle)) == "kamome"); + BOOST_TEST(c_preceding_edge_cost(nodes, 1) == 800); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[2].value_handle)) == "local813"); + BOOST_TEST(c_preceding_edge_cost(nodes, 2) == 600); BOOST_TEST( *reinterpret_cast(tetengo_lattice_entryView_valueOf(nodes[3].value_handle)) == "local817"); + BOOST_TEST(c_preceding_edge_cost(nodes, 3) == 300); BOOST_TEST(!tetengo_lattice_entryView_valueOf(nodes[4].value_handle)); + BOOST_TEST(c_preceding_edge_cost(nodes, 4) == 600); + BOOST_TEST(recalc_c_path_cost(nodes) == tetengo_lattice_path_cost(p_path)); } tetengo_lattice_nBestIterator_next(p_iterator); From 0212014eecc6f88a4f7d592365a24123c88d734f Mon Sep 17 00:00:00 2001 From: kaoru Date: Thu, 3 Nov 2022 23:37:50 +0900 Subject: [PATCH 5/5] Correct the code #265 --- .../test/src/test_tetengo.lattice.custom_vocabulary.cpp | 2 +- library/text/cpp/src/tetengo.text.encoding.utf8.cpp | 6 ++++-- sample/make_dict/src/main.cpp | 2 +- sample/search_dict/src/main.cpp | 5 +++-- sample/transfer_trains/src/main.cpp | 5 +++-- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp b/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp index ccda718d..7ecbdca9 100644 --- a/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp +++ b/library/lattice/test/src/test_tetengo.lattice.custom_vocabulary.cpp @@ -123,7 +123,7 @@ BOOST_AUTO_TEST_CASE(find_entries) BOOST_TEST_REQUIRE(entry_count == 2U); std::vector entries(entry_count); - const auto entry_count_again = tetengo_lattice_vocabulary_findEntries(p_vocabulary, p_input, entries.data()); + const auto entry_count_again = tetengo_lattice_vocabulary_findEntries(p_vocabulary, p_input, std::data(entries)); BOOST_TEST(entry_count_again == entry_count); const auto* const p_entry_key0 = tetengo_lattice_entryView_createKeyOf(entries[0].key_handle); BOOST_SCOPE_EXIT(p_entry_key0) diff --git a/library/text/cpp/src/tetengo.text.encoding.utf8.cpp b/library/text/cpp/src/tetengo.text.encoding.utf8.cpp index 0b8b905c..0c41c403 100644 --- a/library/text/cpp/src/tetengo.text.encoding.utf8.cpp +++ b/library/text/cpp/src/tetengo.text.encoding.utf8.cpp @@ -4,6 +4,7 @@ Copyright (C) 2019-2022 kaoru https://www.tetengo.org/ */ +#include #include #include @@ -40,13 +41,14 @@ namespace tetengo::text::encoding encoded_string_type encode(const string_view_type& utf8) const { - return encoded_string_type{ reinterpret_cast(utf8.data()), + return encoded_string_type{ reinterpret_cast(std::data(utf8)), utf8.length() }; } string_type decode(const encoded_string_view_type& string_) const { - return string_type{ reinterpret_cast(string_.data()), string_.length() }; + return string_type{ reinterpret_cast(std::data(string_)), + string_.length() }; } }; diff --git a/sample/make_dict/src/main.cpp b/sample/make_dict/src/main.cpp index 6204d447..2d5cda7f 100644 --- a/sample/make_dict/src/main.cpp +++ b/sample/make_dict/src/main.cpp @@ -54,7 +54,7 @@ namespace else { const auto encoded = tetengo::text::encoder::instance().encode(string_); - return std::string{ reinterpret_cast(encoded.data()), encoded.length() }; + return std::string{ reinterpret_cast(std::data(encoded)), encoded.length() }; } } diff --git a/sample/search_dict/src/main.cpp b/sample/search_dict/src/main.cpp index 32c03bbb..54ba2439 100644 --- a/sample/search_dict/src/main.cpp +++ b/sample/search_dict/src/main.cpp @@ -43,7 +43,8 @@ namespace } else { - const std::u8string_view utf8_encoded{ reinterpret_cast(encoded.data()), encoded.length() }; + const std::u8string_view utf8_encoded{ reinterpret_cast(std::data(encoded)), + encoded.length() }; return std::string{ tetengo::text::encoder::instance().decode( utf8_encoded) }; } @@ -59,7 +60,7 @@ namespace else { const auto encoded = tetengo::text::encoder::instance().encode(string_); - return std::string{ reinterpret_cast(encoded.data()), encoded.length() }; + return std::string{ reinterpret_cast(std::data(encoded)), encoded.length() }; } } diff --git a/sample/transfer_trains/src/main.cpp b/sample/transfer_trains/src/main.cpp index 549f69f6..26414707 100644 --- a/sample/transfer_trains/src/main.cpp +++ b/sample/transfer_trains/src/main.cpp @@ -57,7 +57,8 @@ namespace } else { - const std::u8string_view utf8_encoded{ reinterpret_cast(encoded.data()), encoded.length() }; + const std::u8string_view utf8_encoded{ reinterpret_cast(std::data(encoded)), + encoded.length() }; return std::string{ tetengo::text::encoder::instance().decode( utf8_encoded) }; } @@ -73,7 +74,7 @@ namespace else { const auto encoded = tetengo::text::encoder::instance().encode(string_); - return std::string{ reinterpret_cast(encoded.data()), encoded.length() }; + return std::string{ reinterpret_cast(std::data(encoded)), encoded.length() }; } }