diff options
Diffstat (limited to 'native/jni/src/defines.h')
-rw-r--r-- | native/jni/src/defines.h | 230 |
1 files changed, 178 insertions, 52 deletions
diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h index cd2fc634a..ad526fb7f 100644 --- a/native/jni/src/defines.h +++ b/native/jni/src/defines.h @@ -1,42 +1,86 @@ /* -** -** Copyright 2010, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ + * Copyright (C) 2010, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #ifndef LATINIME_DEFINES_H #define LATINIME_DEFINES_H +#include <stdint.h> + #if defined(FLAG_DO_PROFILE) || defined(FLAG_DBG) -#include <cutils/log.h> -#define AKLOGE ALOGE -#define AKLOGI ALOGI +#include <android/log.h> +#ifndef LOG_TAG +#define LOG_TAG "LatinIME: " +#endif +#define AKLOGE(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, fmt, ##__VA_ARGS__) +#define AKLOGI(fmt, ...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##__VA_ARGS__) + +#define DUMP_RESULT(words, frequencies, maxWordCount, maxWordLength) do { \ + dumpResult(words, frequencies, maxWordCount, maxWordLength); } while (0) +#define DUMP_WORD(word, length) do { dumpWord(word, length); } while (0) +#define DUMP_WORD_INT(word, length) do { dumpWordInt(word, length); } while (0) +// TODO: INTS_TO_CHARS +#define SHORTS_TO_CHARS(input, length, output) do { \ + shortArrayToCharArray(input, length, output); } while (0) + +static inline void dumpWordInfo(const unsigned short *word, const int length, + const int rank, const int frequency) { + static char charBuf[50]; + int i = 0; + for (; i < length; ++i) { + const unsigned short c = word[i]; + if (c == 0) { + break; + } + // static_cast only for debugging + charBuf[i] = static_cast<char>(c); + } + charBuf[i] = 0; + if (i > 1) { + AKLOGI("%2d [ %s ] (%d)", rank, charBuf, frequency); + } +} -#define DUMP_WORD(word, length) do { dumpWord(word, length); } while(0) -#define DUMP_WORD_INT(word, length) do { dumpWordInt(word, length); } while(0) +static inline void dumpResult( + const unsigned short *outWords, const int *frequencies, const int maxWordCounts, + const int maxWordLength) { + AKLOGI("--- DUMP RESULT ---------"); + for (int i = 0; i < maxWordCounts; ++i) { + dumpWordInfo(&outWords[i * maxWordLength], maxWordLength, i, frequencies[i]); + } + AKLOGI("-------------------------"); +} -static inline void dumpWord(const unsigned short* word, const int length) { +static inline void dumpWord(const unsigned short *word, const int length) { static char charBuf[50]; - - for (int i = 0; i < length; ++i) { - charBuf[i] = word[i]; + int i = 0; + for (; i < length; ++i) { + const unsigned short c = word[i]; + if (c == 0) { + break; + } + // static_cast only for debugging + charBuf[i] = static_cast<char>(c); + } + charBuf[i] = 0; + if (i > 1) { + AKLOGI("[ %s ]", charBuf); } - charBuf[length] = 0; - AKLOGI("[ %s ]", charBuf); } -static inline void dumpWordInt(const int* word, const int length) { +static inline void dumpWordInt(const int *word, const int length) { static char charBuf[50]; for (int i = 0; i < length; ++i) { @@ -46,11 +90,58 @@ static inline void dumpWordInt(const int* word, const int length) { AKLOGI("i[ %s ]", charBuf); } +// TODO: Change this to intArrayToCharArray +static inline void shortArrayToCharArray( + const unsigned short *input, const int length, char *output) { + int i = 0; + for (;i < length; ++i) { + const unsigned short c = input[i]; + if (c == 0) { + break; + } + // static_cast only for debugging + output[i] = static_cast<char>(c); + } + output[i] = 0; +} + +#ifndef __ANDROID__ +#include <cassert> +#include <execinfo.h> +#include <stdlib.h> + +#define ASSERT(success) do { if (!(success)) { showStackTrace(); assert(success);} } while (0) +#define SHOW_STACK_TRACE do { showStackTrace(); } while (0) + +static inline void showStackTrace() { + void *callstack[128]; + int i, frames = backtrace(callstack, 128); + char **strs = backtrace_symbols(callstack, frames); + for (i = 0; i < frames; ++i) { + if (i == 0) { + AKLOGI("=== Trace ==="); + continue; + } + AKLOGI("%s", strs[i]); + } + free(strs); +} +#else +#include <cassert> +#define ASSERT(success) assert(success) +#define SHOW_STACK_TRACE +#endif + #else #define AKLOGE(fmt, ...) #define AKLOGI(fmt, ...) +#define DUMP_RESULT(words, frequencies, maxWordCount, maxWordLength) #define DUMP_WORD(word, length) #define DUMP_WORD_INT(word, length) +#define ASSERT(success) +#define SHOW_STACK_TRACE +// TODO: INTS_TO_CHARS +#define SHORTS_TO_CHARS(input, length, output) #endif #ifdef FLAG_DO_PROFILE @@ -64,14 +155,14 @@ static unsigned int profile_counter[PROF_BUF_SIZE]; #define PROF_RESET prof_reset() #define PROF_COUNT(prof_buf_id) ++profile_counter[prof_buf_id] -#define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while(0) +#define PROF_OPEN do { PROF_RESET; PROF_START(PROF_BUF_SIZE - 1); } while (0) #define PROF_START(prof_buf_id) do { \ - PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while(0) -#define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while(0) + PROF_COUNT(prof_buf_id); profile_old[prof_buf_id] = (clock()); } while (0) +#define PROF_CLOSE do { PROF_END(PROF_BUF_SIZE - 1); PROF_OUTALL; } while (0) #define PROF_END(prof_buf_id) profile_buf[prof_buf_id] += ((clock()) - profile_old[prof_buf_id]) #define PROF_CLOCKOUT(prof_buf_id) \ AKLOGI("%s : clock is %f", __FUNCTION__, (clock() - profile_old[prof_buf_id])) -#define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while(0) +#define PROF_OUTALL do { AKLOGI("--- %s ---", __FUNCTION__); prof_out(); } while (0) static inline void prof_reset(void) { for (int i = 0; i < PROF_BUF_SIZE; ++i) { @@ -86,17 +177,18 @@ static inline void prof_out(void) { AKLOGI("Error: You must call PROF_OPEN before PROF_CLOSE."); } AKLOGI("Total time is %6.3f ms.", - profile_buf[PROF_BUF_SIZE - 1] * 1000 / (float)CLOCKS_PER_SEC); + profile_buf[PROF_BUF_SIZE - 1] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC)); float all = 0; for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { all += profile_buf[i]; } if (all == 0) all = 1; for (int i = 0; i < PROF_BUF_SIZE - 1; ++i) { - if (profile_buf[i] != 0) { + if (profile_buf[i]) { AKLOGI("(%d): Used %4.2f%%, %8.4f ms. Called %d times.", i, (profile_buf[i] * 100 / all), - profile_buf[i] * 1000 / (float)CLOCKS_PER_SEC, profile_counter[i]); + profile_buf[i] * 1000.0f / static_cast<float>(CLOCKS_PER_SEC), + profile_counter[i]); } } } @@ -116,10 +208,6 @@ static inline void prof_out(void) { #endif // FLAG_DO_PROFILE #ifdef FLAG_DBG -#include <cutils/log.h> -#ifndef LOG_TAG -#define LOG_TAG "LatinIME: " -#endif #define DEBUG_DICT true #define DEBUG_DICT_FULL false #define DEBUG_EDIT_DISTANCE false @@ -132,6 +220,12 @@ static inline void prof_out(void) { #define DEBUG_CORRECTION_FREQ false #define DEBUG_WORDS_PRIORITY_QUEUE false +#ifdef FLAG_FULL_DBG +#define DEBUG_GEO_FULL true +#else +#define DEBUG_GEO_FULL false +#endif + #else // FLAG_DBG #define DEBUG_DICT false @@ -146,6 +240,7 @@ static inline void prof_out(void) { #define DEBUG_CORRECTION_FREQ false #define DEBUG_WORDS_PRIORITY_QUEUE false +#define DEBUG_GEO_FULL false #endif // FLAG_DBG @@ -176,15 +271,15 @@ static inline void prof_out(void) { #define FLAG_BIGRAM_FREQ 0x7F #define DICTIONARY_VERSION_MIN 200 -#define NOT_VALID_WORD -99 -#define NOT_A_CHARACTER -1 -#define NOT_A_DISTANCE -1 -#define NOT_A_COORDINATE -1 -#define EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO -2 -#define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO -3 -#define ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO -4 -#define NOT_AN_INDEX -1 -#define NOT_A_PROBABILITY -1 +#define NOT_VALID_WORD (-99) +#define NOT_A_CODE_POINT (-1) +#define NOT_A_DISTANCE (-1) +#define NOT_A_COORDINATE (-1) +#define EQUIVALENT_CHAR_WITHOUT_DISTANCE_INFO (-2) +#define PROXIMITY_CHAR_WITHOUT_DISTANCE_INFO (-3) +#define ADDITIONAL_PROXIMITY_CHAR_DISTANCE_INFO (-4) +#define NOT_AN_INDEX (-1) +#define NOT_A_PROBABILITY (-1) #define KEYCODE_SPACE ' ' @@ -225,9 +320,15 @@ static inline void prof_out(void) { // This is only used for the size of array. Not to be used in c functions. #define MAX_WORD_LENGTH_INTERNAL 48 +// This must be the same as ProximityInfo#MAX_PROXIMITY_CHARS_SIZE, currently it's 16. +#define MAX_PROXIMITY_CHARS_SIZE_INTERNAL 16 + // This must be equal to ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE in KeyDetector.java #define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2 +// Assuming locale strings such as en_US, sr-Latn etc. +#define MAX_LOCALE_STRING_LENGTH 10 + // Word limit for sub queues used in WordsPriorityQueuePool. Sub queues are temporary queues used // for better performance. // Holds up to 1 candidate for each word @@ -252,12 +353,18 @@ static inline void prof_out(void) { #define FIRST_WORD_INDEX 0 +#define MAX_SPACES_INTERNAL 16 + +// Max Distance between point to key +#define MAX_POINT_TO_KEY_LENGTH 10000000 + +// The max number of the keys in one keyboard layout +#define MAX_KEY_COUNT_IN_A_KEYBOARD 64 + // TODO: Reduce this constant if possible; check the maximum number of digraphs in the same // word in the dictionary for languages with digraphs, like German and French #define DEFAULT_MAX_DIGRAPH_SEARCH_DEPTH 5 -// Minimum suggest depth for one word for all cases except for missing space suggestions. -#define MIN_SUGGEST_DEPTH 1 #define MIN_USER_TYPED_LENGTH_FOR_MULTIPLE_WORD_SUGGESTION 3 #define MIN_USER_TYPED_LENGTH_FOR_EXCESSIVE_CHARACTER_SUGGESTION 3 @@ -286,7 +393,26 @@ template<typename T> inline T max(T a, T b) { return a > b ? a : b; } #define NEUTRAL_AREA_RADIUS_RATIO 1.3f // DEBUG -#define INPUTLENGTH_FOR_DEBUG -1 -#define MIN_OUTPUT_INDEX_FOR_DEBUG -1 - +#define INPUTLENGTH_FOR_DEBUG (-1) +#define MIN_OUTPUT_INDEX_FOR_DEBUG (-1) + +#define DISALLOW_COPY_AND_ASSIGN(TypeName) \ + TypeName(const TypeName&); \ + void operator=(const TypeName&) + +#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \ + TypeName(); \ + DISALLOW_COPY_AND_ASSIGN(TypeName) + +// Used as a return value for character comparison +typedef enum { + // Same char, possibly with different case or accent + EQUIVALENT_CHAR, + // It is a char located nearby on the keyboard + NEAR_PROXIMITY_CHAR, + // It is an unrelated char + UNRELATED_CHAR, + // Additional proximity char which can differ by language. + ADDITIONAL_PROXIMITY_CHAR +} ProximityType; #endif // LATINIME_DEFINES_H |