Skip to content

Commit

Permalink
fix style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
qindazhu committed Apr 24, 2020
1 parent 0078010 commit 31d0ee9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
23 changes: 12 additions & 11 deletions k2/csrc/fsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ enum {
kFinalSymbol = -1, // final-costs are represented as arcs with
// kFinalSymbol as their label, to the final
// state (see Fst::final_state).

kEpsilon = 0 // Epsilon, which means "no symbol", is numbered zero,
// like in OpenFst.
kEpsilon = 0 // Epsilon, which means "no symbol", is numbered zero,
// like in OpenFst.
};

/* Range is what we use when we want (begin,end) indexes into some array.
Expand All @@ -38,11 +37,11 @@ struct Arc {
StateId src_state;
StateId dest_state;
Label label; // 'label' as in a finite state acceptor.
// For FSTs, the other label will be present in the
// aux_label array. Which of the two represents the input
// vs. the output can be decided by the user; in general,
// the one that appears on the arc will be the one that
// participates in whatever operation you are doing
// For FSTs, the other label will be present in the
// aux_label array. Which of the two represents the input
// vs. the output can be decided by the user; in general,
// the one that appears on the arc will be the one that
// participates in whatever operation you are doing

/* Note: the costs are not stored here but outside the Fst object, in some
kind of array indexed by arc-index. */
Expand Down Expand Up @@ -74,7 +73,9 @@ struct Fsa {
// Note: an index into the `arcs` array is called an arc-index.
std::vector<Arc> arcs;

inline std::size_t NumStates() { return leaving_arcs.size(); }
StateId NumStates() const {
return static_cast<StateId>(leaving_arcs.size());
}
};

/*
Expand All @@ -96,8 +97,8 @@ struct Fsa {
*/
struct DenseFsa {
Weight* weights; // Would typically be a log-prob or unnormalized log-prob
int32_t T; // The number of time steps == rows in the matrix `weights`;
// this FSA has T + 2 states, see explanation above.
int32_t T; // The number of time steps == rows in the matrix `weights`;
// this FSA has T + 2 states, see explanation above.
int32_t num_symbols; // The number of symbols == columns in the matrix
// `weights`.
int32_t t_stride; // The stride of the matrix `weights`
Expand Down
27 changes: 10 additions & 17 deletions k2/csrc/properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,34 @@

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

#include "k2/csrc/properties.h"

#include <unordered_set>

#include "k2/csrc/properties.h"
#include "k2/csrc/fsa.h"

namespace k2 {

bool IsTopSorted(const Fsa &fsa) {
for (auto &arc : fsa.arcs) {
if (arc.dest_state < arc.src_state) {
return false;
}
for (const auto &arc : fsa.arcs) {
if (arc.dest_state < arc.src_state) return false;
}
return true;
}

bool HasSelfLoops(const Fsa &fsa) {
for (auto &arc : fsa.arcs) {
if (arc.dest_state == arc.src_state) {
return true;
}
for (const auto &arc : fsa.arcs) {
if (arc.dest_state == arc.src_state) return true;
}
return false;
}

bool IsDeterministic(const Fsa &fsa) {
std::unordered_set<Label> labels;
StateId state = 0;
for (auto &arc : fsa.arcs) {
for (const auto &arc : fsa.arcs) {
if (arc.src_state == state) {
if (labels.find(arc.label) != labels.end()) {
return false;
}
if (labels.find(arc.label) != labels.end()) return false;
labels.insert(arc.label);
} else {
state = arc.src_state;
Expand All @@ -49,10 +44,8 @@ bool IsDeterministic(const Fsa &fsa) {
}

bool IsEpsilonFree(const Fsa &fsa) {
for (auto &arc : fsa.arcs) {
if (arc.label == kEpsilon) {
return false;
}
for (const auto &arc : fsa.arcs) {
if (arc.label == kEpsilon) return false;
}
return true;
}
Expand Down
26 changes: 13 additions & 13 deletions k2/csrc/properties_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ TEST(Properties, IsTopSorted) {
EXPECT_TRUE(sorted);
}

TEST(Properties, HasNotSelfLoops) {
TEST(Properties, HasNoSelfLoops) {
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, {1, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, };
Fsa fsa;
fsa.leaving_arcs = leaving_arcs;
fsa.arcs = arcs;
bool hasSelfLoops = HasSelfLoops(fsa);
EXPECT_FALSE(hasSelfLoops);
bool has_self_loops = HasSelfLoops(fsa);
EXPECT_FALSE(has_self_loops);
}

TEST(Properties, HasSelfLoops) {
Expand All @@ -55,8 +55,8 @@ TEST(Properties, HasSelfLoops) {
Fsa fsa;
fsa.leaving_arcs = leaving_arcs;
fsa.arcs = arcs;
bool hasSelfLoops = HasSelfLoops(fsa);
EXPECT_TRUE(hasSelfLoops);
bool has_self_loops = HasSelfLoops(fsa);
EXPECT_TRUE(has_self_loops);
}

TEST(Properties, IsNotDeterministic) {
Expand All @@ -65,8 +65,8 @@ TEST(Properties, IsNotDeterministic) {
Fsa fsa;
fsa.leaving_arcs = leaving_arcs;
fsa.arcs = arcs;
bool isDeterministic = IsDeterministic(fsa);
EXPECT_FALSE(isDeterministic);
bool is_deterministic = IsDeterministic(fsa);
EXPECT_FALSE(is_deterministic);
}

TEST(Properties, IsDeterministic) {
Expand All @@ -75,8 +75,8 @@ TEST(Properties, IsDeterministic) {
Fsa fsa;
fsa.leaving_arcs = leaving_arcs;
fsa.arcs = arcs;
bool isDeterministic = IsDeterministic(fsa);
EXPECT_TRUE(isDeterministic);
bool is_deterministic = IsDeterministic(fsa);
EXPECT_TRUE(is_deterministic);
}

TEST(Properties, IsNotEpsilonFree) {
Expand All @@ -85,8 +85,8 @@ TEST(Properties, IsNotEpsilonFree) {
Fsa fsa;
fsa.leaving_arcs = leaving_arcs;
fsa.arcs = arcs;
bool isEpsilonFree = IsEpsilonFree(fsa);
EXPECT_FALSE(isEpsilonFree);
bool is_epsilon_free = IsEpsilonFree(fsa);
EXPECT_FALSE(is_epsilon_free);
}

TEST(Properties, IsEpsilonFree) {
Expand All @@ -95,6 +95,6 @@ TEST(Properties, IsEpsilonFree) {
Fsa fsa;
fsa.leaving_arcs = leaving_arcs;
fsa.arcs = arcs;
bool isEpsilonFree = IsEpsilonFree(fsa);
EXPECT_TRUE(isEpsilonFree);
bool is_epsilon_free = IsEpsilonFree(fsa);
EXPECT_TRUE(is_epsilon_free);
}

0 comments on commit 31d0ee9

Please sign in to comment.