28 #include <type_traits> 130 inline constexpr
float kDelta = 1.0F / 1024.0F;
147 inline constexpr uint64_t
kPath = 0x0000000000000010ULL;
156 using IsIdempotent = std::bool_constant<(W::Properties() & kIdempotent) != 0>;
159 using IsPath = std::bool_constant<(W::Properties() & kPath) != 0>;
197 return w1 != w2 &&
Plus(w1, w2) == w1;
203 template <
class Weight>
205 auto result = Weight::One();
206 for (
size_t i = 0; i < n; ++i) result =
Times(result, weight);
211 template <
class Weight>
219 sum_ =
Plus(sum_, w);
232 template <
class W1,
class W2>
235 FSTERROR() <<
"WeightConvert: Can't convert weight from " << W1::Type()
236 <<
" to " << W2::Type();
237 return W2::NoWeight();
266 FSTERROR() <<
"WeightGenerate: No random generator for " << W::Type();
267 return W::NoWeight();
279 return {open_paren_, close_paren_};
283 bool error()
const {
return error_; }
306 std::pair<char, char> parentheses);
317 if (i_++ > 0) ostrm_ << separator_;
325 std::ostream &ostrm_;
342 std::pair<char, char> parentheses);
354 bool ReadElement(T *comp,
bool last =
false);
360 std::istream &istrm_;
368 const bool has_parens = open_paren_ != 0;
369 while ((c_ != std::istream::traits_type::eof()) && !std::isspace(c_) &&
370 (c_ != separator_ || depth_ > 1 || last) &&
371 (c_ != close_paren_ || depth_ != 1)) {
374 if (has_parens && c_ == open_paren_) {
376 }
else if (has_parens && c_ == close_paren_) {
379 FSTERROR() <<
"CompositeWeightReader: Unmatched close paren: " 380 <<
"Is the fst_weight_parentheses flag set correctly?";
381 istrm_.clear(std::ios::badbit);
389 FSTERROR() <<
"CompositeWeightReader: Empty element: " 390 <<
"Is the fst_weight_parentheses flag set correctly?";
391 istrm_.clear(std::ios::badbit);
394 std::istringstream istrm(s);
397 if (c_ != std::istream::traits_type::eof() && !std::isspace(c_)) {
400 const bool is_eof = c_ == std::istream::traits_type::eof();
402 if (is_eof && !istrm_.bad()) istrm_.clear(std::ios::eofbit);
403 return !is_eof && !std::isspace(c_);
408 #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