Debian Codegraph @

lib/fmemcmp.c

raw symbols SCIP
1 /*
2 * fstrcmp - fuzzy string compare library
3 * Copyright (C) 2009 Peter Miller
4 *
5 *
6 * Derived from gettext 0.17
7 * Copyright (C) 1988-2006 Free Software Foundation, Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 *
23 * Derived from GNU diff 2.7, analyze.c et al.
24 *
25 * The basic idea is to consider two vectors as similar if, when
26 * transforming the first vector into the second vector through a
27 * sequence of edits (inserts and deletes of one element each), this
28 * sequence is short - or equivalently, if the ordered list of elements
29 * that are untouched by these edits is long. For a good introduction
30 * to the subject, read about the "Levenshtein distance" in Wikipedia.
31 *
32 * The basic algorithm is described in:
33 * "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
34 * Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
35 * see especially section 4.2, which describes the variation used below.
36 *
37 * The basic algorithm was independently discovered as described in:
38 * "Algorithms for Approximate String Matching", E. Ukkonen, Information
39 * and Control Vol. 64, 1985, pp. 100-118.
40 *
41 * Unless the 'find_minimal' flag is set, this code uses the
42 * TOO_EXPENSIVE heuristic, by Paul Eggert, to limit the cost to
43 * O(N**1.5 log N) at the price of producing suboptimal output for large
44 * inputs with many differences.
45 */
46
47 #include <lib/fstrcmp.h>
48
49 #include <lib/ac/string.h>
50 #include <lib/ac/stdio.h>
51 #include <lib/ac/stdlib.h>
52 #include <limits.h>
53
54 #include <lib/minmax.h>
55 #include <lib/nmalloc.h>
56
57 #ifndef uintptr_t
58 # define uintptr_t unsigned long
59 #endif
60
61
62 #define ELEMENT char
63 #define EQUAL(x,y) ((x) == (y))
64 #define OFFSET_T ssize_t
65 #define OFFSET OFFSET_T
66 #define EXTRA_CONTEXT_FIELDS \
67 /* The number of elements inserted or deleted. */ \
68 size_t xvec_edit_count; \
69 size_t yvec_edit_count;
70 #define NOTE_DELETE(ctxt, xoff) ctxt->xvec_edit_count++
71 #define NOTE_INSERT(ctxt, yoff) ctxt->yvec_edit_count++
72 /*
73 * We don't need USE_HEURISTIC, since it is unlikely in typical uses of
74 * fstrcmp().
75 */
76 #include <lib/diffseq.h>
77
78
79 /*
80 * Because fstrcmp is typically called multiple times, attempt to
81 * minimize the number of memory allocations performed. Thus, let a
82 * call reuse the memory already allocated by the previous call, if
83 * it is sufficient.
84 *
85 * This isn't thread safe.
86 */
87
88 static OFFSET_T *buffer;
89 static size_t bufmax;
90
91
92 double
93 fmemcmp(const void *data1, size_t size1, const void *data2, size_t size2)
94 {
95 struct context ctxt;
96 int i;
97 size_t fdiag_len;
98
99 /*
100 * set the info for each string.
101 */
102 ctxt.xvec = data1;
103 ctxt.yvec = data2;
104
105 /*
106 * short-circuit obvious comparisons
107 */
108 if (size1 == 0 && size2 == 0)
109 return 1.0;
110 if (size1 == 0 || size2 == 0)
111 return 0.0;
112
113 /*
114 * Set TOO_EXPENSIVE to be approximate square root of input size,
115 * bounded below by 256.
116 */
117 ctxt.too_expensive = 1;
118 for (i = size1 + size2; i != 0; i >>= 2)
119 ctxt.too_expensive <<= 1;
120 if (ctxt.too_expensive < 256)
121 ctxt.too_expensive = 256;
122
123 /*
124 * Allocate memory for fdiag and bdiag.
125 */
126 fdiag_len = size1 + size2 + 3;
127 if (fdiag_len > bufmax)
128 {
129 /* Need more memory. */
130 bufmax = 2 * bufmax;
131 if (fdiag_len > bufmax)
132 bufmax = fdiag_len;
133 /*
134 * Calling realloc() would be a waste: buffer's contents do not
135 * need to be preserved.
136 */
137 if (buffer != NULL)
138 free(buffer);
139 buffer = (OFFSET_T *)fstrcmp_nmalloc(bufmax, 2 * sizeof(OFFSET_T));
140 if (!buffer)
141 return FSTRCMP_ERROR;
142 }
143 ctxt.fdiag = buffer + size2 + 1;
144 ctxt.bdiag = ctxt.fdiag + fdiag_len;
145
146 /*
147 * Now do the main comparison algorithm
148 */
149 ctxt.xvec_edit_count = 0;
150 ctxt.yvec_edit_count = 0;
151 compareseq(0, size1, 0, size2, 0, &ctxt);
152
153 /*
154 * The result is
155 *
156 * ((number of chars in common) / (average length of the strings)).
157 *
158 * This is admittedly biased towards finding that the strings are
159 * similar, however it does produce meaningful results.
160 */
161 return
162 (
163 (size1 + size2 - ctxt.yvec_edit_count - ctxt.xvec_edit_count)
164 /
165 (double)(size1 + size2)
166 );
167 }