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