27 bool *right_relabel_output) {
31 std::unique_ptr<SymbolTable> merged(
34 bool left_has_all =
true;
35 bool right_has_all =
true;
37 for (
const auto &litem : left) {
38 merged->AddSymbol(litem.Symbol(), litem.Label());
40 int64_t key = right.
Find(litem.Symbol());
42 right_has_all =
false;
43 }
else if (!relabel && key != litem.Label()) {
49 if (right_relabel_output) *right_relabel_output = relabel;
53 std::vector<std::string> conflicts;
54 for (
const auto &ritem : right) {
55 int64_t key = merged->
Find(ritem.Symbol());
58 if (key != ritem.Label()) relabel =
true;
63 if (!merged->Find(ritem.Label()).empty()) {
65 conflicts.push_back(ritem.Symbol());
69 merged->AddSymbol(ritem.Symbol(), ritem.Label());
71 if (right_relabel_output) *right_relabel_output = relabel;
72 if (left_has_all)
return left.Copy();
74 for (
const auto &conflict : conflicts) merged->AddSymbol(conflict);
75 return merged.release();
79 std::map<int64_t, std::string> sorted;
80 for (
const auto &stitem : syms) {
81 sorted[stitem.Label()] = stitem.Symbol();
83 auto *compact =
new SymbolTable(syms.Name() +
"_compact");
85 for (
const auto &kv : sorted) compact->
AddSymbol(kv.second, newkey++);
90 std::ifstream in(source, std::ios_base::in | std::ios_base::binary);
92 LOG(ERROR) <<
"FstReadSymbols: Can't open file " << source;
96 if (!hdr.
Read(in, source)) {
97 LOG(ERROR) <<
"FstReadSymbols: Couldn't read header from " << source;
102 if (isymbols ==
nullptr) {
103 LOG(ERROR) <<
"FstReadSymbols: Couldn't read input symbols from " 107 if (input_symbols)
return isymbols.release();
111 if (osymbols ==
nullptr) {
112 LOG(ERROR) <<
"FstReadSymbols: Couldn't read output symbols from " 116 if (!input_symbols)
return osymbols.release();
118 LOG(ERROR) <<
"FstReadSymbols: The file " << source
119 <<
" doesn't contain the requested symbols";
125 for (int64_t i = 0; i < nlabels; ++i) {
126 auto index = i + start_label;
127 if (index != syms->
AddSymbol(prefix + std::to_string(i), index)) {
128 FSTERROR() <<
"AddAuxiliarySymbols: Symbol table clash";
const std::string & Name() const
int64_t AddSymbol(std::string_view symbol, int64_t key)
virtual SymbolTable * Copy() const
constexpr int64_t kNoSymbol
SymbolTable * CompactSymbolTable(const SymbolTable &syms)
static SymbolTable * Read(std::istream &strm, const std::string &source)
SymbolTable * FstReadSymbols(const std::string &source, bool input)
std::string Find(int64_t key) const
SymbolTable * MergeSymbolTable(const SymbolTable &left, const SymbolTable &right, bool *right_relabel_output=nullptr)
bool AddAuxiliarySymbols(const std::string &prefix, int64_t start_label, int64_t nlabels, SymbolTable *syms)