From 4fd92127c3584b9e482613ecb78f1bd3cc93c1f1 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 15 Jan 2015 19:58:08 +0000 Subject: [PATCH] Add a no copy shared string constructor, used for map lookup, and intern words --- src/shared_string.hh | 3 +++ src/word_db.cc | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/shared_string.hh b/src/shared_string.hh index 50eec956..982cb67f 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -24,6 +24,9 @@ public: StringView::operator=(*m_storage); } } + struct NoCopy{}; + SharedString(StringView str, NoCopy) : StringView(str) {} + SharedString(const char* str) : SharedString(StringView{str}) {} SharedString acquire_substr(ByteCount from, ByteCount length = INT_MAX) const diff --git a/src/word_db.cc b/src/word_db.cc index 53a478c1..844aa196 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -56,7 +56,7 @@ void WordDB::add_words(const WordList& words) { for (auto& w : words) { - WordDB::WordInfo& info = m_words[w]; + WordDB::WordInfo& info = m_words[intern(w)]; ++info.refcount; if (info.letters.none()) info.letters = used_letters(w); @@ -67,7 +67,7 @@ void WordDB::remove_words(const WordList& 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); if (--it->second.refcount == 0) m_words.erase(it); @@ -131,7 +131,7 @@ void WordDB::update_db() 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()) return it->second.refcount; return 0;