Skip to content

Commit

Permalink
Tue Nov 29 09:08:28 AEDT 2016
Browse files Browse the repository at this point in the history
  • Loading branch information
lyndonhenry committed Nov 28, 2016
1 parent d9eec18 commit bac0979
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion doxygen.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ INCLUDED_BY_GRAPH = YES
# So in most cases it will be better to enable call graphs for selected
# functions only using the \callgraph command.

CALL_GRAPH = NO
CALL_GRAPH = YES

# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen
# will graphical hierarchy of all classes instead of a textual one.
Expand Down
120 changes: 60 additions & 60 deletions src/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#pragma once

#include <map>
#include <unordered_map>
#include <functional>
#include <vector>
#include <set>
#include <string>
Expand All @@ -38,95 +39,93 @@ namespace souffle {
*/
class SymbolTable {

/** String pointer comparison class for SymbolTable */
struct StringCmp {
bool operator()(const char* lhs, const char* rhs) const {
return strcmp(lhs, rhs) < 0;
}
};
/** A lock to synchronize parallel accesses */
mutable Lock access;

private:

/** Map integer to string */
/** Map integer to string */
std::vector<char *> numToStr;

/** Map strings kept in the pool to numbers */
std::map<const char *, size_t, StringCmp> strToNum;
std::unordered_map<std::string, size_t> strToNum;

/** A lock to synchronize parallel accesses */
mutable Lock access;
inline void copyAll() {
for(auto& cur : numToStr) cur = strdup(cur);
for(auto& cur : strToNum) const_cast<std::string&>(cur.first) = numToStr[cur.second];
}

inline void freeAll() {
for(auto cur : numToStr) free(cur);
}

inline size_t newSymbol(const char* symbol) {
size_t idx;
auto it = strToNum.find(symbol);
if (it == strToNum.end()) {
char *str = strdup(symbol);
idx = numToStr.size();
strToNum[str] = idx;
numToStr.push_back(str);
} else {
idx = it->second;
}
return idx;
}

public:

/** Private constructor */
SymbolTable() { }

SymbolTable(const SymbolTable& other)
: numToStr(other.numToStr), strToNum(other.strToNum) {
// clone all contained strings
for(auto& cur : numToStr) cur = strdup(cur);
for(auto& cur : strToNum) const_cast<char*&>(cur.first) = numToStr[cur.second];
: numToStr(other.numToStr)
, strToNum(other.strToNum) {
copyAll();
}

SymbolTable(SymbolTable&& other) {
numToStr.swap(other.numToStr);
strToNum.swap(other.strToNum);
}

/** Destructor cleaning up strings */
~SymbolTable() {
for(auto cur : numToStr) free(cur);
freeAll();
}

/** Add support for an assignment operator */
SymbolTable& operator=(const SymbolTable& other) {
// shortcut
if (this == &other) return *this;

// delete this content
for(auto cur : numToStr) free(cur);

// copy in other content
freeAll();
numToStr = other.numToStr;
strToNum = other.strToNum;
for(auto& cur : numToStr) cur = strdup(cur);
for(auto& cur : strToNum) const_cast<char*&>(cur.first) = numToStr[cur.second];

// done
copyAll();
return *this;
}

/** Add support for assignments from r-value references */
SymbolTable& operator=(SymbolTable&& other) {
// steal content of other
numToStr.swap(other.numToStr);
strToNum.swap(other.strToNum);
return *this;
}

/** Look-up a string given by a pointer to @p std::string in the pool and convert it to an index */
size_t lookup(const char *p) {
size_t result;
{
auto lease = access.acquire();
(void) lease; // avoid warning;

auto it = strToNum.find(p);
if (it != strToNum.end()) {
result = (*it).second;
} else {
result = numToStr.size();
char *str = strdup(p); // generate a new string
strToNum[str] = result;
numToStr.push_back(str);
}
}
return result;
size_t lookup(const char *symbol) {
auto lease = access.acquire();
(void) lease; // avoid warning;
return newSymbol(symbol);
}

/** Lookup an index and convert it to a string */
const char* resolve(size_t i) const {
const char* resolve(size_t idx) const {
auto lease = access.acquire();
(void) lease; // avoid warning;
return numToStr[i];
if (idx >= size()) {
// TODO: use different error reporting here!!
std::cerr << "Error: Symbol table index out of bounds.";
exit(1);
}
return numToStr[idx];
}

/* return size */
Expand All @@ -138,22 +137,24 @@ class SymbolTable {
void insert(const char **symbols, size_t n) {
auto lease = access.acquire();
(void) lease; // avoid warning;
for(size_t idx=0; idx < n; idx++) {
const char *p = symbols[idx];
char *str = strdup(p);
strToNum[str] = numToStr.size();
numToStr.push_back(str);
strToNum.reserve(size() + n);
numToStr.reserve(size() + n);
for (size_t idx = 0; idx < n; idx++) {
const char *symbol = symbols[idx];
newSymbol(symbol);
}
}

/** inserts a single symbol into this table */
void insert(const char* symbol) {
insert(&symbol, 1);
auto lease = access.acquire();
(void) lease; // avoid warning;
newSymbol(symbol);
}

void print(std::ostream& out) const {
out << "SymbolTable: {\n\t";
out << join(strToNum, "\n\t", [](std::ostream& out, const std::pair<const char*,std::size_t>& entry) {
out << join(strToNum, "\n\t", [](std::ostream& out, const std::pair<std::string, std::size_t>& entry) {
out << entry.first << "\t => " << entry.second;
}) << "\n";
out << "}\n";
Expand All @@ -164,11 +165,10 @@ class SymbolTable {
return out;
}

template<class Function>
Function map(Function fn) const {
for (size_t i = 0; i < numToStr.size(); ++i)
fn(i, numToStr[i]);
return std::move(fn);
const std::vector<char*> & rawSymbols() const {
return numToStr;
}

};

}

0 comments on commit bac0979

Please sign in to comment.