souffle  2.0.2-371-g6315b36
Public Member Functions | Private Member Functions | Private Attributes | Friends
souffle::SymbolTable Class Reference

#include <SymbolTable.h>

Collaboration diagram for souffle::SymbolTable:
Collaboration graph

Public Member Functions

Lock::Lease acquireLock () const
 
bool contains (const RamDomain index) const
 Check if the symbol table contains an index. More...
 
bool contains (const std::string &symbol) const
 Check if the symbol table contains a string. More...
 
void insert (const std::string &symbol)
 Insert a single symbol into the table, not that this operation should not be used if inserting symbols in bulk. More...
 
void insert (const std::vector< std::string > &symbols)
 Bulk insert symbols into the table, note that this operation is more efficient than repeated inserts of single symbols. More...
 
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. More...
 
RamDomain lookupExisting (const std::string &symbol) const
 Finds the index of a symbol in the table, giving an error if it's not found. More...
 
SymbolTableoperator= (const SymbolTable &other)
 Assignment operator, performs a deep copy and frees memory allocated for all strings. More...
 
SymbolTableoperator= (SymbolTable &&other) noexcept
 Assignment operator for r-value references. More...
 
void print (std::ostream &out) const
 Print the symbol table to the given stream. More...
 
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. More...
 
size_t size () const
 
 SymbolTable ()=default
 Empty constructor. More...
 
 SymbolTable (const SymbolTable &other)
 Copy constructor, performs a deep copy. More...
 
 SymbolTable (std::initializer_list< std::string > symbols)
 
 SymbolTable (SymbolTable &&other) noexcept
 Copy constructor for r-value reference. More...
 
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. More...
 
const std::string & unsafeResolve (const RamDomain index) const
 
virtual ~SymbolTable ()=default
 Destructor, frees memory allocated for all strings. More...
 

Private Member Functions

void newSymbol (const std::string &symbol)
 Convenience method to place a new symbol in the table, if it does not exist. More...
 
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 it. More...
 

Private Attributes

Lock access
 A lock to synchronize parallel accesses. More...
 
std::deque< std::string > numToStr
 Map indices to strings. More...
 
std::unordered_map< std::string, size_t > strToNum
 Map strings to indices. More...
 

Friends

std::ostream & operator<< (std::ostream &out, const SymbolTable &table)
 Stream operator, used as a convenience for print. More...
 

Detailed Description

Global pool of re-usable strings

SymbolTable stores Datalog symbols and converts them to numbers and vice versa.

Definition at line 48 of file SymbolTable.h.

Constructor & Destructor Documentation

◆ SymbolTable() [1/4]

souffle::SymbolTable::SymbolTable ( )
default

Empty constructor.

◆ SymbolTable() [2/4]

souffle::SymbolTable::SymbolTable ( const SymbolTable other)
inline

Copy constructor, performs a deep copy.

Definition at line 87 of file SymbolTable.h.

◆ SymbolTable() [3/4]

souffle::SymbolTable::SymbolTable ( SymbolTable &&  other)
inlinenoexcept

Copy constructor for r-value reference.

Definition at line 90 of file SymbolTable.h.

91  : symbols) {
92  newSymbol(symbol);
93  }

References newSymbol().

Here is the call graph for this function:

◆ SymbolTable() [4/4]

souffle::SymbolTable::SymbolTable ( std::initializer_list< std::string >  symbols)
inline

Definition at line 95 of file SymbolTable.h.

100  {

References numToStr, and strToNum.

◆ ~SymbolTable()

virtual souffle::SymbolTable::~SymbolTable ( )
virtualdefault

Destructor, frees memory allocated for all strings.

Member Function Documentation

◆ acquireLock()

Lock::Lease souffle::SymbolTable::acquireLock ( ) const
inline

Definition at line 238 of file SymbolTable.h.

◆ contains() [1/2]

bool souffle::SymbolTable::contains ( const RamDomain  index) const
inline

Check if the symbol table contains an index.

Definition at line 227 of file SymbolTable.h.

227  {
228  return true;
229  }
230  }
231 
232  Lock::Lease acquireLock() const {
233  return access.acquire();
234  }
235 
236  /** Stream operator, used as a convenience for print. */

◆ contains() [2/2]

bool souffle::SymbolTable::contains ( const std::string &  symbol) const
inline

Check if the symbol table contains a string.

Definition at line 215 of file SymbolTable.h.

215  {
216  return true;
217  }
218  }
219 
220  /** Check if the symbol table contains an index */
221  bool contains(const RamDomain index) const {
222  auto lease = access.acquire();
223  (void)lease; // avoid warning;
224  auto pos = static_cast<size_t>(index);

◆ insert() [1/2]

void souffle::SymbolTable::insert ( const std::string &  symbol)
inline

Insert a single symbol into the table, not that this operation should not be used if inserting symbols in bulk.

Definition at line 193 of file SymbolTable.h.

196  {
197  {
198  out << "SymbolTable: {\n\t";
199  out << join(strToNum, "\n\t",

◆ insert() [2/2]

void souffle::SymbolTable::insert ( const std::vector< std::string > &  symbols)
inline

Bulk insert symbols into the table, note that this operation is more efficient than repeated inserts of single symbols.

Definition at line 179 of file SymbolTable.h.

187  {
188  {

◆ lookup()

RamDomain souffle::SymbolTable::lookup ( const std::string &  symbol)
inline

Find the index of a symbol in the table, inserting a new symbol if it does not exist there already.

Definition at line 124 of file SymbolTable.h.

127  {
128  {
129  auto lease = access.acquire();
130  (void)lease; // avoid warning;

Referenced by souffle::tuple::rewind(), and souffle::interpreter::test::TEST().

◆ lookupExisting()

RamDomain souffle::SymbolTable::lookupExisting ( const std::string &  symbol) const
inline

Finds the index of a symbol in the table, giving an error if it's not found.

Definition at line 133 of file SymbolTable.h.

133  : `%s`", symbol);
134  }
135  return static_cast<RamDomain>(result->second);
136  }
137  }
138 
139  /** Find the index of a symbol in the table, inserting a new symbol if it does not exist there
140  * already. */
141  RamDomain unsafeLookup(const std::string& symbol) {
142  return static_cast<RamDomain>(newSymbolOfIndex(symbol));
143  }

◆ newSymbol()

void souffle::SymbolTable::newSymbol ( const std::string &  symbol)
inlineprivate

Convenience method to place a new symbol in the table, if it does not exist.

Definition at line 75 of file SymbolTable.h.

76  :
77  /** Empty constructor. */
78  SymbolTable() = default;
79 
80  /** Copy constructor, performs a deep copy. */

Referenced by SymbolTable().

◆ newSymbolOfIndex()

size_t souffle::SymbolTable::newSymbolOfIndex ( const std::string &  symbol)
inlineprivate

Convenience method to place a new symbol in the table, if it does not exist, and return the index of it.

Definition at line 61 of file SymbolTable.h.

62  {
63  index = it->second;
64  }
65  return index;
66  }
67 
68  /** Convenience method to place a new symbol in the table, if it does not exist. */
69  inline void newSymbol(const std::string& symbol) {
70  if (strToNum.find(symbol) == strToNum.end()) {
71  strToNum[symbol] = numToStr.size();
72  numToStr.push_back(symbol);

Referenced by operator=().

◆ operator=() [1/2]

SymbolTable& souffle::SymbolTable::operator= ( const SymbolTable other)
inline

Assignment operator, performs a deep copy and frees memory allocated for all strings.

Definition at line 106 of file SymbolTable.h.

110  {
111  numToStr.swap(other.numToStr);
112  strToNum.swap(other.strToNum);
113  return *this;

◆ operator=() [2/2]

SymbolTable& souffle::SymbolTable::operator= ( SymbolTable &&  other)
inlinenoexcept

Assignment operator for r-value references.

Definition at line 116 of file SymbolTable.h.

118  {
119  {
120  auto lease = access.acquire();

References access, souffle::Lock::acquire(), and newSymbolOfIndex().

Here is the call graph for this function:

◆ print()

void souffle::SymbolTable::print ( std::ostream &  out) const
inline

Print the symbol table to the given stream.

Definition at line 202 of file SymbolTable.h.

209  {
210  auto lease = access.acquire();
211  (void)lease; // avoid warning;
212  auto result = strToNum.find(symbol);

◆ resolve()

const std::string& souffle::SymbolTable::resolve ( const RamDomain  index) const
inline

Find a symbol in the table by its index, note that this gives an error if the index is out of bounds.

Definition at line 154 of file SymbolTable.h.

161  {
162  return numToStr[static_cast<size_t>(index)];
163  }
164 
165  /* Return the size of the symbol table, being the number of symbols it currently holds. */

Referenced by souffle::interpreter::RelInterface::iterator_base::operator++(), and souffle::tuple::operator<<().

◆ size()

size_t souffle::SymbolTable::size ( ) const
inline

Definition at line 172 of file SymbolTable.h.

173  {
174  {

References access, souffle::Lock::acquire(), and strToNum.

Referenced by unsafeLookup().

Here is the call graph for this function:

◆ unsafeLookup()

RamDomain souffle::SymbolTable::unsafeLookup ( const std::string &  symbol)
inline

Find the index of a symbol in the table, inserting a new symbol if it does not exist there already.

Definition at line 147 of file SymbolTable.h.

148  {
149  {

References access, souffle::Lock::acquire(), and size().

Here is the call graph for this function:

◆ unsafeResolve()

const std::string& souffle::SymbolTable::unsafeResolve ( const RamDomain  index) const
inline

Definition at line 167 of file SymbolTable.h.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const SymbolTable table 
)
friend

Stream operator, used as a convenience for print.

Definition at line 243 of file SymbolTable.h.

Field Documentation

◆ access

Lock souffle::SymbolTable::access
mutableprivate

A lock to synchronize parallel accesses.

Definition at line 51 of file SymbolTable.h.

Referenced by operator=(), size(), and unsafeLookup().

◆ numToStr

std::deque<std::string> souffle::SymbolTable::numToStr
private

Map indices to strings.

Definition at line 54 of file SymbolTable.h.

Referenced by SymbolTable().

◆ strToNum

std::unordered_map<std::string, size_t> souffle::SymbolTable::strToNum
private

Map strings to indices.

Definition at line 57 of file SymbolTable.h.

Referenced by size(), and SymbolTable().


The documentation for this class was generated from the following file:
souffle::SymbolTable::newSymbol
void newSymbol(const std::string &symbol)
Convenience method to place a new symbol in the table, if it does not exist.
Definition: SymbolTable.h:75
souffle::RamDomain
int32_t RamDomain
Definition: RamTypes.h:56
souffle::SymbolTable::contains
bool contains(const std::string &symbol) const
Check if the symbol table contains a string.
Definition: SymbolTable.h:215
souffle::SymbolTable::numToStr
std::deque< std::string > numToStr
Map indices to strings.
Definition: SymbolTable.h:54
souffle::SymbolTable::acquireLock
Lock::Lease acquireLock() const
Definition: SymbolTable.h:238
souffle::SymbolTable::strToNum
std::unordered_map< std::string, size_t > strToNum
Map strings to indices.
Definition: SymbolTable.h:57
souffle::Lock::acquire
Lease acquire()
Definition: ParallelUtil.h:471
souffle::join
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...
Definition: StreamUtil.h:175
souffle::SymbolTable::SymbolTable
SymbolTable()=default
Empty constructor.
souffle::SymbolTable::access
Lock access
A lock to synchronize parallel accesses.
Definition: SymbolTable.h:51