Get rid of SharedString
This commit is contained in:
parent
94cbd5a837
commit
263ef0b149
|
@ -56,48 +56,6 @@ struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
|
|||
|
||||
using StringDataPtr = RefPtr<StringData>;
|
||||
|
||||
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<StringRegistry>
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -37,7 +37,7 @@ UsedLetters to_lower(UsedLetters letters)
|
|||
using WordList = Vector<StringView>;
|
||||
|
||||
|
||||
static WordList get_words(const SharedString& content)
|
||||
static WordList get_words(StringView content)
|
||||
{
|
||||
WordList res;
|
||||
using Utf8It = utf8::iterator<const char*, utf8::InvalidPolicy::Pass>;
|
||||
|
@ -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;
|
||||
|
|
|
@ -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<SharedString, WordInfo, MemoryDomain::WordDB>;
|
||||
using WordToInfo = UnorderedMap<StringView, WordInfo, MemoryDomain::WordDB>;
|
||||
using Lines = Vector<StringDataPtr, MemoryDomain::WordDB>;
|
||||
|
||||
SafePtr<const Buffer> m_buffer;
|
||||
|
|
Loading…
Reference in New Issue
Block a user