Skip to content

Commit

Permalink
added information dictionary template class
Browse files Browse the repository at this point in the history
the information dictionary is based on the dictionary class and add a
vector of a generic information class; the add function is overloaded
and starts by calling the dictionary add and then adds a new element to
the information vector if a new entry was added to the base dictionary

modified the dictionary add function from returning a boolean new entry
status to return exists, reused, or new entry status; this was needed so
the derived information dictionary knows when to append or replace an
information element or do nothing if the entry already exists
  • Loading branch information
thunder422 committed Oct 6, 2013
1 parent 84b8dd9 commit 6ffb690
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
10 changes: 5 additions & 5 deletions dictionary.cpp
Expand Up @@ -30,10 +30,9 @@ Dictionary::Dictionary(void)
{
}


quint16 Dictionary::add(Token *token, bool *returnNewEntry)
quint16 Dictionary::add(Token *token, Dictionary::EntryType *returnNewEntry)
{
bool newEntry;
EntryType newEntry;

int index = m_keyMap.value(token->string(), -1);
if (index == -1) // string not present?
Expand All @@ -43,20 +42,21 @@ quint16 Dictionary::add(Token *token, bool *returnNewEntry)
index = m_keyList.count();
m_keyList.append(token->string());
m_useCount.append(1);
newEntry = New_Entry;
}
else // use a previously freed index
{
index = m_freeStack.pop();
m_keyList[index] = token->string();
m_useCount[index] = 1;
newEntry = Reused_Entry;
}
m_keyMap[token->string()] = index; // save key/index in map
newEntry = true;
}
else // string already present, update use count
{
m_useCount[index]++;
newEntry = false;
newEntry = Exists_Entry;
}
if (returnNewEntry)
{
Expand Down
35 changes: 34 additions & 1 deletion dictionary.h
Expand Up @@ -37,8 +37,16 @@ class Dictionary
public:
Dictionary(void);

quint16 add(Token *token, bool *returnNewEntry = NULL);
void remove(quint16 index);
enum EntryType
{
New_Entry,
Reused_Entry,
Exists_Entry,
sizeof_Entry
};

quint16 add(Token *token, EntryType *returnNewEntry = NULL);
QString string(int index) const
{
return m_keyList.at(index);
Expand All @@ -52,4 +60,29 @@ class Dictionary
};


template <class Info>
// Info class must have Info(Token *token) constructor
class InfoDictionary : public Dictionary
{
protected:
QVector<Info> m_info; // additional dictionary information

public:
quint16 add(Token *token)
{
EntryType returnNewEntry;
int index = Dictionary::add(token, &returnNewEntry);
if (returnNewEntry == New_Entry)
{
m_info.append(Info(token));
}
else if (returnNewEntry == Reused_Entry)
{
m_info[index] = Info(token);
}
return index;
}
};


#endif // DICTIONARY_H

0 comments on commit 6ffb690

Please sign in to comment.