From 263ef0b149b150c5fb77d264ff7a48a552eb9507 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 5 Feb 2016 09:36:07 +0000 Subject: [PATCH] Get rid of SharedString --- src/shared_string.hh | 42 ------------------------------------------ src/word_db.cc | 31 +++++++++++++++++++------------ src/word_db.hh | 7 ++++--- 3 files changed, 23 insertions(+), 57 deletions(-) diff --git a/src/shared_string.hh b/src/shared_string.hh index 2dfc3d38..112b67f2 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -56,48 +56,6 @@ struct StringData : UseMemoryDomain using StringDataPtr = RefPtr; -class SharedString : public StringView -{ -public: - SharedString() = default; - SharedString(StringView str) - { - if (not str.empty()) - { - m_storage = StringData::create(str); - StringView::operator=(m_storage->strview()); - } - } - 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 - { - return SharedString{StringView::substr(from, length), m_storage}; - } - SharedString acquire_substr(CharCount from, CharCount length = INT_MAX) const - { - return SharedString{StringView::substr(from, length), m_storage}; - } - - explicit SharedString(StringDataPtr storage) - : StringView{storage->strview()}, m_storage(std::move(storage)) {} - - friend size_t hash_value(const SharedString& str) - { - return str.m_storage ? str.m_storage->hash : hash_data(str.data(), (int)str.length()); - } - -private: - SharedString(StringView str, StringDataPtr storage) - : StringView{str}, m_storage(std::move(storage)) {} - - friend class StringRegistry; - StringDataPtr m_storage; -}; - class StringRegistry : public Singleton { public: diff --git a/src/word_db.cc b/src/word_db.cc index 51af7a3f..7e8939f5 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -37,7 +37,7 @@ UsedLetters to_lower(UsedLetters letters) using WordList = Vector; -static WordList get_words(const SharedString& content) +static WordList get_words(StringView content) { WordList res; using Utf8It = utf8::iterator; @@ -56,29 +56,36 @@ static WordList get_words(const SharedString& content) { const ByteCount start = word_start - content.begin(); const ByteCount length = it.base() - word_start; - res.push_back(content.acquire_substr(start, length)); + res.push_back(content.substr(start, length)); in_word = false; } } return res; } -void WordDB::add_words(const SharedString& line) +void WordDB::add_words(StringView line) { for (auto& w : get_words(line)) { - WordDB::WordInfo& info = m_words[SharedString{intern(w)}]; - ++info.refcount; - if (info.letters.none()) + auto it = m_words.find(w); + if (it == m_words.end()) + { + auto word = intern(w); + WordDB::WordInfo& info = m_words[word->strview()]; + info.word = word; info.letters = used_letters(w); + ++info.refcount; + } + else + ++ it->second.refcount; } } -void WordDB::remove_words(const SharedString& line) +void WordDB::remove_words(StringView line) { for (auto& w : get_words(line)) { - auto it = m_words.find({w, SharedString::NoCopy()}); + auto it = m_words.find(w); kak_assert(it != m_words.end() and it->second.refcount > 0); if (--it->second.refcount == 0) m_words.erase(it); @@ -92,7 +99,7 @@ WordDB::WordDB(const Buffer& buffer) for (auto line = 0_line, end = buffer.line_count(); line < end; ++line) { m_lines.push_back(buffer.line_storage(line)); - add_words(SharedString{m_lines.back()}); + add_words(m_lines.back()->strview()); } } @@ -123,13 +130,13 @@ void WordDB::update_db() while (old_line < modif.old_line + modif.num_removed) { kak_assert(old_line < m_lines.size()); - remove_words(SharedString{m_lines[(int)old_line++]}); + remove_words(m_lines[(int)old_line++]->strview()); } for (auto l = 0_line; l < modif.num_added; ++l) { new_lines.push_back(buffer.line_storage(modif.new_line + l)); - add_words(SharedString{new_lines.back()}); + add_words(new_lines.back()->strview()); } } while (old_line != (int)m_lines.size()) @@ -140,7 +147,7 @@ void WordDB::update_db() int WordDB::get_word_occurences(StringView word) const { - auto it = m_words.find({word, SharedString::NoCopy()}); + auto it = m_words.find(word); if (it != m_words.end()) return it->second.refcount; return 0; diff --git a/src/word_db.hh b/src/word_db.hh index ee24bc92..a4644b5b 100644 --- a/src/word_db.hh +++ b/src/word_db.hh @@ -30,15 +30,16 @@ public: int get_word_occurences(StringView word) const; private: void update_db(); - void add_words(const SharedString& line); - void remove_words(const SharedString& line); + void add_words(StringView line); + void remove_words(StringView line); struct WordInfo { + StringDataPtr word; UsedLetters letters; int refcount; }; - using WordToInfo = UnorderedMap; + using WordToInfo = UnorderedMap; using Lines = Vector; SafePtr m_buffer;