2
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
4
* Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
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:
17
17
* NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
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.
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.
51
51
#include "datrie.h"
53
53
template <typename T, encoder_func_ptr encoder>
54
bool CDATrie<T, encoder>::load (const char * fname)
54
bool CDATrie<T, encoder>::load(const char * fname){
59
int fd = open (fname, O_RDONLY);
58
int fd = open(fname, O_RDONLY);
60
59
if (fd == -1) return false;
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);
65
64
#ifdef HAVE_SYS_MMAN_H
66
suc = (m_mem = (char*) mmap (NULL, m_memSize, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED;
67
(char *) mmap(NULL, m_memSize, PROT_READ, MAP_SHARED, fd,
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 */
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)));
80
if (sizeof (T) != elm_size)
83
if (sizeof(T) != elm_size)
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;
90
93
template <typename T, encoder_func_ptr encoder>
91
void CDATrie<T, encoder>::free ()
94
void CDATrie<T, encoder>::free(){
94
96
#ifdef HAVE_SYS_MMAN_H
95
munmap (m_mem, m_memSize);
97
munmap(m_mem, m_memSize);
107
109
template <typename T, encoder_func_ptr encoder>
108
unsigned CDATrie<T, encoder>::walk (unsigned s, unsigned ch, int &v)
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;
113
if (t < m_len && m_check[t] == (T)s && m_base[t]) {
112
unsigned t = abs(m_base[s]) + c;
114
if (t < m_len && m_check[t] == (T) s && m_base[t]) {
117
v = m_base[t] < 0? -1: 0;
118
v = m_base[t] < 0 ? -1 : 0;
126
127
template <typename T, encoder_func_ptr encoder>
127
int CDATrie<T, encoder>::match_longest (const char *str, unsigned &length)
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);
132
132
template <typename T, encoder_func_ptr encoder>
133
int CDATrie<T, encoder>::match_longest (wstring wstr, unsigned &length)
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);
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)
142
int l=0, ret_v=0, curr_state=0;
139
int CDATrie<T, encoder>::match_longest(InputIterator first,
142
int l = 0, ret_v = 0, curr_state = 0;
145
145
for (; first != last; ++first) {
146
146
unsigned ch = *first;
148
curr_state = walk (curr_state, ch, val);
148
curr_state = walk(curr_state, ch, val);
149
149
if (!curr_state) break;