write mask out for facade tables
[platform/upstream/libpinyin.git] / src / storage / facade_chewing_table.h
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2011 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 FACADE_CHEWING_TABLE_H
23 #define FACADE_CHEWING_TABLE_H
24
25 #include "novel_types.h"
26 #include "chewing_large_table.h"
27
28 namespace pinyin{
29
30 /**
31  * FacadeChewingTable:
32  *
33  * The facade class of chewing large table.
34  *
35  */
36
37 class FacadeChewingTable{
38 private:
39     ChewingLargeTable * m_system_chewing_table;
40     ChewingLargeTable * m_user_chewing_table;
41
42     void reset() {
43         if (m_system_chewing_table) {
44             delete m_system_chewing_table;
45             m_system_chewing_table = NULL;
46         }
47
48         if (m_user_chewing_table) {
49             delete m_user_chewing_table;
50             m_user_chewing_table = NULL;
51         }
52     }
53 public:
54     /**
55      * FacadeChewingTable::FacadeChewingTable:
56      *
57      * The constructor of the FacadeChewingTable.
58      *
59      */
60     FacadeChewingTable() {
61         m_system_chewing_table = NULL;
62         m_user_chewing_table = NULL;
63     }
64
65     /**
66      * FacadeChewingTable::~FacadeChewingTable:
67      *
68      * The destructor of the FacadeChewingTable.
69      *
70      */
71     ~FacadeChewingTable() {
72         reset();
73     }
74
75     /**
76      * FacadeChewingTable::set_options:
77      * @options: the pinyin options.
78      * @returns: whether the setting options is successful.
79      *
80      * Set the options of the system and user chewing table.
81      *
82      */
83     bool set_options(pinyin_option_t options) {
84         bool result = false;
85         if (m_system_chewing_table)
86             result = m_system_chewing_table->set_options(options)  || result;
87         if (m_user_chewing_table)
88             result = m_user_chewing_table->set_options(options) || result;
89         return result;
90     }
91
92     /**
93      * FacadeChewingTable::load:
94      * @options: the pinyin options.
95      * @system: the memory chunk of the system chewing table.
96      * @user: the memory chunk of the user chewing table.
97      * @returns: whether the load operation is successful.
98      *
99      * Load the system or user chewing table from the memory chunks.
100      *
101      */
102     bool load(pinyin_option_t options, MemoryChunk * system,
103               MemoryChunk * user){
104         reset();
105
106         bool result = false;
107         if (system) {
108             m_system_chewing_table = new ChewingLargeTable(options);
109             result = m_system_chewing_table->load(system) || result;
110         }
111         if (user) {
112             m_user_chewing_table = new ChewingLargeTable(options);
113             result = m_user_chewing_table->load(user) || result;
114         }
115         return result;
116     }
117
118     /**
119      * FacadeChewingTable::store:
120      * @new_user: the memory chunk to store the user chewing table.
121      * @returns: whether the store operation is successful.
122      *
123      * Store the user chewing table to the memory chunk.
124      *
125      */
126     bool store(MemoryChunk * new_user) {
127         if (NULL == m_user_chewing_table)
128             return false;
129         return m_user_chewing_table->store(new_user);
130     }
131
132     /**
133      * FacadeChewingTable::search:
134      * @phrase_length: the length of the phrase to be searched.
135      * @keys: the pinyin key of the phrase to be searched.
136      * @ranges: the array of GArrays to store the matched phrase token.
137      * @returns: the search result of enum SearchResult.
138      *
139      * Search the phrase tokens according to the pinyin keys.
140      *
141      */
142     int search(int phrase_length, /* in */ ChewingKey keys[],
143                /* out */ PhraseIndexRanges ranges) const {
144
145         /* clear ranges. */
146         for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
147             if (ranges[i])
148                 g_array_set_size(ranges[i], 0);
149         }
150
151         int result = SEARCH_NONE;
152
153         if (NULL != m_system_chewing_table)
154             result |= m_system_chewing_table->search
155                 (phrase_length, keys, ranges);
156
157         if (NULL != m_user_chewing_table)
158             result |= m_user_chewing_table->search
159                 (phrase_length, keys, ranges);
160
161         return result;
162     }
163
164     /**
165      * FacadeChewingTable::add_index:
166      * @phrase_length: the length of the phrase to be added.
167      * @keys: the pinyin keys of the phrase to be added.
168      * @token: the token of the phrase to be added.
169      * @returns: the add result of enum ErrorResult.
170      *
171      * Add the phrase token to the user chewing table.
172      *
173      */
174     int add_index(int phrase_length, /* in */ ChewingKey keys[],
175                   /* in */ phrase_token_t token) {
176         if (NULL == m_user_chewing_table)
177             return ERROR_NO_USER_TABLE;
178         return m_user_chewing_table->add_index(phrase_length, keys, token);
179     }
180
181     /**
182      * FacadeChewingTable::remove_index:
183      * @phrase_length: the length of the phrase to be removed.
184      * @keys: the pinyin keys of the phrase to be removed.
185      * @token: the token of the phrase to be removed.
186      * @returns: the remove result of enum ErrorResult.
187      *
188      * Remove the phrase token from the user chewing table.
189      *
190      */
191     int remove_index(int phrase_length, /* in */ ChewingKey keys[],
192                      /* in */ phrase_token_t token) {
193         if (NULL == m_user_chewing_table)
194             return ERROR_NO_USER_TABLE;
195         return m_user_chewing_table->remove_index(phrase_length, keys, token);
196     }
197
198     /**
199      * FacadeChewingTable::mask_out:
200      * @mask: the mask.
201      * @value: the value.
202      * @returns: whether the mask out operation is successful.
203      *
204      * Mask out the matched chewing index.
205      *
206      */
207     bool mask_out(phrase_token_t mask, phrase_token_t value) {
208         if (NULL == m_user_chewing_table)
209             return false;
210         return m_user_chewing_table->mask_out(mask, value);
211     }
212 };
213
214 };
215
216 #endif