write remove index
authorPeng Wu <alexepico@gmail.com>
Tue, 6 Nov 2012 03:42:15 +0000 (11:42 +0800)
committerPeng Wu <alexepico@gmail.com>
Tue, 6 Nov 2012 03:42:15 +0000 (11:42 +0800)
src/storage/chewing_large_table.cpp
src/storage/phrase_large_table2.cpp

index 7779aae..7639dfe 100644 (file)
@@ -560,8 +560,19 @@ int ChewingLengthIndexLevel::remove_index(int phrase_length,
             (m_chewing_array_indexes,                           \
              ChewingArrayIndexLevel<len> *, len);               \
         if (NULL == array)                                      \
-            return ERROR_REMOVE_ITEM_DONOT_EXISTS;                    \
-        return array->remove_index(keys, token);                \
+            return ERROR_REMOVE_ITEM_DONOT_EXISTS;              \
+        int retval = array->remove_index(keys, token);          \
+                                                                \
+        /* remove empty array. */                               \
+        if (0 == array->get_length()) {                         \
+            delete array;                                       \
+            array = NULL;                                       \
+                                                                \
+            /* shrink self array. */                            \
+            g_array_set_size(m_chewing_array_indexes,           \
+                             get_length());                     \
+        }                                                       \
+        return retval;                                          \
     }
 
     switch (phrase_length) {
index 7ed470f..f336e58 100644 (file)
@@ -359,11 +359,23 @@ int PhraseLengthIndexLevel2::remove_index(int phrase_length,
 
 #define CASE(len) case len:                                             \
     {                                                                   \
-        PhraseArrayIndexLevel2<len> * & array =  g_array_index          \
-            (m_phrase_array_indexes, PhraseArrayIndexLevel2<len> *, len - 1); \
-        if ( !array )                                                   \
+        PhraseArrayIndexLevel2<len> * & array = g_array_index           \
+            (m_phrase_array_indexes,                                    \
+             PhraseArrayIndexLevel2<len> *, len - 1);                   \
+        if (NULL == array)                                              \
             return ERROR_REMOVE_ITEM_DONOT_EXISTS;                      \
-        return array->remove_index(phrase, token);                      \
+        int retval = array->remove_index(phrase, token);                \
+                                                                        \
+        /* remove empty array. */                                       \
+        if (0 == array->get_length()) {                                 \
+            delete array;                                               \
+            array = NULL;                                               \
+                                                                        \
+            /* shrink self array. */                                    \
+            g_array_set_size(m_phrase_array_indexes,                    \
+                             get_length());                             \
+        }                                                               \
+        return retval;                                                  \
     }
 
     switch(phrase_length){