~ubuntu-branches/ubuntu/raring/sunpinyin/raring

« back to all changes in this revision

Viewing changes to src/pinyin/datrie_impl.h

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-03-30 15:31:55 UTC
  • mfrom: (1.1.3) (1.2.7 sid)
  • Revision ID: package-import@ubuntu.com-20120330153155-qgls77sogzgtg9zp
Tags: 2.0.3+git20120222-1
* Team upload: git snapshot 20120222.
   - fix breaks if LDFLAGS in environment contains
       multiple words (Closese #646001).
   - rm patches merged to upstream:
       append-os-environ-toenv.patch
       fix-ftbfs-on-sh.patch
       remove-10-candidate-words-limitation.patch
   - refresh disable-lm-dict-compile.patch.
* Bump stardard version to 3.9.3: no modify needed.
* add libsunpinyin3-dbg and python-sunpinyin packages.
* debian/compat to 9, multiarch it.
* rewrite debian/rules with dh 7 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 
 * 
 
3
 *
4
4
 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5
 
 * 
 
5
 *
6
6
 * The contents of this file are subject to the terms of either the GNU Lesser
7
7
 * General Public License Version 2.1 only ("LGPL") or the Common Development and
8
8
 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9
9
 * file except in compliance with the License. You can obtain a copy of the CDDL at
10
10
 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11
 
 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the 
 
11
 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12
12
 * specific language governing permissions and limitations under the License. When
13
13
 * distributing the software, include this License Header Notice in each file and
14
14
 * include the full text of the License in the License file as well as the
15
15
 * following notice:
16
 
 * 
 
16
 *
17
17
 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18
18
 * (CDDL)
19
19
 * For Covered Software in this distribution, this License shall be governed by the
21
21
 * Any litigation relating to this License shall be subject to the jurisdiction of
22
22
 * the Federal Courts of the Northern District of California and the state courts
23
23
 * of the State of California, with venue lying in Santa Clara County, California.
24
 
 * 
 
24
 *
25
25
 * Contributor(s):
26
 
 * 
 
26
 *
27
27
 * If you wish your version of this file to be governed by only the CDDL or only
28
28
 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29
29
 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
32
32
 * Version 2.1, or to extend the choice of license to its licensees as provided
33
33
 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34
34
 * Version 2 license, then the option applies only if the new code is made subject
35
 
 * to such option by the copyright holder. 
 
35
 * to such option by the copyright holder.
36
36
 */
37
37
 
38
38
#include <stdio.h>
51
51
#include "datrie.h"
52
52
 
53
53
template <typename T, encoder_func_ptr encoder>
54
 
bool CDATrie<T, encoder>::load (const char * fname)
55
 
{
56
 
    free ();
 
54
bool CDATrie<T, encoder>::load(const char * fname){
 
55
    free();
57
56
 
58
57
    bool suc = false;
59
 
    int fd =  open (fname, O_RDONLY);
 
58
    int fd = open(fname, O_RDONLY);
60
59
    if (fd == -1) return false;
61
60
 
62
 
    m_memSize = lseek (fd, 0, SEEK_END);
63
 
    lseek (fd, 0, SEEK_SET);
 
61
    m_memSize = lseek(fd, 0, SEEK_END);
 
62
    lseek(fd, 0, SEEK_SET);
64
63
 
65
64
#ifdef HAVE_SYS_MMAN_H
66
 
    suc = (m_mem = (char*) mmap (NULL, m_memSize, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED;
 
65
    suc =
 
66
        (m_mem =
 
67
             (char *) mmap(NULL, m_memSize, PROT_READ, MAP_SHARED, fd,
 
68
                           0)) != MAP_FAILED;
67
69
#else
68
70
    suc = (m_mem = new char [m_memSize]) != NULL;
69
 
    suc = suc && (read (fd, m_mem, m_memSize) > 0);
 
71
    suc = suc && (read(fd, m_mem, m_memSize) > 0);
70
72
#endif /* HAVE_SYS_MMAN_H */
71
 
    close (fd);
 
73
    close(fd);
72
74
 
73
75
    if (!suc)
74
76
        return suc;
75
77
 
76
 
    m_len = * ((unsigned *) m_mem);
77
 
    unsigned short elm_size  = * ((unsigned short*) (m_mem + sizeof(m_len)));
78
 
    unsigned short has_value = * ((unsigned short*) (m_mem + sizeof(m_len) + sizeof(elm_size)));
 
78
    m_len = *((unsigned *) m_mem);
 
79
    unsigned short elm_size = *((unsigned short *) (m_mem + sizeof(m_len)));
 
80
    unsigned short has_value =
 
81
        *((unsigned short *) (m_mem + sizeof(m_len) + sizeof(elm_size)));
79
82
 
80
 
    if (sizeof (T) != elm_size)
 
83
    if (sizeof(T) != elm_size)
81
84
        return false;
82
85
 
83
86
    m_base = (T *) (m_mem + sizeof(m_len) + sizeof(elm_size) + sizeof(has_value));
84
87
    m_check = m_base + m_len;
85
 
    m_value = has_value? (int *)(m_check + m_len): NULL;
 
88
    m_value = has_value ? (int *) (m_check + m_len) : NULL;
86
89
 
87
90
    return suc;
88
91
}
89
92
 
90
93
template <typename T, encoder_func_ptr encoder>
91
 
void CDATrie<T, encoder>::free ()
92
 
{
 
94
void CDATrie<T, encoder>::free(){
93
95
    if (m_mem) {
94
96
#ifdef HAVE_SYS_MMAN_H
95
 
        munmap (m_mem, m_memSize);
 
97
        munmap(m_mem, m_memSize);
96
98
#else
97
99
        delete [] m_mem;
98
100
#endif
105
107
}
106
108
 
107
109
template <typename T, encoder_func_ptr encoder>
108
 
unsigned CDATrie<T, encoder>::walk (unsigned s, unsigned ch, int &v)
109
 
{
 
110
unsigned CDATrie<T, encoder>::walk(unsigned s, unsigned ch, int &v){
110
111
    unsigned c = encoder(ch);
111
 
    unsigned t = abs (m_base[s]) + c;
112
 
        
113
 
    if (t < m_len && m_check[t] == (T)s && m_base[t]) {
 
112
    unsigned t = abs(m_base[s]) + c;
 
113
 
 
114
    if (t < m_len && m_check[t] == (T) s && m_base[t]) {
114
115
        if (m_value)
115
116
            v = m_value[t];
116
117
        else
117
 
            v = m_base[t] < 0? -1: 0;
 
118
            v = m_base[t] < 0 ? -1 : 0;
118
119
 
119
120
        return t;
120
121
    }
124
125
}
125
126
 
126
127
template <typename T, encoder_func_ptr encoder>
127
 
int CDATrie<T, encoder>::match_longest (const char *str, unsigned &length)
128
 
{
129
 
    return match_longest (str, str+strlen(str), length);
 
128
int CDATrie<T, encoder>::match_longest(const char *str, unsigned &length){
 
129
    return match_longest(str, str + strlen(str), length);
130
130
}
131
131
 
132
132
template <typename T, encoder_func_ptr encoder>
133
 
int CDATrie<T, encoder>::match_longest (wstring wstr, unsigned &length)
134
 
{
135
 
    return match_longest (wstr.begin(), wstr.end(), length);
 
133
int CDATrie<T, encoder>::match_longest(wstring wstr, unsigned &length){
 
134
    return match_longest(wstr.begin(), wstr.end(), length);
136
135
}
137
136
 
138
137
template <typename T, encoder_func_ptr encoder>
139
138
template <typename InputIterator>
140
 
int CDATrie<T, encoder>::match_longest (InputIterator first, InputIterator last, unsigned &length)
141
 
{
142
 
    int l=0, ret_v=0, curr_state=0;
 
139
int CDATrie<T, encoder>::match_longest(InputIterator first,
 
140
                                       InputIterator last,
 
141
                                       unsigned &length){
 
142
    int l = 0, ret_v = 0, curr_state = 0;
143
143
    length = 0;
144
144
 
145
145
    for (; first != last; ++first) {
146
146
        unsigned ch = *first;
147
147
        int val;
148
 
        curr_state = walk (curr_state, ch, val);
 
148
        curr_state = walk(curr_state, ch, val);
149
149
        if (!curr_state) break;
150
150
 
151
151
        l += 1;