ArcIterator< GrammarFst > Class Template Reference

This is the overridden template for class ArcIterator for GrammarFst. More...

#include <grammar-fst.h>

Collaboration diagram for ArcIterator< GrammarFst >:

Public Types

using Arc = typename GrammarFst::Arc
 
using BaseArc = StdArc
 
using StateId = typename Arc::StateId
 
using BaseStateId = typename StdArc::StateId
 
using ExpandedState = GrammarFst::ExpandedState
 

Public Member Functions

 ArcIterator (const GrammarFst &fst_in, StateId s)
 
bool Done ()
 
void Next ()
 
const ArcValue () const
 

Private Member Functions

void CopyArcToTemp ()
 

Private Attributes

ArcIteratorData< StdArcdata_
 
int32 dest_instance_
 
size_t i_
 
Arc arc_
 

Detailed Description

template<>
class fst::ArcIterator< GrammarFst >

This is the overridden template for class ArcIterator for GrammarFst.

This is only used in the decoder, and the GrammarFst is not a "real" FST (it just has a very similar-looking interface), so we don't need to implement all the functionality that the regular ArcIterator has.

Definition at line 496 of file grammar-fst.h.

Member Typedef Documentation

◆ Arc

using Arc = typename GrammarFst::Arc

Definition at line 498 of file grammar-fst.h.

◆ BaseArc

using BaseArc = StdArc

Definition at line 499 of file grammar-fst.h.

◆ BaseStateId

using BaseStateId = typename StdArc::StateId

Definition at line 501 of file grammar-fst.h.

◆ ExpandedState

Definition at line 502 of file grammar-fst.h.

◆ StateId

using StateId = typename Arc::StateId

Definition at line 500 of file grammar-fst.h.

Constructor & Destructor Documentation

◆ ArcIterator()

ArcIterator ( const GrammarFst fst_in,
StateId  s 
)
inline

Definition at line 506 of file grammar-fst.h.

References GrammarFst::ExpandedState::arcs, data_, GrammarFst::ExpandedState::dest_fst_instance, GrammarFst::FstInstance::fst, GrammarFst::GetExpandedState(), GrammarFst::instances_, and KALDI_GRAMMAR_FST_SPECIAL_WEIGHT.

506  {
507  GrammarFst &fst = const_cast<GrammarFst&>(fst_in);
508  // 'instance_id' is the high order bits of the state.
509  int32 instance_id = s >> 32;
510  // 'base_state' is low order bits of the state. It's important to
511  // explicitly say int32 below, not BaseStateId == int, which might on some
512  // compilers be a 64-bit type.
513  BaseStateId base_state = static_cast<int32>(s);
514  const GrammarFst::FstInstance &instance = fst.instances_[instance_id];
515  const ConstFst<StdArc> *base_fst = instance.fst;
516  if (base_fst->Final(base_state).Value() != KALDI_GRAMMAR_FST_SPECIAL_WEIGHT) {
517  // A normal state
518  dest_instance_ = instance_id;
519  base_fst->InitArcIterator(s, &data_);
520  i_ = 0;
521  } else {
522  // A special state
523  ExpandedState *expanded_state = fst.GetExpandedState(instance_id,
524  base_state);
525  dest_instance_ = expanded_state->dest_fst_instance;
526  // it's ok to leave the other members of data_ uninitialized, as they will
527  // never be interrogated.
528  data_.arcs = &(expanded_state->arcs[0]);
529  data_.narcs = expanded_state->arcs.size();
530  i_ = 0;
531  }
532  // Ideally we want to call CopyArcToTemp() now, but we rely on the fact that
533  // the calling code needs to call Done() before accessing Value(); we call
534  // CopyArcToTemp() from Done(). Of course this is slightly against the
535  // semantics of Done(), but it's more efficient to have Done() call
536  // CopyArcToTemp() than this function or Next(), as Done() already has to
537  // test that the arc-iterator has not reached the end.
538  }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
kaldi::int32 int32
#define KALDI_GRAMMAR_FST_SPECIAL_WEIGHT
Definition: grammar-fst.h:67
ArcIteratorData< StdArc > data_
Definition: grammar-fst.h:576
typename StdArc::StateId BaseStateId
Definition: grammar-fst.h:501
GrammarFst::ExpandedState ExpandedState
Definition: grammar-fst.h:502

Member Function Documentation

◆ CopyArcToTemp()

void CopyArcToTemp ( )
inlineprivate

Definition at line 564 of file grammar-fst.h.

References data_.

564  {
565  const StdArc &src = data_.arcs[i_];
566  arc_.ilabel = src.ilabel;
567  arc_.olabel = src.olabel;
568  arc_.weight = src.weight;
569  arc_.nextstate = (static_cast<int64>(dest_instance_) << 32) |
570  src.nextstate;
571  }
fst::StdArc StdArc
ArcIteratorData< StdArc > data_
Definition: grammar-fst.h:576

◆ Done()

bool Done ( )
inline

Definition at line 540 of file grammar-fst.h.

References data_.

Referenced by fst::CopyToVectorFst().

540  {
541  if (i_ < data_.narcs) {
542  CopyArcToTemp();
543  return false;
544  } else {
545  return true;
546  }
547  }
ArcIteratorData< StdArc > data_
Definition: grammar-fst.h:576

◆ Next()

void Next ( )
inline

Definition at line 549 of file grammar-fst.h.

Referenced by fst::CopyToVectorFst().

549  {
550  i_++;
551  // Note: logically, at this point we should do:
552  // if (i_ < data_.size)
553  // CopyArcToTemp();
554  // Instead we move this CopyArcToTemp() invocation into Done(), which we
555  // know will always be called after Next() and before Value(), because the
556  // user has no other way of knowing whether the iterator is still valid.
557  // This is for efficiency.
558  }

◆ Value()

const Arc& Value ( ) const
inline

Definition at line 560 of file grammar-fst.h.

Referenced by fst::CopyToVectorFst().

560 { return arc_; }

Member Data Documentation

◆ arc_

Arc arc_
private

Definition at line 583 of file grammar-fst.h.

◆ data_

ArcIteratorData<StdArc> data_
private

Definition at line 576 of file grammar-fst.h.

◆ dest_instance_

int32 dest_instance_
private

Definition at line 579 of file grammar-fst.h.

◆ i_

size_t i_
private

Definition at line 581 of file grammar-fst.h.


The documentation for this class was generated from the following file: