1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <assert.h>
18 #include "../include/mystdlib.h"
19 #include "../include/searchutility.h"
20
21 namespace ime_pinyin {
22
is_system_lemma(LemmaIdType lma_id)23 bool is_system_lemma(LemmaIdType lma_id) {
24 return (0 < lma_id && lma_id <= kSysDictIdEnd);
25 }
26
is_user_lemma(LemmaIdType lma_id)27 bool is_user_lemma(LemmaIdType lma_id) {
28 return (kUserDictIdStart <= lma_id && lma_id <= kUserDictIdEnd);
29 }
30
is_composing_lemma(LemmaIdType lma_id)31 bool is_composing_lemma(LemmaIdType lma_id) {
32 return (kLemmaIdComposing == lma_id);
33 }
34
cmp_lpi_with_psb(const void * p1,const void * p2)35 int cmp_lpi_with_psb(const void *p1, const void *p2) {
36 if ((static_cast<const LmaPsbItem*>(p1))->psb >
37 (static_cast<const LmaPsbItem*>(p2))->psb)
38 return 1;
39 if ((static_cast<const LmaPsbItem*>(p1))->psb <
40 (static_cast<const LmaPsbItem*>(p2))->psb)
41 return -1;
42 return 0;
43 }
44
cmp_lpi_with_unified_psb(const void * p1,const void * p2)45 int cmp_lpi_with_unified_psb(const void *p1, const void *p2) {
46 const LmaPsbItem *item1 = static_cast<const LmaPsbItem*>(p1);
47 const LmaPsbItem *item2 = static_cast<const LmaPsbItem*>(p2);
48
49 // The real unified psb is psb1 / lma_len1 and psb2 * lma_len2
50 // But we use psb1 * lma_len2 and psb2 * lma_len1 to get better
51 // precision.
52 size_t up1 = item1->psb * (item2->lma_len);
53 size_t up2 = item2->psb * (item1->lma_len);
54 if (up1 < up2) {
55 return -1;
56 }
57 if (up1 > up2) {
58 return 1;
59 }
60 return 0;
61 }
62
cmp_lpi_with_id(const void * p1,const void * p2)63 int cmp_lpi_with_id(const void *p1, const void *p2) {
64 if ((static_cast<const LmaPsbItem*>(p1))->id <
65 (static_cast<const LmaPsbItem*>(p2))->id)
66 return -1;
67 if ((static_cast<const LmaPsbItem*>(p1))->id >
68 (static_cast<const LmaPsbItem*>(p2))->id)
69 return 1;
70 return 0;
71 }
72
cmp_lpi_with_hanzi(const void * p1,const void * p2)73 int cmp_lpi_with_hanzi(const void *p1, const void *p2) {
74 if ((static_cast<const LmaPsbItem*>(p1))->hanzi <
75 (static_cast<const LmaPsbItem*>(p2))->hanzi)
76 return -1;
77 if ((static_cast<const LmaPsbItem*>(p1))->hanzi >
78 (static_cast<const LmaPsbItem*>(p2))->hanzi)
79 return 1;
80
81 return 0;
82 }
83
cmp_lpsi_with_str(const void * p1,const void * p2)84 int cmp_lpsi_with_str(const void *p1, const void *p2) {
85 return utf16_strcmp((static_cast<const LmaPsbStrItem*>(p1))->str,
86 (static_cast<const LmaPsbStrItem*>(p2))->str);
87 }
88
89
cmp_hanzis_1(const void * p1,const void * p2)90 int cmp_hanzis_1(const void *p1, const void *p2) {
91 if (*static_cast<const char16*>(p1) <
92 *static_cast<const char16*>(p2))
93 return -1;
94
95 if (*static_cast<const char16*>(p1) >
96 *static_cast<const char16*>(p2))
97 return 1;
98 return 0;
99 }
100
cmp_hanzis_2(const void * p1,const void * p2)101 int cmp_hanzis_2(const void *p1, const void *p2) {
102 return utf16_strncmp(static_cast<const char16*>(p1),
103 static_cast<const char16*>(p2), 2);
104 }
105
cmp_hanzis_3(const void * p1,const void * p2)106 int cmp_hanzis_3(const void *p1, const void *p2) {
107 return utf16_strncmp(static_cast<const char16*>(p1),
108 static_cast<const char16*>(p2), 3);
109 }
110
cmp_hanzis_4(const void * p1,const void * p2)111 int cmp_hanzis_4(const void *p1, const void *p2) {
112 return utf16_strncmp(static_cast<const char16*>(p1),
113 static_cast<const char16*>(p2), 4);
114 }
115
cmp_hanzis_5(const void * p1,const void * p2)116 int cmp_hanzis_5(const void *p1, const void *p2) {
117 return utf16_strncmp(static_cast<const char16*>(p1),
118 static_cast<const char16*>(p2), 5);
119 }
120
cmp_hanzis_6(const void * p1,const void * p2)121 int cmp_hanzis_6(const void *p1, const void *p2) {
122 return utf16_strncmp(static_cast<const char16*>(p1),
123 static_cast<const char16*>(p2), 6);
124 }
125
cmp_hanzis_7(const void * p1,const void * p2)126 int cmp_hanzis_7(const void *p1, const void *p2) {
127 return utf16_strncmp(static_cast<const char16*>(p1),
128 static_cast<const char16*>(p2), 7);
129 }
130
cmp_hanzis_8(const void * p1,const void * p2)131 int cmp_hanzis_8(const void *p1, const void *p2) {
132 return utf16_strncmp(static_cast<const char16*>(p1),
133 static_cast<const char16*>(p2), 8);
134 }
135
cmp_npre_by_score(const void * p1,const void * p2)136 int cmp_npre_by_score(const void *p1, const void *p2) {
137 if ((static_cast<const NPredictItem*>(p1))->psb >
138 (static_cast<const NPredictItem*>(p2))->psb)
139 return 1;
140
141 if ((static_cast<const NPredictItem*>(p1))->psb <
142 (static_cast<const NPredictItem*>(p2))->psb)
143 return -1;
144
145 return 0;
146 }
147
cmp_npre_by_hislen_score(const void * p1,const void * p2)148 int cmp_npre_by_hislen_score(const void *p1, const void *p2) {
149 if ((static_cast<const NPredictItem*>(p1))->his_len <
150 (static_cast<const NPredictItem*>(p2))->his_len)
151 return 1;
152
153 if ((static_cast<const NPredictItem*>(p1))->his_len >
154 (static_cast<const NPredictItem*>(p2))->his_len)
155 return -1;
156
157 if ((static_cast<const NPredictItem*>(p1))->psb >
158 (static_cast<const NPredictItem*>(p2))->psb)
159 return 1;
160
161 if ((static_cast<const NPredictItem*>(p1))->psb <
162 (static_cast<const NPredictItem*>(p2))->psb)
163 return -1;
164
165 return 0;
166 }
167
cmp_npre_by_hanzi_score(const void * p1,const void * p2)168 int cmp_npre_by_hanzi_score(const void *p1, const void *p2) {
169 int ret_v = (utf16_strncmp((static_cast<const NPredictItem*>(p1))->pre_hzs,
170 (static_cast<const NPredictItem*>(p2))->pre_hzs, kMaxPredictSize));
171 if (0 != ret_v)
172 return ret_v;
173
174 if ((static_cast<const NPredictItem*>(p1))->psb >
175 (static_cast<const NPredictItem*>(p2))->psb)
176 return 1;
177
178 if ((static_cast<const NPredictItem*>(p1))->psb <
179 (static_cast<const NPredictItem*>(p2))->psb)
180 return -1;
181
182 return 0;
183 }
184
remove_duplicate_npre(NPredictItem * npre_items,size_t npre_num)185 size_t remove_duplicate_npre(NPredictItem *npre_items, size_t npre_num) {
186 if (NULL == npre_items || 0 == npre_num)
187 return 0;
188
189 myqsort(npre_items, npre_num, sizeof(NPredictItem), cmp_npre_by_hanzi_score);
190
191 size_t remain_num = 1; // The first one is reserved.
192 for (size_t pos = 1; pos < npre_num; pos++) {
193 if (utf16_strncmp(npre_items[pos].pre_hzs,
194 npre_items[remain_num - 1].pre_hzs,
195 kMaxPredictSize) != 0) {
196 if (remain_num != pos) {
197 npre_items[remain_num] = npre_items[pos];
198 }
199 remain_num++;
200 }
201 }
202 return remain_num;
203 }
204
align_to_size_t(size_t size)205 size_t align_to_size_t(size_t size) {
206 size_t s = sizeof(size_t);
207 return (size + s -1) / s * s;
208 }
209
210 } // namespace ime_pinyin
211