Skip to content

Commit

Permalink
Merge pull request #266 from tetengo/node_index_in_step
Browse files Browse the repository at this point in the history
Node index in step
  • Loading branch information
kaorut authored Nov 3, 2022
2 parents 10fedaa + 0212014 commit 3bf495f
Show file tree
Hide file tree
Showing 26 changed files with 314 additions and 57 deletions.
5 changes: 5 additions & 0 deletions library/lattice/c/include/tetengo/lattice/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions library/lattice/c/src/tetengo_lattice_constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace
cpp_path.emplace_back(
p_node_key ? &p_node_key->cpp_input() : nullptr,
reinterpret_cast<const std::any*>(p_node->value_handle),
p_node->index_in_step,
p_node->preceding_step,
&cpp_preceding_edge_cost_lists.back(),
p_node->best_preceding_node,
Expand Down
2 changes: 2 additions & 0 deletions library/lattice/c/src/tetengo_lattice_constraintElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const std::any*>(p_node->value_handle),
p_node->index_in_step,
p_node->preceding_step,
&cpp_preceding_edge_costs,
p_node->best_preceding_node,
Expand Down Expand Up @@ -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<const std::any*>(p_node->value_handle),
p_node->index_in_step,
p_node->preceding_step,
&cpp_preceding_edge_costs,
p_node->best_preceding_node,
Expand Down
1 change: 1 addition & 0 deletions library/lattice/c/src/tetengo_lattice_lattice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ size_t tetengo_lattice_lattice_nodesAt(
p_nodes[i].key_handle = reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_nodes[i].p_key());
p_nodes[i].value_handle =
reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&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());
Expand Down
2 changes: 2 additions & 0 deletions library/lattice/c/src/tetengo_lattice_nBestIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const std::any*>(p_eos_node->value_handle),
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,
Expand Down Expand Up @@ -132,6 +133,7 @@ tetengo_lattice_nBestIterator_createPath(const tetengo_lattice_nBestIterator_t*
{
nodes.push_back({ reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key()),
reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&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()),
Expand Down
11 changes: 8 additions & 3 deletions library/lattice/c/src/tetengo_lattice_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -138,12 +139,13 @@ bool tetengo_lattice_node_toNode(
reinterpret_cast<const std::any*>(p_entry->value_handle),
p_entry->cost };
const std::vector<int> 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, index_in_step,
preceding_step, &cpp_preceding_edge_costs,
best_preceding_node, path_cost };

p_node->key_handle = reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key());
p_node->value_handle = reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&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;
Expand Down Expand Up @@ -185,6 +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<const std::any*>(p_one->value_handle),
p_one->index_in_step,
p_one->preceding_step,
&cpp_preceding_edge_costs_one,
p_one->best_preceding_node,
Expand All @@ -201,6 +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<const std::any*>(p_another->value_handle),
p_another->index_in_step,
p_another->preceding_step,
&cpp_preceding_edge_costs_another,
p_another->best_preceding_node,
Expand Down Expand Up @@ -235,6 +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<const std::any*>(p_node->value_handle),
p_node->index_in_step,
p_node->preceding_step,
&cpp_preceding_edge_costs,
p_node->best_preceding_node,
Expand Down
2 changes: 2 additions & 0 deletions library/lattice/c/src/tetengo_lattice_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const std::any*>(node.value_handle),
node.index_in_step,
node.preceding_step,
&cpp_preceding_edge_cost_list,
node.best_preceding_node,
Expand Down Expand Up @@ -163,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<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key());
c_node.value_handle = reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&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());
Expand Down
2 changes: 2 additions & 0 deletions library/lattice/c/src/tetengo_lattice_vocabulary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ namespace
const tetengo_lattice_node_t c_from{ reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(from.p_key()),
reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(
&from.value()),
from.index_in_step(),
from.preceding_step(),
std::data(from.preceding_edge_costs()),
std::size(from.preceding_edge_costs()),
Expand Down Expand Up @@ -349,6 +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<const std::any*>(p_from->value_handle),
p_from->index_in_step,
p_from->preceding_step,
&cpp_preceding_edge_costs,
p_from->best_preceding_node,
Expand Down
20 changes: 18 additions & 2 deletions library/lattice/cpp/include/tetengo/lattice/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -74,13 +75,15 @@ 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<int>* p_preceding_edge_costs,
std::size_t best_preceding_node,
int node_cost,
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 },
Expand All @@ -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.
Expand All @@ -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<int>* 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 }
{}


Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<int>* m_p_preceding_edge_costs;
Expand Down
1 change: 1 addition & 0 deletions library/lattice/cpp/src/tetengo.lattice.lattice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ namespace tetengo::lattice

nodes.emplace_back(
entry,
std::size(nodes),
i,
&preceding_edge_costs,
best_preceding_node_index_,
Expand Down
3 changes: 2 additions & 1 deletion library/lattice/cpp/src/tetengo.lattice.node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace tetengo::lattice
node node::bos(const std::vector<int>* const p_preceding_edge_costs)
{
return node{ entry_view::bos_eos(),
0,
std::numeric_limits<std::size_t>::max(),
p_preceding_edge_costs,
std::numeric_limits<std::size_t>::max(),
Expand All @@ -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
Expand Down
23 changes: 12 additions & 11 deletions library/lattice/test/src/test_tetengo.lattice.constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ namespace
static const tetengo::lattice::string_input key_tsubame{ "tsubame" };
static const std::vector<tetengo::lattice::node> 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;
Expand All @@ -81,9 +81,9 @@ namespace
static const tetengo::lattice::string_input key_tsubame{ "tsubame" };
static const std::vector<tetengo::lattice::node> 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;
Expand All @@ -95,8 +95,8 @@ namespace
static const tetengo::lattice::string_input key_tsubame{ "tsubame" };
static const std::vector<tetengo::lattice::node> 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;
Expand All @@ -109,9 +109,9 @@ namespace
static const tetengo::lattice::string_input key_kumagawa{ "kumagawa" };
static const std::vector<tetengo::lattice::node> 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;
Expand All @@ -123,6 +123,7 @@ namespace

c_node.key_handle = reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key());
c_node.value_handle = reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&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());
Expand Down
Loading

0 comments on commit 3bf495f

Please sign in to comment.