-
Notifications
You must be signed in to change notification settings - Fork 26
/
fmlocate.cpp
126 lines (113 loc) · 3.15 KB
/
fmlocate.cpp
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
/* FM-Index - Text Index
* Copyright (C) 2011 Matthias Petri
*
* 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/>. */
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "FM.h"
static void
print_usage(const char *program)
{
fprintf(stderr, "USAGE: %s -i <index> <qrys>\n", program);
fprintf(stderr, " qrys : file containing queries\n");
fprintf(stderr, " index : index file\n");
fprintf(stderr, " -v verbose output\n");
fprintf(stderr, "\n");
fprintf(stderr, "EXAMPLE: %s -i alice29.fm alice29.qrys\n",program);
fprintf(stderr, "\n");
return;
}
/*
*
*/
int main(int argc, char** argv) {
int32_t opt,nqrys,maxqry,i;
char* idxname;char* qryname;
FILE* f;
FM* FMIdx;
uint8_t** queries;
char buf[4096];
uint32_t start,stop,matches,j;
uint32_t* result;
/* parse command line parameter */
if (argc <= 3) {
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
opt = -1;
idxname = qryname = NULL;
while ((opt = getopt(argc, argv, "vhi:")) != -1) {
switch (opt) {
case 'i':
idxname = optarg;
break;
case 'v':
FM::verbose = 1;
break;
case 'h':
default:
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
/* read filenames */
if(optind < argc) {
qryname = argv[optind];
}
if(qryname==NULL) {
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
/* load index */
FMIdx = FM::load(idxname);
if(!FMIdx) {
perror("error loading index from file");
exit(EXIT_FAILURE);
}
/* read queries */
f = safe_fopen(qryname,"r");
maxqry = REALLOC_INCREMENT;
queries = (uint8_t**) safe_malloc(REALLOC_INCREMENT * sizeof(uint8_t*));
nqrys = 0;
while( fscanf(f,"%s\n",buf) == 1 ) {
queries[nqrys] = (uint8_t*) safe_strdup(buf);
if(nqrys == maxqry-1) {
queries = (uint8_t**) safe_realloc(queries,
(maxqry*2)*sizeof(uint8_t*));
maxqry *= 2;
}
nqrys++;
}
fclose(f);
FM::info("read %d queries",nqrys);
start = gettime();
for(i=0;i<nqrys;i++) {
result = FMIdx->locate(queries[i],strlen((char*)queries[i]),&matches);
fprintf(stdout,"%s (%d) : ",queries[i],matches);
for(j=0;j<matches-1;j++) fprintf(stdout,"%d ",result[j]);
fprintf(stdout,"%d\n",result[matches-1]);
free(result);
}
stop = gettime();
FM::info("finished processing queries: %.3f sec",((float)(stop-start))/1000000);
/* clean up */
for(i=0;i<nqrys;i++) free(queries[i]);
free(queries);
delete FMIdx;
/* T already deleted in FMIdx */
return (EXIT_SUCCESS);
}