data: removed Singleton from Dictionary and made it a member of SearchIndex

Dictionary never had a good reason of being a Singleton in the first place so this will avoid concurrency and
structural issues in the future.
This commit is contained in:
Eberhard Graether
2014-09-30 22:10:42 +02:00
parent 95ece9ef2e
commit 390df1db1f
13 changed files with 128 additions and 103 deletions
+33 -15
View File
@@ -2,22 +2,34 @@
#include "utility/utilityString.h"
std::shared_ptr<Dictionary> Dictionary::getInstance()
Dictionary::Dictionary()
{
std::lock_guard<std::mutex> lockGuard(s_instanceMutex);
if (!s_instance)
{
s_instance = std::shared_ptr<Dictionary>(new Dictionary());
}
return s_instance;
}
Dictionary::~Dictionary()
{
}
void Dictionary::clear()
{
m_words.clear();
}
Id Dictionary::getWordId(const std::string& word)
{
Id wordId = getWordIdConst(word);
if (wordId)
{
return wordId;
}
m_words.emplace(++s_nextId, word);
return s_nextId;
}
Id Dictionary::getWordIdConst(const std::string& word) const
{
// TODO: This word lookup is very inefficient, use something smarter like a trie.
for (std::unordered_map<Id, std::string>::const_iterator it = m_words.begin(); it != m_words.end(); it++)
{
if (it->second == word)
@@ -26,8 +38,7 @@ Id Dictionary::getWordId(const std::string& word)
}
}
m_words.emplace(++s_nextId, word);
return s_nextId;
return 0;
}
std::deque<Id> Dictionary::getWordIds(const std::string& wordList, const std::string& delimiter)
@@ -43,6 +54,19 @@ std::deque<Id> Dictionary::getWordIds(const std::string& wordList, const std::st
return ids;
}
std::deque<Id> Dictionary::getWordIdsConst(const std::string& wordList, const std::string& delimiter) const
{
std::deque<std::string> words = utility::split(wordList, delimiter);
std::deque<Id> ids;
for (const std::string& word: words)
{
ids.push_back(getWordIdConst(word));
}
return ids;
}
const std::string& Dictionary::getWord(Id id) const
{
std::unordered_map<Id, std::string>::const_iterator it = m_words.find(id);
@@ -72,10 +96,4 @@ std::string Dictionary::getWord(const std::deque<Id> ids, const std::string& del
return word;
}
Dictionary::Dictionary()
{
}
std::shared_ptr<Dictionary> Dictionary::s_instance;
std::mutex Dictionary::s_instanceMutex;
Id Dictionary::s_nextId = 0;
+6 -7
View File
@@ -12,23 +12,22 @@
class Dictionary
{
public:
static std::shared_ptr<Dictionary> getInstance();
Dictionary();
~Dictionary();
void clear();
Id getWordId(const std::string& word);
Id getWordIdConst(const std::string& word) const;
std::deque<Id> getWordIds(const std::string& wordList, const std::string& delimiter);
std::deque<Id> getWordIdsConst(const std::string& wordList, const std::string& delimiter) const;
// Note: References to values in an unordered_map don't change on rehashing so they can be saved and used elsewhere.
const std::string& getWord(Id id) const;
std::string getWord(const std::deque<Id> ids, const std::string& delimiter) const;
private:
Dictionary();
Dictionary(const Dictionary&);
void operator=(const Dictionary&);
static std::shared_ptr<Dictionary> s_instance;
static std::mutex s_instanceMutex;
static Id s_nextId;
std::unordered_map<Id, std::string> m_words;