diff --git a/.gitignore b/.gitignore index 71e7b25e0..766d882ea 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,12 @@ # emacs save files *.~ + +# Files generated by ctags +CTAGS +GTAGS +GRTAGS +GSYMS +GPATH +tags +TAGS diff --git a/k2/csrc/fsa.h b/k2/csrc/fsa.h index e4dfaaaee..f6d591983 100644 --- a/k2/csrc/fsa.h +++ b/k2/csrc/fsa.h @@ -12,6 +12,10 @@ namespace k2 { +using Label = int32_t; +using StateId = int32_t; +using Weight = float; + enum { kFinalSymbol = -1, // final-costs are represented as arcs with // kFinalSymbol as their label, to the final @@ -31,9 +35,9 @@ struct Range { }; struct Arc { - int32_t src_state; - int32_t dest_state; - int32_t label; // 'label' as in a finite state acceptor. + 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, @@ -91,7 +95,7 @@ struct Fsa { */ struct DenseFsa { - float* weights; // Would typically be a log-prob or unnormalized log-prob + 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 num_symbols; // The number of symbols == columns in the matrix @@ -105,7 +109,7 @@ struct DenseFsa { CAUTION: we may later enforce that stride == num_symbols, in order to be able to know the layout of a phantom matrix of arcs. (?) */ - DenseFsa(float* data, int32_t T, int32_t num_symbols, int32_t stride); + DenseFsa(Weight* data, int32_t T, int32_t num_symbols, int32_t stride); }; /* diff --git a/k2/csrc/properties.cc b/k2/csrc/properties.cc index b3fab9dc2..0821416da 100644 --- a/k2/csrc/properties.cc +++ b/k2/csrc/properties.cc @@ -1,36 +1,57 @@ // k2/csrc/properties.cc -// Copyright (c) 2020 Daniel Povey +// Copyright (c) 2020 Haowen Qiu +// Daniel Povey // See ../../LICENSE for clarification regarding multiple authors -#include "k2/csrc/properties.h" +#include +#include "k2/csrc/properties.h" #include "k2/csrc/fsa.h" namespace k2 { -bool IsTopSorted(const Fsa& fsa) { - for (const auto& range : fsa.leaving_arcs) { - for (auto arc_idx = range.begin; arc_idx < range.end; ++arc_idx) { - const Arc& arc = fsa.arcs[arc_idx]; - if (arc.dest_state < arc.src_state) { - return false; - } +bool IsTopSorted(const Fsa &fsa) { + for (auto &arc : fsa.arcs) { + if (arc.dest_state < arc.src_state) { + return false; } } return true; } -bool HasSelfLoops(const Fsa& fsa) { - // TODO(haowen): refactor code below as we have - // so many for-for-loop structures - for (const auto& range : fsa.leaving_arcs) { - for (auto arc_idx = range.begin; arc_idx < range.end; ++arc_idx) { - const Arc& arc = fsa.arcs[arc_idx]; - if (arc.dest_state == arc.src_state) { +bool HasSelfLoops(const Fsa &fsa) { + for (auto &arc : fsa.arcs) { + if (arc.dest_state == arc.src_state) { + return true; + } + } + return false; +} + +bool IsDeterministic(const Fsa &fsa) { + std::unordered_set