1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-23 03:21:55 +03:00
kakoune/src/word_db.hh

64 lines
1.4 KiB
C++
Raw Normal View History

#ifndef word_db_hh_INCLUDED
#define word_db_hh_INCLUDED
#include "buffer.hh"
#include "shared_string.hh"
#include "unordered_map.hh"
#include "vector.hh"
2014-10-28 22:23:02 +03:00
#include <bitset>
namespace Kakoune
{
2014-12-23 22:32:42 +03:00
using UsedLetters = std::bitset<64>;
UsedLetters used_letters(StringView str);
// maintain a database of words available in a buffer
class WordDB
{
public:
WordDB(const Buffer& buffer);
2014-12-22 23:08:53 +03:00
WordDB(const WordDB&) = delete;
2014-10-28 22:23:02 +03:00
WordDB(WordDB&&) = default;
using WordList = Vector<StringView>;
2014-12-23 22:32:42 +03:00
template<typename MatchFunc>
WordList find_matching(StringView str, MatchFunc match)
{
update_db();
const UsedLetters letters = used_letters(str);
2015-01-11 22:28:03 +03:00
WordList res;
2014-12-23 22:32:42 +03:00
for (auto&& word : m_words)
{
if ((letters & word.second.letters) == letters and
match(word.first, str))
res.push_back(word.first);
}
return res;
}
int get_word_occurences(StringView word) const;
2015-01-12 16:58:41 +03:00
private:
2015-01-13 16:57:44 +03:00
void update_db();
void add_words(const WordList& words);
void remove_words(const WordList& words);
2014-10-28 22:23:02 +03:00
struct WordInfo
{
2014-12-23 22:32:42 +03:00
UsedLetters letters;
2014-10-28 22:23:02 +03:00
int refcount;
};
using WordToInfo = UnorderedMap<SharedString, WordInfo, MemoryDomain::WordDB>;
using Lines = Vector<ref_ptr<StringStorage>, MemoryDomain::WordDB>;
safe_ptr<const Buffer> m_buffer;
size_t m_timestamp;
2014-12-23 22:32:42 +03:00
WordToInfo m_words;
Lines m_lines;
};
}
#endif // word_db_hh_INCLUDED