add get_shengmu/yunmu_string
[platform/upstream/libpinyin.git] / src / storage / chewing_large_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 CHEWING_LARGE_TABLE_H
23 #define CHEWING_LARGE_TABLE_H
24
25
26 #include <stdio.h>
27 #include "novel_types.h"
28 #include "memory_chunk.h"
29 #include "chewing_key.h"
30
31 namespace pinyin{
32
33 class ChewingLengthIndexLevel;
34
35 class ChewingBitmapIndexLevel{
36
37 protected:
38     pinyin_option_t m_options;
39
40 protected:
41     ChewingLengthIndexLevel * m_chewing_length_indexes
42     [CHEWING_NUMBER_OF_INITIALS][CHEWING_NUMBER_OF_MIDDLES]
43     [CHEWING_NUMBER_OF_FINALS][CHEWING_NUMBER_OF_TONES];
44
45     /* search functions */
46     int initial_level_search(int phrase_length, /* in */ ChewingKey keys[],
47                              /* out */ PhraseIndexRanges ranges) const;
48
49     int middle_and_final_level_search(ChewingInitial initial,
50                                       int phrase_length,
51                                       /* in */ ChewingKey keys[],
52                                       /* out */ PhraseIndexRanges ranges) const;
53     int tone_level_search(ChewingInitial initial, ChewingMiddle middle,
54                           ChewingFinal final, int phrase_length,
55                           /* in */ ChewingKey keys[],
56                           /* out */ PhraseIndexRanges ranges) const;
57
58     void reset();
59
60 public:
61     /* constructor/destructor */
62     ChewingBitmapIndexLevel(pinyin_option_t options);
63     ~ChewingBitmapIndexLevel() { reset(); }
64
65     /* set options method */
66     bool set_options(pinyin_option_t options) {
67         m_options = options;
68         return true;
69     }
70
71     /* load/store method */
72     bool load(MemoryChunk * chunk, table_offset_t offset, table_offset_t end);
73     bool store(MemoryChunk * new_chunk, table_offset_t offset,
74                table_offset_t & end);
75
76     /* search method */
77     int search(int phrase_length, /* in */ ChewingKey keys[],
78                /* out */ PhraseIndexRanges ranges) const;
79
80     /* add/remove index method */
81     int add_index(int phrase_length, /* in */ ChewingKey keys[],
82                   /* in */ phrase_token_t token);
83     int remove_index(int phrase_length, /* in */ ChewingKey keys[],
84                      /* in */ phrase_token_t token);
85
86     /* mask out method */
87     bool mask_out(phrase_token_t mask, phrase_token_t value);
88 };
89
90
91 class ChewingLargeTable{
92 protected:
93     ChewingBitmapIndexLevel m_bitmap_table;
94     MemoryChunk * m_chunk;
95
96     void reset(){
97         if (m_chunk) {
98             delete m_chunk; m_chunk = NULL;
99         }
100     }
101
102 public:
103     /* constructor/destructor */
104     ChewingLargeTable(pinyin_option_t options):
105         m_bitmap_table(options), m_chunk(NULL) {}
106
107     ~ChewingLargeTable() { reset(); }
108
109     /* set options method */
110     bool set_options(pinyin_option_t options) {
111         return m_bitmap_table.set_options(options);
112     }
113
114     /* load/store method */
115     bool load(MemoryChunk * chunk) {
116         reset();
117         m_chunk = chunk;
118         return m_bitmap_table.load(chunk, 0, chunk->size());
119     }
120
121     bool store(MemoryChunk * new_chunk) {
122         table_offset_t end;
123         return m_bitmap_table.store(new_chunk, 0, end);
124     }
125
126     bool load_text(FILE * file);
127
128     /* search method */
129     int search(int phrase_length, /* in */ ChewingKey keys[],
130                /* out */ PhraseIndexRanges ranges) const {
131         return m_bitmap_table.search(phrase_length, keys, ranges);
132     }
133
134     /* add/remove index method */
135     int add_index(int phrase_length, /* in */ ChewingKey keys[],
136                   /* in */ phrase_token_t token) {
137         return m_bitmap_table.add_index(phrase_length, keys, token);
138     }
139
140     int remove_index(int phrase_length, /* in */ ChewingKey keys[],
141                      /* in */ phrase_token_t token) {
142         return m_bitmap_table.remove_index(phrase_length, keys, token);
143     }
144
145     /* mask out method */
146     bool mask_out(phrase_token_t mask, phrase_token_t value) {
147         return m_bitmap_table.mask_out(mask, value);
148     }
149 };
150
151 };
152
153 #endif