OLD | NEW |
(Empty) | |
| 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 "base/macros.h" |
| 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_DO_PROFILE) || defined(FLAG_DBG) |
| 29 #undef AK_FORCE_INLINE |
| 30 #define AK_FORCE_INLINE inline |
| 31 #endif // defined(FLAG_DO_PROFILE) || 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 vari
able-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 |
| 48 AK_FORCE_INLINE static int intArrayToCharArray(const int *const source, const in
t sourceSize, |
| 49 char *dest, const int destSize) { |
| 50 // We want to always terminate with a 0 char, so stop one short of the lengt
h 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 int codePoint = 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 |
| 120 static inline void dumpWordInfo(const int *word, const int length, const int ran
k, |
| 121 const int probability) { |
| 122 static char charBuf[50]; |
| 123 const int N = intArrayToCharArray(word, length, charBuf, NELEMS(charBuf)); |
| 124 if (N > 1) { |
| 125 AKLOGI("%2d [ %s ] (%d)", rank, charBuf, probability); |
| 126 } |
| 127 } |
| 128 |
| 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 |
| 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_DO_PROFILE |
| 178 // Profiler |
| 179 #include <time.h> |
| 180 |
| 181 #define PROF_BUF_SIZE 100 |
| 182 static float profile_buf[PROF_BUF_SIZE]; |
| 183 static float profile_old[PROF_BUF_SIZE]; |
| 184 static unsigned int profile_counter[PROF_BUF_SIZE]; |
| 185 |
| 186 #define PROF_RESET prof_reset() |
| 187 #define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id] |
| 188 #define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1);
} while (0) |
| 189 #define PROF_START(prof_buf_id) do { \ |
| 190 PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while (
0) |
| 191 #define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL;
} while (0) |
| 192 #define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profil
e_old[prof_buf_id]) |
| 193 #define PROF_CLOCKOUT(prof_buf_id) \ |
| 194 AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf
_id])) |
| 195 #define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_o
ut(); } while (0) |
| 196 |
| 197 static inline void prof_reset(void) { |
| 198 for (int i = 0; i < PROF_BUF_SIZE; ++i) { |
| 199 profile_buf[i] = 0; |
| 200 profile_old[i] = 0; |
| 201 profile_counter[i] = 0; |
| 202 } |
| 203 } |
| 204 |
| 205 static inline void prof_out(void) { |
| 206 if (profile_counter[PROF_BUF_SIZE - 1] != 1) { |
| 207 AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE."); |
| 208 } |
| 209 AKLOGI("Total time is %6.3f ms.", |
| 210 profile_buf[PROF_BUF_SIZE - 1] * 1000.0f / static_cast<float>(CLOCKS
_PER_SEC)); |
| 211 float all = 0.0f; |
| 212 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { |
| 213 all += profile_buf[i]; |
| 214 } |
| 215 if (all < 1.0f) all = 1.0f; |
| 216 for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { |
| 217 if (profile_buf[i] > 0.0f) { |
| 218 AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.", |
| 219 i, (profile_buf[i] * 100.0f / all), |
| 220 profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC
), |
| 221 profile_counter[i]); |
| 222 } |
| 223 } |
| 224 } |
| 225 |
| 226 #else // FLAG_DO_PROFILE |
| 227 #define PROF_BUF_SIZE 0 |
| 228 #define PROF_RESET |
| 229 #define PROF_COUNT(prof_buf_id) |
| 230 #define PROF_OPEN |
| 231 #define PROF_START(prof_buf_id) |
| 232 #define PROF_CLOSE |
| 233 #define PROF_END(prof_buf_id) |
| 234 #define PROF_CLOCK_OUT(prof_buf_id) |
| 235 #define PROF_CLOCKOUT(prof_buf_id) |
| 236 #define PROF_OUTALL |
| 237 |
| 238 #endif // FLAG_DO_PROFILE |
| 239 |
| 240 #ifdef FLAG_DBG |
| 241 #define DEBUG_DICT true |
| 242 #define DEBUG_DICT_FULL false |
| 243 #define DEBUG_EDIT_DISTANCE false |
| 244 #define DEBUG_NODE DEBUG_DICT_FULL |
| 245 #define DEBUG_TRACE DEBUG_DICT_FULL |
| 246 #define DEBUG_PROXIMITY_INFO false |
| 247 #define DEBUG_PROXIMITY_CHARS false |
| 248 #define DEBUG_CORRECTION false |
| 249 #define DEBUG_CORRECTION_FREQ false |
| 250 #define DEBUG_SAMPLING_POINTS false |
| 251 #define DEBUG_POINTS_PROBABILITY false |
| 252 #define DEBUG_DOUBLE_LETTER false |
| 253 #define DEBUG_CACHE false |
| 254 #define DEBUG_DUMP_ERROR false |
| 255 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false |
| 256 |
| 257 #ifdef FLAG_FULL_DBG |
| 258 #define DEBUG_GEO_FULL true |
| 259 #else |
| 260 #define DEBUG_GEO_FULL false |
| 261 #endif |
| 262 |
| 263 #else // FLAG_DBG |
| 264 |
| 265 #define DEBUG_DICT false |
| 266 #define DEBUG_DICT_FULL false |
| 267 #define DEBUG_EDIT_DISTANCE false |
| 268 #define DEBUG_NODE false |
| 269 #define DEBUG_TRACE false |
| 270 #define DEBUG_PROXIMITY_INFO false |
| 271 #define DEBUG_PROXIMITY_CHARS false |
| 272 #define DEBUG_CORRECTION false |
| 273 #define DEBUG_CORRECTION_FREQ false |
| 274 #define DEBUG_SAMPLING_POINTS false |
| 275 #define DEBUG_POINTS_PROBABILITY false |
| 276 #define DEBUG_DOUBLE_LETTER false |
| 277 #define DEBUG_CACHE false |
| 278 #define DEBUG_DUMP_ERROR false |
| 279 #define DEBUG_EVALUATE_MOST_PROBABLE_STRING false |
| 280 |
| 281 #define DEBUG_GEO_FULL false |
| 282 |
| 283 #endif // FLAG_DBG |
| 284 |
| 285 #ifndef S_INT_MAX |
| 286 #define S_INT_MAX 2147483647 // ((1 << 31) - 1) |
| 287 #endif |
| 288 #ifndef S_INT_MIN |
| 289 // The literal constant -2147483648 does not work in C prior C90, because |
| 290 // the compiler tries to fit the positive number into an int and then negate it. |
| 291 // GCC warns about this. |
| 292 #define S_INT_MIN (-2147483647 - 1) // -(1 << 31) |
| 293 #endif |
| 294 |
| 295 #define M_PI_F 3.14159265f |
| 296 #define MAX_PERCENTILE 100 |
| 297 |
| 298 #define NOT_A_CODE_POINT (-1) |
| 299 #define NOT_A_DISTANCE (-1) |
| 300 #define NOT_A_COORDINATE (-1) |
| 301 #define NOT_AN_INDEX (-1) |
| 302 #define NOT_A_PROBABILITY (-1) |
| 303 #define NOT_A_DICT_POS (S_INT_MIN) |
| 304 #define NOT_A_TIMESTAMP (-1) |
| 305 #define NOT_A_LANGUAGE_WEIGHT (-1.0f) |
| 306 |
| 307 // A special value to mean the first word confidence makes no sense in this case
, |
| 308 // e.g. this is not a multi-word suggestion. |
| 309 #define NOT_A_FIRST_WORD_CONFIDENCE (S_INT_MIN) |
| 310 // How high the confidence needs to be for us to auto-commit. Arbitrary. |
| 311 // This needs to be the same as CONFIDENCE_FOR_AUTO_COMMIT in BinaryDictionary.j
ava |
| 312 #define CONFIDENCE_FOR_AUTO_COMMIT (1000000) |
| 313 // 80% of the full confidence |
| 314 #define DISTANCE_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 100) |
| 315 // 100% of the full confidence |
| 316 #define LENGTH_WEIGHT_FOR_AUTO_COMMIT (CONFIDENCE_FOR_AUTO_COMMIT) |
| 317 // 80% of the full confidence |
| 318 #define SPACE_COUNT_WEIGHT_FOR_AUTO_COMMIT (80 * CONFIDENCE_FOR_AUTO_COMMIT / 10
0) |
| 319 |
| 320 #define KEYCODE_SPACE ' ' |
| 321 #define KEYCODE_SINGLE_QUOTE '\'' |
| 322 #define KEYCODE_HYPHEN_MINUS '-' |
| 323 // Code point to indicate beginning-of-sentence. This is not in the code point s
pace of unicode. |
| 324 #define CODE_POINT_BEGINNING_OF_SENTENCE 0x110000 |
| 325 |
| 326 #define SUGGEST_INTERFACE_OUTPUT_SCALE 1000000.0f |
| 327 #define MAX_PROBABILITY 255 |
| 328 #define MAX_BIGRAM_ENCODED_PROBABILITY 15 |
| 329 |
| 330 // Max value for length, distance and probability which are used in weighting |
| 331 // TODO: Remove |
| 332 #define MAX_VALUE_FOR_WEIGHTING 10000000 |
| 333 |
| 334 // The max number of the keys in one keyboard layout |
| 335 #define MAX_KEY_COUNT_IN_A_KEYBOARD 64 |
| 336 |
| 337 // TODO: Remove |
| 338 #define MAX_POINTER_COUNT 1 |
| 339 #define MAX_POINTER_COUNT_G 2 |
| 340 |
| 341 // (MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1)-gram is supported. |
| 342 #define MAX_PREV_WORD_COUNT_FOR_N_GRAM 1 |
| 343 |
| 344 #define DISALLOW_DEFAULT_CONSTRUCTOR(TypeName) \ |
| 345 TypeName() = delete |
| 346 |
| 347 #define DISALLOW_COPY_CONSTRUCTOR(TypeName) \ |
| 348 TypeName(const TypeName&) = delete |
| 349 |
| 350 #define DISALLOW_ASSIGNMENT_OPERATOR(TypeName) \ |
| 351 void operator=(const TypeName&) = delete |
| 352 |
| 353 /* |
| 354 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 355 DISALLOW_COPY_CONSTRUCTOR(TypeName); \ |
| 356 DISALLOW_ASSIGNMENT_OPERATOR(TypeName) |
| 357 |
| 358 #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ |
| 359 DISALLOW_DEFAULT_CONSTRUCTOR(TypeName); \ |
| 360 DISALLOW_COPY_AND_ASSIGN(TypeName) |
| 361 */ |
| 362 |
| 363 // Used as a return value for character comparison |
| 364 typedef enum { |
| 365 // Same char, possibly with different case or accent |
| 366 MATCH_CHAR, |
| 367 // It is a char located nearby on the keyboard |
| 368 PROXIMITY_CHAR, |
| 369 // Additional proximity char which can differ by language. |
| 370 ADDITIONAL_PROXIMITY_CHAR, |
| 371 // It is a substitution char |
| 372 SUBSTITUTION_CHAR, |
| 373 // It is an unrelated char |
| 374 UNRELATED_CHAR, |
| 375 } ProximityType; |
| 376 |
| 377 typedef enum { |
| 378 NOT_A_DOUBLE_LETTER, |
| 379 A_DOUBLE_LETTER, |
| 380 A_STRONG_DOUBLE_LETTER |
| 381 } DoubleLetterLevel; |
| 382 |
| 383 typedef enum { |
| 384 // Correction for MATCH_CHAR |
| 385 CT_MATCH, |
| 386 // Correction for PROXIMITY_CHAR |
| 387 CT_PROXIMITY, |
| 388 // Correction for ADDITIONAL_PROXIMITY_CHAR |
| 389 CT_ADDITIONAL_PROXIMITY, |
| 390 // Correction for SUBSTITUTION_CHAR |
| 391 CT_SUBSTITUTION, |
| 392 // Skip one omitted letter |
| 393 CT_OMISSION, |
| 394 // Delete an unnecessarily inserted letter |
| 395 CT_INSERTION, |
| 396 // Swap the order of next two touch points |
| 397 CT_TRANSPOSITION, |
| 398 CT_COMPLETION, |
| 399 CT_TERMINAL, |
| 400 CT_TERMINAL_INSERTION, |
| 401 // Create new word with space omission |
| 402 CT_NEW_WORD_SPACE_OMISSION, |
| 403 // Create new word with space substitution |
| 404 CT_NEW_WORD_SPACE_SUBSTITUTION, |
| 405 } CorrectionType; |
| 406 #endif // LATINIME_DEFINES_H |
OLD | NEW |