CacheDeterministicOnDemandFst< Arc > Class Template Reference

#include <deterministic-fst.h>

Inheritance diagram for CacheDeterministicOnDemandFst< Arc >:
Collaboration diagram for CacheDeterministicOnDemandFst< Arc >:

Public Types

typedef Arc::StateId StateId
 
typedef Arc::Weight Weight
 
typedef Arc::Label Label
 
- Public Types inherited from DeterministicOnDemandFst< Arc >
typedef Arc::StateId StateId
 
typedef Arc::Weight Weight
 
typedef Arc::Label Label
 

Public Member Functions

 CacheDeterministicOnDemandFst (DeterministicOnDemandFst< Arc > *fst, StateId num_cached_arcs=100000)
 We don't take ownership of this pointer. The argument is "really" const. More...
 
virtual StateId Start ()
 
virtual Weight Final (StateId s)
 We don't bother caching the final-probs, just the arcs. More...
 
virtual bool GetArc (StateId s, Label ilabel, Arc *oarc)
 Note: ilabel must not be epsilon. More...
 
- Public Member Functions inherited from DeterministicOnDemandFst< Arc >
virtual ~DeterministicOnDemandFst ()
 

Private Member Functions

size_t GetIndex (StateId src_state, Label ilabel)
 

Private Attributes

DeterministicOnDemandFst< Arc > * fst_
 
StateId num_cached_arcs_
 
std::vector< std::pair< StateId, Arc > > cached_arcs_
 

Detailed Description

template<class Arc>
class fst::CacheDeterministicOnDemandFst< Arc >

Definition at line 230 of file deterministic-fst.h.

Member Typedef Documentation

◆ Label

typedef Arc::Label Label

Definition at line 234 of file deterministic-fst.h.

◆ StateId

typedef Arc::StateId StateId

Definition at line 232 of file deterministic-fst.h.

◆ Weight

typedef Arc::Weight Weight

Definition at line 233 of file deterministic-fst.h.

Constructor & Destructor Documentation

◆ CacheDeterministicOnDemandFst()

CacheDeterministicOnDemandFst ( DeterministicOnDemandFst< Arc > *  fst,
StateId  num_cached_arcs = 100000 
)

We don't take ownership of this pointer. The argument is "really" const.

Definition at line 223 of file deterministic-fst-inl.h.

References CacheDeterministicOnDemandFst< Arc >::cached_arcs_, rnnlm::i, and KALDI_ASSERT.

225  : fst_(fst),
226  num_cached_arcs_(num_cached_arcs),
227  cached_arcs_(num_cached_arcs) {
228  KALDI_ASSERT(num_cached_arcs > 0);
229  for (StateId i = 0; i < num_cached_arcs; i++)
230  cached_arcs_[i].first = kNoStateId; // Invalidate all elements of the cache.
231 }
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
std::vector< std::pair< StateId, Arc > > cached_arcs_
DeterministicOnDemandFst< Arc > * fst_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Function Documentation

◆ Final()

virtual Weight Final ( StateId  s)
inlinevirtual

We don't bother caching the final-probs, just the arcs.

Implements DeterministicOnDemandFst< Arc >.

Definition at line 243 of file deterministic-fst.h.

References DeterministicOnDemandFst< Arc >::GetArc().

Referenced by fst::TestBackoffAndCache().

243 { return fst_->Final(s); }
DeterministicOnDemandFst< Arc > * fst_

◆ GetArc()

bool GetArc ( StateId  s,
Label  ilabel,
Arc *  oarc 
)
virtual

Note: ilabel must not be epsilon.

Implements DeterministicOnDemandFst< Arc >.

Definition at line 234 of file deterministic-fst-inl.h.

References CacheDeterministicOnDemandFst< Arc >::cached_arcs_, CacheDeterministicOnDemandFst< Arc >::fst_, CacheDeterministicOnDemandFst< Arc >::GetIndex(), and KALDI_ASSERT.

Referenced by fst::TestBackoffAndCache().

235  {
236  // Note: we don't cache anything in case a requested arc does not exist.
237  // In the uses that we imagine this will be put to, essentially all the
238  // requested arcs will exist. This only affects efficiency.
239  KALDI_ASSERT(s >= 0 && ilabel != 0);
240  size_t index = this->GetIndex(s, ilabel);
241  if (cached_arcs_[index].first == s &&
242  cached_arcs_[index].second.ilabel == ilabel) {
243  *oarc = cached_arcs_[index].second;
244  return true;
245  } else {
246  Arc arc;
247  if (fst_->GetArc(s, ilabel, &arc)) {
248  cached_arcs_[index].first = s;
249  cached_arcs_[index].second = arc;
250  *oarc = arc;
251  return true;
252  } else {
253  return false;
254  }
255  }
256 }
size_t GetIndex(StateId src_state, Label ilabel)
std::vector< std::pair< StateId, Arc > > cached_arcs_
DeterministicOnDemandFst< Arc > * fst_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetIndex()

size_t GetIndex ( StateId  src_state,
Label  ilabel 
)
inlineprivate

Definition at line 210 of file deterministic-fst-inl.h.

Referenced by CacheDeterministicOnDemandFst< Arc >::GetArc().

211  {
212  const StateId p1 = 26597, p2 = 50329; // these are two
213  // values that I drew at random from a table of primes.
214  // note: num_cached_arcs_ > 0.
215 
216  // We cast to size_t before the modulus, to ensure the
217  // result is positive.
218  return static_cast<size_t>(src_state * p1 + ilabel * p2) %
219  static_cast<size_t>(num_cached_arcs_);
220 }

◆ Start()

virtual StateId Start ( )
inlinevirtual

Implements DeterministicOnDemandFst< Arc >.

Definition at line 240 of file deterministic-fst.h.

Referenced by fst::TestCompose().

240 { return fst_->Start(); }
DeterministicOnDemandFst< Arc > * fst_

Member Data Documentation

◆ cached_arcs_

std::vector<std::pair<StateId, Arc> > cached_arcs_
private

◆ fst_

DeterministicOnDemandFst<Arc>* fst_
private

Definition at line 251 of file deterministic-fst.h.

Referenced by CacheDeterministicOnDemandFst< Arc >::GetArc().

◆ num_cached_arcs_

StateId num_cached_arcs_
private

Definition at line 252 of file deterministic-fst.h.


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