37 #ifndef RERANKER_SYMBOL_TABLE_H_
38 #define RERANKER_SYMBOL_TABLE_H_
42 #include <unordered_map>
51 using std::unordered_map;
66 virtual size_t size()
const = 0;
73 virtual int GetIndex(
const string &symbol) = 0;
77 virtual const string &
GetSymbol(
int index)
const = 0;
79 virtual void SetIndex(
const string &symbol,
int index) = 0;
82 virtual void Clear() = 0;
104 virtual ostream &
Output(ostream &os) = 0;
126 virtual size_t size()
const {
return symbols_.size(); }
135 virtual int GetIndex(
const string &symbol);
139 virtual const string &
GetSymbol(
int index)
const;
141 virtual void SetIndex(
const string &symbol,
int index) {
142 unordered_map<string, int>::const_iterator it = symbols_.find(symbol);
143 if (it != symbols_.end()) {
146 symbols_.erase(symbol);
147 indices_to_symbols_.erase(index);
149 symbols_[symbol] = index;
150 indices_to_symbols_[index] = symbol;
156 indices_to_symbols_.clear();
166 for (unordered_map<string, int>::const_iterator it = symbols_.begin();
167 it != symbols_.end();
169 os << it->first <<
"\t" << it->second <<
"\n";
177 static unordered_map<string, int> symbols_;
178 static unordered_map<int, string> indices_to_symbols_;
191 virtual size_t size()
const {
return symbols_.size(); }
196 virtual int GetIndex(
const string &symbol);
199 virtual const string &
GetSymbol(
int index)
const;
201 virtual void SetIndex(
const string &symbol,
int index) {
202 unordered_map<string, int>::const_iterator it = symbols_.find(symbol);
203 if (it != symbols_.end()) {
204 cerr <<
"LocalSymbolTable::SetIndex: warning: symbol \"" << symbol
205 <<
"\" already has an index " << it->second <<
"; new index: "
208 symbols_.erase(symbol);
209 indices_to_symbols_.erase(index);
211 symbols_[symbol] = index;
212 indices_to_symbols_[index] = symbol;
218 indices_to_symbols_.clear();
228 for (unordered_map<string, int>::const_iterator it = symbols_.begin();
229 it != symbols_.end();
231 os << it->first <<
"\t" << it->second <<
"\n";
239 unordered_map<string, int> symbols_;
240 unordered_map<int, string> indices_to_symbols_;
virtual const_iterator begin()
virtual size_t size() const =0
Returns the number of symbols in this table.
virtual Symbols * Clone() const
Creates a newly-constructed clone of this Symbols instance that has the same runtime type...
virtual Symbols * Clone() const
Creates a newly-constructed clone of this Symbols instance that has the same runtime type...
virtual Symbols * Clone() const =0
Creates a newly-constructed clone of this Symbols instance that has the same runtime type...
virtual void SetIndex(const string &symbol, int index)
virtual int GetIndex(const string &symbol)
Returns the unique index for the specified symbol.
virtual void Clear()=0
Clears all symbols from this symbol table.
virtual ostream & Output(ostream &os)
Outputs the symbol table to the specified output stream in a simple format, one symbol-to-index mappi...
virtual const_iterator end()=0
virtual const_iterator end()
A symbol table that stores the mapping from symbols to int’s and vice versa in local (non-static) dat...
virtual void Clear()
Clears all symbols from this symbol table.
virtual const_iterator end()
virtual void SetIndex(const string &symbol, int index)
virtual const string & GetSymbol(int index) const =0
Returns the unique symbol for the specified index, or the empty string if the specified index does no...
virtual void Clear()
Clears all symbols from this symbol table.
A converter from symbols (strings) to int indices.
virtual const_iterator begin()
unordered_map< string, int >::const_iterator const_iterator
static string null_symbol
virtual int GetIndex(const string &symbol)=0
Converts the specified symbol to a unique integer.
virtual size_t size() const
Returns the number of symbols in this table.
virtual ostream & Output(ostream &os)=0
Outputs the symbol table to the specified output stream in a simple format, one symbol-to-index mappi...
An interface specifying a converter from symbols (strings) to int indices.
virtual ostream & Output(ostream &os)
Outputs the symbol table to the specified output stream in a simple format, one symbol-to-index mappi...
virtual const string & GetSymbol(int index) const
Returns the unique symbol for the specified index, or the empty string if the specified index does no...
virtual const_iterator begin()=0
virtual int GetIndex(const string &symbol)
Converts the specified symbol to a unique integer.
virtual void SetIndex(const string &symbol, int index)=0
virtual const string & GetSymbol(int index) const
Returns the unique symbol for the specified index, or the empty string if the specified index does no...
virtual size_t size() const
Returns the number of symbols in this table.