summaryrefslogtreecommitdiffstats
path: root/src/storage/pinyin_phrase.h
blob: b9a88126beb15ea984f471f35442b09c4a3fd59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2006-2007 Peng Wu
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef PINYIN_PHRASE_H
#define PINYIN_PHRASE_H

#include <string.h>
#include "stl_lite.h"

namespace pinyin{

inline int pinyin_exact_compare(const PinyinKey key_lhs[], 
				const PinyinKey key_rhs[],
				int phrase_length){
  int i;
  int result;
  for ( i = 0 ; i < phrase_length ; i++){
    result = key_lhs[i].m_initial - key_rhs[i].m_initial;
    if ( result != 0 )
      return result;
  }
  for( i = 0 ; i < phrase_length ; i++){
    result = key_lhs[i].m_final - key_rhs[i].m_final;
    if ( result != 0 )
      return result;
  }
  for( i = 0 ; i < phrase_length ; i++){
    result = key_lhs[i].m_tone - key_rhs[i].m_tone;
    if ( result != 0 )
      return result;
  }
  return 0;
}


inline int pinyin_compare_with_ambiguities(const PinyinCustomSettings &custom,
					   const PinyinKey* key_lhs,
					   const PinyinKey* key_rhs,
					   int phrase_length){
    int i;
    int result;
    for ( i = 0 ; i < phrase_length ; i++){
	result = pinyin_compare_initial
	    (custom, 
	     (PinyinInitial)key_lhs[i].m_initial, 
	     (PinyinInitial)key_rhs[i].m_initial);
	if ( result != 0 )
	    return result;
    }
    for( i = 0 ; i < phrase_length ; i++){
	result = pinyin_compare_final
	    (custom, 
	     (PinyinFinal)key_lhs[i].m_final, 
	     (PinyinFinal)key_rhs[i].m_final);
	if ( result != 0 )
	    return result;
    }
    for( i = 0 ; i < phrase_length ; i++){
	result = pinyin_compare_tone
	    (custom,
	     (PinyinTone)key_lhs[i].m_tone,
	     (PinyinTone)key_rhs[i].m_tone);
	if ( result != 0 )
	    return result;
    }
    return 0;
}

//compute pinyin lower bound
//maybe replace by table lookup
inline void compute_lower_value(const PinyinCustomSettings &custom,
				PinyinKey in_keys[], 
				PinyinKey out_keys[], 
				int phrase_length){
    PinyinKey aKey = in_keys[0];
    
    for ( int i = 0; i < phrase_length; i++){
	int k; int sel;
	aKey = in_keys[i];
	//deal with initial
	sel = aKey.m_initial;
	for( k = aKey.m_initial - 1; k >= PINYIN_ZeroInitial; k--){
	    if ( 0 != pinyin_compare_initial
                 (custom, (PinyinInitial)aKey.m_initial, (PinyinInitial)k) )
		break;
	    else
		sel = k;
	}
	aKey.m_initial = (PinyinInitial)sel;
	//deal with final
	sel = aKey.m_final;
	for( k = aKey.m_final - 1; k >= PINYIN_ZeroFinal; k--){
	    if ( 0 != pinyin_compare_final
                 (custom, (PinyinFinal)aKey.m_final, (PinyinFinal)k) )
		break;
	    else
		sel = k;
	}
	aKey.m_final = (PinyinFinal)sel;
	//deal with tone
	sel = aKey.m_tone;
	for( k = aKey.m_tone - 1; k >= PINYIN_ZeroTone; k--){
	    if ( 0 != pinyin_compare_tone
                 (custom, (PinyinTone)aKey.m_tone, (PinyinTone)k) )
		break;
	    else
	    sel = k;
	}
	aKey.m_tone = (PinyinTone)sel;
	//save the result
	out_keys[i] = aKey;
    }
}

//compute pinyin upper bound
//maybe replace by table lookup
inline void compute_upper_value(const PinyinCustomSettings &custom,
				PinyinKey in_keys[], 
				PinyinKey out_keys[],
				int phrase_length){
    PinyinKey aKey = in_keys[0];
    
    for ( int i = 0; i < phrase_length; i++){
	int k; int sel;
	aKey = in_keys[i];
	//deal with initial
	sel = aKey.m_initial;
	for( k = aKey.m_initial + 1; k <= PINYIN_LastInitial; k++){
	    if ( 0 != pinyin_compare_initial
                 (custom, (PinyinInitial)aKey.m_initial, (PinyinInitial)k) )
		break;
	    else
		sel = k;
	}
	aKey.m_initial = (PinyinInitial)sel;
	//deal with final
	sel = aKey.m_final;
	for( k = aKey.m_final + 1; k <= PINYIN_LastFinal; k++){
	    if ( 0 != pinyin_compare_final
                 (custom, (PinyinFinal)aKey.m_final, (PinyinFinal)k) )
		break;
	    else
		sel = k;
	}
	aKey.m_final = (PinyinFinal)sel;
	//deal with tone
	sel = aKey.m_tone;
	for( k = aKey.m_tone + 1; k <= PINYIN_LastTone; k++){
	    if ( 0 != pinyin_compare_tone
                 (custom, (PinyinTone)aKey.m_tone, (PinyinTone)k) )
		break;
	    else
		sel = k;
	}
	aKey.m_tone = (PinyinTone)sel;
	//save the result
	out_keys[i] = aKey;
    }
}

template<size_t phrase_length>
struct PinyinIndexItem{
    phrase_token_t m_token;
    PinyinKey m_keys[phrase_length];
public:
    PinyinIndexItem<phrase_length>(PinyinKey * keys, phrase_token_t token){
	memmove(m_keys, keys, sizeof(PinyinKey) * phrase_length);
	m_token = token;
    }
};


//for find the element in the phrase array
template<int phrase_length>
inline int phrase_exact_compare(const PinyinIndexItem<phrase_length> &lhs,
                                const PinyinIndexItem<phrase_length> &rhs)
{
    PinyinKey * key_lhs = (PinyinKey *) lhs.m_keys;
    PinyinKey * key_rhs = (PinyinKey *) rhs.m_keys;
    return pinyin_exact_compare(key_lhs, key_rhs, phrase_length);
}

template<int phrase_length>
inline bool phrase_exact_less_than(const PinyinIndexItem<phrase_length> &lhs,
                                   const PinyinIndexItem<phrase_length> &rhs)
{
    return 0 > phrase_exact_compare<phrase_length>(lhs, rhs);
}


#if 0

template<int phrase_length>
class PhraseExactCompare
  : public std_lite::binary_function <const PinyinIndexItem<phrase_length>
				 ,const PinyinIndexItem<phrase_length>, int>
{
public:
  int operator () (const PinyinIndexItem<phrase_length> &lhs,
		   const PinyinIndexItem<phrase_length> &rhs) const{
    PinyinKey * key_lhs = (PinyinKey *) lhs.m_keys;
    PinyinKey * key_rhs = (PinyinKey *) rhs.m_keys;
    
    return pinyin_exact_compare(key_lhs, key_rhs, phrase_length);
  }
};


template<int phrase_length>
class PhraseExactLessThan
    : public std_lite::binary_function <const PinyinIndexItem<phrase_length>
				   ,const PinyinIndexItem<phrase_length>,
    bool>
{
 private:
  PhraseExactCompare<phrase_length> m_compare;
 public:
  bool operator () (const PinyinIndexItem<phrase_length> &lhs,
		   const PinyinIndexItem<phrase_length> &rhs) const{
    return 0 > m_compare(lhs, rhs);
  }
};

#endif

};

#endif