forked from mischasan/aho-corasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acism_dump.c
168 lines (151 loc) · 5.28 KB
/
acism_dump.c
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
/*
** Copyright (C) 2009-2014 Mischa Sandberg <mischasan@gmail.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License Version 3 as
** published by the Free Software Foundation. You may not use, modify or
** distribute this program under any other version of the GNU Lesser General
** Public License.
**
** 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <ctype.h>
#include <stdio.h>
#include "_acism.h"
#if ACISM_SIZE == 4
# define FX ""
#elif __LONG_MAX__ < 9223372036854775807
# define FX "ll"
#else
# define FX "l"
#endif
static void printrans(ACISM const*,TRAN,const char*,FILE*,MEMREF const*);
static void printree(ACISM const* psp,
int state,
int depth,
char* str,
const char* charv,
FILE* out,
MEMREF const* pattv);
#define PSTR(_psp,_i,_pattv) _i, \
_pattv ? (int)_pattv[_i].len : 0, \
_pattv ? _pattv[_i].ptr : ""
//--------------|---------------------------------------------
void
acism_dump(ACISM const* psp, PS_DUMP_TYPE pdt, FILE *out, MEMREF const*pattv)
{
int i, empty;
char charv[256];
int symdist[257] = {};
for (i = 256; --i >=0;) charv[psp->symv[i]] = i;
if (pdt & PS_STATS) {
for (i = psp->tran_size, empty = 0; --i >= 0;) {
if (psp->tranv[i]) {
++symdist[t_sym(psp, psp->tranv[i])];
} else ++empty;
}
fprintf(out, "strs:%d syms:%d chars:%d "
"trans:%d empty:%d mod:%d hash:%d size:%lu\n",
psp->nstrs, psp->nsyms, psp->nchars,
psp->tran_size, empty, psp->hash_mod, psp->hash_size,
(long)sizeof(ACISM) + p_size(psp));
}
if (pdt & PS_TRAN) {
fprintf(out, "==== TRAN:\n%8s %8s Ch MS %8s\n", "Cell", "State", "Next");
for (i = 1; i < (int)psp->tran_size; ++i) {
fprintf(out, "%8d %8d ", i, i - t_sym(psp, psp->tranv[i]));
printrans(psp, i, charv, out, pattv);
}
}
if (pdt & PS_HASH) {
fprintf(out, "==== HASH:\n.....: state strno\n");
for (i = 0; i < (int)psp->hash_size; ++i) {
STATE state = psp->hashv[i].state;
if (state)
fprintf(out, "%5d: %7"FX"u %3d %8"FX"u %.*s\n",
i, state, i - p_hash(psp, state),
PSTR(psp, psp->hashv[i].strno, pattv));
else
fprintf(out, "%5d: %7"FX"d --- %8"FX"d\n", i, state,
psp->hashv[i].strno);
}
}
if (pdt & PS_TREE) {
fprintf(out, "==== TREE:\n");
char str[psp->maxlen + 1];
printree(psp, 0, 0, str, charv, out, pattv);
}
//TODO: calculate stats: backref chain lengths ...
}
static void
printrans(ACISM const*psp, STATE s, char const *charv,
FILE *out, MEMREF const *pattv)
{
(void)pattv;
TRAN x = psp->tranv[s];
if (!x) {
fprintf(out, "(empty)\n");
return;
}
SYMBOL sym = t_sym(psp,x);
char c = charv[sym];
if (sym)
fprintf(out, "--");
else
fprintf(out, "%02X ", c);
putc("M-"[!(x & IS_MATCH)], out);
putc("S-"[!(x & IS_SUFFIX)], out);
STATE next = t_next(psp, x);
if (t_isleaf(psp, x)) {
fprintf(out, " => %d\n", t_strno(psp, x));
} else {
fprintf(out, " %7"FX"d", next);
if (x & IS_MATCH) {
int i;
for (i = p_hash(psp, s); psp->hashv[i].state != s; ++i);
fprintf(out, " #> %"FX"d", psp->hashv[i].strno);
}
putc('\n', out);
}
}
static void
printree(ACISM const*psp, int state, int depth, char *str,
char const *charv, FILE*out, MEMREF const*pattv)
{
SYMBOL sym;
TRAN x;
if (depth > (int)psp->maxlen) {
fputs("oops\n", out);
return;
}
x = psp->tranv[state];
fprintf(out, "%5d:%.*s", state, depth, str);
if (t_valid(psp,x) && t_next(psp,x))
fprintf(out, " b=%"FX"d%s", t_next(psp,x), x & T_FLAGS ? " BAD" : "");
fprintf(out, "\n");
for (sym = 1; sym < psp->nsyms; ++sym) {
x = p_tran(psp, state, sym);
if (t_valid(psp, x)) {
str[depth] = charv[sym];
fprintf(out, "%*s%c %c%c",
depth+6, "", charv[sym],
x & IS_MATCH ? 'M' : '-',
x & IS_SUFFIX ? 'S' : '-');
if (x & IS_MATCH && pattv && t_isleaf(psp, x))
fprintf(out, " %.0d -> %.*s", PSTR(psp, t_strno(psp,x), pattv));
if (x & IS_SUFFIX)
fprintf(out, " ->S %"FX"d", t_next(psp, psp->tranv[state]));
fprintf(out, "\n");
if (!t_isleaf(psp, x))
printree(psp, t_next(psp, x), depth+1, str, charv, out, pattv);
}
}
}
//EOF