File: dartsdic.cpp

package info (click to toggle)
chasen 2.4.5-46.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,616 kB
  • sloc: sh: 10,024; ansic: 5,983; cpp: 179; makefile: 111; perl: 10
file content (208 lines) | stat: -rw-r--r-- 5,606 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2003 Nara Institute of Science and Technology
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name Nara Institute of Science and Technology may not be used to
 *    endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Nara Institute of Science and Technology 
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE Nara Institute
 * of Science and Technology BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: dartsdic.cpp,v 1.4 2008/05/16 04:59:21 masayu-a Exp $
 */

#include <string>
#include <vector>
#include <map>
#include <iostream>
#pragma GCC visibility push(hidden)
#include <darts.h>
#pragma GCC visibility pop
extern "C" {
#include <stdlib.h>
#include <stdio.h>
#include "dartsdic.h"
#include "chalib.h"
}

#pragma GCC visibility push(hidden)
typedef Darts::DoubleArrayImpl<char, unsigned char, long, unsigned long>
DoubleArrayL;

struct _darts_t {
    DoubleArrayL *da;
    cha_mmap_t *da_mmap;
    cha_mmap_t *lex_mmap;
    cha_mmap_t *dat_mmap;
};

typedef std::multimap<std::string, long> Hash;
typedef Hash::value_type HashVal;

struct _da_build_t {
    Hash *entries;
    std::string* path;
};

darts_t *
da_open(char *daname, char *lexname, char *datname)
{
    darts_t *da;
    DoubleArrayL *darts = new DoubleArrayL;

    da = (darts_t*)cha_malloc(sizeof(darts_t));
    da->da_mmap = cha_mmap_file(daname);
    darts->set_array(cha_mmap_map(da->da_mmap));
    da->da = darts;
    da->lex_mmap = cha_mmap_file(lexname);
    da->dat_mmap = cha_mmap_file(datname);

    return da;
}

int
da_lookup(darts_t *da, char *key, int key_len, long *indecies, int num)
{
    return da->da
	->commonPrefixSearch(key, indecies, num, key_len);
}

long
da_exact_lookup(darts_t *da, char *key, int key_len)
{
	     return da->da	
				 ->exactMatchSearch<long>(key,  key_len);	

}

#define lex_map(d) cha_mmap_map((d)->lex_mmap)
#define dat_map(d) cha_mmap_map((d)->dat_mmap)

int
da_get_lex(darts_t *da, long index, da_lex_t *lex_data, int *key_len)
{
    int num, i;
    char *base = (char *)lex_map(da) + index;

    *key_len = ((short *)base)[0];
    num = ((short *)base)[1];
    base += sizeof(short) * 2;

    for (i = 0; i < num; i++) {
	memcpy((void*)(lex_data + i),
	       (void*)base, sizeof(da_lex_t));
	base += sizeof(da_lex_t);
    }

    return num;
}

void *
da_get_lex_base(darts_t *da)
{
    return lex_map(da);
}

void *
da_get_dat_base(darts_t *da)
{
    return dat_map(da);
}

#pragma GCC visibility push(default)
da_build_t *
da_build_new(char *path)
{
    da_build_t *builder;

    builder = (da_build_t*)cha_malloc(sizeof(da_build_t));
    builder->entries = new Hash;
    builder->path = new std::string(path);

    return builder;
}

void
da_build_add(da_build_t *builder, char *key, long val)
{
    builder->entries->insert(HashVal(key, val));
}

static int
redump_lex(size_t key_len, std::vector<long>& indices,
	   char* tmpfile, FILE* lexfile)
{
    long index = ftell(lexfile);
    short buf;
    
    buf = (short)key_len;
    fwrite(&buf, sizeof(short), 1, lexfile);
    buf = (short)indices.size();
    fwrite(&buf, sizeof(short), 1, lexfile);
    for (std::vector<long>::iterator i = indices.begin();
	 i != indices.end(); i++) {
	da_lex_t* lex = (da_lex_t*)(tmpfile + *i);
	fwrite(lex, sizeof(da_lex_t), 1, lexfile);
    }

    return index;
}

int
da_build_dump(da_build_t* builder, char* tmpfile, FILE* lexfile)
{
    Hash::iterator i, last;
    Hash* entries = builder->entries;
    char** keys = new char*[entries->size()];
    size_t* lens = new size_t[entries->size()];
    long* vals = new long[entries->size()];
    int size = 0;
    std::vector<long> lex_indices;

    std::cerr << entries->size() << " entries" << std::endl;

    i = entries->begin();
    while (i != entries->end()) {
	const std::string& key = i->first;
	last = entries->upper_bound(key);
	lex_indices.clear();
	for (; i != last; i++) {
	    lex_indices.push_back(i->second);
	}
	lens[size] = key.size();
	keys[size] = (char*) key.data();
	vals[size] = redump_lex(lens[size], lex_indices, tmpfile, lexfile);
	if (vals[size] < 0) {
	    std::cerr << "Unexpected error at " << key << std::endl;
	    cha_exit_perror("build darts file");
	}
	size++;
    }
    std::cerr << size << " keys" << std::endl;

    DoubleArrayL da;
    da.build(size, (const char**) keys, lens, vals);
    da.save(builder->path->c_str(), "wb");

    return builder->entries->size();
}
#pragma GCC visibility pop