write reduce_tokens
[platform/upstream/libpinyin.git] / src / storage / phrase_large_table2.h
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2012 Peng Wu <alexepico@gmail.com>
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #ifndef PHRASE_LARGE_TABLE2_H
23 #define PHRASE_LARGE_TABLE2_H
24
25 #include <stdio.h>
26 #include "novel_types.h"
27 #include "memory_chunk.h"
28
29 namespace pinyin{
30
31 const size_t PHRASE_NUMBER_OF_BITMAP_INDEX = 1<<(sizeof(ucs4_t) / 4 * 8);
32
33 class PhraseLengthIndexLevel2;
34
35 class PhraseBitmapIndexLevel2{
36 protected:
37     PhraseLengthIndexLevel2 * m_phrase_length_indexes[PHRASE_NUMBER_OF_BITMAP_INDEX];
38     /* use the third byte of ucs4_t for class PhraseLengthIndexLevel2. */
39     void reset();
40 public:
41     PhraseBitmapIndexLevel2();
42     ~PhraseBitmapIndexLevel2(){
43         reset();
44     }
45
46     /* load/store method */
47     bool load(MemoryChunk * chunk, table_offset_t offset, table_offset_t end);
48     bool store(MemoryChunk * new_chunk, table_offset_t offset, table_offset_t & end);
49
50     /* search method */
51     int search(int phrase_length, /* in */ ucs4_t phrase[],
52                /* out */ PhraseTokens tokens) const;
53
54     /* add_index/remove_index method */
55     int add_index(int phrase_length, /* in */ ucs4_t phrase[], /* in */ phrase_token_t token);
56
57     int remove_index(int phrase_length, /* in */ ucs4_t phrase[], /* in */ phrase_token_t token);
58 };
59
60
61 class PhraseLargeTable2{
62 protected:
63     PhraseBitmapIndexLevel2 m_bitmap_table;
64     MemoryChunk * m_chunk;
65
66     void reset(){
67         if ( m_chunk ){
68             delete m_chunk;
69             m_chunk = NULL;
70         }
71     }
72 public:
73     PhraseLargeTable2(){
74         m_chunk = NULL;
75     }
76
77     ~PhraseLargeTable2(){
78         reset();
79     }
80
81     /* load/store method */
82     bool load(MemoryChunk * chunk){
83         reset();
84         m_chunk = chunk;
85         return m_bitmap_table.load(chunk, 0, chunk->size());
86     }
87
88     bool store(MemoryChunk * new_chunk){
89         table_offset_t end;
90         return m_bitmap_table.store(new_chunk, 0, end);
91     }
92
93     bool load_text(FILE * file);
94
95     /* search method */
96     int search(int phrase_length, /* in */ ucs4_t phrase[],
97                /* out */ PhraseTokens tokens) const {
98         return m_bitmap_table.search(phrase_length, phrase, tokens);
99     }
100
101     /* add_index/remove_index method */
102     int add_index(int phrase_length, /* in */ ucs4_t phrase[], /* in */ phrase_token_t token) {
103         return m_bitmap_table.add_index(phrase_length, phrase, token);
104     }
105
106     int remove_index(int phrase_length, /* in */ ucs4_t phrase[], /* in */ phrase_token_t token) {
107         return m_bitmap_table.remove_index(phrase_length, phrase, token);
108     }
109 };
110
111
112 static inline int reduce_tokens(PhraseTokens tokens,
113                                 GArray * tokenarray) {
114     int num = 0;
115
116     for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
117         GArray * array = tokens[i];
118         if (NULL == array)
119             continue;
120
121         num += array->len;
122
123         for (size_t j = 0; j < array->len; ++j) {
124             phrase_token_t token = g_array_index(array, phrase_token_t, j);
125             g_array_append_val(tokenarray, token);
126         }
127     }
128
129     /* the following line will be removed in future after code are verified. */
130     assert(0 == num || 1 == num);
131
132     return num;
133 }
134
135 /* for compatibility. */
136 static inline int get_first_token(PhraseTokens tokens,
137                                   /* out */ phrase_token_t & token){
138     int num = 0; token = null_token;
139
140     for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
141         GArray * array = tokens[i];
142         if (NULL == array || 0 == array->len)
143             continue;
144
145         num += array->len;
146
147         if (null_token == token) {
148             token = g_array_index(array, phrase_token_t, 0);
149         }
150     }
151
152     /* the following line will be removed in future after code are verified. */
153     assert(0 == num || 1 == num);
154
155     return num;
156 }
157
158 };
159
160 #endif