All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 186 of file deterministic-fst.h.

Member Typedef Documentation

typedef Arc::Label Label

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

typedef Arc::StateId StateId

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

typedef Arc::Weight Weight

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

Constructor & Destructor Documentation

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 222 of file deterministic-fst-inl.h.

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

224  : fst_(fst),
225  num_cached_arcs_(num_cached_arcs),
226  cached_arcs_(num_cached_arcs) {
227  KALDI_ASSERT(num_cached_arcs > 0);
228  for (StateId i = 0; i < num_cached_arcs; i++)
229  cached_arcs_[i].first = kNoStateId; // Invalidate all elements of the cache.
230 }
Definition: graph.dox:21
std::vector< std::pair< StateId, Arc > > cached_arcs_
DeterministicOnDemandFst< Arc > * fst_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

Member Function Documentation

virtual Weight Final ( StateId  s)
inlinevirtual

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

Implements DeterministicOnDemandFst< Arc >.

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

References CacheDeterministicOnDemandFst< Arc >::fst_.

Referenced by fst::TestBackoffAndCache().

199 { return fst_->Final(s); }
DeterministicOnDemandFst< Arc > * fst_
bool GetArc ( StateId  s,
Label  ilabel,
Arc *  oarc 
)
virtual

Note: ilabel must not be epsilon.

Implements DeterministicOnDemandFst< Arc >.

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

References KALDI_ASSERT.

Referenced by fst::TestBackoffAndCache().

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

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

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

Implements DeterministicOnDemandFst< Arc >.

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

References CacheDeterministicOnDemandFst< Arc >::fst_.

Referenced by fst::TestCompose().

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

Member Data Documentation

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

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


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