33 #include <type_traits> 133 inline constexpr
float kDelta = 1.0F / 1024.0F;
150 inline constexpr uint64_t
kPath = 0x0000000000000010ULL;
159 using IsIdempotent = std::bool_constant<(W::Properties() & kIdempotent) != 0>;
162 using IsPath = std::bool_constant<(W::Properties() & kPath) != 0>;
198 return w1 != w2 &&
Plus(w1, w2) == w1;
204 template <
class Weight>
206 auto result = Weight::One();
207 for (
size_t i = 0; i < n; ++i) result =
Times(result, weight);
212 template <
class Weight>
220 sum_ =
Plus(sum_, w);
233 template <
class W1,
class W2>
236 FSTERROR() <<
"WeightConvert: Can't convert weight from " << W1::Type()
237 <<
" to " << W2::Type();
238 return W2::NoWeight();
267 FSTERROR() <<
"WeightGenerate: No random generator for " << W::Type();
268 return W::NoWeight();
280 return {open_paren_, close_paren_};
284 bool error()
const {
return error_; }
307 std::pair<char, char> parentheses);
318 if (i_++ > 0) ostrm_ << separator_;
326 std::ostream &ostrm_;
343 std::pair<char, char> parentheses);
355 bool ReadElement(T *comp,
bool last =
false);
361 std::istream &istrm_;
369 const bool has_parens = open_paren_ != 0;
370 while ((c_ != std::istream::traits_type::eof()) && !std::isspace(c_) &&
371 (c_ != separator_ || depth_ > 1 || last) &&
372 (c_ != close_paren_ || depth_ != 1)) {
375 if (has_parens && c_ == open_paren_) {
377 }
else if (has_parens && c_ == close_paren_) {
380 FSTERROR() <<
"CompositeWeightReader: Unmatched close paren: " 381 <<
"Is the fst_weight_parentheses flag set correctly?";
382 istrm_.clear(std::ios::badbit);
390 FSTERROR() <<
"CompositeWeightReader: Empty element: " 391 <<
"Is the fst_weight_parentheses flag set correctly?";
392 istrm_.clear(std::ios::badbit);
395 std::istringstream istrm(s);
398 if (c_ != std::istream::traits_type::eof() && !std::isspace(c_)) {
401 const bool is_eof = c_ == std::istream::traits_type::eof();
403 if (is_eof && !istrm_.bad()) istrm_.clear(std::ios::eofbit);
404 return !is_eof && !std::isspace(c_);
409 #endif // FST_WEIGHT_H_
constexpr uint64_t kSemiring
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
constexpr uint64_t kIdempotent
std::bool_constant<(W::Properties()&kIdempotent)!=0 > IsIdempotent
constexpr W operator()(W weight) const
constexpr uint64_t kRightSemiring
Weight Add(const Weight &w)
constexpr uint64_t kCommutative
bool operator()(const Weight &w1, const Weight &w2) const
DECLARE_string(fst_weight_parentheses)
bool ReadElement(T *comp, bool last=false)
std::pair< char, char > parentheses() const
constexpr size_t kNumRandomWeights
W2 operator()(W1 w1) const
void WriteElement(const T &comp)
void Reset(Weight w=Weight::Zero())
constexpr TropicalWeightTpl< T > Power(const TropicalWeightTpl< T > &w, V n)
constexpr uint64_t kLeftSemiring
std::bool_constant<(W::Properties()&kPath)!=0 > IsPath