All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TableMatcherImpl< F, BackoffMatcher > Class Template Reference

#include <table-matcher.h>

Inheritance diagram for TableMatcherImpl< F, BackoffMatcher >:
Collaboration diagram for TableMatcherImpl< F, BackoffMatcher >:

Public Types

typedef F FST
 
typedef F::Arc Arc
 
typedef Arc::Label Label
 
typedef Arc::StateId StateId
 
typedef StateId ArcId
 
typedef Arc::Weight Weight
 

Public Member Functions

 TableMatcherImpl (const FST &fst, MatchType match_type, const TableMatcherOptions &opts=TableMatcherOptions())
 
virtual const FSTGetFst () const
 
virtual ~TableMatcherImpl ()
 
virtual MatchType Type (bool test) const
 
void SetState (StateId s)
 
bool Find (Label match_label)
 
const ArcValue () const
 
void Next ()
 
bool Done () const
 
const ArcValue ()
 
virtual TableMatcherImpl< FST > * Copy (bool safe=false) const
 
virtual uint64 Properties (uint64 props) const
 
int RefCount () const
 
int IncrRefCount ()
 
int DecrRefCount ()
 

Private Member Functions

virtual void SetState_ (StateId s)
 
virtual bool Find_ (Label label)
 
virtual bool Done_ () const
 
virtual const ArcValue_ () const
 
virtual void Next_ ()
 

Private Attributes

RefCounter ref_count_
 
MatchType match_type_
 
FSTfst_
 
bool current_loop_
 
Label match_label_
 
Arc loop_
 
ArcIterator< FST > * aiter_
 
StateId s_
 
vector< vector< ArcId > * > tables_
 
TableMatcherOptions opts_
 
BackoffMatcher backoff_matcher_
 

Detailed Description

template<class F, class BackoffMatcher = SortedMatcher<F>>
class fst::TableMatcherImpl< F, BackoffMatcher >

Definition at line 53 of file table-matcher.h.

Member Typedef Documentation

typedef F::Arc Arc

Definition at line 56 of file table-matcher.h.

typedef StateId ArcId

Definition at line 59 of file table-matcher.h.

typedef F FST

Definition at line 55 of file table-matcher.h.

typedef Arc::Label Label

Definition at line 57 of file table-matcher.h.

typedef Arc::StateId StateId

Definition at line 58 of file table-matcher.h.

typedef Arc::Weight Weight

Definition at line 61 of file table-matcher.h.

Constructor & Destructor Documentation

TableMatcherImpl ( const FST fst,
MatchType  match_type,
const TableMatcherOptions opts = TableMatcherOptions() 
)
inline

Definition at line 66 of file table-matcher.h.

67  :
68  match_type_(match_type),
69  fst_(fst.Copy()),
70  loop_(match_type == MATCH_INPUT ?
71  Arc(kNoLabel, 0, Weight::One(), kNoStateId) :
72  Arc(0, kNoLabel, Weight::One(), kNoStateId)),
73  aiter_(NULL),
74  s_(kNoStateId), opts_(opts),
75  backoff_matcher_(fst, match_type)
76  {
77  assert(opts_.min_table_size > 0);
78  if (match_type == MATCH_INPUT)
79  assert(fst_->Properties(kILabelSorted, true) == kILabelSorted);
80  else if (match_type == MATCH_OUTPUT)
81  assert(fst_->Properties(kOLabelSorted, true) == kOLabelSorted);
82  else
83  assert(0 && "Invalid FST properties");
84  }
Definition: graph.dox:21
TableMatcherOptions opts_
ArcIterator< FST > * aiter_
BackoffMatcher backoff_matcher_
virtual ~TableMatcherImpl ( )
inlinevirtual

Definition at line 88 of file table-matcher.h.

88  {
89  assert(RefCount() == 0);
90  vector<ArcId> *const empty = ((vector<ArcId>*)(NULL)) + 1; // special marker.
91  for (size_t i = 0; i < tables_.size(); i++) {
92  if (tables_[i] != NULL && tables_[i] != empty)
93  delete tables_[i];
94  }
95  delete aiter_;
96  delete fst_;
97  }
vector< vector< ArcId > * > tables_
ArcIterator< FST > * aiter_

Member Function Documentation

virtual TableMatcherImpl<FST>* Copy ( bool  safe = false) const
inlinevirtual

Definition at line 213 of file table-matcher.h.

213  {
214  assert(0); // shouldn't be called. This is not a "real" matcher,
215  // although we derive from MatcherBase for convenience.
216  return NULL;
217  }
int DecrRefCount ( )
inline

Definition at line 230 of file table-matcher.h.

230  {
231  return ref_count_.Decr();
232  }
bool Done ( ) const
inline

Definition at line 194 of file table-matcher.h.

Referenced by TableMatcherImpl< fst::Fst< fst::StdArc >, BackoffMatcher >::Done_(), and TableMatcher< fst::Fst< fst::StdArc > >::Done_().

194  {
195  if (aiter_ != NULL) {
196  if (current_loop_)
197  return false;
198  if (aiter_->Done())
199  return true;
200  Label label = (match_type_ == MATCH_OUTPUT ?
201  aiter_->Value().olabel : aiter_->Value().ilabel);
202  return (label != match_label_);
203  } else
204  return backoff_matcher_.Done();
205  }
ArcIterator< FST > * aiter_
BackoffMatcher backoff_matcher_
virtual bool Done_ ( ) const
inlineprivatevirtual

Definition at line 238 of file table-matcher.h.

238 { return Done(); }
bool Find ( Label  match_label)
inline

Definition at line 161 of file table-matcher.h.

Referenced by TableMatcherImpl< fst::Fst< fst::StdArc >, BackoffMatcher >::Find_(), and TableMatcher< fst::Fst< fst::StdArc > >::Find_().

161  {
162  if (!aiter_) return backoff_matcher_.Find(match_label);
163  else {
164  match_label_ = match_label;
165  current_loop_ = (match_label == 0);
166  // kNoLabel means the implicit loop on the other FST --
167  // matches real epsilons but not the self-loop.
168  match_label_ = (match_label_ == kNoLabel ? 0 : match_label_);
169  if (static_cast<size_t>(match_label_) < tables_[s_]->size() &&
170  (*(tables_[s_]))[match_label_] != kNoStateId) {
171  aiter_->Seek( (*(tables_[s_]))[match_label_] ); // label exists.
172  return true;
173  }
174  return current_loop_;
175  }
176  }
vector< vector< ArcId > * > tables_
ArcIterator< FST > * aiter_
BackoffMatcher backoff_matcher_
virtual bool Find_ ( Label  label)
inlineprivatevirtual

Definition at line 237 of file table-matcher.h.

237 { return Find(label); }
bool Find(Label match_label)
virtual const FST& GetFst ( ) const
inlinevirtual

Definition at line 86 of file table-matcher.h.

86 { return *fst_; }
int IncrRefCount ( )
inline

Definition at line 226 of file table-matcher.h.

226  {
227  return ref_count_.Incr();
228  }
void Next ( )
inline

Definition at line 184 of file table-matcher.h.

Referenced by TableMatcherImpl< fst::Fst< fst::StdArc >, BackoffMatcher >::Next_(), and TableMatcher< fst::Fst< fst::StdArc > >::Next_().

184  {
185  if (aiter_) {
186  if (current_loop_)
187  current_loop_ = false;
188  else
189  aiter_->Next();
190  } else
191  backoff_matcher_.Next();
192  }
ArcIterator< FST > * aiter_
BackoffMatcher backoff_matcher_
virtual void Next_ ( )
inlineprivatevirtual

Definition at line 240 of file table-matcher.h.

240 { Next(); }
virtual uint64 Properties ( uint64  props) const
inlinevirtual

Definition at line 219 of file table-matcher.h.

219 { return props; } // simple matcher that does
int RefCount ( ) const
inline

Definition at line 222 of file table-matcher.h.

Referenced by TableMatcherImpl< fst::Fst< fst::StdArc >, BackoffMatcher >::~TableMatcherImpl().

222  {
223  return ref_count_.count();
224  }
void SetState ( StateId  s)
inline

Definition at line 103 of file table-matcher.h.

Referenced by TableMatcherImpl< fst::Fst< fst::StdArc >, BackoffMatcher >::SetState_(), and TableMatcher< fst::Fst< fst::StdArc > >::SetState_().

103  {
104  if (aiter_) {
105  delete aiter_;
106  aiter_ = NULL;
107  }
108  if (match_type_ == MATCH_NONE)
109  LOG(FATAL) << "TableMatcher: bad match type";
110  s_ = s;
111  vector<ArcId> *const empty = ((vector<ArcId>*)(NULL)) + 1; // special marker.
112  if (static_cast<size_t>(s) >= tables_.size()) {
113  assert(s>=0);
114  tables_.resize(s+1, NULL);
115  }
116  vector<ArcId>* &this_table_ = tables_[s]; // note: ref to ptr.
117  if (this_table_ == empty) {
118  backoff_matcher_.SetState(s);
119  return;
120  } else if (this_table_ == NULL) { // NULL means has not been set.
121  ArcId num_arcs = fst_->NumArcs(s);
122  if (num_arcs == 0 || num_arcs < opts_.min_table_size) {
123  this_table_ = empty;
124  backoff_matcher_.SetState(s);
125  return;
126  }
127  ArcIterator<FST> aiter(*fst_, s);
128  aiter.SetFlags(kArcNoCache|(match_type_ == MATCH_OUTPUT?kArcOLabelValue:kArcILabelValue),
129  kArcNoCache|kArcValueFlags);
130  // the statement above, says: "Don't cache stuff; and I only need the ilabel/olabel
131  // to be computed.
132  aiter.Seek(num_arcs - 1);
133  Label highest_label = (match_type_ == MATCH_OUTPUT ?
134  aiter.Value().olabel : aiter.Value().ilabel);
135  if ((highest_label+1) * opts_.table_ratio > num_arcs) {
136  this_table_ = empty;
137  backoff_matcher_.SetState(s);
138  return; // table would be too sparse.
139  }
140  // OK, now we are creating the table.
141  this_table_ = new vector<ArcId> (highest_label+1, kNoStateId);
142  ArcId pos = 0;
143  for (aiter.Seek(0); !aiter.Done(); aiter.Next(), pos++) {
144  Label label = (match_type_ == MATCH_OUTPUT ?
145  aiter.Value().olabel : aiter.Value().ilabel);
146  assert((size_t)label <= (size_t)highest_label); // also checks >= 0.
147  if ((*this_table_)[label] == kNoStateId) (*this_table_)[label] = pos;
148  // set this_table_[label] to first position where arc has this
149  // label.
150  }
151  }
152  // At this point in the code, this_table_ != NULL and != empty.
153  aiter_ = new ArcIterator<FST>(*fst_, s);
154  aiter_->SetFlags(kArcNoCache, kArcNoCache); // don't need to cache arcs as may only
155  // need a small subset.
156  loop_.nextstate = s;
157  // aiter_ = NULL;
158  // backoff_matcher_.SetState(s);
159  }
vector< vector< ArcId > * > tables_
TableMatcherOptions opts_
ArcIterator< FST > * aiter_
BackoffMatcher backoff_matcher_
virtual void SetState_ ( StateId  s)
inlineprivatevirtual

Definition at line 236 of file table-matcher.h.

236 { SetState(s); }
void SetState(StateId s)
virtual MatchType Type ( bool  test) const
inlinevirtual

Definition at line 99 of file table-matcher.h.

99  {
100  return match_type_;
101  }
const Arc& Value ( ) const
inline

Definition at line 177 of file table-matcher.h.

Referenced by TableMatcherImpl< fst::Fst< fst::StdArc >, BackoffMatcher >::Value_(), and TableMatcher< fst::Fst< fst::StdArc > >::Value_().

177  {
178  if (aiter_)
179  return current_loop_ ? loop_ : aiter_->Value();
180  else
181  return backoff_matcher_.Value();
182  }
ArcIterator< FST > * aiter_
BackoffMatcher backoff_matcher_
const Arc& Value ( )
inline

Definition at line 206 of file table-matcher.h.

206  {
207  if (aiter_ != NULL) {
208  return (current_loop_ ? loop_ : aiter_->Value() );
209  } else
210  return backoff_matcher_.Value();
211  }
ArcIterator< FST > * aiter_
BackoffMatcher backoff_matcher_
virtual const Arc& Value_ ( ) const
inlineprivatevirtual

Definition at line 239 of file table-matcher.h.

239 { return Value(); }
const Arc & Value() const

Member Data Documentation


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