• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 #ifndef LATINIME_DEFINES_H
18 #define LATINIME_DEFINES_H
19 
20 #include <cstdint>
21 
22 #ifdef __GNUC__
23 #define AK_FORCE_INLINE __attribute__((always_inline)) __inline__
24 #else // __GNUC__
25 #define AK_FORCE_INLINE inline
26 #endif // __GNUC__
27 
28 #if defined(FLAG_DBG)
29 #undef AK_FORCE_INLINE
30 #define AK_FORCE_INLINE inline
31 #endif // defined(FLAG_DBG)
32 
33 // Must be equal to Constants.Dictionary.MAX_WORD_LENGTH in Java
34 #define MAX_WORD_LENGTH 48
35 // Must be equal to BinaryDictionary.MAX_RESULTS in Java
36 #define MAX_RESULTS 18
37 // Must be equal to ProximityInfo.MAX_PROXIMITY_CHARS_SIZE in Java
38 #define MAX_PROXIMITY_CHARS_SIZE 16
39 #define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2
40 
41 // TODO: Use size_t instead of int.
42 // Disclaimer: You will see a compile error if you use this macro against a variable-length array.
43 // Sorry for the inconvenience. It isn't supported.
44 template <typename T, int N>
45 char (&ArraySizeHelper(T (&array)[N]))[N];
46 #define NELEMS(x) (sizeof(ArraySizeHelper(x)))
47 
intArrayToCharArray(const int * const source,const int sourceSize,char * dest,const int destSize)48 AK_FORCE_INLINE static int intArrayToCharArray(const int *const source, const int sourceSize,
49         char *dest, const int destSize) {
50     // We want to always terminate with a 0 char, so stop one short of the length to make
51     // sure there is room.
52     const int destLimit = destSize - 1;
53     int si = 0;
54     int di = 0;
55     while (si < sourceSize && di < destLimit && 0 != source[si]) {
56         const uint32_t codePoint = static_cast<uint32_t>(source[si++]);
57         if (codePoint < 0x7F) { // One byte
58             dest[di++] = codePoint;
59         } else if (codePoint < 0x7FF) { // Two bytes
60             if (di + 1 >= destLimit) break;
61             dest[di++] = 0xC0 + (codePoint >> 6);
62             dest[di++] = 0x80 + (codePoint & 0x3F);
63         } else if (codePoint < 0xFFFF) { // Three bytes
64             if (di + 2 >= destLimit) break;
65             dest[di++] = 0xE0 + (codePoint >> 12);
66             dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
67             dest[di++] = 0x80 + (codePoint & 0x3F);
68         } else if (codePoint <= 0x1FFFFF) { // Four bytes
69             if (di + 3 >= destLimit) break;
70             dest[di++] = 0xF0 + (codePoint >> 18);
71             dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
72             dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
73             dest[di++] = 0x80 + (codePoint & 0x3F);
74         } else if (codePoint <= 0x3FFFFFF) { // Five bytes
75             if (di + 4 >= destLimit) break;
76             dest[di++] = 0xF8 + (codePoint >> 24);
77             dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
78             dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
79             dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
80             dest[di++] = codePoint & 0x3F;
81         } else if (codePoint <= 0x7FFFFFFF) { // Six bytes
82             if (di + 5 >= destLimit) break;
83             dest[di++] = 0xFC + (codePoint >> 30);
84             dest[di++] = 0x80 + ((codePoint >> 24) & 0x3F);
85             dest[di++] = 0x80 + ((codePoint >> 18) & 0x3F);
86             dest[di++] = 0x80 + ((codePoint >> 12) & 0x3F);
87             dest[di++] = 0x80 + ((codePoint >> 6) & 0x3F);
88             dest[di++] = codePoint & 0x3F;
89         } else {
90             // Not a code point... skip.
91         }
92     }
93     dest[di] = 0;
94     return di;
95 }
96 
97 #if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
98 #if defined(__ANDROID__)
99 #include <android/log.h>
100 #endif // defined(__ANDROID__)
101 #ifndef LOG_TAG
102 #define LOG_TAG "LatinIME: "
103 #endif // LOG_TAG
104 
105 #if defined(HOST_TOOL)
106 #include <stdio.h>
107 #define AKLOGE(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
108 #define AKLOGI(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
109 #else // defined(HOST_TOOL)
110 #define AKLOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__)
111 #define AKLOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__)
112 #endif // defined(HOST_TOOL)
113 
114 #define DUMP_SUGGESTION(words, frequencies, index, score) \
115         do { dumpWordInfo(words, frequencies, index, score); } while (0)
116 #define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0)
117 #define INTS_TO_CHARS(input, length, output, outlength) do { \
118         intArrayToCharArray(input, length, output, outlength); } while (0)
119 
dumpWordInfo(const int * word,const int length,const int rank,const int probability)120 static inline void dumpWordInfo(const int *word, const int length, const int rank,
121         const int probability) {
122     static char charBuf[50];
123     const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf));
124     if (N > 0) {
125         AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability);
126     }
127 }
128 
dumpWord(const int * word,const int length)129 static AK_FORCE_INLINE void dumpWord(const int *word, const int length) {
130     static char charBuf[50];
131     const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf));
132     if (N > 1) {
133         AKLOGI("[ %s ]", charBuf);
134     }
135 }
136 
137 #ifndef __ANDROID__
138 #include <cassert>
139 #include <execinfo.h>
140 #include <stdlib.h>
141 
142 #define DO_ASSERT_TEST
143 #define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0)
144 #define SHOW_STACK_TRACE do { showStackTrace(); } while (0)
145 
showStackTrace()146 static inline void showStackTrace() {
147     void *callstack[128];
148     int i, frames = backtrace(callstack, 128);
149     char **strs = backtrace_symbols(callstack, frames);
150     for (i = 0; i < frames; ++i) {
151         if (i == 0) {
152             AKLOGI("=== Trace ===");
153             continue;
154         }
155         AKLOGI("%s", strs[i]);
156     }
157     free(strs);
158 }
159 #else // __ANDROID__
160 #include <cassert>
161 #define DO_ASSERT_TEST
162 #define ASSERT(success) assert(success)
163 #define SHOW_STACK_TRACE
164 #endif // __ANDROID__
165 
166 #else // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
167 #define AKLOGE(fmt, ...)
168 #define AKLOGI(fmt, ...)
169 #define DUMP_SUGGESTION(words, frequencies, index, score)
170 #define DUMP_WORD(word, length)
171 #undef DO_ASSERT_TEST
172 #define ASSERT(success)
173 #define SHOW_STACK_TRACE
174 #define INTS_TO_CHARS(input, length, output)
175 #endif // defined(FLAG_DO_PROFILE) || defined(FLAG_DBG)
176 
177 #ifdef FLAG_DBG
178 #define DEBUG_DICT true
179 #define DEBUG_DICT_FULL false
180 #define DEBUG_EDIT_DISTANCE false
181 #define DEBUG_NODE DEBUG_DICT_FULL
182 #define DEBUG_TRACE DEBUG_DICT_FULL
183 #define DEBUG_PROXIMITY_INFO false
184 #define DEBUG_PROXIMITY_CHARS false
185 #define DEBUG_CORRECTION false
186 #define DEBUG_CORRECTION_FREQ false
187 #define DEBUG_SAMPLING_POINTS false
188 #define DEBUG_POINTS_PROBABILITY false
189 #define DEBUG_DOUBLE_LETTER false
190 #define DEBUG_CACHE false
191 #define DEBUG_DUMP_ERROR false
192 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
193 
194 #ifdef FLAG_FULL_DBG
195 #define DEBUG_GEO_FULL true
196 #else
197 #define DEBUG_GEO_FULL false
198 #endif
199 
200 #else // FLAG_DBG
201 
202 #define DEBUG_DICT false
203 #define DEBUG_DICT_FULL false
204 #define DEBUG_EDIT_DISTANCE false
205 #define DEBUG_NODE false
206 #define DEBUG_TRACE false
207 #define DEBUG_PROXIMITY_INFO false
208 #define DEBUG_PROXIMITY_CHARS false
209 #define DEBUG_CORRECTION false
210 #define DEBUG_CORRECTION_FREQ false
211 #define DEBUG_SAMPLING_POINTS false
212 #define DEBUG_POINTS_PROBABILITY false
213 #define DEBUG_DOUBLE_LETTER false
214 #define DEBUG_CACHE false
215 #define DEBUG_DUMP_ERROR false
216 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false
217 
218 #define DEBUG_GEO_FULL false
219 
220 #endif // FLAG_DBG
221 
222 #ifndef S_INT_MAX
223 #define S_INT_MAX 2147483647 // ((1 << 31) - 1)
224 #endif
225 #ifndef S_INT_MIN
226 // The literal constant -2147483648 does not work in C prior C90, because
227 // the compiler tries to fit the positive number into an int and then negate it.
228 // GCC warns about this.
229 #define S_INT_MIN (-2147483647 - 1) // -(1 << 31)
230 #endif
231 
232 #define M_PI_F 3.14159265f
233 #define MAX_PERCENTILE 100
234 
235 #define NOT_A_CODE_POINT (-1)
236 #define NOT_A_DISTANCE (-1)
237 #define NOT_A_COORDINATE (-1)
238 #define NOT_AN_INDEX (-1)
239 #define NOT_A_PROBABILITY (-1)
240 #define NOT_A_DICT_POS (S_INT_MIN)
241 #define NOT_A_WORD_ID (S_INT_MIN)
242 #define NOT_A_TIMESTAMP (-1)
243 #define NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL (-1.0f)
244 
245 // A special value to mean the first word confidence makes no sense in this case,
246 // e.g. this is not a multi-word suggestion.
247 #define NOT_A_FIRST_WORD_CONFIDENCE (S_INT_MIN)
248 // How high the confidence needs to be for us to auto-commit. Arbitrary.
249 // This needs to be the same as CONFIDENCE_FOR_AUTO_COMMIT in BinaryDictionary.java
250 #define CONFIDENCE_FOR_AUTO_COMMIT (1000000)
251 // 80% of the full confidence
252 #define DISTANCE_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100)
253 // 100% of the full confidence
254 #define LENGTH_WEIGHT_FOR_AUTO_COMMIT (CONFIDENCE_FOR_AUTO_COMMIT)
255 // 80% of the full confidence
256 #define SPACE_COUNT_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100)
257 
258 #define KEYCODE_SPACE ' '
259 #define KEYCODE_SINGLE_QUOTE '\''
260 #define KEYCODE_HYPHEN_MINUS '-'
261 // Code point to indicate beginning-of-sentence. This is not in the code point space of unicode.
262 #define CODE_POINT_BEGINNING_OF_SENTENCE 0x110000
263 
264 #define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f
265 #define MAX_PROBABILITY 255
266 #define MAX_BIGRAM_ENCODED_PROBABILITY 15
267 
268 // Max value for length, distance and probability which are used in weighting
269 // TODO: Remove
270 #define MAX_VALUE_FOR_WEIGHTING 10000000
271 
272 // The max number of the keys in one keyboard layout
273 #define MAX_KEY_COUNT_IN_A_KEYBOARD 64
274 
275 // TODO: Remove
276 #define MAX_POINTER_COUNT 1
277 #define MAX_POINTER_COUNT_G 2
278 
279 // (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram is supported.
280 #define MAX_PREV_WORD_COUNT_FOR_N_GRAM 3
281 
282 #define DISALLOW_DEFAULT_CONSTRUCTOR(TypeName) \
283   TypeName() = delete
284 
285 #define DISALLOW_COPY_CONSTRUCTOR(TypeName) \
286   TypeName(const TypeName&) = delete
287 
288 #define DISALLOW_ASSIGNMENT_OPERATOR(TypeName) \
289   void operator=(const TypeName&) = delete
290 
291 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
292   DISALLOW_COPY_CONSTRUCTOR(TypeName);     \
293   DISALLOW_ASSIGNMENT_OPERATOR(TypeName)
294 
295 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
296   DISALLOW_DEFAULT_CONSTRUCTOR(TypeName);        \
297   DISALLOW_COPY_AND_ASSIGN(TypeName)
298 
299 // Used as a return value for character comparison
300 typedef enum {
301     // Same char, possibly with different case or accent
302     MATCH_CHAR,
303     // It is a char located nearby on the keyboard
304     PROXIMITY_CHAR,
305     // Additional proximity char which can differ by language.
306     ADDITIONAL_PROXIMITY_CHAR,
307     // It is a substitution char
308     SUBSTITUTION_CHAR,
309     // It is an unrelated char
310     UNRELATED_CHAR,
311 } ProximityType;
312 
313 typedef enum {
314     NOT_A_DOUBLE_LETTER,
315     A_DOUBLE_LETTER,
316     A_STRONG_DOUBLE_LETTER
317 } DoubleLetterLevel;
318 
319 typedef enum {
320     // Correction for MATCH_CHAR
321     CT_MATCH,
322     // Correction for PROXIMITY_CHAR
323     CT_PROXIMITY,
324     // Correction for ADDITIONAL_PROXIMITY_CHAR
325     CT_ADDITIONAL_PROXIMITY,
326     // Correction for SUBSTITUTION_CHAR
327     CT_SUBSTITUTION,
328     // Skip one omitted letter
329     CT_OMISSION,
330     // Delete an unnecessarily inserted letter
331     CT_INSERTION,
332     // Swap the order of next two touch points
333     CT_TRANSPOSITION,
334     CT_COMPLETION,
335     CT_TERMINAL,
336     CT_TERMINAL_INSERTION,
337     // Create new word with space omission
338     CT_NEW_WORD_SPACE_OMISSION,
339     // Create new word with space substitution
340     CT_NEW_WORD_SPACE_SUBSTITUTION,
341 } CorrectionType;
342 #endif // LATINIME_DEFINES_H
343