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