Skip to content

Commit 118dc5f

Browse files
committed
Clang-tidy and include fix on libime/core
1 parent c84dd04 commit 118dc5f

30 files changed

+213
-86
lines changed

src/libime/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ set(LIBIME_HDRS
5050

5151
set(LIBIME_SRCS
5252
datrie.cpp
53+
dictionary.cpp
5354
decoder.cpp
5455
languagemodel.cpp
5556
inputbuffer.cpp

src/libime/core/datrie.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020
#include <cmath>
2121
#include <cstdint>
2222
#include <cstring>
23+
#include <fcitx-utils/macros.h>
2324
#include <fstream>
2425
#include <ios>
2526
#include <istream>
2627
#include <limits>
28+
#include <memory>
2729
#include <ostream>
2830
#include <stdexcept>
2931
#include <string>
3032
#include <string_view>
33+
#include <sys/types.h>
3134
#include <tuple>
3235
#include <vector>
3336

src/libime/core/datrie.h

+5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212

1313
#include "libimecore_export.h"
1414

15+
#include <cstddef>
1516
#include <cstdint>
1617
#include <fcitx-utils/macros.h>
1718
#include <functional>
19+
#include <istream>
1820
#include <memory>
21+
#include <ostream>
22+
#include <string>
1923
#include <string_view>
24+
#include <tuple>
2025
#include <vector>
2126

2227
namespace libime {

src/libime/core/decoder.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ void DecoderPrivate::backwardSearch(const SegmentGraph &graph, Lattice &l,
270270
std::priority_queue<std::shared_ptr<NBestNode>,
271271
std::vector<std::shared_ptr<NBestNode>>,
272272
NBestNodeLess<std::shared_ptr<NBestNode>>>;
273-
PriorityQueueType q, result;
273+
PriorityQueueType q;
274+
PriorityQueueType result;
274275

275276
auto *eos = &lattice[nullptr][0];
276277
auto newNBestNode = [](const LatticeNode *node) {

src/libime/core/decoder.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
#define _FCITX_LIBIME_CORE_DECODER_H_
88

99
#include "libimecore_export.h"
10+
#include <cstddef>
1011
#include <fcitx-utils/macros.h>
1112
#include <libime/core/dictionary.h>
13+
#include <libime/core/languagemodel.h>
1214
#include <libime/core/lattice.h>
1315
#include <libime/core/segmentgraph.h>
16+
#include <limits>
1417
#include <memory>
18+
#include <string_view>
19+
#include <utility>
1520

1621
namespace libime {
1722

@@ -55,8 +60,8 @@ class LIBIMECORE_EXPORT Decoder {
5560
const State &state, float cost, std::unique_ptr<LatticeNodeData> data,
5661
bool onlyPath) const;
5762

58-
virtual bool needSort(const SegmentGraph &,
59-
const SegmentGraphNode *) const {
63+
virtual bool needSort(const SegmentGraph & /*graph*/,
64+
const SegmentGraphNode * /*node*/) const {
6065
return true;
6166
}
6267

src/libime/core/dictionary.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
*/
66

77
#include "dictionary.h"
8+
#include "segmentgraph.h"
9+
#include <unordered_set>
10+
11+
void libime::Dictionary::matchPrefix(
12+
const SegmentGraph &graph, const GraphMatchCallback &callback,
13+
const std::unordered_set<const SegmentGraphNode *> &ignore,
14+
void *helper) const {
15+
matchPrefixImpl(graph, callback, ignore, helper);
16+
}

src/libime/core/dictionary.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,24 @@
1010
#include <functional>
1111
#include <libime/core/lattice.h>
1212
#include <libime/core/segmentgraph.h>
13-
#include <string_view>
13+
#include <memory>
14+
#include <unordered_set>
1415

1516
namespace libime {
1617

1718
class WordNode;
1819

1920
// The callback accepts the passed path that matches the word.
20-
typedef std::function<void(const SegmentGraphPath &, WordNode &, float,
21-
std::unique_ptr<LatticeNodeData>)>
22-
GraphMatchCallback;
21+
using GraphMatchCallback =
22+
std::function<void(const SegmentGraphPath &, WordNode &, float,
23+
std::unique_ptr<LatticeNodeData>)>;
2324

2425
class LIBIMECORE_EXPORT Dictionary {
2526
public:
2627
void
2728
matchPrefix(const SegmentGraph &graph, const GraphMatchCallback &callback,
2829
const std::unordered_set<const SegmentGraphNode *> &ignore = {},
29-
void *helper = nullptr) const {
30-
matchPrefixImpl(graph, callback, ignore, helper);
31-
}
30+
void *helper = nullptr) const;
3231

3332
protected:
3433
virtual void

src/libime/core/historybigram.cpp

+26-13
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,36 @@
66
#include "historybigram.h"
77
#include "constants.h"
88
#include "datrie.h"
9+
#include "lattice.h"
910
#include "utils.h"
1011
#include "zstdfilter.h"
12+
#include <algorithm>
13+
#include <array>
1114
#include <boost/algorithm/cxx11/all_of.hpp>
1215
#include <boost/algorithm/string.hpp>
16+
#include <boost/algorithm/string/predicate.hpp>
1317
#include <boost/range/adaptor/reversed.hpp>
1418
#include <boost/range/adaptor/transformed.hpp>
1519
#include <boost/range/algorithm.hpp>
20+
#include <boost/range/algorithm/for_each.hpp>
21+
#include <cassert>
1622
#include <cmath>
23+
#include <cstddef>
24+
#include <cstdint>
25+
#include <fcitx-utils/macros.h>
1726
#include <fcitx-utils/stringutils.h>
27+
#include <functional>
28+
#include <istream>
1829
#include <iterator>
30+
#include <list>
31+
#include <memory>
1932
#include <ostream>
2033
#include <stdexcept>
2134
#include <string>
35+
#include <string_view>
36+
#include <unordered_set>
37+
#include <utility>
38+
#include <vector>
2239

2340
namespace libime {
2441

@@ -53,7 +70,7 @@ struct WeightedTrie {
5370

5471
void decFreq(std::string_view s, int32_t delta) {
5572
auto v = trie_.exactMatchSearch(s.data(), s.size());
56-
if (trie_.isNoValue(v)) {
73+
if (TrieType::isNoValue(v)) {
5774
return;
5875
}
5976
if (v <= delta) {
@@ -68,7 +85,7 @@ struct WeightedTrie {
6885

6986
void eraseByKey(std::string_view s) {
7087
auto v = trie_.exactMatchSearch(s.data(), s.size());
71-
if (trie_.isNoValue(v)) {
88+
if (TrieType::isNoValue(v)) {
7289
return;
7390
}
7491
trie_.erase(s);
@@ -121,10 +138,7 @@ struct WeightedTrie {
121138
}
122139
words.emplace(std::move(buf));
123140

124-
if (maxSize > 0 && words.size() >= maxSize) {
125-
return false;
126-
}
127-
return true;
141+
return maxSize <= 0 || words.size() < maxSize;
128142
});
129143
}
130144

@@ -168,8 +182,9 @@ class HistoryBigramPool {
168182
std::vector<std::string> lines;
169183
while (std::getline(in, buf)) {
170184
lines.emplace_back(buf);
171-
if (lines.size() >= maxSize_)
185+
if (lines.size() >= maxSize_) {
172186
break;
187+
}
173188
}
174189
for (auto &line : lines | boost::adaptors::reversed) {
175190
std::vector<std::string> sentence =
@@ -348,8 +363,6 @@ class HistoryBigramPool {
348363
// And then we define alpha as p = 1 / (1 + alpha).
349364
class HistoryBigramPrivate {
350365
public:
351-
HistoryBigramPrivate() {}
352-
353366
void populateSentence(std::list<std::vector<std::string>> popedSentence) {
354367
for (size_t i = 1; !popedSentence.empty() && i < pools_.size(); i++) {
355368
std::list<std::vector<std::string>> nextSentences;
@@ -405,7 +418,7 @@ HistoryBigram::HistoryBigram()
405418
d->poolWeight_.reserve(poolSize.size());
406419
for (auto size : poolSize) {
407420
d->pools_.emplace_back(size);
408-
float portion = 1.0f;
421+
float portion = 1.0F;
409422
if (d->pools_.size() != poolSize.size()) {
410423
portion *= 1 - p;
411424
}
@@ -472,11 +485,11 @@ float HistoryBigram::score(std::string_view prev, std::string_view cur) const {
472485
auto bf = d->bigramFreq(prev, cur);
473486
auto uf1 = d->unigramFreq(cur);
474487

475-
float bigramWeight = d->useOnlyUnigram_ ? 0.0f : 0.8f;
488+
float bigramWeight = d->useOnlyUnigram_ ? 0.0F : 0.8F;
476489
// add 0.5 to avoid div 0
477-
float pr = 0.0f;
490+
float pr = 0.0F;
478491
pr += bigramWeight * float(bf) / float(uf0 + d->poolWeight_[0] / 2);
479-
pr += (1.0f - bigramWeight) * float(uf1) /
492+
pr += (1.0F - bigramWeight) * float(uf1) /
480493
float(d->unigramSize() + d->poolWeight_[0] / 2);
481494

482495
if (pr >= 1.0) {

src/libime/core/historybigram.h

+4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
#define _FCITX_LIBIME_CORE_HISTORYBIGRAM_H_
88

99
#include "libimecore_export.h"
10+
#include <cstddef>
1011
#include <fcitx-utils/macros.h>
12+
#include <istream>
1113
#include <libime/core/lattice.h>
1214
#include <memory>
15+
#include <ostream>
1316
#include <string>
1417
#include <string_view>
18+
#include <unordered_set>
1519
#include <vector>
1620

1721
namespace libime {

src/libime/core/inputbuffer.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
* SPDX-License-Identifier: LGPL-2.1-or-later
55
*/
66
#include "inputbuffer.h"
7+
#include <cstddef>
78
#include <fcitx-utils/utf8.h>
9+
#include <string_view>
810

911
namespace libime {
1012

11-
std::string_view InputBuffer::at(size_t i) const {
12-
size_t start, end;
13-
std::tie(start, end) = rangeAt(i);
14-
return std::string_view(userInput()).substr(start, end - start);
15-
}
13+
std::string_view InputBuffer::at(size_t i) const { return viewAt(i); }
1614
} // namespace libime

src/libime/core/inputbuffer.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#define _FCITX_LIBIME_CORE_INPUTBUFFER_H_
88

99
#include "libimecore_export.h"
10+
#include <boost/iterator/iterator_categories.hpp>
1011
#include <boost/iterator/iterator_facade.hpp>
12+
#include <cstddef>
1113
#include <fcitx-utils/inputbuffer.h>
1214
#include <string_view>
1315

@@ -21,7 +23,7 @@ class LIBIMECORE_EXPORT InputBuffer : public fcitx::InputBuffer {
2123
boost::bidirectional_traversal_tag,
2224
std::string_view> {
2325
public:
24-
iterator() {}
26+
iterator() = default;
2527
iterator(const InputBuffer *buffer, size_t idx)
2628
: buffer_(buffer), idx_(idx) {}
2729

@@ -49,9 +51,9 @@ class LIBIMECORE_EXPORT InputBuffer : public fcitx::InputBuffer {
4951

5052
std::string_view operator[](size_t i) const { return at(i); }
5153

52-
iterator begin() { return iterator(this, 0); }
54+
iterator begin() { return {this, 0}; }
5355

54-
iterator end() { return iterator(this, size()); }
56+
iterator end() { return {this, size()}; }
5557
};
5658
} // namespace libime
5759

src/libime/core/languagemodel.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@
77
#include "languagemodel.h"
88
#include "config.h"
99
#include "constants.h"
10+
#include "datrie.h"
1011
#include "lattice.h"
12+
#include "lm/config.hh"
13+
#include "lm/lm_exception.hh"
1114
#include "lm/model.hh"
15+
#include "lm/state.hh"
16+
#include "lm/word_index.hh"
17+
#include "util/string_piece.hh"
18+
#include <cassert>
1219
#include <cmath>
20+
#include <cstdlib>
1321
#include <fcitx-utils/fs.h>
22+
#include <fcitx-utils/macros.h>
23+
#include <fcitx-utils/stringutils.h>
1424
#include <fstream>
25+
#include <ios>
26+
#include <memory>
27+
#include <string>
28+
#include <string_view>
1529
#include <type_traits>
30+
#include <unordered_map>
31+
#include <utility>
32+
#include <vector>
1633

1734
namespace libime {
1835

@@ -74,13 +91,14 @@ float LanguageModelBase::singleWordScore(const State &state,
7491
float LanguageModelBase::wordsScore(
7592
const State &_state, const std::vector<std::string_view> &words) const {
7693
float s = 0;
77-
State state = _state, outState;
94+
State state = _state;
95+
State outState;
7896
std::vector<WordNode> nodes;
7997
for (auto word : words) {
8098
auto idx = index(word);
8199
nodes.emplace_back(word, idx);
82100
s += score(state, nodes.back(), outState);
83-
state = std::move(outState);
101+
state = outState;
84102
}
85103
return s;
86104
}
@@ -188,10 +206,10 @@ float LanguageModel::score(const State &state, const WordNode &node,
188206
return d->unknown_;
189207
}
190208
return d->model()->Score(lmState(state), node.idx(), lmState(out)) +
191-
(node.idx() == unknown() ? d->unknown_ : 0.0f);
209+
(node.idx() == unknown() ? d->unknown_ : 0.0F);
192210
}
193211

194-
bool LanguageModel::isUnknown(WordIndex idx, std::string_view) const {
212+
bool LanguageModel::isUnknown(WordIndex idx, std::string_view /*word*/) const {
195213
return idx == unknown();
196214
}
197215

src/libime/core/languagemodel.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
#define _FCITX_LIBIME_CORE_LANGUAGEMODEL_H_
88

99
#include "libimecore_export.h"
10+
#include <array>
11+
#include <cstddef>
1012
#include <fcitx-utils/macros.h>
1113
#include <libime/core/datrie.h>
1214
#include <limits>
1315
#include <memory>
16+
#include <string>
1417
#include <string_view>
1518
#include <vector>
1619

@@ -77,10 +80,10 @@ class LIBIMECORE_EXPORT LanguageModel : public LanguageModelBase {
7780
WordIndex unknown() const override;
7881
const State &beginState() const override;
7982
const State &nullState() const override;
80-
WordIndex index(std::string_view view) const override;
81-
float score(const State &state, const WordNode &word,
83+
WordIndex index(std::string_view word) const override;
84+
float score(const State &state, const WordNode &node,
8285
State &out) const override;
83-
bool isUnknown(WordIndex idx, std::string_view view) const override;
86+
bool isUnknown(WordIndex idx, std::string_view word) const override;
8487
void setUnknownPenalty(float unknown);
8588
float unknownPenalty() const;
8689

0 commit comments

Comments
 (0)