summaryrefslogtreecommitdiff
path: root/src/word_db.hh
blob: 999d03b012e82a4154b039ee5baa9b499e80725b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef word_db_hh_INCLUDED
#define word_db_hh_INCLUDED

#include "buffer.hh"
#include "interned_string.hh"

#include <map>
#include <bitset>

namespace Kakoune
{

// maintain a database of words available in a buffer
class WordDB
{
public:
    WordDB(const Buffer& buffer);
    WordDB(const WordDB&) { kak_assert(false); }
    WordDB(WordDB&&) = default;

    std::vector<InternedString> find_prefix(StringView prefix);
    std::vector<InternedString> find_subsequence(StringView subsequence);
    int get_word_occurences(StringView word) const;

    using UsedChars = std::bitset<64>;
    struct WordInfo
    {
        UsedChars letters;
        int refcount;
    };
    using WordList = std::unordered_map<InternedString, WordInfo>;
private:
    using LineToWords = std::vector<std::vector<InternedString>>;

    void update_db();

    safe_ptr<const Buffer> m_buffer;
    size_t m_timestamp;
    WordList m_words;
    LineToWords m_line_to_words;
};

}

#endif // word_db_hh_INCLUDED