summaryrefslogtreecommitdiffstats
path: root/src/storage/chewing_large_table2.h
blob: ebf6114cbf586c9d26bdcac748679104ddef1967 (plain)
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
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2016 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#ifndef CHEWING_LARGE_TABLE2_H
#define CHEWING_LARGE_TABLE2_H

#include "novel_types.h"
#include "memory_chunk.h"
#include "chewing_key.h"
#include "pinyin_phrase3.h"

#ifdef HAVE_BERKELEY_DB
#include "chewing_large_table2_bdb.h"
#endif

#ifdef HAVE_KYOTO_CABINET
#include "chewing_large_table2_kyotodb.h"
#endif

namespace pinyin{

class MaskOutVisitor2;

/* As this is a template class, the code will be in the header file. */
template<int phrase_length>
class ChewingTableEntry{
    friend class ChewingLargeTable2;
    friend class MaskOutVisitor2;
protected:
    typedef PinyinIndexItem2<phrase_length> IndexItem;

protected:
    MemoryChunk m_chunk;

private:
    /* Disallow used outside. */
    ChewingTableEntry() {}

public:
    /* convert method. */
    /* compress consecutive tokens */
    int convert(const ChewingKey keys[],
     const IndexItem * begin, const IndexItem * end,
     PhraseIndexRanges ranges) const {
        const IndexItem * iter = NULL;
        PhraseIndexRange cursor;
        GArray * head, * cursor_head = NULL;

        int result = SEARCH_NONE;
        /* TODO: check the below code */
        cursor.m_range_begin = null_token; cursor.m_range_end = null_token;
        for (iter = begin; iter != end; ++iter) {
            if (0 != pinyin_compare_with_tones
                (keys, iter->m_keys, phrase_length))
                continue;

            phrase_token_t token = iter->m_token;
            head = ranges[PHRASE_INDEX_LIBRARY_INDEX(token)];
            if (NULL == head)
                continue;

            result |= SEARCH_OK;

            if (null_token == cursor.m_range_begin) {
                cursor.m_range_begin = token;
                cursor.m_range_end   = token + 1;
                cursor_head = head;
            } else if (cursor.m_range_end == token &&
                       PHRASE_INDEX_LIBRARY_INDEX(cursor.m_range_begin) ==
                       PHRASE_INDEX_LIBRARY_INDEX(token)) {
                ++cursor.m_range_end;
            } else {
                g_array_append_val(cursor_head, cursor);
                cursor.m_range_begin = token; cursor.m_range_end = token + 1;
                cursor_head = head;
            }
        }

        if (null_token == cursor.m_range_begin)
            return result;

        g_array_append_val(cursor_head, cursor);
        return result;
    }

    /* search method */
    int search(/* in */ const ChewingKey keys[],
               /* out */ PhraseIndexRanges ranges) const {
        IndexItem item;
        if (contains_incomplete_pinyin(keys, phrase_length)) {
            compute_incomplete_chewing_index(keys, item.m_keys, phrase_length);
        } else {
            compute_chewing_index(keys, item.m_keys, phrase_length);
        }

        const IndexItem * begin = (IndexItem *) m_chunk.begin();
        const IndexItem * end = (IndexItem *) m_chunk.end();

        std_lite::pair<const IndexItem *, const IndexItem *> range =
            std_lite::equal_range(begin, end, item,
                                  phrase_less_than_with_tones<phrase_length>);

        return convert(keys, range.first, range.second, ranges);
    }

    /* add/remove index method */
    int add_index(/* in */ const ChewingKey keys[],
                  /* in */ phrase_token_t token) {
        const IndexItem * begin = (IndexItem *) m_chunk.begin();
        const IndexItem * end = (IndexItem *) m_chunk.end();

        const IndexItem add_elem(keys, token);

        std_lite::pair<const IndexItem *, const IndexItem *> range =
            std_lite::equal_range(begin, end, add_elem,
                                  phrase_exact_less_than2<phrase_length>);

        const IndexItem * cur_elem;
        for (cur_elem = range.first;
             cur_elem != range.second; ++cur_elem) {
            if (cur_elem->m_token == token)
                return ERROR_INSERT_ITEM_EXISTS;
            if (cur_elem->m_token > token)
                break;
        }

        int offset = (cur_elem - begin) * sizeof(IndexItem);
        m_chunk.insert_content(offset, &add_elem, sizeof(IndexItem));
        return ERROR_OK;
    }

    int remove_index(/* in */ const ChewingKey keys[],
                     /* in */ phrase_token_t token) {
        const IndexItem * begin = (IndexItem *) m_chunk.begin();
        const IndexItem * end = (IndexItem *) m_chunk.end();

        const IndexItem remove_elem(keys, token);

        std_lite::pair<const IndexItem *, const IndexItem *> range =
            std_lite::equal_range(begin, end, remove_elem,
                                  phrase_exact_less_than2<phrase_length>);

        const IndexItem * cur_elem;
        for (cur_elem = range.first;
             cur_elem != range.second; ++cur_elem) {
            if (cur_elem->m_token == token)
                break;
        }

        if (cur_elem == range.second)
            return ERROR_REMOVE_ITEM_DONOT_EXISTS;

        int offset = (cur_elem - begin) * sizeof(IndexItem);
        m_chunk.remove_content(offset, sizeof(IndexItem));
        return ERROR_OK;
    }

    /* get length method */
    int get_length() const {
        const IndexItem * begin = (IndexItem *) m_chunk.begin();
        const IndexItem * end = (IndexItem *) m_chunk.end();

        return end - begin;
    }

    /* mask out method */
    bool mask_out(phrase_token_t mask, phrase_token_t value) {
        const IndexItem * begin = (IndexItem *) m_chunk.begin();
        const IndexItem * end = (IndexItem *) m_chunk.end();

        const IndexItem * cur_elem;
        for (cur_elem = begin; cur_elem != end; ++cur_elem) {
            /* not match. */
            if ((cur_elem->m_token & mask) != value)
                continue;

            int offset = (cur_elem - begin) * sizeof(IndexItem);
            m_chunk.remove_content(offset, sizeof(IndexItem));

            /* update chunk end. */
            end = (IndexItem *) m_chunk.end();
            --cur_elem;
        }

        return true;
    }

};

};

#endif