Add a no copy shared string constructor, used for map lookup, and intern words

This commit is contained in:
Maxime Coste 2015-01-15 19:58:08 +00:00
parent bb915f0bc5
commit 4fd92127c3
2 changed files with 6 additions and 3 deletions

View File

@ -24,6 +24,9 @@ public:
StringView::operator=(*m_storage); StringView::operator=(*m_storage);
} }
} }
struct NoCopy{};
SharedString(StringView str, NoCopy) : StringView(str) {}
SharedString(const char* str) : SharedString(StringView{str}) {} SharedString(const char* str) : SharedString(StringView{str}) {}
SharedString acquire_substr(ByteCount from, ByteCount length = INT_MAX) const SharedString acquire_substr(ByteCount from, ByteCount length = INT_MAX) const

View File

@ -56,7 +56,7 @@ void WordDB::add_words(const WordList& words)
{ {
for (auto& w : words) for (auto& w : words)
{ {
WordDB::WordInfo& info = m_words[w]; WordDB::WordInfo& info = m_words[intern(w)];
++info.refcount; ++info.refcount;
if (info.letters.none()) if (info.letters.none())
info.letters = used_letters(w); info.letters = used_letters(w);
@ -67,7 +67,7 @@ void WordDB::remove_words(const WordList& words)
{ {
for (auto& w : words) for (auto& w : words)
{ {
auto it = m_words.find(w); auto it = m_words.find({w, SharedString::NoCopy()});
kak_assert(it != m_words.end() and it->second.refcount > 0); kak_assert(it != m_words.end() and it->second.refcount > 0);
if (--it->second.refcount == 0) if (--it->second.refcount == 0)
m_words.erase(it); m_words.erase(it);
@ -131,7 +131,7 @@ void WordDB::update_db()
int WordDB::get_word_occurences(StringView word) const int WordDB::get_word_occurences(StringView word) const
{ {
auto it = m_words.find(word); auto it = m_words.find({word, SharedString::NoCopy()});
if (it != m_words.end()) if (it != m_words.end())
return it->second.refcount; return it->second.refcount;
return 0; return 0;