fixes phrase_index.h
[platform/upstream/libpinyin.git] / src / storage / pinyin_phrase2.h
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #ifndef PINYIN_PHRASE2_H
23 #define PINYIN_PHRASE2_H
24
25 #include "novel_types.h"
26 #include "chewing_key.h"
27 #include "pinyin_custom2.h"
28 #include "pinyin_parser2.h"
29
30 namespace pinyin{
31
32 inline int pinyin_exact_compare2(const ChewingKey * key_lhs,
33                                  const ChewingKey * key_rhs,
34                                  int phrase_length){
35     int i;
36     int result;
37
38     /* compare initial */
39     for (i = 0; i < phrase_length; ++i) {
40         result = key_lhs[i].m_initial - key_rhs[i].m_initial;
41         if (0 != result)
42             return result;
43     }
44
45     /* compare middle and final */
46     for (i = 0; i < phrase_length; ++i) {
47         result = key_lhs[i].m_middle - key_rhs[i].m_middle;
48         if (0 != result)
49             return result;
50         result = key_lhs[i].m_final - key_rhs[i].m_final;
51         if (0 != result)
52             return result;
53     }
54
55     /* compare tone */
56     for (i = 0; i < phrase_length; ++i) {
57         result = key_lhs[i].m_tone - key_rhs[i].m_tone;
58         if (0 != result)
59             return result;
60     }
61
62     return 0;
63 }
64
65
66 inline int pinyin_compare_with_ambiguities2(pinyin_option_t options,
67                                             const ChewingKey * key_lhs,
68                                             const ChewingKey * key_rhs,
69                                             int phrase_length){
70     int i;
71     int result;
72
73     /* compare initial */
74     for (i = 0; i < phrase_length; ++i) {
75         result = pinyin_compare_initial2
76             (options,
77              (ChewingInitial)key_lhs[i].m_initial,
78              (ChewingInitial)key_rhs[i].m_initial);
79         if (0 != result)
80             return result;
81     }
82
83     /* compare middle and final */
84     for (i = 0; i < phrase_length; ++i) {
85         result = pinyin_compare_middle_and_final2
86             (options,
87              (ChewingMiddle)key_lhs[i].m_middle,
88              (ChewingMiddle)key_rhs[i].m_middle,
89              (ChewingFinal) key_lhs[i].m_final,
90              (ChewingFinal) key_rhs[i].m_final);
91         if (0 != result)
92             return result;
93     }
94
95     /* compare tone */
96     for (i = 0; i < phrase_length; ++i) {
97         result = pinyin_compare_tone2
98             (options,
99              (ChewingTone)key_lhs[i].m_tone,
100              (ChewingTone)key_rhs[i].m_tone);
101         if (0 != result)
102             return result;
103     }
104
105     return 0;
106 }
107
108 /* compute pinyin lower bound */
109 inline void compute_lower_value2(pinyin_option_t options,
110                                   ChewingKey * in_keys,
111                                   ChewingKey * out_keys,
112                                   int phrase_length) {
113     ChewingKey aKey;
114
115     for (int i = 0; i < phrase_length; ++i) {
116         int k; int sel;
117         aKey = in_keys[i];
118
119         /* compute lower initial */
120         sel = aKey.m_initial;
121         for (k = aKey.m_initial - 1; k >= CHEWING_ZERO_INITIAL; --k) {
122             if (0 != pinyin_compare_initial2
123                 (options, (ChewingInitial)aKey.m_initial, (ChewingInitial)k))
124                 break;
125             else
126                 sel = k;
127         }
128         aKey.m_initial = (ChewingInitial)sel;
129
130         /* compute lower middle, skipped as no fuzzy pinyin here.
131          * if needed in future, still use pinyin_compare_middle_and_final2
132          * to check lower bound.
133          */
134
135         /* compute lower final */
136         sel = aKey.m_final;
137         for (k = aKey.m_final - 1; k >= CHEWING_ZERO_FINAL; --k) {
138             if (0 != pinyin_compare_middle_and_final2
139                 (options,
140                  (ChewingMiddle)aKey.m_middle, (ChewingMiddle) aKey.m_middle,
141                  (ChewingFinal)aKey.m_final, (ChewingFinal)k))
142                 break;
143             else
144                 sel = k;
145         }
146         aKey.m_final = (ChewingFinal)sel;
147
148         /* compute lower tone */
149         sel = aKey.m_tone;
150         for (k = aKey.m_tone - 1; k >= CHEWING_ZERO_TONE; --k) {
151             if (0 != pinyin_compare_tone2
152                 (options, (ChewingTone)aKey.m_tone, (ChewingTone)k))
153                 break;
154             else
155                 sel = k;
156         }
157         aKey.m_tone = (ChewingTone)sel;
158
159         /* save the result */
160         out_keys[i] = aKey;
161     }
162 }
163
164 /* compute pinyin upper bound */
165 inline void compute_upper_value2(pinyin_option_t options,
166                                  ChewingKey * in_keys,
167                                  ChewingKey * out_keys,
168                                  int phrase_length) {
169     ChewingKey aKey;
170
171     for (int i = 0; i < phrase_length; ++i) {
172         int k; int sel;
173         aKey = in_keys[i];
174
175         /* compute upper initial */
176         sel = aKey.m_initial;
177         for (k = aKey.m_initial + 1; k <= CHEWING_LAST_INITIAL; ++k) {
178             if (0 != pinyin_compare_initial2
179                 (options, (ChewingInitial)aKey.m_initial, (ChewingInitial)k))
180                 break;
181             else
182                 sel = k;
183         }
184         aKey.m_initial = (ChewingInitial)sel;
185
186         /* compute upper middle, skipped as no fuzzy pinyin here.
187          * if needed in future, still use pinyin_compare_middle_and_final2
188          * to check upper bound.
189          */
190
191         /* compute upper final */
192         sel = aKey.m_final;
193         for (k = aKey.m_final + 1; k <= CHEWING_LAST_FINAL; ++k) {
194             if (0 != pinyin_compare_middle_and_final2
195                 (options,
196                  (ChewingMiddle)aKey.m_middle, (ChewingMiddle)aKey.m_middle,
197                  (ChewingFinal)aKey.m_final, (ChewingFinal)k))
198                 break;
199             else
200                 sel = k;
201         }
202         aKey.m_final = (ChewingFinal)sel;
203
204         /* compute upper tone */
205         sel = aKey.m_tone;
206         for (k = aKey.m_tone + 1; k <= CHEWING_LAST_TONE; ++k) {
207             if (0 != pinyin_compare_tone2
208                 (options, (ChewingTone)aKey.m_tone, (ChewingTone)k))
209                 break;
210             else
211                 sel = k;
212         }
213         aKey.m_tone = (ChewingTone)sel;
214
215         /* save the result */
216         out_keys[i] = aKey;
217     }
218 }
219
220
221 template<size_t phrase_length>
222 struct PinyinIndexItem2{
223     phrase_token_t m_token;
224     ChewingKey m_keys[phrase_length];
225 public:
226     PinyinIndexItem2<phrase_length> (ChewingKey * keys, phrase_token_t token) {
227         memmove(m_keys, keys, sizeof(ChewingKey) * phrase_length);
228         m_token = token;
229     }
230 };
231
232
233 /* for find the element in the phrase array */
234 template<size_t phrase_length>
235 inline int phrase_exact_compare2(const PinyinIndexItem2<phrase_length> &lhs,
236                                  const PinyinIndexItem2<phrase_length> &rhs)
237 {
238     ChewingKey * keys_lhs = (ChewingKey *) lhs.m_keys;
239     ChewingKey * keys_rhs = (ChewingKey *) rhs.m_keys;
240     return pinyin_exact_compare2(keys_lhs, keys_rhs, phrase_length);
241 }
242
243 template<size_t phrase_length>
244 inline bool phrase_exact_less_than2(const PinyinIndexItem2<phrase_length> &lhs,
245                                     const PinyinIndexItem2<phrase_length> &rhs)
246 {
247     return 0 > phrase_exact_compare2<phrase_length>(lhs, rhs);
248 }
249
250 };
251
252 #endif