1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
* libpinyin
* Library to deal with pinyin.
*
* Copyright (C) 2012 Peng Wu <alexepico@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <assert.h>
#include <string.h>
#include "phrase_large_table2.h"
/* class definition */
namespace pinyin{
class PhraseLengthIndexLevel2{
protected:
GArray * m_phrase_array_indexes;
public:
PhraseLengthIndexLevel2();
~PhraseLengthIndexLevel2();
/* load/store method */
bool load(MemoryChunk * chunk, table_offset_t offset, table_offset_t end);
bool store(MemoryChunk * new_chunk, table_offset_t offset, table_offset_t & end);
/* search method */
int search(int phrase_length, /* in */ ucs4_t phrase[],
/* out */ PhraseTokens tokens) const;
/* add_index/remove_index method */
int add_index(int phrase_length, /* in */ ucs4_t phrase[],
/* in */ phrase_token_t token);
int remove_index(int phrase_length, /* in */ ucs4_t phrase[],
/* in */ phrase_token_t token);
};
template<size_t phrase_length>
struct PhraseIndexItem2{
phrase_token_t m_token;
ucs4_t m_phrase[phrase_length];
public:
PhraseIndexItem2<phrase_length>(ucs4_t phrase[], phrase_token_t token){
memmove(m_phrase, phrase, sizeof(ucs4_t) * phrase_length);
m_token = token;
}
};
template<size_t phrase_length>
class PhraseArrayIndexLevel2{
protected:
typedef PhraseIndexItem2<phrase_length> IndexItem;
protected:
MemoryChunk m_chunk;
public:
bool load(MemoryChunk * chunk, table_offset_t offset, table_offset_t end);
bool store(MemoryChunk * new_chunk, table_offset_t offset, table_offset_t & end);
/* search method */
int search(/* in */ ucs4_t phrase[], /* out */ PhraseTokens tokens) const;
/* add_index/remove_index method */
int add_index(/* in */ ucs4_t phrase[], /* in */ phrase_token_t token);
int remove_index(/* in */ ucs4_t phrase[], /* in */ phrase_token_t token);
};
};
using namespace pinyin;
/* class implementation */
template<size_t phrase_length>
static int phrase_compare(const PhraseIndexItem2<phrase_length> &lhs,
const PhraseIndexItem2<phrase_length> &rhs){
ucs4_t * phrase_lhs = (ucs4_t *) lhs.m_phrase;
ucs4_t * phrase_rhs = (ucs4_t *) rhs.m_phrase;
return memcmp(phrase_lhs, phrase_rhs, sizeof(ucs4_t) * phrase_length);
}
template<size_t phrase_length>
static bool phrase_less_than(const PhraseIndexItem2<phrase_length> & lhs,
const PhraseIndexItem2<phrase_length> & rhs){
return 0 > phrase_compare(lhs, rhs);
}
PhraseBitmapIndexLevel2::PhraseBitmapIndexLevel2(){
memset(m_phrase_length_indexes, 0, sizeof(m_phrase_length_indexes));
}
void PhraseBitmapIndexLevel2::reset(){
for ( size_t i = 0; i < PHRASE_NUMBER_OF_BITMAP_INDEX; i++){
PhraseLengthIndexLevel2 * length_array =
m_phrase_length_indexes[i];
if ( length_array )
delete length_array;
}
}
int PhraseBitmapIndexLevel2::search(int phrase_length,
/* in */ ucs4_t phrase[],
/* out */ PhraseTokens tokens) const {
assert(phrase_length > 0);
int result = SEARCH_NONE;
/* use the first 8-bit of the lower 16-bit for bitmap index,
* as most the higher 16-bit are zero.
*/
guint8 first_key = (phrase[0] & 0xFF00) >> 8;
PhraseLengthIndexLevel2 * phrase_array = m_phrase_length_indexes[first_key];
if ( phrase_array )
return phrase_array->search(phrase_length, phrase, tokens);
return result;
}
PhraseLengthIndexLevel2::PhraseLengthIndexLevel2(){
m_phrase_array_indexes = g_array_new(FALSE, TRUE, sizeof(void *));
}
PhraseLengthIndexLevel2::~PhraseLengthIndexLevel2(){
#define CASE(len) case len: \
{ \
PhraseArrayIndexLevel2<len> * & array = g_array_index \
(m_phrase_array_indexes, PhraseArrayIndexLevel2<len> *, len - 1); \
if ( array ) { \
delete array; \
array = NULL; \
} \
break; \
}
for (size_t i = 1; i <= m_phrase_array_indexes->len; ++i){
switch (i){
CASE(1);
CASE(2);
CASE(3);
CASE(4);
CASE(5);
CASE(6);
CASE(7);
CASE(8);
CASE(9);
CASE(10);
CASE(11);
CASE(12);
CASE(13);
CASE(14);
CASE(15);
CASE(16);
default:
assert(false);
}
}
g_array_free(m_phrase_array_indexes, TRUE);
#undef CASE
}
int PhraseLengthIndexLevel2::search(int phrase_length,
/* in */ ucs4_t phrase[],
/* out */ PhraseTokens tokens) const {
int result = SEARCH_NONE;
if(m_phrase_array_indexes->len < phrase_length)
return result;
if (m_phrase_array_indexes->len > phrase_length)
result |= SEARCH_CONTINUED;
#define CASE(len) case len: \
{ \
PhraseArrayIndexLevel2<len> * array = g_array_index \
(m_phrase_array_indexes, PhraseArrayIndexLevel2<len> *, len - 1); \
if ( !array ) \
return result; \
result |= array->search(phrase, tokens); \
return result; \
}
switch ( phrase_length ){
CASE(1);
CASE(2);
CASE(3);
CASE(4);
CASE(5);
CASE(6);
CASE(7);
CASE(8);
CASE(9);
CASE(10);
CASE(11);
CASE(12);
CASE(13);
CASE(14);
CASE(15);
CASE(16);
default:
assert(false);
}
#undef CASE
}
|