From 019b3235b0e7aaad086631e1ee9b3102f096d349 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 21 Oct 2015 20:16:36 +0100 Subject: [PATCH] Remove legacy WordDB::find_matching method --- src/word_db.cc | 43 ++++++++++++++++++++++++++++++++----------- src/word_db.hh | 16 ---------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/word_db.cc b/src/word_db.cc index addb9fab..c1c8f967 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -34,9 +34,12 @@ UsedLetters to_lower(UsedLetters letters) return ((letters & upper_mask) >> 26) | (letters & (~upper_mask)); } -static WordDB::WordList get_words(const SharedString& content) +using WordList = Vector; + + +static WordList get_words(const SharedString& content) { - WordDB::WordList res; + WordList res; using Utf8It = utf8::iterator; const char* word_start = content.begin(); bool in_word = false; @@ -189,6 +192,12 @@ WordDB::RankedWordList WordDB::find_matching(StringView query) RankedWordList res; for (auto&& word : m_words) { + if (query.empty()) + { + res.push_back({word.first, 1 }); + continue; + } + UsedLetters word_letters = word.second.letters; if (not matches(to_lower(letters), to_lower(word_letters)) or not matches(letters & upper_mask, word_letters & upper_mask)) @@ -202,6 +211,18 @@ WordDB::RankedWordList WordDB::find_matching(StringView query) UnitTest test_word_db{[]() { + auto cmp_words = [](const WordDB::RankedWord& lhs, const WordDB::RankedWord& rhs) { + return lhs.word < rhs.word; + }; + + auto eq = [](ArrayView lhs, const WordList& rhs) { + return lhs.size() == rhs.size() and + std::equal(lhs.begin(), lhs.end(), rhs.begin(), + [](const WordDB::RankedWord& lhs, const StringView& rhs) { + return lhs.word == rhs; + }); + }; + Buffer buffer("test", Buffer::Flags::None, "tchou mutch\n" "tchou kanaky tchou\n" @@ -209,19 +230,19 @@ UnitTest test_word_db{[]() "tchaa tchaa\n" "allo\n"); WordDB word_db(buffer); - auto res = word_db.find_matching("", prefix_match); - std::sort(res.begin(), res.end()); - kak_assert(res == WordDB::WordList{ "allo" COMMA "kanaky" COMMA "mutch" COMMA "tchaa" COMMA "tchou" }); + auto res = word_db.find_matching(""); + std::sort(res.begin(), res.end(), cmp_words); + kak_assert(eq(res, WordList{ "allo" COMMA "kanaky" COMMA "mutch" COMMA "tchaa" COMMA "tchou" })); kak_assert(word_db.get_word_occurences("tchou") == 3); kak_assert(word_db.get_word_occurences("allo") == 1); buffer.erase(buffer.iterator_at({1, 6}), buffer.iterator_at({4, 0})); - res = word_db.find_matching("", prefix_match); - std::sort(res.begin(), res.end()); - kak_assert(res == WordDB::WordList{ "allo" COMMA "mutch" COMMA "tchou" }); + res = word_db.find_matching(""); + std::sort(res.begin(), res.end(), cmp_words); + kak_assert(eq(res, WordList{ "allo" COMMA "mutch" COMMA "tchou" })); buffer.insert(buffer.iterator_at({1, 0}), "re"); - res = word_db.find_matching("", subsequence_match); - std::sort(res.begin(), res.end()); - kak_assert(res == WordDB::WordList{ "allo" COMMA "mutch" COMMA "retchou" COMMA "tchou" }); + res = word_db.find_matching(""); + std::sort(res.begin(), res.end(), cmp_words); + kak_assert(eq(res, WordList{ "allo" COMMA "mutch" COMMA "retchou" COMMA "tchou" })); }}; UnitTest test_used_letters{[]() diff --git a/src/word_db.hh b/src/word_db.hh index 4854e881..45aa5228 100644 --- a/src/word_db.hh +++ b/src/word_db.hh @@ -22,22 +22,6 @@ public: WordDB(const WordDB&) = delete; WordDB(WordDB&&) = default; - using WordList = Vector; - template - WordList find_matching(StringView str, MatchFunc match) - { - update_db(); - const UsedLetters letters = used_letters(str); - WordList res; - for (auto&& word : m_words) - { - if ((letters & word.second.letters) == letters and - match(word.first, str)) - res.push_back(word.first); - } - return res; - } - struct RankedWord { StringView word;