Skip to content

Commit

Permalink
table: assign entry indexes instead of using array index
Browse files Browse the repository at this point in the history
added static index to entry pointer vector
- renamed alternate vector alias to entry vector (used for this vector)
- also renamed alternate vector array to entry vector array
- added set index and add entry function (added call from erector)
- changed entry from index access function to use vector and made inline
added index instance member (and made inline)
- changed access function to return member (instead of calculating)
removed comments from instance members (except function pointers)
  • Loading branch information
thunder422 committed Jan 25, 2015
1 parent 0314085 commit 9d74aac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions erector.h
Expand Up @@ -60,6 +60,7 @@ class Erector
inline void Erector::operator()()
try
{
m_entry->setIndexAndAddEntry();
m_entry->addToCodeMap();
if (not addTwoWordCommandToNameMap()
&& m_entry->hasName() && not addNewPrimaryToNameMap()
Expand Down
13 changes: 1 addition & 12 deletions table.cpp
Expand Up @@ -85,6 +85,7 @@ static ExprInfo None_Int_ExprInfo(DataType::None, Operands_Int);
static ExprInfo None_Str_ExprInfo(DataType::None, Operands_Str);


Table::EntryVector Table::s_indexToEntry;
Table::NameMap Table::s_nameToEntry;
Table::CodeMap Table::s_codeToEntry;
Table::AlternateMap Table::s_alternate;
Expand Down Expand Up @@ -1514,12 +1515,6 @@ Table::Table(Code code, const std::string name, const std::string name2,
}


int Table::index() const
{
return this - tableEntries;
}


std::string Table::commandName() const
{
std::string string {m_name};
Expand Down Expand Up @@ -1618,12 +1613,6 @@ Table *Table::entry(Code code, DataType dataType)
}


Table *Table::entry(int index)
{
return &tableEntries[index];
}


Table *Table::find(const std::string &string)
{
auto iterator = s_nameToEntry.find(string);
Expand Down
41 changes: 28 additions & 13 deletions table.h
Expand Up @@ -107,7 +107,6 @@ class Table
Table(Table &&) = delete;

// INSTANCE ACCESS FUNCTIONS
int index() const;
Code code() const
{
return m_code;
Expand Down Expand Up @@ -158,6 +157,10 @@ class Table
{
return m_precedence;
}
int index() const
{
return m_index;
}
DataType returnDataType() const
{
return m_exprInfo->m_returnDataType;
Expand Down Expand Up @@ -222,7 +225,10 @@ class Table
// STATIC ACCESS FUNCTIONS
static Table *entry(Code code);
static Table *entry(Code code, DataType dataType);
static Table *entry(int index);
static Table *entry(int index)
{
return s_indexToEntry[index];
}
static Table *find(const std::string &string);
static Table *find(const std::string &word1, const std::string &word2)
{
Expand Down Expand Up @@ -290,20 +296,29 @@ class Table
}

// INSTANCE MEMBERS
Code m_code; // code type of table entry
const std::string m_name; // name for table entry
const std::string m_name2; // name of second word of command
const std::string m_option; // name of option sub-code
unsigned m_flags; // flags for entry
int m_precedence; // precedence of code
ExprInfo *m_exprInfo; // expression info pointer (NULL for none)
Code m_code;
const std::string m_name;
const std::string m_name2;
const std::string m_option;
unsigned m_flags;
int m_precedence;
u_int16_t m_index;
ExprInfo *m_exprInfo;

TranslateFunction translate; // pointer to translate function
EncodeFunction encode; // pointer to encode function
OperandTextFunction operandText;// pointer to operand text function
RemoveFunction remove; // pointer to remove function
RecreateFunction recreate; // pointer to recreate function

// STATIC ACCESS FUNCTIONS
using EntryVector = std::vector<Table *>;
void setIndexAndAddEntry()
{
m_index = s_indexToEntry.size();
s_indexToEntry.push_back(this);
}

using NameMap = std::unordered_map<std::string, Table *,
CaseOptionalHash, CaseOptionalEqual>;
void addCommandNameToNameMap()
Expand Down Expand Up @@ -336,14 +351,13 @@ class Table
}
}

using AlternateVector = std::vector<Table *>;
using AlternateVectorArray = std::array<AlternateVector, 3>;
using AlternateMap = std::unordered_map<Table *, AlternateVectorArray>;
using EntryVectorArray = std::array<EntryVector, 3>;
using AlternateMap = std::unordered_map<Table *, EntryVectorArray>;
void appendAlternate(int operand, Table *alternate)
{
s_alternate[this][operand].push_back(alternate);
}
AlternateVector &alternateVector(int operand)
EntryVector &alternateVector(int operand)
{
return s_alternate[this][operand];
}
Expand Down Expand Up @@ -380,6 +394,7 @@ class Table
}

// STATIC MEMBERS
static EntryVector s_indexToEntry;
static NameMap s_nameToEntry;
static CodeMap s_codeToEntry;
static AlternateMap s_alternate;
Expand Down

0 comments on commit 9d74aac

Please sign in to comment.