write add/remove index method in progress
authorPeng Wu <alexepico@gmail.com>
Tue, 28 Aug 2012 03:13:53 +0000 (11:13 +0800)
committerPeng Wu <alexepico@gmail.com>
Tue, 28 Aug 2012 03:14:36 +0000 (11:14 +0800)
src/storage/phrase_large_table2.cpp

index e42b530..00c9a90 100644 (file)
@@ -284,3 +284,86 @@ int PhraseBitmapIndexLevel2::remove_index(int phrase_length,
 
     return ERROR_REMOVE_ITEM_DONOT_EXISTS;
 }
+
+int PhraseLengthIndexLevel2::add_index(int phrase_length,
+                                       /* in */ ucs4_t phrase[],
+                                       /* in */ phrase_token_t token) {
+    if (phrase_length >= MAX_PHRASE_LENGTH)
+        return ERROR_PHRASE_TOO_LONG;
+
+    if (m_phrase_array_indexes->len < phrase_length)
+        g_array_set_size(m_phrase_array_indexes, phrase_length);
+
+#define CASE(len) case len:                                             \
+    {                                                                   \
+        PhraseArrayIndexLevel2<len> * & array = g_array_index           \
+            (m_phrase_array_indexes, PhraseArrayIndexLevel2<len> *, len - 1); \
+        if ( !array )                                                   \
+            array = new PhraseArrayIndexLevel2<len>;                    \
+        return array->add_index(phrase, token);                         \
+    }
+
+    switch(phrase_length){
+       CASE(1);
+       CASE(2);
+       CASE(3);
+       CASE(4);
+       CASE(5);
+       CASE(6);
+       CASE(7);
+       CASE(8);
+       CASE(9);
+       CASE(10);
+       CASE(11);
+       CASE(12);
+       CASE(13);
+       CASE(14);
+       CASE(15);
+        CASE(16);
+    default:
+       assert(false);
+    }
+
+#undef CASE
+}
+
+int PhraseLengthIndexLevel2::remove_index(int phrase_length,
+                                          /* in */ ucs4_t phrase[],
+                                          /* in */ phrase_token_t token) {
+    if (phrase_length >= MAX_PHRASE_LENGTH)
+        return ERROR_PHRASE_TOO_LONG;
+
+    if (m_phrase_array_indexes->len < phrase_length)
+        return ERROR_REMOVE_ITEM_DONOT_EXISTS;
+
+#define CASE(len) case len:                                             \
+    {                                                                   \
+        PhraseArrayIndexLevel2<len> * & array =  g_array_index          \
+            (m_phrase_array_indexes, PhraseArrayIndexLevel2<len> *, len - 1); \
+        if ( !array )                                                   \
+            return ERROR_REMOVE_ITEM_DONOT_EXISTS;                      \
+        return array->remove_index(phrase, token);                      \
+    }
+
+    switch(phrase_length){
+       CASE(1);
+       CASE(2);
+       CASE(3);
+       CASE(4);
+       CASE(5);
+       CASE(6);
+       CASE(7);
+       CASE(8);
+       CASE(9);
+       CASE(10);
+       CASE(11);
+       CASE(12);
+       CASE(13);
+       CASE(14);
+       CASE(15);
+       CASE(16);
+    default:
+       assert(false);
+    }
+#undef CASE
+}