1 /* Functions to make fuzzy comparisons between strings
2 Copyright (C) 1988-1989, 1992-1993, 1995, 2001-2003, 2006, 2008-2020 Free
3 Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
32 #include "glthread/lock.h"
33 #include "glthread/tls.h"
39 #define EQUAL(x,y) ((x) == (y))
40 #define OFFSET ptrdiff_t
41 #define EXTRA_CONTEXT_FIELDS \
42 /* The number of edits beyond which the computation can be aborted. */ \
43 ptrdiff_t edit_count_limit; \
44 /* The number of edits (= number of elements inserted, plus the number of \
45 elements deleted), temporarily minus edit_count_limit. */ \
47 #define NOTE_DELETE(ctxt, xoff) ctxt->edit_count++
48 #define NOTE_INSERT(ctxt, yoff) ctxt->edit_count++
49 #define NOTE_ORDERED false
50 #define EARLY_ABORT(ctxt) ctxt->edit_count > 0
51 /* We don't need USE_HEURISTIC, since it is unlikely in typical uses of
56 /* Because fstrcmp is typically called multiple times, attempt to minimize
57 the number of memory allocations performed. Thus, let a call reuse the
58 memory already allocated by the previous call, if it is sufficient.
59 To make it multithread-safe, without need for a lock that protects the
60 already allocated memory, store the allocated memory per thread. Free
61 it only when the thread exits. */
63 static gl_tls_key_t buffer_key
; /* TLS key for a 'ptrdiff_t *' */
64 static gl_tls_key_t bufmax_key
; /* TLS key for a 'uintptr_t' */
69 gl_tls_key_init (buffer_key
, free
);
70 gl_tls_key_init (bufmax_key
, NULL
);
71 /* The per-thread initial values are NULL and 0, respectively. */
74 /* Ensure that keys_init is called once only. */
75 gl_once_define(static, keys_init_once
)
78 fstrcmp_free_resources (void)
82 gl_once (keys_init_once
, keys_init
);
83 buffer
= gl_tls_get (buffer_key
);
86 gl_tls_set (buffer_key
, NULL
);
87 gl_tls_set (bufmax_key
, (void *) (uintptr_t) 0);
93 /* In the code below, branch probabilities were measured by Ralf Wildenhues,
94 by running "msgmerge LL.po coreutils.pot" with msgmerge 0.18 for many
95 values of LL. The probability indicates that the condition evaluates
96 to true; whether that leads to a branch or a non-branch in the code,
97 depends on the compiler's reordering of basic blocks. */
101 fstrcmp_bounded (const char *string1
, const char *string2
, double lower_bound
)
104 size_t xvec_length
= strlen (string1
);
105 size_t yvec_length
= strlen (string2
);
106 size_t length_sum
= xvec_length
+ yvec_length
;
113 /* short-circuit obvious comparisons */
114 if (xvec_length
== 0 || yvec_length
== 0) /* Prob: 1% */
115 return length_sum
== 0;
117 if (! (xvec_length
<= length_sum
118 && length_sum
<= MIN (UINTPTR_MAX
, PTRDIFF_MAX
) - 3))
123 /* Compute a quick upper bound.
124 Each edit is an insertion or deletion of an element, hence modifies
125 the length of the sequence by at most 1.
126 Therefore, when starting from a sequence X and ending at a sequence Y,
127 with N edits, | yvec_length - xvec_length | <= N. (Proof by
129 So, at the end, we will have
130 edit_count >= | xvec_length - yvec_length |.
133 = (xvec_length + yvec_length - edit_count)
134 / (xvec_length + yvec_length)
135 <= (xvec_length + yvec_length - | yvec_length - xvec_length |)
136 / (xvec_length + yvec_length)
137 = 2 * min (xvec_length, yvec_length) / (xvec_length + yvec_length).
139 ptrdiff_t length_min
= MIN (xvec_length
, yvec_length
);
140 volatile double upper_bound
= 2.0 * length_min
/ length_sum
;
142 if (upper_bound
< lower_bound
) /* Prob: 74% */
143 /* Return an arbitrary value < LOWER_BOUND. */
147 /* When X and Y are both small, avoid the overhead of setting up an
148 array of size 256. */
149 if (length_sum
>= 20) /* Prob: 99% */
151 /* Compute a less quick upper bound.
152 Each edit is an insertion or deletion of a character, hence
153 modifies the occurrence count of a character by 1 and leaves the
154 other occurrence counts unchanged.
155 Therefore, when starting from a sequence X and ending at a
156 sequence Y, and denoting the occurrence count of C in X with
157 OCC (X, C), with N edits,
158 sum_C | OCC (X, C) - OCC (Y, C) | <= N.
159 (Proof by induction over N.)
160 So, at the end, we will have
161 edit_count >= sum_C | OCC (X, C) - OCC (Y, C) |,
164 = (xvec_length + yvec_length - edit_count)
165 / (xvec_length + yvec_length)
166 <= (xvec_length + yvec_length - sum_C | OCC(X,C) - OCC(Y,C) |)
167 / (xvec_length + yvec_length).
169 ptrdiff_t occ_diff
[UCHAR_MAX
+ 1]; /* array C -> OCC(X,C) - OCC(Y,C) */
173 /* Determine the occurrence counts in X. */
174 memset (occ_diff
, 0, sizeof (occ_diff
));
175 for (i
= xvec_length
- 1; i
>= 0; i
--)
176 occ_diff
[(unsigned char) string1
[i
]]++;
177 /* Subtract the occurrence counts in Y. */
178 for (i
= yvec_length
- 1; i
>= 0; i
--)
179 occ_diff
[(unsigned char) string2
[i
]]--;
180 /* Sum up the absolute values. */
182 for (i
= 0; i
<= UCHAR_MAX
; i
++)
184 ptrdiff_t d
= occ_diff
[i
];
185 sum
+= (d
>= 0 ? d
: -d
);
189 upper_bound
= 1.0 - dsum
/ length_sum
;
191 if (upper_bound
< lower_bound
) /* Prob: 66% */
192 /* Return an arbitrary value < LOWER_BOUND. */
198 /* set the info for each string. */
202 /* Set TOO_EXPENSIVE to be approximate square root of input size,
203 bounded below by 4096. */
204 ctxt
.too_expensive
= 1;
205 for (i
= xvec_length
+ yvec_length
; i
!= 0; i
>>= 2)
206 ctxt
.too_expensive
<<= 1;
207 if (ctxt
.too_expensive
< 4096)
208 ctxt
.too_expensive
= 4096;
210 /* Allocate memory for fdiag and bdiag from a thread-local pool. */
211 fdiag_len
= length_sum
+ 3;
212 gl_once (keys_init_once
, keys_init
);
213 buffer
= gl_tls_get (buffer_key
);
214 bufmax
= (uintptr_t) gl_tls_get (bufmax_key
);
215 if (fdiag_len
> bufmax
)
217 /* Need more memory. */
219 if (fdiag_len
> bufmax
)
221 /* Calling xrealloc would be a waste: buffer's contents does not need
224 buffer
= xnmalloc (bufmax
, 2 * sizeof *buffer
);
225 gl_tls_set (buffer_key
, buffer
);
226 gl_tls_set (bufmax_key
, (void *) (uintptr_t) bufmax
);
228 ctxt
.fdiag
= buffer
+ yvec_length
+ 1;
229 ctxt
.bdiag
= ctxt
.fdiag
+ fdiag_len
;
231 /* The edit_count is only ever increased. The computation can be aborted
233 (xvec_length + yvec_length - edit_count) / (xvec_length + yvec_length)
236 edit_count > (xvec_length + yvec_length) * (1 - lower_bound)
238 edit_count > floor((xvec_length + yvec_length) * (1 - lower_bound)).
239 We need to add an epsilon inside the floor(...) argument, to neutralize
241 ctxt
.edit_count_limit
=
243 ? (ptrdiff_t) (length_sum
* (1.0 - lower_bound
+ 0.000001))
246 /* Now do the main comparison algorithm */
247 ctxt
.edit_count
= - ctxt
.edit_count_limit
;
248 if (compareseq (0, xvec_length
, 0, yvec_length
, 0, &ctxt
)) /* Prob: 98% */
249 /* The edit_count passed the limit. Hence the result would be
250 < lower_bound. We can return any value < lower_bound instead. */
252 ctxt
.edit_count
+= ctxt
.edit_count_limit
;
255 ((number of chars in common) / (average length of the strings)).
257 = xvec_length - (number of calls to NOTE_DELETE)
258 = yvec_length - (number of calls to NOTE_INSERT)
259 = 1/2 * (xvec_length + yvec_length - (number of edits)).
260 This is admittedly biased towards finding that the strings are
261 similar, however it does produce meaningful results. */
262 return ((double) (xvec_length
+ yvec_length
- ctxt
.edit_count
)
263 / (xvec_length
+ yvec_length
));