Skip to content

Commit 011af6c

Browse files
authored
Merge pull request #9 from csukuangfj/fix-get-entering-arcs
fix GetEnteringArcs.
2 parents 5ca755c + ff6a419 commit 011af6c

File tree

4 files changed

+53
-70
lines changed

4 files changed

+53
-70
lines changed

k2/csrc/fsa_renderer.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ using k2::StateId;
3636
std::string ProcessState(const Fsa &fsa, int32_t state) {
3737
std::ostringstream os;
3838
os << " " << state << " [label = \"" << state
39-
<< "\", shape = circle, style = bold, fontsize=14]"
39+
<< "\", shape = circle, style = bold, fontsize = 14]"
4040
<< "\n";
4141

4242
int32_t begin = fsa.leaving_arcs[state].begin;

k2/csrc/fsa_util.cc

+15-20
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,29 @@
1111

1212
namespace k2 {
1313

14-
void GetEnteringArcs(const Fsa &fsa, VecOfVec *entering_arcs) {
14+
void GetEnteringArcs(const Fsa &fsa, std::vector<int32_t> *arc_index,
15+
std::vector<int32_t> *end_index) {
1516
// CHECK(CheckProperties(fsa, KTopSorted));
1617

1718
int32_t num_states = fsa.NumStates();
18-
std::vector<std::vector<std::pair<Label, StateId>>> vec(num_states);
19+
std::vector<std::vector<int32_t>> vec(num_states);
1920
int32_t num_arcs = 0;
21+
int32_t k = 0;
2022
for (const auto &arc : fsa.arcs) {
21-
auto src_state = arc.src_state;
2223
auto dest_state = arc.dest_state;
23-
auto label = arc.label;
24-
vec[dest_state].emplace_back(label, src_state);
24+
vec[dest_state].push_back(k);
2525
++num_arcs;
26+
++k;
2627
}
27-
28-
auto &ranges = entering_arcs->ranges;
29-
auto &values = entering_arcs->values;
30-
ranges.clear();
31-
values.clear();
32-
ranges.reserve(num_states);
33-
values.reserve(num_arcs);
34-
35-
int32_t start = 0;
36-
int32_t end = 0;
37-
for (const auto &label_state : vec) {
38-
values.insert(values.end(), label_state.begin(), label_state.end());
39-
start = end;
40-
end += static_cast<int32_t>(label_state.size());
41-
ranges.push_back({start, end});
28+
arc_index->clear();
29+
end_index->clear();
30+
arc_index->reserve(num_arcs);
31+
end_index->reserve(num_states);
32+
33+
for (const auto &indices : vec) {
34+
arc_index->insert(arc_index->end(), indices.begin(), indices.end());
35+
auto end = static_cast<int32_t>(arc_index->size());
36+
end_index->push_back(end);
4237
}
4338
}
4439

k2/csrc/fsa_util.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
// See ../../LICENSE for clarification regarding multiple authors
66

7-
#include "k2/csrc/fsa.h"
8-
97
#ifndef K2_CSRC_FSA_UTIL_H_
108
#define K2_CSRC_FSA_UTIL_H_
119

10+
#include <vector>
11+
12+
#include "k2/csrc/fsa.h"
13+
1214
namespace k2 {
1315

1416
/*
@@ -20,19 +22,19 @@ namespace k2 {
2022
2123
@param [out] arc_index A list of arc indexes.
2224
For states 0 < s < fsa.NumStates(),
23-
the elements arc_index[i] for end_index[s-1] <= i < end_index[s]
24-
contain the arc-indexes in fsa.arcs for arcs that
25-
enter state s.
25+
the elements arc_index[i] for
26+
end_index[s-1] <= i < end_index[s] contain the
27+
arc-indexes in fsa.arcs for arcs that enter
28+
state s.
2629
@param [out] end_index For each state, the `end` index in `arc_index`
2730
where we can find arcs entering this state, i.e.
28-
one past the index of the last element in `arc_index`
29-
that points to an arc entering this state.
31+
one past the index of the last element in
32+
`arc_index` that points to an arc entering
33+
this state.
3034
*/
31-
void GetEnteringArcs(const Fsa &fsa,
32-
std::vector<int32_t> *arc_index,
35+
void GetEnteringArcs(const Fsa &fsa, std::vector<int32_t> *arc_index,
3336
std::vector<int32_t> *end_index);
3437

35-
3638
} // namespace k2
3739

3840
#endif // K2_CSRC_FSA_UTIL_H_

k2/csrc/fsa_util_test.cc

+25-39
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ namespace k2 {
1515

1616
TEST(FsaUtil, GetEnteringArcs) {
1717
std::vector<Arc> arcs = {
18-
{0, 1, 2}, {0, 2, 1}, {1, 2, 0}, {1, 3, 5}, {2, 3, 6},
18+
{0, 1, 2}, // 0
19+
{0, 2, 1}, // 1
20+
{1, 2, 0}, // 2
21+
{1, 3, 5}, // 3
22+
{2, 3, 6}, // 4
1923
};
2024
std::vector<Range> leaving_arcs = {
2125
{0, 2}, {2, 4}, {4, 5}, {0, 0}, // the last state has no leaving arcs
@@ -25,44 +29,26 @@ TEST(FsaUtil, GetEnteringArcs) {
2529
fsa.leaving_arcs = std::move(leaving_arcs);
2630
fsa.arcs = std::move(arcs);
2731

28-
VecOfVec entering_arcs;
29-
GetEnteringArcs(fsa, &entering_arcs);
30-
31-
const auto &ranges = entering_arcs.ranges;
32-
const auto &values = entering_arcs.values;
33-
EXPECT_EQ(ranges.size(), 4u); // there are 4 states
34-
EXPECT_EQ(values.size(), 5u); // there are 5 arcs
35-
36-
// state 0, no entering arcs
37-
EXPECT_EQ(ranges[0].begin, ranges[0].end);
38-
39-
// state 1 has one entering arc from state 0 with label 2
40-
EXPECT_EQ(ranges[1].begin, 0);
41-
EXPECT_EQ(ranges[1].end, 1);
42-
EXPECT_EQ(values[0].first, 2); // label is 2
43-
EXPECT_EQ(values[0].second, 0); // state is 0
44-
45-
// state 2 has two entering arcs
46-
// the first one: from state 0 with label 1
47-
// the second one: from state 1 with label 0
48-
EXPECT_EQ(ranges[2].begin, 1);
49-
EXPECT_EQ(ranges[2].end, 3);
50-
EXPECT_EQ(values[1].first, 1); // label is 1
51-
EXPECT_EQ(values[1].second, 0); // state is 0
52-
53-
EXPECT_EQ(values[2].first, 0); // label is 0
54-
EXPECT_EQ(values[2].second, 1); // state is 1
55-
56-
// state 3 has two entering arcs
57-
// the first one: from state 1 with label 5
58-
// the second one: from state 2 with label 6
59-
EXPECT_EQ(ranges[3].begin, 3);
60-
EXPECT_EQ(ranges[3].end, 5);
61-
EXPECT_EQ(values[3].first, 5); // label is 5
62-
EXPECT_EQ(values[3].second, 1); // state is 1
63-
64-
EXPECT_EQ(values[4].first, 6); // label is 6
65-
EXPECT_EQ(values[4].second, 2); // state is 2
32+
std::vector<int32_t> arc_index(10); // an arbitray number
33+
std::vector<int32_t> end_index(20);
34+
35+
GetEnteringArcs(fsa, &arc_index, &end_index);
36+
37+
EXPECT_EQ(end_index.size(), 4u); // there are 4 states
38+
EXPECT_EQ(arc_index.size(), 5u); // there are 5 arcs
39+
40+
EXPECT_EQ(end_index[0], 0); // state 0 has no entering arcs
41+
42+
EXPECT_EQ(end_index[1], 1); // state 1 has one entering arc
43+
EXPECT_EQ(arc_index[0], 0); // arc index 0 from state 0
44+
45+
EXPECT_EQ(end_index[2], 3); // state 2 has two entering arcs
46+
EXPECT_EQ(arc_index[1], 1); // arc index 1 from state 0
47+
EXPECT_EQ(arc_index[2], 2); // arc index 2 from state 1
48+
49+
EXPECT_EQ(end_index[3], 5); // state 3 has two entering arcs
50+
EXPECT_EQ(arc_index[3], 3); // arc index 3 from state 1
51+
EXPECT_EQ(arc_index[4], 4); // arc index 4 from state 2
6652
}
6753

6854
} // namespace k2

0 commit comments

Comments
 (0)