libime
pinyinmatchstate.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 
7 #include "libime/pinyin/pinyinmatchstate.h"
8 #include <cstddef>
9 #include <memory>
10 #include <unordered_set>
11 #include <fcitx-utils/macros.h>
12 #include "libime/pinyin/pinyinencoder.h"
13 #include "pinyincontext.h"
14 #include "pinyinime.h"
15 #include "pinyinmatchstate_p.h"
16 
17 namespace libime {
18 
19 PinyinMatchState::PinyinMatchState(PinyinContext *context)
20  : d_ptr(std::make_unique<PinyinMatchStatePrivate>(context)) {}
21 PinyinMatchState::~PinyinMatchState() {}
22 
23 void PinyinMatchState::clear() {
24  FCITX_D();
25  d->matchedPaths_.clear();
26  d->nodeCacheMap_.clear();
27  d->matchCacheMap_.clear();
28 }
29 
30 void PinyinMatchState::discardNode(
31  const std::unordered_set<const SegmentGraphNode *> &nodes) {
32  FCITX_D();
33  for (const auto *node : nodes) {
34  d->matchedPaths_.erase(node);
35  }
36  for (auto &p : d->matchedPaths_) {
37  auto &l = p.second;
38  auto iter = l.begin();
39  while (iter != l.end()) {
40  if (nodes.contains(iter->path_.front())) {
41  iter = l.erase(iter);
42  } else {
43  iter++;
44  }
45  }
46  }
47 }
48 
49 PinyinFuzzyFlags PinyinMatchState::fuzzyFlags() const {
50  FCITX_D();
51  return d->context_->ime()->fuzzyFlags();
52 }
53 
54 std::shared_ptr<const ShuangpinProfile>
55 PinyinMatchState::shuangpinProfile() const {
56  FCITX_D();
57  if (d->context_->useShuangpin()) {
58  return d->context_->ime()->shuangpinProfile();
59  }
60  return {};
61 }
62 
63 std::shared_ptr<const PinyinCorrectionProfile>
64 PinyinMatchState::correctionProfile() const {
65  FCITX_D();
66  if (d->context_->ime()->fuzzyFlags().test(PinyinFuzzyFlag::Correction)) {
67  return d->context_->ime()->correctionProfile();
68  }
69  return {};
70 }
71 
72 size_t PinyinMatchState::partialLongWordLimit() const {
73  FCITX_D();
74  return d->context_->ime()->partialLongWordLimit();
75 }
76 
77 void PinyinMatchState::discardDictionary(size_t idx) {
78  FCITX_D();
79  d->matchCacheMap_.erase(d->context_->ime()->dict()->trie(idx));
80  d->nodeCacheMap_.erase(d->context_->ime()->dict()->trie(idx));
81 }
82 } // namespace libime