begin to write import iterator
[platform/upstream/libpinyin.git] / src / storage / phrase_large_table.h
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2010 Peng Wu
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_TABLE_H
23 #define PHRASE_LARGE_TABLE_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) / 2 * 8);
32
33 class PhraseLengthIndexLevel;
34
35 class PhraseBitmapIndexLevel{
36 protected:
37     PhraseLengthIndexLevel * m_phrase_length_indexes[PHRASE_NUMBER_OF_BITMAP_INDEX];
38     /* use a half ucs4_t for class PhraseLengthIndexLevel, just like PinyinLengthIndexLevel. */
39     void reset();
40 public:
41     PhraseBitmapIndexLevel();
42     ~PhraseBitmapIndexLevel(){
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/add_index/remove_index method */
51     int search( int phrase_length, /* in */ ucs4_t phrase[],
52                 /* out */ phrase_token_t & token);
53
54     int add_index( int phrase_length, /* in */ ucs4_t phrase[], /* in */ phrase_token_t token);
55     int remove_index( int phrase_length, /* in */ ucs4_t phrase[], /* out */ phrase_token_t & token);
56 };
57
58 class PhraseLargeTable{
59 protected:
60     PhraseBitmapIndexLevel m_bitmap_table;
61     MemoryChunk * m_chunk;
62
63     void reset(){
64         if ( m_chunk ){
65             delete m_chunk;
66             m_chunk = NULL;
67         }
68     }
69 public:
70     PhraseLargeTable(){
71         m_chunk = NULL;
72     }
73
74     ~PhraseLargeTable(){
75         reset();
76     }
77
78     /* load/store method */
79     bool load(MemoryChunk * chunk){
80         reset();
81         m_chunk = chunk;
82         return m_bitmap_table.load(chunk, 0, chunk->size());
83     }
84
85     bool store(MemoryChunk * new_chunk){
86         table_offset_t end;
87         return m_bitmap_table.store(new_chunk, 0, end);
88     }
89
90     bool load_text(FILE * file);
91
92     /* search/add_index/remove_index method */
93     int search( int phrase_length, /* in */ ucs4_t phrase[],
94                 /* out */ phrase_token_t & token){
95         return m_bitmap_table.search(phrase_length, phrase, token);
96     }
97
98     int add_index( int phrase_length, /* in */ ucs4_t phrase[], /* in */ phrase_token_t token){
99         return m_bitmap_table.add_index(phrase_length, phrase, token);
100     }
101
102     int remove_index( int phrase_length, /* in */ ucs4_t phrase[], /* out */ phrase_token_t & token){
103         return m_bitmap_table.remove_index(phrase_length, phrase, token);
104     }
105 };
106
107 };
108
109 #endif