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
 
std::istream & Read (std::istream &strm)
 
std::ostream & Write (std::ostream &strm) const
 
size_t Hash () const
 

Static Public Member Functions

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

Protected Member Functions

std::istream & ReadNoParen (std::istream &strm, char separator)
 

Static Protected Member Functions

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

Private Attributes

T value1_
 
T value2_
 

Friends

std::istream & operator>> (std::istream &, LatticeWeightTpl< FloatType > &)
 
std::ostream & operator (std::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

◆ ReverseWeight

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

◆ T

typedef FloatType T

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

Constructor & Destructor Documentation

◆ LatticeWeightTpl() [1/3]

◆ LatticeWeightTpl() [2/3]

LatticeWeightTpl ( T  a,
T  b 
)
inline

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

◆ LatticeWeightTpl() [3/3]

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

◆ Hash()

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  }

◆ Member()

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_ == -std::numeric_limits<T>::infinity() ||
101  value2_ == -std::numeric_limits<T>::infinity()) return false; // -infty not allowed
102  if (value1_ == std::numeric_limits<T>::infinity() ||
103  value2_ == std::numeric_limits<T>::infinity()) {
104  if (value1_ != std::numeric_limits<T>::infinity() ||
105  value2_ != std::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  }

◆ NoWeight()

static const LatticeWeightTpl NoWeight ( )
inlinestatic

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

90  {
91  return LatticeWeightTpl(std::numeric_limits<FloatType>::quiet_NaN(),
92  std::numeric_limits<FloatType>::quiet_NaN());
93  }

◆ One()

static const LatticeWeightTpl One ( )
inlinestatic

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

81  {
82  return LatticeWeightTpl(0.0, 0.0);
83  }

◆ operator=()

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  }

◆ Properties()

static constexpr uint64 Properties ( )
inlinestatic

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

122  {
123  return kLeftSemiring | kRightSemiring | kCommutative |
124  kPath | kIdempotent;
125  }

◆ Quantize()

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_ == -std::numeric_limits<T>::infinity()) {
113  return LatticeWeightTpl(-std::numeric_limits<T>::infinity(), -std::numeric_limits<T>::infinity());
114  } else if (value1_ + value2_ == std::numeric_limits<T>::infinity()) {
115  return LatticeWeightTpl(std::numeric_limits<T>::infinity(), std::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  }

◆ Read()

std::istream& Read ( std::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  }

◆ ReadFloatType()

static void ReadFloatType ( std::istream &  strm,
T f 
)
inlinestaticprotected

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

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

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

◆ ReadNoParen()

std::istream& ReadNoParen ( std::istream &  strm,
char  separator 
)
inlineprotected

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

Referenced by fst::operator>>().

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

◆ Reverse()

LatticeWeightTpl<FloatType> Reverse ( ) const
inline

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

Referenced by fst::LatticeWeightTest().

72  {
73  return *this;
74  }

◆ SetValue1()

◆ SetValue2()

◆ Type()

static const std::string& Type ( )
inlinestatic

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

85  {
86  static const std::string type = (sizeof(T) == 4 ? "lattice4" : "lattice8") ;
87  return type;
88  }

◆ Value1()

◆ Value2()

◆ Write()

std::ostream& Write ( std::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  }

◆ WriteFloatType()

static void WriteFloatType ( std::ostream &  strm,
const T f 
)
inlinestaticprotected

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

Referenced by fst::operator<<().

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

◆ Zero()

static const LatticeWeightTpl Zero ( )
inlinestatic

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

Referenced by fst::Divide().

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

Friends And Related Function Documentation

◆ operator

std::ostream& operator ( std::ostream &  ,
const LatticeWeightTpl< FloatType > &   
)
friend

◆ operator>>

std::istream& operator>> ( std::istream &  strm,
LatticeWeightTpl< FloatType > &  w 
)
friend

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

407  {
408  CHECK(FLAGS_fst_weight_separator.size() == 1);
409  // separator defaults to ','
410  return w1.ReadNoParen(strm, FLAGS_fst_weight_separator[0]);
411 }

Member Data Documentation

◆ value1_

◆ value2_


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