示例#1
0
void
PinyinEnhanceMapAdd(PyEnhanceMap **map, FcitxMemoryPool *pool,
                    const char *key, int key_l, const char *word, int word_l)
{
    PyEnhanceMapWord *py_word;
    PyEnhanceMap *py_map;
#define uthash_malloc(sz) fcitx_memory_pool_alloc_align(pool, sz, 1)
#define uthash_free(ptr)
    word_l++;
    py_word = uthash_malloc(sizeof(PyEnhanceMapWord) + word_l);
    memcpy(py_enhance_map_word(py_word), word, word_l);
    HASH_FIND(hh, *map, key, key_l, py_map);
    if (py_map) {
        py_word->next = py_map->words;
        py_map->words = py_word;
    } else {
        py_map = uthash_malloc(sizeof(PyEnhanceMap) + key_l + 1);
        py_map->words = py_word;
        py_word->next = NULL;
        memcpy(py_enhance_map_key(py_map), key, key_l + 1);
        HASH_ADD_KEYPTR(hh, *map, py_enhance_map_key(py_map), key_l, py_map);
    }
#undef uthash_malloc
#undef uthash_free
}
示例#2
0
static void
py_enhance_stroke_add_word(PyEnhanceStrokeTree *tree, FcitxMemoryPool *pool,
                           const char *key_s, int key_l,
                           const char *word_s, int word_l)
{
    PyEnhanceMapWord **word_p;
    switch (key_l) {
    case 1:
        word_p = &tree->singles[key_s[0] - '1'];
        break;
    case 2:
        word_p = &tree->doubles[key_s[0] - '1'][key_s[1] - '1'];
        break;
    default: {
        PyEnhanceStrokeKey *key;
        PyEnhanceStrokeKey **key_p;
        key_p = (&tree->multiples[key_s[0] - '1'][key_s[1] - '1']
                 [key_s[2] - '1']);
        key = *key_p;
        int res;
        key_s += 3;
        /**
         * since all the words are ordered, which means res <= 0,
         * the loop is actually not doing anything.
         **/
        for (;;key_p = &key->next, key = *key_p) {
            if ((!key) || key_l - 3 < key->key_l ||
                (res = strcmp(key_s,
                              py_enhance_stroke_get_key(key))) < 0) {
                PyEnhanceStrokeKey *new_key;
                new_key = fcitx_memory_pool_alloc_align(
                    pool, sizeof(PyEnhanceStrokeKey) + key_l - 2, 1);
                new_key->words = NULL;
                new_key->next = key;
                new_key->key_l = key_l - 3;
                *key_p = new_key;
                memcpy(py_enhance_stroke_get_key(new_key), key_s, key_l - 2);
                word_p = &new_key->words;
                break;
            } else if (fcitx_likely(res == 0)) {
                word_p = &key->words;
                break;
            }
        }
    }
    }
    PyEnhanceMapWord *new_word;
    new_word = fcitx_memory_pool_alloc_align(
        pool, sizeof(PyEnhanceMapWord) + word_l + 1, 1);
    new_word->next = *word_p;
    *word_p = new_word;
    memcpy(py_enhance_map_word(new_word), word_s, word_l + 1);
}