souffle
2.0.2-371-g6315b36
|
Go to the documentation of this file.
26 #include <initializer_list>
29 #include <unordered_map>
51 std::unordered_map<std::string, size_t>
strToNum;
69 inline void newSymbol(
const std::string& symbol) {
89 SymbolTable(std::initializer_list<std::string> symbols) {
91 for (
const auto& symbol : symbols) {
101 if (
this == &other) {
131 auto result =
strToNum.find(symbol);
133 fatal(
"Error string not found in call to `SymbolTable::lookupExisting`: `%s`", symbol);
135 return static_cast<RamDomain>(result->second);
152 auto pos =
static_cast<size_t>(index);
155 fatal(
"Error index out of bounds in call to `SymbolTable::resolve`. index = `%d`", index);
162 return numToStr[
static_cast<size_t>(index)];
166 size_t size()
const {
173 void insert(
const std::vector<std::string>& symbols) {
178 for (
auto& symbol : symbols) {
187 void insert(
const std::string& symbol) {
196 void print(std::ostream& out)
const {
198 out <<
"SymbolTable: {\n\t";
200 [](std::ostream& out,
const std::pair<std::string, std::size_t>& entry) {
201 out << entry.first <<
"\t => " << entry.second;
209 bool contains(
const std::string& symbol)
const {
212 auto result =
strToNum.find(symbol);
224 auto pos =
static_cast<size_t>(index);
void insert(const std::vector< std::string > &symbols)
Bulk insert symbols into the table, note that this operation is more efficient than repeated inserts ...
const std::string & unsafeResolve(const RamDomain index) const
const std::string & resolve(const RamDomain index) const
Find a symbol in the table by its index, note that this gives an error if the index is out of bounds.
virtual ~SymbolTable()=default
Destructor, frees memory allocated for all strings.
void newSymbol(const std::string &symbol)
Convenience method to place a new symbol in the table, if it does not exist.
bool contains(const std::string &symbol) const
Check if the symbol table contains a string.
std::deque< std::string > numToStr
Map indices to strings.
Lock::Lease acquireLock() const
SymbolTable & operator=(const SymbolTable &other)
Assignment operator, performs a deep copy and frees memory allocated for all strings.
std::unordered_map< std::string, size_t > strToNum
Map strings to indices.
RamDomain lookup(const std::string &symbol)
Find the index of a symbol in the table, inserting a new symbol if it does not exist there already.
RamDomain lookupExisting(const std::string &symbol) const
Finds the index of a symbol in the table, giving an error if it's not found.
void print(std::ostream &out) const
Print the symbol table to the given stream.
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
SymbolTable()=default
Empty constructor.
friend std::ostream & operator<<(std::ostream &out, const SymbolTable &table)
Stream operator, used as a convenience for print.
Lock access
A lock to synchronize parallel accesses.
void fatal(const char *format, const Args &... args)
RamDomain unsafeLookup(const std::string &symbol)
Find the index of a symbol in the table, inserting a new symbol if it does not exist there already.
size_t newSymbolOfIndex(const std::string &symbol)
Convenience method to place a new symbol in the table, if it does not exist, and return the index of ...