From 0d2c765a160775f1b97be83c3bd8a045b7b5600a Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 19 May 2016 17:51:37 +0800 Subject: refactor search_matrix function --- src/storage/phonetic_key_matrix.cpp | 37 +++++++++---------------------------- src/storage/phonetic_key_matrix.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 28 deletions(-) (limited to 'src/storage') diff --git a/src/storage/phonetic_key_matrix.cpp b/src/storage/phonetic_key_matrix.cpp index eb72410..bd6c561 100644 --- a/src/storage/phonetic_key_matrix.cpp +++ b/src/storage/phonetic_key_matrix.cpp @@ -211,28 +211,21 @@ int search_matrix_recur(GArray * cached_keys, } int result = SEARCH_NONE; - GArray * keys = g_array_new(TRUE, TRUE, sizeof(ChewingKey)); - GArray * key_rests = g_array_new(TRUE, TRUE, sizeof(ChewingKeyRest)); - matrix->get_items(start, keys, key_rests); + const size_t size = matrix->get_column_size(start); /* assume pinyin parsers will filter invalid keys. */ - assert(0 != keys->len); + assert(0 != size); - for (size_t i = 0; i < keys->len; ++i) { - const ChewingKey key = g_array_index(keys, ChewingKey, i); - const ChewingKeyRest key_rest = g_array_index(key_rests, - ChewingKeyRest, i); + for (size_t i = 0; i < size; ++i) { + ChewingKey key; ChewingKeyRest key_rest; + matrix->get_item(start, i, key, key_rest); size_t newstart = key_rest.m_raw_end; const ChewingKey zero_key; if (zero_key == key) { - /* assume only one key here. */ - assert(1 == keys->len); - - g_array_free(keys, TRUE); - g_array_free(key_rests, TRUE); - + /* assume only one key here for "'" or the last key. */ + assert(1 == size); return search_matrix_recur(cached_keys, table, matrix, newstart, end, ranges, longest); } @@ -248,8 +241,6 @@ int search_matrix_recur(GArray * cached_keys, g_array_set_size(cached_keys, cached_keys->len - 1); } - g_array_free(keys, TRUE); - g_array_free(key_rests, TRUE); return true; } @@ -259,21 +250,11 @@ int search_matrix(FacadeChewingTable2 * table, PhraseIndexRanges ranges) { assert(end < matrix->size()); - GArray * keys = g_array_new(TRUE, TRUE, sizeof(ChewingKey)); - GArray * key_rests = g_array_new(TRUE, TRUE, sizeof(ChewingKeyRest)); - - matrix->get_items(start, keys, key_rests); - const size_t start_len = keys->len; - - matrix->get_items(end, keys, key_rests); - const size_t end_len = keys->len; - - g_array_free(keys, TRUE); - g_array_free(key_rests, TRUE); - + const size_t start_len = matrix->get_column_size(start); if (0 == start_len) return SEARCH_NONE; + const size_t end_len = matrix->get_column_size(end); /* for empty column simply return SEARCH_CONTINUED. */ if (0 == end_len) return SEARCH_CONTINUED; diff --git a/src/storage/phonetic_key_matrix.h b/src/storage/phonetic_key_matrix.h index 6a1940e..3d37829 100644 --- a/src/storage/phonetic_key_matrix.h +++ b/src/storage/phonetic_key_matrix.h @@ -99,6 +99,25 @@ public: return true; } + size_t get_column_size(size_t index) { + assert(index < m_table_content->len); + + GArray * column = (GArray *) + g_ptr_array_index(m_table_content, index); + return column->len; + } + + bool get_item(size_t index, size_t row, Item & item) { + assert(index < m_table_content->len); + + GArray * column = (GArray *) + g_ptr_array_index(m_table_content, index); + assert(row < column->len); + + item = g_array_index(column, Item, row); + return true; + } + }; class PhoneticKeyMatrix { @@ -136,6 +155,18 @@ public: m_key_rests.append(index, key_rest); } + size_t get_column_size(size_t index) { + const size_t size = m_keys.get_column_size(index); + assert(size == m_key_rests.get_column_size(index)); + return size; + } + + bool get_item(size_t index, size_t row, + ChewingKey & key, ChewingKeyRest & key_rest) { + return m_keys.get_item(index, row, key) && + m_key_rests.get_item(index, row, key_rest); + } + }; /** -- cgit