Skip to content

Commit

Permalink
dictionary: made class a proper base class
Browse files Browse the repository at this point in the history
changed clear, add, remove and string to 'virtual' functions
changed return value of add function from index to index/entry type pair
- removed optional return new entry pointer argument (now returned)
- updated encode function to get first or pair return value (index)
- also removed dereference operator on back inserter (not necessary)
changed return value of remove function from int to bool
- added return value to info dictionary (to match base class)
added 'override' to info dictionary clear, add and remove functions

made correction to dictionary clear function for gcc 4.9.2
  • Loading branch information
thunder422 committed Feb 12, 2015
1 parent 66a0db8 commit c3f9335
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
12 changes: 6 additions & 6 deletions basic/basic.cpp
Expand Up @@ -33,7 +33,7 @@
void remEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
*backInserter = programUnit->remDictionary()->add(token);
backInserter = programUnit->remDictionary()->add(token).first;
}

const std::string remOperandText(ProgramLineReader &programLineReader)
Expand Down Expand Up @@ -71,7 +71,7 @@ void ConstNumInfo::setElement(int index, Token *token)
void constNumEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
*backInserter = programUnit->constNumDictionary()->add(token);
backInserter = programUnit->constNumDictionary()->add(token).first;
}

const std::string constNumOperandText(ProgramLineReader &programLineReader)
Expand Down Expand Up @@ -118,7 +118,7 @@ ConstStrInfo::~ConstStrInfo(void)
void constStrEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
*backInserter = programUnit->constStrDictionary()->add(token);
backInserter = programUnit->constStrDictionary()->add(token).first;
}

const std::string constStrOperandText(ProgramLineReader &programLineReader)
Expand Down Expand Up @@ -154,19 +154,19 @@ void constStrRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
void varDblEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
*backInserter = programUnit->varDblDictionary()->add(token);
backInserter = programUnit->varDblDictionary()->add(token).first;
}

void varIntEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
*backInserter = programUnit->varIntDictionary()->add(token);
backInserter = programUnit->varIntDictionary()->add(token).first;
}

void varStrEncode(ProgramModel *programUnit,
ProgramCode::BackInserter backInserter, Token *token)
{
*backInserter = programUnit->varStrDictionary()->add(token);
backInserter = programUnit->varStrDictionary()->add(token).first;
}


Expand Down
29 changes: 13 additions & 16 deletions dictionary.cpp
Expand Up @@ -35,13 +35,13 @@

void Dictionary::clear(void)
{
m_freeStack = {};
m_freeStack = std::stack<uint16_t>{};
m_iterator.clear();
m_keyMap.clear();
}


uint16_t Dictionary::add(Token *token, Dictionary::EntryType *returnNewEntry)
std::pair<uint16_t, Dictionary::EntryType> Dictionary::add(Token *token)
{
EntryType newEntry;

Expand Down Expand Up @@ -72,15 +72,11 @@ uint16_t Dictionary::add(Token *token, Dictionary::EntryType *returnNewEntry)
iterator->second.m_useCount++;
newEntry = EntryType::Exists;
}
if (returnNewEntry)
{
*returnNewEntry = newEntry;
}
return index;
return std::make_pair(index, newEntry);
}


int Dictionary::remove(uint16_t index)
bool Dictionary::remove(uint16_t index)
{
auto iterator = m_iterator[index];
if (iterator != m_keyMap.end() && --iterator->second.m_useCount == 0)
Expand Down Expand Up @@ -110,34 +106,35 @@ void InfoDictionary::clear(void)


// function to possibly add a new dictionary entry and return its index
uint16_t InfoDictionary::add(Token *token)
std::pair<uint16_t, Dictionary::EntryType> InfoDictionary::add(Token *token)
{
EntryType returnNewEntry;
int index {Dictionary::add(token, &returnNewEntry)};
if (returnNewEntry == EntryType::New)
auto indexEntryType = Dictionary::add(token);
if (indexEntryType.second == EntryType::New)
{
// a new entry was added to the dictionary
// so add a new element to the additional information
m_info->addElement(token);
}
else if (returnNewEntry == EntryType::Reused)
else if (indexEntryType.second == EntryType::Reused)
{
// for a new entry or a reused entry,
// set the additional information from the token
m_info->setElement(index, token);
m_info->setElement(indexEntryType.first, token);
}
return index;
return indexEntryType;
}


// function to remove an entry from the dictionary
void InfoDictionary::remove(uint16_t index)
bool InfoDictionary::remove(uint16_t index)
{
if (Dictionary::remove(index))
{
// clear the additional information if the entry was removed
m_info->clearElement(index);
return true;
}
return false;
}


Expand Down
14 changes: 7 additions & 7 deletions dictionary.h
Expand Up @@ -50,10 +50,10 @@ class Dictionary
m_keyMap {10, CaseOptionalHash {caseSensitive},
CaseOptionalEqual {caseSensitive}} {}

void clear(void);
uint16_t add(Token *token, EntryType *returnNewEntry = {});
int remove(uint16_t index);
std::string string(uint16_t index) const
virtual void clear(void);
virtual std::pair<uint16_t, EntryType> add(Token *token);
virtual bool remove(uint16_t index);
virtual std::string string(uint16_t index) const
{
return m_iterator[index]->first;
}
Expand Down Expand Up @@ -108,9 +108,9 @@ class InfoDictionary : public Dictionary
InfoDictionary(CaseSensitive caseSensitive = CaseSensitive::No) :
Dictionary {caseSensitive} {}

void clear(void);
uint16_t add(Token *token);
void remove(uint16_t index);
void clear(void) override;
std::pair<uint16_t, EntryType> add(Token *token) override;
bool remove(uint16_t index) override;

protected:
std::unique_ptr<AbstractInfo> m_info; // pointer to additional information
Expand Down

0 comments on commit c3f9335

Please sign in to comment.