From 53948ad9daa5efd0da6e2e01f8e0e4545283fc1e Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 17 Jan 2017 15:21:10 +0800 Subject: write clear and prepare methods --- src/lookup/phonetic_lookup.h | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/lookup/phonetic_lookup.h b/src/lookup/phonetic_lookup.h index 6ca7d4f..65602c7 100644 --- a/src/lookup/phonetic_lookup.h +++ b/src/lookup/phonetic_lookup.h @@ -117,8 +117,38 @@ private: GPtrArray * m_steps_content; public: - bool clear(); - bool prepare(gint32 nstep); + bool clear() { + /* clear m_steps_index */ + for ( size_t i = 0; i < m_steps_index->len; ++i){ + GHashTable * table = (GHashTable *) g_ptr_array_index(m_steps_index, i); + g_hash_table_destroy(table); + g_ptr_array_index(m_steps_index, i) = NULL; + } + + /* clear m_steps_content */ + for ( size_t i = 0; i < m_steps_content->len; ++i){ + GArray * array = (GArray *) g_ptr_array_index(m_steps_content, i); + g_array_free(array, TRUE); + g_ptr_array_index(m_steps_content, i) = NULL; + } + + return true; + } + + bool prepare(gint32 nstep) { + /* add null start step */ + g_ptr_array_set_size(m_steps_index, nstep); + g_ptr_array_set_size(m_steps_content, nstep); + + for (int i = 0; i < nstep; ++i) { + /* initialize m_steps_index */ + g_ptr_array_index(m_steps_index, i) = g_hash_table_new(g_direct_hash, g_direct_equal); + /* initialize m_steps_content */ + g_ptr_array_index(m_steps_content, i) = g_array_new(FALSE, FALSE, sizeof(trellis_node)); + } + + return true; + } /* Array of phrase_token_t */ bool fill_prefixes(/* in */ TokenVector prefixes); -- cgit