All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LatticeWeightTpl< FloatType > Class Template Reference

#include <lattice-weight.h>

Collaboration diagram for LatticeWeightTpl< FloatType >:

Public Types

typedef FloatType T
 
typedef LatticeWeightTpl ReverseWeight
 

Public Member Functions

T Value1 () const
 
T Value2 () const
 
void SetValue1 (T f)
 
void SetValue2 (T f)
 
 LatticeWeightTpl ()
 
 LatticeWeightTpl (T a, T b)
 
 LatticeWeightTpl (const LatticeWeightTpl &other)
 
LatticeWeightTploperator= (const LatticeWeightTpl &w)
 
LatticeWeightTpl< FloatType > Reverse () const
 
bool Member () const
 
LatticeWeightTpl Quantize (float delta=kDelta) const
 
istream & Read (istream &strm)
 
ostream & Write (ostream &strm) const
 
size_t Hash () const
 

Static Public Member Functions

static const LatticeWeightTpl Zero ()
 
static const LatticeWeightTpl One ()
 
static const string & Type ()
 
static const LatticeWeightTpl NoWeight ()
 
static uint64 Properties ()
 

Protected Member Functions

istream & ReadNoParen (istream &strm, char separator)
 

Static Protected Member Functions

static void WriteFloatType (ostream &strm, const T &f)
 
static void ReadFloatType (istream &strm, T &f)
 

Private Attributes

T value1_
 
T value2_
 

Friends

istream & operator>> (istream &, LatticeWeightTpl< FloatType > &)
 
ostream & operator (ostream &, const LatticeWeightTpl< FloatType > &)
 

Detailed Description

template<class FloatType>
class fst::LatticeWeightTpl< FloatType >

Definition at line 37 of file lattice-weight.h.

Member Typedef Documentation

Definition at line 50 of file lattice-weight.h.

typedef FloatType T

Definition at line 49 of file lattice-weight.h.

Constructor & Destructor Documentation

LatticeWeightTpl ( T  a,
T  b 
)
inline

Definition at line 62 of file lattice-weight.h.

LatticeWeightTpl ( const LatticeWeightTpl< FloatType > &  other)
inline

Definition at line 64 of file lattice-weight.h.

64 : value1_(other.value1_), value2_(other.value2_) { }

Member Function Documentation

size_t Hash ( ) const
inline

Definition at line 147 of file lattice-weight.h.

147  {
148  size_t ans;
149  union {
150  T f;
151  size_t s;
152  } u;
153  u.s = 0;
154  u.f = value1_;
155  ans = u.s;
156  u.f = value2_;
157  ans += u.s;
158  return ans;
159  }
bool Member ( ) const
inline

Definition at line 95 of file lattice-weight.h.

Referenced by fst::LatticeWeightTest().

95  {
96  // value1_ == value1_ tests for NaN.
97  // also test for no -inf, and either both or neither
98  // must be +inf, and
99  if (value1_ != value1_ || value2_ != value2_) return false; // NaN
100  if (value1_ == -numeric_limits<T>::infinity() ||
101  value2_ == -numeric_limits<T>::infinity()) return false; // -infty not allowed
102  if (value1_ == numeric_limits<T>::infinity() ||
103  value2_ == numeric_limits<T>::infinity()) {
104  if (value1_ != numeric_limits<T>::infinity() ||
105  value2_ != numeric_limits<T>::infinity()) return false; // both must be +infty;
106  // this is necessary so that the semiring has only one zero.
107  }
108  return true;
109  }
static const LatticeWeightTpl NoWeight ( )
inlinestatic

Definition at line 90 of file lattice-weight.h.

90  {
91  return LatticeWeightTpl(numeric_limits<FloatType>::quiet_NaN(),
92  numeric_limits<FloatType>::quiet_NaN());
93  }
static const LatticeWeightTpl One ( )
inlinestatic

Definition at line 81 of file lattice-weight.h.

81  {
82  return LatticeWeightTpl(0.0, 0.0);
83  }
LatticeWeightTpl& operator= ( const LatticeWeightTpl< FloatType > &  w)
inline

Definition at line 66 of file lattice-weight.h.

66  {
67  value1_ = w.value1_;
68  value2_ = w.value2_;
69  return *this;
70  }
static uint64 Properties ( )
inlinestatic

Definition at line 122 of file lattice-weight.h.

122  {
123  return kLeftSemiring | kRightSemiring | kCommutative |
124  kPath | kIdempotent;
125  }
LatticeWeightTpl Quantize ( float  delta = kDelta) const
inline

Definition at line 111 of file lattice-weight.h.

Referenced by fst::LatticeWeightTest().

111  {
112  if (value1_+value2_ == -numeric_limits<T>::infinity()) {
113  return LatticeWeightTpl(-numeric_limits<T>::infinity(), -numeric_limits<T>::infinity());
114  } else if (value1_+value2_ == numeric_limits<T>::infinity()) {
115  return LatticeWeightTpl(numeric_limits<T>::infinity(), numeric_limits<T>::infinity());
116  } else if (value1_+value2_ != value1_+value2_) { // NaN
118  } else {
119  return LatticeWeightTpl(floor(value1_/delta + 0.5F)*delta, floor(value2_/delta + 0.5F) * delta);
120  }
121  }
istream& Read ( istream &  strm)
inline

Definition at line 129 of file lattice-weight.h.

Referenced by fst::LatticeWeightTest().

129  {
130  // Always read/write as float, even if T is double,
131  // so we can use OpenFst-style read/write and still maintain
132  // compatibility when compiling with different FloatTypes
133  ReadType(strm, &value1_);
134  ReadType(strm, &value2_);
135  return strm;
136  }
static void ReadFloatType ( istream &  strm,
T f 
)
inlinestaticprotected

Definition at line 174 of file lattice-weight.h.

Referenced by LatticeWeightTpl< BaseFloat >::ReadNoParen().

174  {
175  string s;
176  strm >> s;
177  if (s == "Infinity") {
178  f = numeric_limits<T>::infinity();
179  } else if (s == "-Infinity") {
180  f = -numeric_limits<T>::infinity();
181  } else if (s == "BadNumber") {
182  f = numeric_limits<T>::infinity();
183  f -= f; // get NaN
184  } else {
185  char *p;
186  f = strtod(s.c_str(), &p);
187  if (p < s.c_str() + s.size())
188  strm.clear(std::ios::badbit);
189  }
190  }
istream& ReadNoParen ( istream &  strm,
char  separator 
)
inlineprotected

Definition at line 194 of file lattice-weight.h.

Referenced by fst::operator>>().

195  {
196  int c;
197  do {
198  c = strm.get();
199  } while (isspace(c));
200 
201  string s1;
202  while (c != separator) {
203  if (c == EOF) {
204  strm.clear(std::ios::badbit);
205  return strm;
206  }
207  s1 += c;
208  c = strm.get();
209  }
210  istringstream strm1(s1);
211  ReadFloatType(strm1, value1_); // ReadFloatType is class member function
212  // read second element
213  ReadFloatType(strm, value2_);
214  return strm;
215  }
static void ReadFloatType(istream &strm, T &f)
LatticeWeightTpl<FloatType> Reverse ( ) const
inline

Definition at line 72 of file lattice-weight.h.

Referenced by fst::LatticeWeightTest().

72  {
73  return *this;
74  }
static const string& Type ( )
inlinestatic

Definition at line 85 of file lattice-weight.h.

85  {
86  static const string type = (sizeof(T) == 4 ? "lattice4" : "lattice8") ;
87  return type;
88  }
ostream& Write ( ostream &  strm) const
inline

Definition at line 141 of file lattice-weight.h.

141  {
142  WriteType(strm, value1_);
143  WriteType(strm, value2_);
144  return strm;
145  }
static void WriteFloatType ( ostream &  strm,
const T f 
)
inlinestaticprotected

Definition at line 162 of file lattice-weight.h.

Referenced by fst::operator<<().

162  {
163  if (f == numeric_limits<T>::infinity())
164  strm << "Infinity";
165  else if (f == -numeric_limits<T>::infinity())
166  strm << "-Infinity";
167  else if (f != f)
168  strm << "BadNumber";
169  else
170  strm << f;
171  }
static const LatticeWeightTpl Zero ( )
inlinestatic

Definition at line 76 of file lattice-weight.h.

Referenced by fst::Divide().

76  {
77  return LatticeWeightTpl(numeric_limits<T>::infinity(),
78  numeric_limits<T>::infinity());
79  }

Friends And Related Function Documentation

ostream& operator ( ostream &  ,
const LatticeWeightTpl< FloatType > &   
)
friend
istream& operator>> ( istream &  strm,
LatticeWeightTpl< FloatType > &  w 
)
friend

Definition at line 377 of file lattice-weight.h.

377  {
378  CHECK(FLAGS_fst_weight_separator.size() == 1);
379  // separator defaults to ','
380  return w1.ReadNoParen(strm, FLAGS_fst_weight_separator[0]);
381 }

Member Data Documentation


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