20 #ifndef FST_EXTENSIONS_FAR_MAP_REDUCE_H_ 21 #define FST_EXTENSIONS_FAR_MAP_REDUCE_H_ 26 #include <string_view> 45 template <
class Arc,
class Functor>
47 for (; !reader.
Done(); reader.
Next()) {
48 const auto &key = reader.
GetKey();
49 const auto fst = functor(key, reader.
GetFst());
67 template <
class Arc,
class Functor>
69 Functor functor, std::string_view begin_key =
"",
70 std::string_view end_key =
"") {
71 if (!begin_key.empty()) {
72 const bool find_begin1 = reader1.
Find(begin_key);
73 const bool find_begin2 = reader2.
Find(begin_key);
74 if (!find_begin1 || !find_begin2) {
75 const bool ret = !find_begin1 && !find_begin2;
77 LOG(ERROR) <<
"MapAllReduce: Key " << begin_key <<
" missing from " 78 << (find_begin1 ?
"second" :
"first") <<
" FAR";
84 const auto &key1 = reader1.
GetKey();
85 const auto &key2 = reader2.
GetKey();
86 if (!end_key.empty() && end_key < key1 && end_key < key2) {
90 LOG(ERROR) <<
"MapAllReduce: Mismatched keys " << key1 <<
" and " << key2;
93 if (!functor(key1, reader1.
GetFst(), reader2.
GetFst()))
return false;
95 if (reader1.
Done() && !reader2.
Done()) {
96 LOG(ERROR) <<
"MapAllReduce: Key " << reader2.
GetKey()
97 <<
" missing from first FAR";
99 }
else if (reader2.
Done() && !reader1.
Done()) {
100 LOG(ERROR) <<
"MapAllReduce: Key " << reader1.
GetKey()
101 <<
" missing from second FAR";
110 #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