libime
autophrasedict.h
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 #ifndef _FCITX_LIBIME_TABLE_AUTOPHRASEDICT_H_
7 #define _FCITX_LIBIME_TABLE_AUTOPHRASEDICT_H_
8 
9 #include <cstddef>
10 #include <cstdint>
11 #include <functional>
12 #include <istream>
13 #include <memory>
14 #include <string>
15 #include <string_view>
16 #include <fcitx-utils/macros.h>
17 #include <libime/table/libimetable_export.h>
18 
19 namespace libime {
20 
21 class AutoPhraseDictPrivate;
22 
23 /// \brief A simple MRU based dictionary.
24 class LIBIMETABLE_EXPORT AutoPhraseDict {
25 public:
26  AutoPhraseDict(size_t maxItems);
27  AutoPhraseDict(size_t maxItems, std::istream &in);
28  FCITX_DECLARE_VIRTUAL_DTOR_COPY_AND_MOVE(AutoPhraseDict)
29 
30  /// \brief Insert a word into dictionary and refresh the MRU.
31  ///
32  /// Set the value of entry to value if value is positive. Otherwise if the
33  /// value is 0, the actual value will be increased the value by 1.
34  void insert(const std::string &entry, uint32_t value = 0);
35 
36  /// \brief Check if any word starting with s exists in the dictionary.
37  bool search(
38  std::string_view s,
39  const std::function<bool(std::string_view, uint32_t)> &callback) const;
40 
41  /// \brief Returns 0 if there is no such word.
42  uint32_t exactSearch(std::string_view s) const;
43  void erase(std::string_view s);
44  void clear();
45 
46  void load(std::istream &in);
47  void save(std::ostream &out);
48 
49  bool empty() const;
50 
51 private:
52  std::unique_ptr<AutoPhraseDictPrivate> d_ptr;
53  FCITX_DECLARE_PRIVATE(AutoPhraseDict);
54 };
55 } // namespace libime
56 
57 #endif // _FCITX_LIBIME_TABLE_AUTOPHRASEDICT_H_
A simple MRU based dictionary.