diff --git a/dictionary.cpp b/dictionary.cpp index a352d9d..8f29c9d 100644 --- a/dictionary.cpp +++ b/dictionary.cpp @@ -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? @@ -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) { diff --git a/dictionary.h b/dictionary.h index 74d50e6..ff62f8a 100644 --- a/dictionary.h +++ b/dictionary.h @@ -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); @@ -52,4 +60,29 @@ class Dictionary }; +template +// Info class must have Info(Token *token) constructor +class InfoDictionary : public Dictionary +{ +protected: + QVector 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