20 #ifndef FST_EXTENSIONS_FAR_MAP_REDUCE_H_ 21 #define FST_EXTENSIONS_FAR_MAP_REDUCE_H_ 29 #include <string_view> 48 template <
class Arc,
class Functor>
50 for (; !reader.
Done(); reader.
Next()) {
51 const auto &key = reader.
GetKey();
52 const auto fst = functor(key, reader.
GetFst());
70 template <
class Arc,
class Functor>
72 Functor functor, std::string_view begin_key =
"",
73 std::string_view end_key =
"") {
74 if (!begin_key.empty()) {
75 const bool find_begin1 = reader1.
Find(begin_key);
76 const bool find_begin2 = reader2.
Find(begin_key);
77 if (!find_begin1 || !find_begin2) {
78 const bool ret = !find_begin1 && !find_begin2;
80 LOG(ERROR) <<
"MapAllReduce: Key " << begin_key <<
" missing from " 81 << (find_begin1 ?
"second" :
"first") <<
" FAR";
87 const auto &key1 = reader1.
GetKey();
88 const auto &key2 = reader2.
GetKey();
89 if (!end_key.empty() && end_key < key1 && end_key < key2) {
93 LOG(ERROR) <<
"MapAllReduce: Mismatched keys " << key1 <<
" and " << key2;
96 if (!functor(key1, reader1.
GetFst(), reader2.
GetFst()))
return false;
98 if (reader1.
Done() && !reader2.
Done()) {
99 LOG(ERROR) <<
"MapAllReduce: Key " << reader2.
GetKey()
100 <<
" missing from first FAR";
102 }
else if (reader2.
Done() && !reader1.
Done()) {
103 LOG(ERROR) <<
"MapAllReduce: Key " << reader1.
GetKey()
104 <<
" missing from second FAR";
113 #endif // FST_EXTENSIONS_FAR_MAP_REDUCE_H_ virtual const std::string & GetKey() const =0
bool MapAllReduce(FarReader< Arc > &reader1, FarReader< Arc > &reader2, Functor functor, std::string_view begin_key="", std::string_view end_key="")
virtual bool Done() const =0
virtual const Fst< Arc > * GetFst() const =0
void Map(FarReader< Arc > &reader, FarWriter< Arc > &writer, Functor functor)
virtual void Add(std::string_view key, const Fst< Arc > &fst)=0
virtual bool Find(std::string_view key)=0