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