libime
triedictionary.h
1 /*
2  * SPDX-FileCopyrightText: 2018-2018 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _LIBIME_LIBIME_CORE_TRIEDICTIONARY_H_
8 #define _LIBIME_LIBIME_CORE_TRIEDICTIONARY_H_
9 
10 #include <cstddef>
11 #include <memory>
12 #include <string_view>
13 #include <fcitx-utils/connectableobject.h>
14 #include <fcitx-utils/macros.h>
15 #include <libime/core/datrie.h>
16 #include <libime/core/dictionary.h>
17 #include <libime/core/libimecore_export.h>
18 
19 namespace libime {
20 
21 class TrieDictionaryPrivate;
22 
23 class LIBIMECORE_EXPORT TrieDictionary : public Dictionary,
24  public fcitx::ConnectableObject {
25 public:
26  using TrieType = DATrie<float>;
27 
28  static const size_t SystemDict = 0;
29  static const size_t UserDict = 1;
30  explicit TrieDictionary();
31  ~TrieDictionary();
32 
33  // Append a dictionary at the end.
34  void addEmptyDict();
35 
36  // Remove all dictionary except system and user.
37  void removeAll();
38 
39  /**
40  * Remove all dictionary from given index.
41  *
42  * @param idx the index need to be within [UserDict + 1, dictSize())
43  * @since 1.0.10
44  */
45  void removeFrom(size_t idx);
46 
47  // Clear dictionary.
48  void clear(size_t idx);
49 
50  const TrieType *trie(size_t idx) const;
51 
52  /**
53  * Set trie from external source.
54  *
55  * There is no validation on the data within it, subclass may expect a
56  * certain way of organize the key and value.
57  *
58  * @param idx the index need to be within [0, dictSize())
59  * @param trie new trie.
60  * @since 1.1.7
61  */
62  void setTrie(size_t idx, TrieType trie);
63 
64  // Total number to dictionary.
65  size_t dictSize() const;
66 
67  FCITX_DECLARE_SIGNAL(TrieDictionary, dictionaryChanged, void(size_t));
68  FCITX_DECLARE_SIGNAL(TrieDictionary, dictSizeChanged, void(size_t));
69 
70 protected:
71  TrieType *mutableTrie(size_t idx);
72  void addWord(size_t idx, std::string_view key, float cost = 0.0F);
73  bool removeWord(size_t idx, std::string_view key);
74 
75  std::unique_ptr<TrieDictionaryPrivate> d_ptr;
76  FCITX_DECLARE_PRIVATE(TrieDictionary);
77 };
78 } // namespace libime
79 
80 #endif // _LIBIME_LIBIME_CORE_TRIEDICTIONARY_H_
Provide a DATrie implementation.