Skip to content

Commit 3bf495f

Browse files
authored
Merge pull request #266 from tetengo/node_index_in_step
Node index in step
2 parents 10fedaa + 0212014 commit 3bf495f

26 files changed

+314
-57
lines changed

library/lattice/c/include/tetengo/lattice/node.h

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ typedef struct tetengo_lattice_node_tag
2929
/*! A value handle. */
3030
tetengo_lattice_entryView_valueHandle_t value_handle;
3131

32+
/*! An index in the step. */
33+
size_t index_in_step;
34+
3235
/*! An index of a preceding step. */
3336
size_t preceding_step;
3437

@@ -90,6 +93,7 @@ bool tetengo_lattice_node_eos(
9093
\brief Makes a node from an entry.
9194
9295
\param p_entry A pointer to an entry.
96+
\param index_in_step An index of the step.
9397
\param preceding_step An index of a preceding step.
9498
\param p_preceding_edge_costs A pointer to preceding edge costs.
9599
\param preceding_edge_count A preceding edge count.
@@ -102,6 +106,7 @@ bool tetengo_lattice_node_eos(
102106
*/
103107
bool tetengo_lattice_node_toNode(
104108
const tetengo_lattice_entryView_t* p_entry,
109+
size_t index_in_step,
105110
size_t preceding_step,
106111
const int* p_preceding_edge_costs,
107112
size_t preceding_edge_count,

library/lattice/c/src/tetengo_lattice_constraint.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace
5353
cpp_path.emplace_back(
5454
p_node_key ? &p_node_key->cpp_input() : nullptr,
5555
reinterpret_cast<const std::any*>(p_node->value_handle),
56+
p_node->index_in_step,
5657
p_node->preceding_step,
5758
&cpp_preceding_edge_cost_lists.back(),
5859
p_node->best_preceding_node,

library/lattice/c/src/tetengo_lattice_constraintElement.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ tetengo_lattice_constraintElement_createNodeConstraintElement(const tetengo_latt
4949
BOOST_SCOPE_EXIT_END;
5050
tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr,
5151
reinterpret_cast<const std::any*>(p_node->value_handle),
52+
p_node->index_in_step,
5253
p_node->preceding_step,
5354
&cpp_preceding_edge_costs,
5455
p_node->best_preceding_node,
@@ -119,6 +120,7 @@ int tetengo_lattice_constraintElement_matches(
119120
BOOST_SCOPE_EXIT_END;
120121
const tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr,
121122
reinterpret_cast<const std::any*>(p_node->value_handle),
123+
p_node->index_in_step,
122124
p_node->preceding_step,
123125
&cpp_preceding_edge_costs,
124126
p_node->best_preceding_node,

library/lattice/c/src/tetengo_lattice_lattice.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ size_t tetengo_lattice_lattice_nodesAt(
103103
p_nodes[i].key_handle = reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_nodes[i].p_key());
104104
p_nodes[i].value_handle =
105105
reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&cpp_nodes[i].value());
106+
p_nodes[i].index_in_step = cpp_nodes[i].index_in_step();
106107
p_nodes[i].preceding_step = cpp_nodes[i].preceding_step();
107108
p_nodes[i].p_preceding_edge_costs = std::data(cpp_nodes[i].preceding_edge_costs());
108109
p_nodes[i].preceding_edge_cost_count = std::size(cpp_nodes[i].preceding_edge_costs());

library/lattice/c/src/tetengo_lattice_nBestIterator.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ tetengo_lattice_nBestIterator_t* tetengo_lattice_nBestIterator_create(
8484
p_eos_node->p_preceding_edge_costs + p_eos_node->preceding_edge_cost_count);
8585
tetengo::lattice::node cpp_eos_node{ p_cpp_node_key ? &p_cpp_node_key->cpp_input() : nullptr,
8686
reinterpret_cast<const std::any*>(p_eos_node->value_handle),
87+
p_eos_node->index_in_step,
8788
p_eos_node->preceding_step,
8889
std::to_address(p_cpp_preceding_edge_costs),
8990
p_eos_node->best_preceding_node,
@@ -132,6 +133,7 @@ tetengo_lattice_nBestIterator_createPath(const tetengo_lattice_nBestIterator_t*
132133
{
133134
nodes.push_back({ reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key()),
134135
reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&cpp_node.value()),
136+
cpp_node.index_in_step(),
135137
cpp_node.preceding_step(),
136138
std::data(cpp_node.preceding_edge_costs()),
137139
std::size(cpp_node.preceding_edge_costs()),

library/lattice/c/src/tetengo_lattice_node.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ bool tetengo_lattice_node_eos(
104104

105105
bool tetengo_lattice_node_toNode(
106106
const tetengo_lattice_entryView_t* const p_entry,
107+
size_t index_in_step,
107108
const size_t preceding_step,
108109
const int* const p_preceding_edge_costs,
109110
const size_t preceding_edge_count,
@@ -138,12 +139,13 @@ bool tetengo_lattice_node_toNode(
138139
reinterpret_cast<const std::any*>(p_entry->value_handle),
139140
p_entry->cost };
140141
const std::vector<int> cpp_preceding_edge_costs{};
141-
const tetengo::lattice::node cpp_node{
142-
cpp_entry, preceding_step, &cpp_preceding_edge_costs, best_preceding_node, path_cost
143-
};
142+
const tetengo::lattice::node cpp_node{ cpp_entry, index_in_step,
143+
preceding_step, &cpp_preceding_edge_costs,
144+
best_preceding_node, path_cost };
144145

145146
p_node->key_handle = reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key());
146147
p_node->value_handle = reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&cpp_node.value());
148+
p_node->index_in_step = cpp_node.index_in_step();
147149
p_node->preceding_step = cpp_node.preceding_step();
148150
p_node->p_preceding_edge_costs = p_preceding_edge_costs;
149151
p_node->preceding_edge_cost_count = preceding_edge_count;
@@ -185,6 +187,7 @@ bool tetengo_lattice_node_equal(
185187
BOOST_SCOPE_EXIT_END;
186188
const tetengo::lattice::node cpp_one{ p_one_key ? &p_one_key->cpp_input() : nullptr,
187189
reinterpret_cast<const std::any*>(p_one->value_handle),
190+
p_one->index_in_step,
188191
p_one->preceding_step,
189192
&cpp_preceding_edge_costs_one,
190193
p_one->best_preceding_node,
@@ -201,6 +204,7 @@ bool tetengo_lattice_node_equal(
201204
BOOST_SCOPE_EXIT_END;
202205
const tetengo::lattice::node cpp_another{ p_another_key ? &p_another_key->cpp_input() : nullptr,
203206
reinterpret_cast<const std::any*>(p_another->value_handle),
207+
p_another->index_in_step,
204208
p_another->preceding_step,
205209
&cpp_preceding_edge_costs_another,
206210
p_another->best_preceding_node,
@@ -235,6 +239,7 @@ bool tetengo_lattice_node_isBos(const tetengo_lattice_node_t* const p_node)
235239
BOOST_SCOPE_EXIT_END;
236240
const tetengo::lattice::node cpp_node{ p_node_key ? &p_node_key->cpp_input() : nullptr,
237241
reinterpret_cast<const std::any*>(p_node->value_handle),
242+
p_node->index_in_step,
238243
p_node->preceding_step,
239244
&cpp_preceding_edge_costs,
240245
p_node->best_preceding_node,

library/lattice/c/src/tetengo_lattice_path.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ tetengo_lattice_path_create(const tetengo_lattice_node_t* const p_nodes, const s
9898
cpp_nodes.emplace_back(
9999
p_cpp_node_key ? &p_cpp_node_key->cpp_input() : nullptr,
100100
reinterpret_cast<const std::any*>(node.value_handle),
101+
node.index_in_step,
101102
node.preceding_step,
102103
&cpp_preceding_edge_cost_list,
103104
node.best_preceding_node,
@@ -163,6 +164,7 @@ size_t tetengo_lattice_path_pNodes(const tetengo_lattice_path_t* const p_path, t
163164
tetengo_lattice_node_t c_node{};
164165
c_node.key_handle = reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key());
165166
c_node.value_handle = reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&cpp_node.value());
167+
c_node.index_in_step = cpp_node.index_in_step();
166168
c_node.preceding_step = cpp_node.preceding_step();
167169
c_node.p_preceding_edge_costs = std::data(cpp_node.preceding_edge_costs());
168170
c_node.preceding_edge_cost_count = std::size(cpp_node.preceding_edge_costs());

library/lattice/c/src/tetengo_lattice_vocabulary.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ namespace
222222
const tetengo_lattice_node_t c_from{ reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(from.p_key()),
223223
reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(
224224
&from.value()),
225+
from.index_in_step(),
225226
from.preceding_step(),
226227
std::data(from.preceding_edge_costs()),
227228
std::size(from.preceding_edge_costs()),
@@ -349,6 +350,7 @@ bool tetengo_lattice_vocabulary_findConnection(
349350
BOOST_SCOPE_EXIT_END;
350351
const tetengo::lattice::node cpp_from{ p_cpp_from_key ? &p_cpp_from_key->cpp_input() : nullptr,
351352
reinterpret_cast<const std::any*>(p_from->value_handle),
353+
p_from->index_in_step,
352354
p_from->preceding_step,
353355
&cpp_preceding_edge_costs,
354356
p_from->best_preceding_node,

library/lattice/cpp/include/tetengo/lattice/node.hpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace tetengo::lattice
6565
6666
\param p_key A pointer to a key.
6767
\param p_value A pointer to a value.
68+
\param index_in_step An index in the step.
6869
\param preceding_step An index of a preceding step.
6970
\param p_preceding_edge_costs A pointer to preceding edge costs.
7071
\param best_preceding_node An index of a best preceding node.
@@ -74,13 +75,15 @@ namespace tetengo::lattice
7475
constexpr node(
7576
const input* p_key,
7677
const std::any* p_value,
78+
std::size_t index_in_step,
7779
std::size_t preceding_step,
7880
const std::vector<int>* p_preceding_edge_costs,
7981
std::size_t best_preceding_node,
8082
int node_cost,
8183
int path_cost) :
8284
m_p_key{ p_key },
8385
m_p_value{ p_value },
86+
m_index_in_step{ index_in_step },
8487
m_preceding_step{ preceding_step },
8588
m_p_preceding_edge_costs{ p_preceding_edge_costs },
8689
m_best_preceding_node{ best_preceding_node },
@@ -92,6 +95,7 @@ namespace tetengo::lattice
9295
\brief Creates a node from a vocabulary entry.
9396
9497
\param entry An entry.
98+
\param index_in_step An index in the step.
9599
\param preceding_step An index of a preceding step.
96100
\param p_preceding_edge_costs A pointer to preceding edge costs.
97101
\param best_preceding_node An index of a best preceding node.
@@ -101,12 +105,13 @@ namespace tetengo::lattice
101105
*/
102106
constexpr node(
103107
const entry_view& entry,
108+
std::size_t index_in_step,
104109
std::size_t preceding_step,
105110
const std::vector<int>* p_preceding_edge_costs,
106111
std::size_t best_preceding_node,
107112
int path_cost) :
108-
node{ entry.p_key(), entry.value(), preceding_step, p_preceding_edge_costs,
109-
best_preceding_node, entry.cost(), path_cost }
113+
node{ entry.p_key(), entry.value(), index_in_step, preceding_step,
114+
p_preceding_edge_costs, best_preceding_node, entry.cost(), path_cost }
110115
{}
111116

112117

@@ -151,6 +156,15 @@ namespace tetengo::lattice
151156
return *m_p_value;
152157
}
153158

159+
/*!
160+
\brief Returns the index in the step.
161+
162+
\return The index in the step.
163+
*/
164+
[[nodiscard]] constexpr std::size_t index_in_step() const
165+
{
166+
return m_index_in_step;
167+
}
154168
/*!
155169
\brief Returns the index of the preceding step.
156170
@@ -218,6 +232,8 @@ namespace tetengo::lattice
218232

219233
const std::any* m_p_value;
220234

235+
std::size_t m_index_in_step;
236+
221237
std::size_t m_preceding_step;
222238

223239
const std::vector<int>* m_p_preceding_edge_costs;

library/lattice/cpp/src/tetengo.lattice.lattice.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ namespace tetengo::lattice
143143

144144
nodes.emplace_back(
145145
entry,
146+
std::size(nodes),
146147
i,
147148
&preceding_edge_costs,
148149
best_preceding_node_index_,

library/lattice/cpp/src/tetengo.lattice.node.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace tetengo::lattice
1717
node node::bos(const std::vector<int>* const p_preceding_edge_costs)
1818
{
1919
return node{ entry_view::bos_eos(),
20+
0,
2021
std::numeric_limits<std::size_t>::max(),
2122
p_preceding_edge_costs,
2223
std::numeric_limits<std::size_t>::max(),
@@ -29,7 +30,7 @@ namespace tetengo::lattice
2930
const std::size_t best_preceding_node,
3031
const int path_cost)
3132
{
32-
return node{ entry_view::bos_eos(), preceding_step, p_preceding_edge_costs, best_preceding_node, path_cost };
33+
return node{ entry_view::bos_eos(), 0, preceding_step, p_preceding_edge_costs, best_preceding_node, path_cost };
3334
}
3435

3536
bool node::is_bos() const

library/lattice/test/src/test_tetengo.lattice.constraint.cpp

+12-11
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ namespace
6666
static const tetengo::lattice::string_input key_tsubame{ "tsubame" };
6767
static const std::vector<tetengo::lattice::node> singleton{
6868
tetengo::lattice::node::bos(&bos_preceding_edge_costs()),
69-
tetengo::lattice::node{ &key_mizuho, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 },
70-
tetengo::lattice::node{ &key_sakura, &node_value(), 1, &preceding_edge_costs(), 0, 0, 0 },
71-
tetengo::lattice::node{ &key_tsubame, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 },
69+
tetengo::lattice::node{ &key_mizuho, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 },
70+
tetengo::lattice::node{ &key_sakura, &node_value(), 0, 1, &preceding_edge_costs(), 0, 0, 0 },
71+
tetengo::lattice::node{ &key_tsubame, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 },
7272
tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0)
7373
};
7474
return singleton;
@@ -81,9 +81,9 @@ namespace
8181
static const tetengo::lattice::string_input key_tsubame{ "tsubame" };
8282
static const std::vector<tetengo::lattice::node> singleton{
8383
tetengo::lattice::node::bos(&bos_preceding_edge_costs()),
84-
tetengo::lattice::node{ &key_mizuho, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 },
85-
tetengo::lattice::node{ &key_ariake, &node_value(), 1, &preceding_edge_costs(), 0, 0, 0 },
86-
tetengo::lattice::node{ &key_tsubame, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 },
84+
tetengo::lattice::node{ &key_mizuho, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 },
85+
tetengo::lattice::node{ &key_ariake, &node_value(), 0, 1, &preceding_edge_costs(), 0, 0, 0 },
86+
tetengo::lattice::node{ &key_tsubame, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 },
8787
tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0)
8888
};
8989
return singleton;
@@ -95,8 +95,8 @@ namespace
9595
static const tetengo::lattice::string_input key_tsubame{ "tsubame" };
9696
static const std::vector<tetengo::lattice::node> singleton{
9797
tetengo::lattice::node::bos(&bos_preceding_edge_costs()),
98-
tetengo::lattice::node{ &key_hinokuni, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 },
99-
tetengo::lattice::node{ &key_tsubame, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 },
98+
tetengo::lattice::node{ &key_hinokuni, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 },
99+
tetengo::lattice::node{ &key_tsubame, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 },
100100
tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0)
101101
};
102102
return singleton;
@@ -109,9 +109,9 @@ namespace
109109
static const tetengo::lattice::string_input key_kumagawa{ "kumagawa" };
110110
static const std::vector<tetengo::lattice::node> singleton{
111111
tetengo::lattice::node::bos(&bos_preceding_edge_costs()),
112-
tetengo::lattice::node{ &key_kamome, &node_value(), 0, &preceding_edge_costs(), 0, 0, 0 },
113-
tetengo::lattice::node{ &key_sakura, &node_value(), 1, &preceding_edge_costs(), 0, 0, 0 },
114-
tetengo::lattice::node{ &key_kumagawa, &node_value(), 2, &preceding_edge_costs(), 0, 0, 0 },
112+
tetengo::lattice::node{ &key_kamome, &node_value(), 0, 0, &preceding_edge_costs(), 0, 0, 0 },
113+
tetengo::lattice::node{ &key_sakura, &node_value(), 0, 1, &preceding_edge_costs(), 0, 0, 0 },
114+
tetengo::lattice::node{ &key_kumagawa, &node_value(), 0, 2, &preceding_edge_costs(), 0, 0, 0 },
115115
tetengo::lattice::node::eos(3, &preceding_edge_costs(), 0, 0)
116116
};
117117
return singleton;
@@ -123,6 +123,7 @@ namespace
123123

124124
c_node.key_handle = reinterpret_cast<tetengo_lattice_entryView_keyHandle_t>(cpp_node.p_key());
125125
c_node.value_handle = reinterpret_cast<tetengo_lattice_entryView_valueHandle_t>(&cpp_node.value());
126+
c_node.index_in_step = cpp_node.index_in_step();
126127
c_node.preceding_step = cpp_node.preceding_step();
127128
c_node.p_preceding_edge_costs = std::data(cpp_node.preceding_edge_costs());
128129
c_node.preceding_edge_cost_count = std::size(cpp_node.preceding_edge_costs());

0 commit comments

Comments
 (0)