libime
pinyindata.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_PINYIN_PINYINDATA_H_
7 #define _FCITX_LIBIME_PINYIN_PINYINDATA_H_
8 
9 #include <cstddef>
10 #include <string>
11 #include <string_view>
12 #include <unordered_map>
13 #include <utility>
14 #include <vector>
15 #include <boost/container_hash/hash.hpp>
16 #include <boost/multi_index/hashed_index.hpp>
17 #include <boost/multi_index/indexed_by.hpp>
18 #include <boost/multi_index/mem_fun.hpp>
19 #include <boost/multi_index_container.hpp>
20 #include <libime/pinyin/libimepinyin_export.h>
21 #include <libime/pinyin/pinyinencoder.h>
22 
23 namespace libime {
24 struct LIBIMEPINYIN_EXPORT PinyinHash {
25  std::size_t operator()(std::string_view const &val) const {
26  return boost::hash_range(val.begin(), val.end());
27  }
28 };
29 
30 class LIBIMEPINYIN_EXPORT PinyinEntry {
31 public:
32  PinyinEntry(const char *pinyin, PinyinInitial initial, PinyinFinal final,
33  PinyinFuzzyFlags flags)
34  : pinyin_(pinyin), initial_(initial), final_(final), flags_(flags) {}
35 
36  std::string_view pinyinView() const { return pinyin_; }
37  constexpr const std::string &pinyin() const { return pinyin_; }
38  constexpr PinyinInitial initial() const { return initial_; }
39  constexpr PinyinFinal final() const { return final_; }
40  constexpr PinyinFuzzyFlags flags() const { return flags_; }
41 
42 private:
43  std::string pinyin_;
44  PinyinInitial initial_;
45  PinyinFinal final_;
46  PinyinFuzzyFlags flags_;
47 };
48 
49 using PinyinMap = boost::multi_index_container<
51  boost::multi_index::indexed_by<boost::multi_index::hashed_non_unique<
52  boost::multi_index::const_mem_fun<PinyinEntry, std::string_view,
53  &PinyinEntry::pinyinView>,
54  PinyinHash>>>;
55 
56 LIBIMEPINYIN_EXPORT const PinyinMap &getPinyinMap();
57 LIBIMEPINYIN_EXPORT const PinyinMap &getPinyinMapV2();
58 LIBIMEPINYIN_EXPORT const std::vector<bool> &getEncodedInitialFinal();
59 
60 LIBIMEPINYIN_EXPORT const
61  std::unordered_map<std::string, std::pair<std::string, std::string>> &
62  getInnerSegment();
63 } // namespace libime
64 
65 #endif // _FCITX_LIBIME_PINYIN_PINYINDATA_H_