| 1 |
/* Search algorithm. |
| 2 |
Copyright (C) 1989-1998, 2000, 2002-2003, 2009, 2016, 2025 Free Software Foundation, Inc. |
| 3 |
Written by Douglas C. Schmidt <[email protected]> |
| 4 |
and Bruno Haible <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU GPERF. |
| 7 |
|
| 8 |
This program is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
/* Specification. */ |
| 24 |
#include "search.h" |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> /* declares exit(), rand(), srand() */ |
| 28 |
#include <string.h> /* declares memset(), memcmp() */ |
| 29 |
#include <time.h> /* declares time() */ |
| 30 |
#include <math.h> /* declares exp() */ |
| 31 |
#include <limits.h> /* defines INT_MIN, INT_MAX, UINT_MAX, CHAR_BIT */ |
| 32 |
#include "gl_map.hh" |
| 33 |
#include "gl_hash_map.h" |
| 34 |
#include "options.h" |
| 35 |
#include "arraylist.h" |
| 36 |
#include "hash-table.h" |
| 37 |
|
| 38 |
/* ============================== Portability ============================== */ |
| 39 |
|
| 40 |
/* Assume ISO C++ 'for' scoping rule. */ |
| 41 |
#define for if (0) ; else for |
| 42 |
|
| 43 |
/* Dynamically allocated array with dynamic extent: |
| 44 |
|
| 45 |
Example: |
| 46 |
DYNAMIC_ARRAY (my_array, int, n); |
| 47 |
... |
| 48 |
FREE_DYNAMIC_ARRAY (my_array); |
| 49 |
|
| 50 |
Attention: depending on your implementation my_array is either the array |
| 51 |
itself or a pointer to the array! Always use my_array only as expression! |
| 52 |
*/ |
| 53 |
#if HAVE_DYNAMIC_ARRAY |
| 54 |
#define DYNAMIC_ARRAY(var,eltype,size) eltype var[size] |
| 55 |
#define FREE_DYNAMIC_ARRAY(var) |
| 56 |
#else |
| 57 |
#define DYNAMIC_ARRAY(var,eltype,size) eltype *var = new eltype[size] |
| 58 |
#define FREE_DYNAMIC_ARRAY(var) delete[] var |
| 59 |
#endif |
| 60 |
|
| 61 |
/* ================================ Theory ================================= */ |
| 62 |
|
| 63 |
/* The general form of the hash function is |
| 64 |
|
| 65 |
hash (keyword) = sum (asso_values[keyword[i] + alpha_inc[i]] : i in Pos) |
| 66 |
+ len (keyword) |
| 67 |
|
| 68 |
where Pos is a set of byte positions, |
| 69 |
each alpha_inc[i] is a nonnegative integer, |
| 70 |
each asso_values[c] is a nonnegative integer, |
| 71 |
len (keyword) is the keyword's length if _hash_includes_len, or 0 otherwise. |
| 72 |
|
| 73 |
Theorem 1: If all keywords are different, there is a set Pos such that |
| 74 |
all tuples (keyword[i] : i in Pos) are different. |
| 75 |
|
| 76 |
Theorem 2: If all tuples (keyword[i] : i in Pos) are different, there |
| 77 |
are nonnegative integers alpha_inc[i] such that all multisets |
| 78 |
{keyword[i] + alpha_inc[i] : i in Pos} are different. |
| 79 |
|
| 80 |
Define selchars[keyword] := {keyword[i] + alpha_inc[i] : i in Pos}. |
| 81 |
|
| 82 |
Theorem 3: If all multisets selchars[keyword] are different, there are |
| 83 |
nonnegative integers asso_values[c] such that all hash values |
| 84 |
sum (asso_values[c] : c in selchars[keyword]) are different. |
| 85 |
|
| 86 |
Based on these three facts, we find the hash function in three steps: |
| 87 |
|
| 88 |
Step 1 (Finding good byte positions): |
| 89 |
Find a set Pos, as small as possible, such that all tuples |
| 90 |
(keyword[i] : i in Pos) are different. |
| 91 |
|
| 92 |
Step 2 (Finding good alpha increments): |
| 93 |
Find nonnegative integers alpha_inc[i], as many of them as possible being |
| 94 |
zero, and the others being as small as possible, such that all multisets |
| 95 |
{keyword[i] + alpha_inc[i] : i in Pos} are different. |
| 96 |
|
| 97 |
Step 3 (Finding good asso_values): |
| 98 |
Find asso_values[c] such that all hash (keyword) are different. |
| 99 |
|
| 100 |
In other words, each step finds a projection that is injective on the |
| 101 |
given finite set: |
| 102 |
proj1 : String --> Map (Pos --> N) |
| 103 |
proj2 : Map (Pos --> N) --> Map (Pos --> N) / S(Pos) |
| 104 |
proj3 : Map (Pos --> N) / S(Pos) --> N |
| 105 |
where |
| 106 |
N denotes the set of nonnegative integers, |
| 107 |
Map (A --> B) := Hom_Set (A, B) is the set of maps from A to B, and |
| 108 |
S(Pos) is the symmetric group over Pos. |
| 109 |
|
| 110 |
This was the theory for !_hash_includes_len; if _hash_includes_len, slight |
| 111 |
modifications apply: |
| 112 |
proj1 : String --> Map (Pos --> N) x N |
| 113 |
proj2 : Map (Pos --> N) x N --> Map (Pos --> N) / S(Pos) x N |
| 114 |
proj3 : Map (Pos --> N) / S(Pos) x N --> N |
| 115 |
|
| 116 |
For a case-insensitive hash function, the general form is |
| 117 |
|
| 118 |
hash (keyword) = |
| 119 |
sum (asso_values[alpha_unify[keyword[i] + alpha_inc[i]]] : i in Pos) |
| 120 |
+ len (keyword) |
| 121 |
|
| 122 |
where alpha_unify[c] is chosen so that an upper/lower case change in |
| 123 |
keyword[i] doesn't change alpha_unify[keyword[i] + alpha_inc[i]]. |
| 124 |
*/ |
| 125 |
|
| 126 |
/* ==================== Initialization and Preparation ===================== */ |
| 127 |
|
| 128 |
Search::Search (KeywordExt_List *list) |
| 129 |
: _head (list) |
| 130 |
{ |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
Search::prepare () |
| 135 |
{ |
| 136 |
/* Compute the total number of keywords. */ |
| 137 |
_total_keys = 0; |
| 138 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 139 |
_total_keys++; |
| 140 |
|
| 141 |
/* Compute the minimum and maximum keyword length. */ |
| 142 |
_max_key_len = INT_MIN; |
| 143 |
_min_key_len = INT_MAX; |
| 144 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 145 |
{ |
| 146 |
KeywordExt *keyword = temp->first(); |
| 147 |
|
| 148 |
if (_max_key_len < keyword->_allchars_length) |
| 149 |
_max_key_len = keyword->_allchars_length; |
| 150 |
if (_min_key_len > keyword->_allchars_length) |
| 151 |
_min_key_len = keyword->_allchars_length; |
| 152 |
} |
| 153 |
|
| 154 |
/* Exit program if an empty string is used as keyword, since the comparison |
| 155 |
expressions don't work correctly for looking up an empty string. */ |
| 156 |
if (_min_key_len == 0) |
| 157 |
{ |
| 158 |
fprintf (stderr, "Empty input keyword is not allowed.\n" |
| 159 |
"To recognize an empty input keyword, your code should check for\n" |
| 160 |
"len == 0 before calling the gperf generated lookup function.\n"); |
| 161 |
exit (1); |
| 162 |
} |
| 163 |
|
| 164 |
/* Exit program if the characters in the keywords are not in the required |
| 165 |
range. */ |
| 166 |
if (option[SEVENBIT]) |
| 167 |
{ |
| 168 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 169 |
{ |
| 170 |
KeywordExt *keyword = temp->first(); |
| 171 |
|
| 172 |
const char *k = keyword->_allchars; |
| 173 |
for (int i = keyword->_allchars_length; i > 0; k++, i--) |
| 174 |
if (!(static_cast<unsigned char>(*k) < 128)) |
| 175 |
{ |
| 176 |
fprintf (stderr, "Option --seven-bit has been specified,\n" |
| 177 |
"but keyword \"%.*s\" contains non-ASCII characters.\n" |
| 178 |
"Try removing option --seven-bit.\n", |
| 179 |
keyword->_allchars_length, keyword->_allchars); |
| 180 |
exit (1); |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/* Determine whether the hash function shall include the length. */ |
| 186 |
_hash_includes_len = !(option[NOLENGTH] || (_min_key_len == _max_key_len)); |
| 187 |
} |
| 188 |
|
| 189 |
/* ====================== Finding good byte positions ====================== */ |
| 190 |
|
| 191 |
/* Computes the upper bound on the indices passed to asso_values[], |
| 192 |
assuming no alpha_increments. */ |
| 193 |
unsigned int |
| 194 |
Search::compute_alpha_size () const |
| 195 |
{ |
| 196 |
return (option[SEVENBIT] ? 128 : 256); |
| 197 |
} |
| 198 |
|
| 199 |
/* Computes the unification rules between different asso_values[c], |
| 200 |
assuming no alpha_increments. */ |
| 201 |
unsigned int * |
| 202 |
Search::compute_alpha_unify () const |
| 203 |
{ |
| 204 |
if (option[UPPERLOWER]) |
| 205 |
{ |
| 206 |
/* Uppercase to lowercase mapping. */ |
| 207 |
unsigned int alpha_size = compute_alpha_size(); |
| 208 |
unsigned int *alpha_unify = new unsigned int[alpha_size]; |
| 209 |
for (unsigned int c = 0; c < alpha_size; c++) |
| 210 |
alpha_unify[c] = c; |
| 211 |
for (unsigned int c = 'A'; c <= 'Z'; c++) |
| 212 |
alpha_unify[c] = c + ('a'-'A'); |
| 213 |
return alpha_unify; |
| 214 |
} |
| 215 |
else |
| 216 |
/* Identity mapping. */ |
| 217 |
return NULL; |
| 218 |
} |
| 219 |
|
| 220 |
/* Initializes each keyword's _selchars array. */ |
| 221 |
void |
| 222 |
Search::init_selchars_tuple (const Positions& positions, const unsigned int *alpha_unify) const |
| 223 |
{ |
| 224 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 225 |
temp->first()->init_selchars_tuple(positions, alpha_unify); |
| 226 |
} |
| 227 |
|
| 228 |
/* Deletes each keyword's _selchars array. */ |
| 229 |
void |
| 230 |
Search::delete_selchars () const |
| 231 |
{ |
| 232 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 233 |
temp->first()->delete_selchars(); |
| 234 |
} |
| 235 |
|
| 236 |
/* Count the duplicate keywords that occur with a given set of positions. |
| 237 |
In other words, it returns the difference |
| 238 |
# K - # proj1 (K) |
| 239 |
where K is the multiset of given keywords. */ |
| 240 |
unsigned int |
| 241 |
Search::count_duplicates_tuple (const Positions& positions, const unsigned int *alpha_unify) const |
| 242 |
{ |
| 243 |
/* Run through the keyword list and count the duplicates incrementally. |
| 244 |
The result does not depend on the order of the keyword list, thanks to |
| 245 |
the formula above. */ |
| 246 |
init_selchars_tuple (positions, alpha_unify); |
| 247 |
|
| 248 |
unsigned int count = 0; |
| 249 |
{ |
| 250 |
Hash_Table representatives (_total_keys, !_hash_includes_len); |
| 251 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 252 |
{ |
| 253 |
KeywordExt *keyword = temp->first(); |
| 254 |
if (representatives.insert (keyword)) |
| 255 |
count++; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
delete_selchars (); |
| 260 |
|
| 261 |
return count; |
| 262 |
} |
| 263 |
|
| 264 |
/* Find good key positions. */ |
| 265 |
|
| 266 |
void |
| 267 |
Search::find_positions () |
| 268 |
{ |
| 269 |
/* If the user gave the key positions, we use them. */ |
| 270 |
if (option[POSITIONS]) |
| 271 |
{ |
| 272 |
_key_positions = option.get_key_positions(); |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
/* Compute preliminary alpha_unify table. */ |
| 277 |
unsigned int *alpha_unify = compute_alpha_unify (); |
| 278 |
|
| 279 |
/* 1. Find positions that must occur in order to distinguish duplicates. */ |
| 280 |
Positions mandatory; |
| 281 |
|
| 282 |
if (!option[DUP]) |
| 283 |
{ |
| 284 |
for (KeywordExt_List *l1 = _head; l1 && l1->rest(); l1 = l1->rest()) |
| 285 |
{ |
| 286 |
KeywordExt *keyword1 = l1->first(); |
| 287 |
for (KeywordExt_List *l2 = l1->rest(); l2; l2 = l2->rest()) |
| 288 |
{ |
| 289 |
KeywordExt *keyword2 = l2->first(); |
| 290 |
|
| 291 |
/* If keyword1 and keyword2 have the same length and differ |
| 292 |
in just one position, and it is not the last character, |
| 293 |
this position is mandatory. */ |
| 294 |
if (keyword1->_allchars_length == keyword2->_allchars_length) |
| 295 |
{ |
| 296 |
int n = keyword1->_allchars_length; |
| 297 |
int i; |
| 298 |
for (i = 0; i < n - 1; i++) |
| 299 |
{ |
| 300 |
unsigned char c1 = keyword1->_allchars[i]; |
| 301 |
unsigned char c2 = keyword2->_allchars[i]; |
| 302 |
if (option[UPPERLOWER]) |
| 303 |
{ |
| 304 |
if (c1 >= 'A' && c1 <= 'Z') |
| 305 |
c1 += 'a' - 'A'; |
| 306 |
if (c2 >= 'A' && c2 <= 'Z') |
| 307 |
c2 += 'a' - 'A'; |
| 308 |
} |
| 309 |
if (c1 != c2) |
| 310 |
break; |
| 311 |
} |
| 312 |
if (i < n - 1) |
| 313 |
{ |
| 314 |
int j; |
| 315 |
for (j = i + 1; j < n; j++) |
| 316 |
{ |
| 317 |
unsigned char c1 = keyword1->_allchars[j]; |
| 318 |
unsigned char c2 = keyword2->_allchars[j]; |
| 319 |
if (option[UPPERLOWER]) |
| 320 |
{ |
| 321 |
if (c1 >= 'A' && c1 <= 'Z') |
| 322 |
c1 += 'a' - 'A'; |
| 323 |
if (c2 >= 'A' && c2 <= 'Z') |
| 324 |
c2 += 'a' - 'A'; |
| 325 |
} |
| 326 |
if (c1 != c2) |
| 327 |
break; |
| 328 |
} |
| 329 |
if (j >= n) |
| 330 |
{ |
| 331 |
/* Position i is mandatory. */ |
| 332 |
if (!mandatory.contains (i)) |
| 333 |
mandatory.add (i); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/* 2. Add positions, as long as this decreases the duplicates count. */ |
| 342 |
int imax = (_max_key_len - 1 < Positions::MAX_KEY_POS - 1 |
| 343 |
? _max_key_len - 1 : Positions::MAX_KEY_POS - 1); |
| 344 |
Positions current = mandatory; |
| 345 |
unsigned int current_duplicates_count = |
| 346 |
count_duplicates_tuple (current, alpha_unify); |
| 347 |
for (;;) |
| 348 |
{ |
| 349 |
Positions best; |
| 350 |
unsigned int best_duplicates_count = UINT_MAX; |
| 351 |
|
| 352 |
for (int i = imax; i >= -1; i--) |
| 353 |
if (!current.contains (i)) |
| 354 |
{ |
| 355 |
Positions tryal = current; |
| 356 |
tryal.add (i); |
| 357 |
unsigned int try_duplicates_count = |
| 358 |
count_duplicates_tuple (tryal, alpha_unify); |
| 359 |
|
| 360 |
/* We prefer 'try' to 'best' if it produces less duplicates, |
| 361 |
or if it produces the same number of duplicates but with |
| 362 |
a more efficient hash function. */ |
| 363 |
if (try_duplicates_count < best_duplicates_count |
| 364 |
|| (try_duplicates_count == best_duplicates_count && i >= 0)) |
| 365 |
{ |
| 366 |
best = tryal; |
| 367 |
best_duplicates_count = try_duplicates_count; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* Stop adding positions when it gives no improvement. */ |
| 372 |
if (best_duplicates_count >= current_duplicates_count) |
| 373 |
break; |
| 374 |
|
| 375 |
current = best; |
| 376 |
current_duplicates_count = best_duplicates_count; |
| 377 |
} |
| 378 |
|
| 379 |
/* 3. Remove positions, as long as this doesn't increase the duplicates |
| 380 |
count. */ |
| 381 |
for (;;) |
| 382 |
{ |
| 383 |
Positions best; |
| 384 |
unsigned int best_duplicates_count = UINT_MAX; |
| 385 |
|
| 386 |
for (int i = imax; i >= -1; i--) |
| 387 |
if (current.contains (i) && !mandatory.contains (i)) |
| 388 |
{ |
| 389 |
Positions tryal = current; |
| 390 |
tryal.remove (i); |
| 391 |
unsigned int try_duplicates_count = |
| 392 |
count_duplicates_tuple (tryal, alpha_unify); |
| 393 |
|
| 394 |
/* We prefer 'try' to 'best' if it produces less duplicates, |
| 395 |
or if it produces the same number of duplicates but with |
| 396 |
a more efficient hash function. */ |
| 397 |
if (try_duplicates_count < best_duplicates_count |
| 398 |
|| (try_duplicates_count == best_duplicates_count && i == -1)) |
| 399 |
{ |
| 400 |
best = tryal; |
| 401 |
best_duplicates_count = try_duplicates_count; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
/* Stop removing positions when it gives no improvement. */ |
| 406 |
if (best_duplicates_count > current_duplicates_count) |
| 407 |
break; |
| 408 |
|
| 409 |
current = best; |
| 410 |
current_duplicates_count = best_duplicates_count; |
| 411 |
} |
| 412 |
|
| 413 |
/* 4. Replace two positions by one, as long as this doesn't increase the |
| 414 |
duplicates count. */ |
| 415 |
for (;;) |
| 416 |
{ |
| 417 |
Positions best; |
| 418 |
unsigned int best_duplicates_count = UINT_MAX; |
| 419 |
|
| 420 |
/* Loop over all pairs { i1, i2 } of currently selected positions. |
| 421 |
W.l.o.g. we can assume i1 > i2. */ |
| 422 |
for (int i1 = imax; i1 >= -1; i1--) |
| 423 |
if (current.contains (i1) && !mandatory.contains (i1)) |
| 424 |
{ |
| 425 |
for (int i2 = i1 - 1; i2 >= -1; i2--) |
| 426 |
if (current.contains (i2) && !mandatory.contains (i2)) |
| 427 |
{ |
| 428 |
for (int i3 = imax; i3 >= -1; i3--) |
| 429 |
if (!current.contains (i3)) |
| 430 |
{ |
| 431 |
Positions tryal = current; |
| 432 |
tryal.remove (i1); |
| 433 |
tryal.remove (i2); |
| 434 |
tryal.add (i3); |
| 435 |
unsigned int try_duplicates_count = |
| 436 |
count_duplicates_tuple (tryal, alpha_unify); |
| 437 |
|
| 438 |
/* We prefer 'try' to 'best' if it produces less |
| 439 |
duplicates, or if it produces the same number |
| 440 |
of duplicates but with a more efficient hash |
| 441 |
function. */ |
| 442 |
if (try_duplicates_count < best_duplicates_count |
| 443 |
|| (try_duplicates_count == best_duplicates_count |
| 444 |
&& (i1 == -1 || i2 == -1) && i3 >= 0)) |
| 445 |
{ |
| 446 |
best = tryal; |
| 447 |
best_duplicates_count = try_duplicates_count; |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
/* Stop removing positions when it gives no improvement. */ |
| 454 |
if (best_duplicates_count > current_duplicates_count) |
| 455 |
break; |
| 456 |
|
| 457 |
current = best; |
| 458 |
current_duplicates_count = best_duplicates_count; |
| 459 |
} |
| 460 |
|
| 461 |
/* That's it. Hope it's good enough. */ |
| 462 |
_key_positions = current; |
| 463 |
|
| 464 |
if (option[DEBUG]) |
| 465 |
{ |
| 466 |
/* Print the result. */ |
| 467 |
fprintf (stderr, "\nComputed positions: "); |
| 468 |
PositionReverseIterator iter = _key_positions.reviterator(); |
| 469 |
bool seen_lastchar = false; |
| 470 |
bool first = true; |
| 471 |
for (int i; (i = iter.next ()) != PositionReverseIterator::EOS; ) |
| 472 |
{ |
| 473 |
if (!first) |
| 474 |
fprintf (stderr, ", "); |
| 475 |
if (i == Positions::LASTCHAR) |
| 476 |
seen_lastchar = true; |
| 477 |
else |
| 478 |
{ |
| 479 |
fprintf (stderr, "%d", i + 1); |
| 480 |
first = false; |
| 481 |
} |
| 482 |
} |
| 483 |
if (seen_lastchar) |
| 484 |
{ |
| 485 |
if (!first) |
| 486 |
fprintf (stderr, ", "); |
| 487 |
fprintf (stderr, "$"); |
| 488 |
} |
| 489 |
fprintf (stderr, "\n"); |
| 490 |
} |
| 491 |
|
| 492 |
/* Free preliminary alpha_unify table. */ |
| 493 |
delete[] alpha_unify; |
| 494 |
} |
| 495 |
|
| 496 |
/* Count the duplicate keywords that occur with the found set of positions. |
| 497 |
In other words, it returns the difference |
| 498 |
# K - # proj1 (K) |
| 499 |
where K is the multiset of given keywords. */ |
| 500 |
unsigned int |
| 501 |
Search::count_duplicates_tuple () const |
| 502 |
{ |
| 503 |
unsigned int *alpha_unify = compute_alpha_unify (); |
| 504 |
unsigned int count = count_duplicates_tuple (_key_positions, alpha_unify); |
| 505 |
delete[] alpha_unify; |
| 506 |
return count; |
| 507 |
} |
| 508 |
|
| 509 |
/* ===================== Finding good alpha increments ===================== */ |
| 510 |
|
| 511 |
/* Computes the upper bound on the indices passed to asso_values[]. */ |
| 512 |
unsigned int |
| 513 |
Search::compute_alpha_size (const unsigned int *alpha_inc) const |
| 514 |
{ |
| 515 |
unsigned int max_alpha_inc = 0; |
| 516 |
for (int i = 0; i < _max_key_len; i++) |
| 517 |
if (max_alpha_inc < alpha_inc[i]) |
| 518 |
max_alpha_inc = alpha_inc[i]; |
| 519 |
return (option[SEVENBIT] ? 128 : 256) + max_alpha_inc; |
| 520 |
} |
| 521 |
|
| 522 |
/* Computes the unification rules between different asso_values[c]. */ |
| 523 |
unsigned int * |
| 524 |
Search::compute_alpha_unify (const Positions& positions, const unsigned int *alpha_inc) const |
| 525 |
{ |
| 526 |
if (option[UPPERLOWER]) |
| 527 |
{ |
| 528 |
/* Without alpha increments, we would simply unify |
| 529 |
'A' -> 'a', ..., 'Z' -> 'z'. |
| 530 |
But when a keyword contains at position i a character c, |
| 531 |
we have the constraint |
| 532 |
asso_values[tolower(c) + alpha_inc[i]] == |
| 533 |
asso_values[toupper(c) + alpha_inc[i]]. |
| 534 |
This introduces a unification |
| 535 |
toupper(c) + alpha_inc[i] -> tolower(c) + alpha_inc[i]. |
| 536 |
Note that this unification can extend outside the range of |
| 537 |
ASCII letters! But still every unified character pair is at |
| 538 |
a distance of 'a'-'A' = 32, or (after chained unification) |
| 539 |
at a multiple of 32. So in the end the alpha_unify vector has |
| 540 |
the form c -> c + 32 * f(c) where f(c) is a nonnegative |
| 541 |
integer. */ |
| 542 |
unsigned int alpha_size = compute_alpha_size (alpha_inc); |
| 543 |
|
| 544 |
unsigned int *alpha_unify = new unsigned int[alpha_size]; |
| 545 |
for (unsigned int c = 0; c < alpha_size; c++) |
| 546 |
alpha_unify[c] = c; |
| 547 |
|
| 548 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 549 |
{ |
| 550 |
KeywordExt *keyword = temp->first(); |
| 551 |
|
| 552 |
/* Iterate through the selected character positions. */ |
| 553 |
PositionIterator iter = positions.iterator(keyword->_allchars_length); |
| 554 |
|
| 555 |
for (int i; (i = iter.next ()) != PositionIterator::EOS; ) |
| 556 |
{ |
| 557 |
unsigned int c; |
| 558 |
if (i == Positions::LASTCHAR) |
| 559 |
c = static_cast<unsigned char>(keyword->_allchars[keyword->_allchars_length - 1]); |
| 560 |
else if (i < keyword->_allchars_length) |
| 561 |
c = static_cast<unsigned char>(keyword->_allchars[i]); |
| 562 |
else |
| 563 |
abort (); |
| 564 |
if (c >= 'A' && c <= 'Z') |
| 565 |
c += 'a' - 'A'; |
| 566 |
if (c >= 'a' && c <= 'z') |
| 567 |
{ |
| 568 |
if (i != Positions::LASTCHAR) |
| 569 |
c += alpha_inc[i]; |
| 570 |
/* Unify c with c - ('a'-'A'). */ |
| 571 |
unsigned int d = alpha_unify[c]; |
| 572 |
unsigned int b = c - ('a'-'A'); |
| 573 |
for (int a = b; a >= 0 && alpha_unify[a] == b; a -= ('a'-'A')) |
| 574 |
alpha_unify[a] = d; |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
return alpha_unify; |
| 579 |
} |
| 580 |
else |
| 581 |
/* Identity mapping. */ |
| 582 |
return NULL; |
| 583 |
} |
| 584 |
|
| 585 |
/* Initializes each keyword's _selchars array. */ |
| 586 |
void |
| 587 |
Search::init_selchars_multiset (const Positions& positions, const unsigned int *alpha_unify, const unsigned int *alpha_inc) const |
| 588 |
{ |
| 589 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 590 |
temp->first()->init_selchars_multiset(positions, alpha_unify, alpha_inc); |
| 591 |
} |
| 592 |
|
| 593 |
/* Count the duplicate keywords that occur with the given set of positions |
| 594 |
and a given alpha_inc[] array. |
| 595 |
In other words, it returns the difference |
| 596 |
# K - # proj2 (proj1 (K)) |
| 597 |
where K is the multiset of given keywords. */ |
| 598 |
unsigned int |
| 599 |
Search::count_duplicates_multiset (const unsigned int *alpha_inc) const |
| 600 |
{ |
| 601 |
/* Run through the keyword list and count the duplicates incrementally. |
| 602 |
The result does not depend on the order of the keyword list, thanks to |
| 603 |
the formula above. */ |
| 604 |
unsigned int *alpha_unify = compute_alpha_unify (_key_positions, alpha_inc); |
| 605 |
init_selchars_multiset (_key_positions, alpha_unify, alpha_inc); |
| 606 |
|
| 607 |
unsigned int count = 0; |
| 608 |
{ |
| 609 |
Hash_Table representatives (_total_keys, !_hash_includes_len); |
| 610 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 611 |
{ |
| 612 |
KeywordExt *keyword = temp->first(); |
| 613 |
if (representatives.insert (keyword)) |
| 614 |
count++; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
delete_selchars (); |
| 619 |
delete[] alpha_unify; |
| 620 |
|
| 621 |
return count; |
| 622 |
} |
| 623 |
|
| 624 |
/* Find good _alpha_inc[]. */ |
| 625 |
|
| 626 |
void |
| 627 |
Search::find_alpha_inc () |
| 628 |
{ |
| 629 |
/* The goal is to choose _alpha_inc[] such that it doesn't introduce |
| 630 |
artificial duplicates. |
| 631 |
In other words, the goal is # proj2 (proj1 (K)) = # proj1 (K). */ |
| 632 |
unsigned int duplicates_goal = count_duplicates_tuple (); |
| 633 |
|
| 634 |
/* Start with zero increments. This is sufficient in most cases. */ |
| 635 |
unsigned int *current = new unsigned int [_max_key_len]; |
| 636 |
for (int i = 0; i < _max_key_len; i++) |
| 637 |
current[i] = 0; |
| 638 |
unsigned int current_duplicates_count = count_duplicates_multiset (current); |
| 639 |
|
| 640 |
if (current_duplicates_count > duplicates_goal) |
| 641 |
{ |
| 642 |
/* Look which _alpha_inc[i] we are free to increment. */ |
| 643 |
unsigned int nindices; |
| 644 |
{ |
| 645 |
nindices = 0; |
| 646 |
PositionIterator iter = _key_positions.iterator(_max_key_len); |
| 647 |
for (;;) |
| 648 |
{ |
| 649 |
int key_pos = iter.next (); |
| 650 |
if (key_pos == PositionIterator::EOS) |
| 651 |
break; |
| 652 |
if (key_pos != Positions::LASTCHAR) |
| 653 |
nindices++; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
DYNAMIC_ARRAY (indices, unsigned int, nindices); |
| 658 |
{ |
| 659 |
unsigned int j = 0; |
| 660 |
PositionIterator iter = _key_positions.iterator(_max_key_len); |
| 661 |
for (;;) |
| 662 |
{ |
| 663 |
int key_pos = iter.next (); |
| 664 |
if (key_pos == PositionIterator::EOS) |
| 665 |
break; |
| 666 |
if (key_pos != Positions::LASTCHAR) |
| 667 |
indices[j++] = key_pos; |
| 668 |
} |
| 669 |
if (!(j == nindices)) |
| 670 |
abort (); |
| 671 |
} |
| 672 |
|
| 673 |
/* Perform several rounds of searching for a good alpha increment. |
| 674 |
Each round reduces the number of artificial collisions by adding |
| 675 |
an increment in a single key position. */ |
| 676 |
DYNAMIC_ARRAY (best, unsigned int, _max_key_len); |
| 677 |
DYNAMIC_ARRAY (tryal, unsigned int, _max_key_len); |
| 678 |
do |
| 679 |
{ |
| 680 |
/* An increment of 1 is not always enough. Try higher increments |
| 681 |
also. */ |
| 682 |
for (unsigned int inc = 1; ; inc++) |
| 683 |
{ |
| 684 |
unsigned int best_duplicates_count = UINT_MAX; |
| 685 |
|
| 686 |
for (unsigned int j = 0; j < nindices; j++) |
| 687 |
{ |
| 688 |
memcpy (tryal, current, _max_key_len * sizeof (unsigned int)); |
| 689 |
tryal[indices[j]] += inc; |
| 690 |
unsigned int try_duplicates_count = |
| 691 |
count_duplicates_multiset (tryal); |
| 692 |
|
| 693 |
/* We prefer 'try' to 'best' if it produces less |
| 694 |
duplicates. */ |
| 695 |
if (try_duplicates_count < best_duplicates_count) |
| 696 |
{ |
| 697 |
memcpy (best, tryal, _max_key_len * sizeof (unsigned int)); |
| 698 |
best_duplicates_count = try_duplicates_count; |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
/* Stop this round when we got an improvement. */ |
| 703 |
if (best_duplicates_count < current_duplicates_count) |
| 704 |
{ |
| 705 |
memcpy (current, best, _max_key_len * sizeof (unsigned int)); |
| 706 |
current_duplicates_count = best_duplicates_count; |
| 707 |
break; |
| 708 |
} |
| 709 |
} |
| 710 |
} |
| 711 |
while (current_duplicates_count > duplicates_goal); |
| 712 |
FREE_DYNAMIC_ARRAY (tryal); |
| 713 |
FREE_DYNAMIC_ARRAY (best); |
| 714 |
|
| 715 |
if (option[DEBUG]) |
| 716 |
{ |
| 717 |
/* Print the result. */ |
| 718 |
fprintf (stderr, "\nComputed alpha increments: "); |
| 719 |
bool first = true; |
| 720 |
for (unsigned int j = nindices; j-- > 0; ) |
| 721 |
if (current[indices[j]] != 0) |
| 722 |
{ |
| 723 |
if (!first) |
| 724 |
fprintf (stderr, ", "); |
| 725 |
fprintf (stderr, "%u:+%u", |
| 726 |
indices[j] + 1, current[indices[j]]); |
| 727 |
first = false; |
| 728 |
} |
| 729 |
fprintf (stderr, "\n"); |
| 730 |
} |
| 731 |
FREE_DYNAMIC_ARRAY (indices); |
| 732 |
} |
| 733 |
|
| 734 |
_alpha_inc = current; |
| 735 |
_alpha_size = compute_alpha_size (_alpha_inc); |
| 736 |
_alpha_unify = compute_alpha_unify (_key_positions, _alpha_inc); |
| 737 |
} |
| 738 |
|
| 739 |
/* ======================= Finding good asso_values ======================== */ |
| 740 |
|
| 741 |
/* Initializes the asso_values[] related parameters. */ |
| 742 |
|
| 743 |
void |
| 744 |
Search::prepare_asso_values () |
| 745 |
{ |
| 746 |
KeywordExt_List *temp; |
| 747 |
|
| 748 |
/* Initialize each keyword's _selchars array. */ |
| 749 |
init_selchars_multiset(_key_positions, _alpha_unify, _alpha_inc); |
| 750 |
|
| 751 |
/* Compute the maximum _selchars_length over all keywords. */ |
| 752 |
_max_selchars_length = _key_positions.iterator(_max_key_len).remaining(); |
| 753 |
|
| 754 |
/* Check for duplicates, i.e. keywords with the same _selchars array |
| 755 |
(and - if _hash_includes_len - also the same length). |
| 756 |
We deal with these by building an equivalence class, so that only |
| 757 |
1 keyword is representative of the entire collection. Only this |
| 758 |
representative remains in the keyword list; the others are accessible |
| 759 |
through the _duplicate_link chain, starting at the representative. |
| 760 |
This *greatly* simplifies processing during later stages of the program. |
| 761 |
Set _total_duplicates and _list_len = _total_keys - _total_duplicates. */ |
| 762 |
{ |
| 763 |
_list_len = _total_keys; |
| 764 |
_total_duplicates = 0; |
| 765 |
/* Make hash table for efficiency. */ |
| 766 |
Hash_Table representatives (_list_len, !_hash_includes_len); |
| 767 |
|
| 768 |
KeywordExt_List *prev = NULL; /* list node before temp */ |
| 769 |
for (temp = _head; temp; ) |
| 770 |
{ |
| 771 |
KeywordExt *keyword = temp->first(); |
| 772 |
KeywordExt *other_keyword = representatives.insert (keyword); |
| 773 |
KeywordExt_List *garbage = NULL; |
| 774 |
|
| 775 |
if (other_keyword) |
| 776 |
{ |
| 777 |
_total_duplicates++; |
| 778 |
_list_len--; |
| 779 |
/* Remove keyword from the main list. */ |
| 780 |
prev->rest() = temp->rest(); |
| 781 |
garbage = temp; |
| 782 |
/* And insert it on other_keyword's duplicate list. */ |
| 783 |
keyword->_duplicate_link = other_keyword->_duplicate_link; |
| 784 |
other_keyword->_duplicate_link = keyword; |
| 785 |
|
| 786 |
/* Complain if user hasn't enabled the duplicate option. */ |
| 787 |
if (!option[DUP] || option[DEBUG]) |
| 788 |
{ |
| 789 |
fprintf (stderr, "Key link: \"%.*s\" = \"%.*s\", with key set \"", |
| 790 |
keyword->_allchars_length, keyword->_allchars, |
| 791 |
other_keyword->_allchars_length, other_keyword->_allchars); |
| 792 |
for (int j = 0; j < keyword->_selchars_length; j++) |
| 793 |
putc (keyword->_selchars[j], stderr); |
| 794 |
fprintf (stderr, "\".\n"); |
| 795 |
} |
| 796 |
} |
| 797 |
else |
| 798 |
{ |
| 799 |
keyword->_duplicate_link = NULL; |
| 800 |
prev = temp; |
| 801 |
} |
| 802 |
temp = temp->rest(); |
| 803 |
if (garbage) |
| 804 |
delete garbage; |
| 805 |
} |
| 806 |
if (option[DEBUG]) |
| 807 |
representatives.dump(); |
| 808 |
} |
| 809 |
|
| 810 |
/* Exit program if duplicates exists and option[DUP] not set, since we |
| 811 |
don't want to continue in this case. (We don't want to turn on |
| 812 |
option[DUP] implicitly, because the generated code is usually much |
| 813 |
slower. */ |
| 814 |
if (_total_duplicates) |
| 815 |
{ |
| 816 |
if (option[DUP]) |
| 817 |
fprintf (stderr, "%d input keys have identical hash values, examine output carefully...\n", |
| 818 |
_total_duplicates); |
| 819 |
else |
| 820 |
{ |
| 821 |
fprintf (stderr, "%d input keys have identical hash values,\n", |
| 822 |
_total_duplicates); |
| 823 |
if (option[POSITIONS]) |
| 824 |
fprintf (stderr, "try different key positions or use option -D.\n"); |
| 825 |
else |
| 826 |
fprintf (stderr, "use option -D.\n"); |
| 827 |
exit (1); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
/* Compute the occurrences of each character in the alphabet. */ |
| 832 |
_occurrences = new int[_alpha_size]; |
| 833 |
memset (_occurrences, 0, _alpha_size * sizeof (_occurrences[0])); |
| 834 |
for (temp = _head; temp; temp = temp->rest()) |
| 835 |
{ |
| 836 |
KeywordExt *keyword = temp->first(); |
| 837 |
const unsigned int *ptr = keyword->_selchars; |
| 838 |
for (int count = keyword->_selchars_length; count > 0; ptr++, count--) |
| 839 |
_occurrences[*ptr]++; |
| 840 |
} |
| 841 |
|
| 842 |
/* Memory allocation. */ |
| 843 |
_asso_values = new int[_alpha_size]; |
| 844 |
|
| 845 |
/* Memory allocation in each Keyword. */ |
| 846 |
for (temp = _head; temp; temp = temp->rest()) |
| 847 |
{ |
| 848 |
KeywordExt *keyword = temp->first(); |
| 849 |
keyword->_undetermined_chars = new unsigned int[keyword->_selchars_length]; |
| 850 |
} |
| 851 |
|
| 852 |
int non_linked_length = _list_len; |
| 853 |
unsigned int asso_value_max; |
| 854 |
|
| 855 |
asso_value_max = |
| 856 |
static_cast<unsigned int>(non_linked_length * option.get_size_multiple()); |
| 857 |
/* Round up to the next power of two. This makes it easy to ensure |
| 858 |
an _asso_value[c] is >= 0 and < asso_value_max. Also, the jump value |
| 859 |
being odd, it guarantees that Search::try_asso_value() will iterate |
| 860 |
through different values for _asso_value[c]. */ |
| 861 |
if (asso_value_max == 0) |
| 862 |
asso_value_max = 1; |
| 863 |
asso_value_max |= asso_value_max >> 1; |
| 864 |
asso_value_max |= asso_value_max >> 2; |
| 865 |
asso_value_max |= asso_value_max >> 4; |
| 866 |
asso_value_max |= asso_value_max >> 8; |
| 867 |
asso_value_max |= asso_value_max >> 16; |
| 868 |
asso_value_max++; |
| 869 |
_asso_value_max = asso_value_max; |
| 870 |
|
| 871 |
/* Given the bound for _asso_values[c], we have a bound for the possible |
| 872 |
hash values, as computed in compute_hash(). */ |
| 873 |
_max_hash_value = (_hash_includes_len ? _max_key_len : 0) |
| 874 |
+ (_asso_value_max - 1) * _max_selchars_length; |
| 875 |
/* Allocate a sparse bit vector for detection of collisions of hash |
| 876 |
values. */ |
| 877 |
_collision_detector = new Bool_Array (_max_hash_value + 1); |
| 878 |
|
| 879 |
if (option[DEBUG]) |
| 880 |
{ |
| 881 |
fprintf (stderr, "total non-linked keys = %d\nmaximum associated value is %d" |
| 882 |
"\nmaximum size of generated hash table is %d\n", |
| 883 |
non_linked_length, asso_value_max, _max_hash_value); |
| 884 |
|
| 885 |
int field_width; |
| 886 |
|
| 887 |
field_width = 0; |
| 888 |
{ |
| 889 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 890 |
{ |
| 891 |
KeywordExt *keyword = temp->first(); |
| 892 |
if (field_width < keyword->_selchars_length) |
| 893 |
field_width = keyword->_selchars_length; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
fprintf (stderr, "\ndumping the keyword list without duplicates\n"); |
| 898 |
fprintf (stderr, "keyword #, %*s, keyword\n", field_width, "keysig"); |
| 899 |
int i = 0; |
| 900 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 901 |
{ |
| 902 |
KeywordExt *keyword = temp->first(); |
| 903 |
fprintf (stderr, "%9d, ", ++i); |
| 904 |
if (field_width > keyword->_selchars_length) |
| 905 |
fprintf (stderr, "%*s", field_width - keyword->_selchars_length, ""); |
| 906 |
for (int j = 0; j < keyword->_selchars_length; j++) |
| 907 |
putc (keyword->_selchars[j], stderr); |
| 908 |
fprintf (stderr, ", %.*s\n", |
| 909 |
keyword->_allchars_length, keyword->_allchars); |
| 910 |
} |
| 911 |
fprintf (stderr, "\nend of keyword list\n\n"); |
| 912 |
} |
| 913 |
|
| 914 |
if (option[RANDOM] || option.get_jump () == 0) |
| 915 |
/* We will use rand(), so initialize the random number generator. */ |
| 916 |
srand (static_cast<long>(time (0))); |
| 917 |
|
| 918 |
_initial_asso_value = (option[RANDOM] ? -1 : option.get_initial_asso_value ()); |
| 919 |
_jump = option.get_jump (); |
| 920 |
} |
| 921 |
|
| 922 |
/* Finds some _asso_values[] that fit. */ |
| 923 |
|
| 924 |
/* The idea is to choose the _asso_values[] one by one, in a way that |
| 925 |
a choice that has been made never needs to be undone later. This |
| 926 |
means that we split the work into several steps. Each step chooses |
| 927 |
one or more _asso_values[c]. The result of choosing one or more |
| 928 |
_asso_values[c] is that the partitioning of the keyword set gets |
| 929 |
broader. |
| 930 |
Look at this partitioning: After every step, the _asso_values[] of a |
| 931 |
certain set C of characters are undetermined. (At the beginning, C |
| 932 |
is the set of characters c with _occurrences[c] > 0. At the end, C |
| 933 |
is empty.) To each keyword K, we associate the multiset of _selchars |
| 934 |
for which the _asso_values[] are undetermined: |
| 935 |
K --> K->_selchars ∩ C. |
| 936 |
Consider two keywords equivalent if their value under this mapping is |
| 937 |
the same. This introduces an equivalence relation on the set of |
| 938 |
keywords. The equivalence classes partition the keyword set. (At the |
| 939 |
beginning, the partition is the finest possible: each K is an equivalence |
| 940 |
class by itself, because all K have a different _selchars. At the end, |
| 941 |
all K have been merged into a single equivalence class.) |
| 942 |
The partition before a step is always a refinement of the partition |
| 943 |
after the step. |
| 944 |
We choose the steps in such a way that the partition really becomes |
| 945 |
broader at each step. (A step that only chooses an _asso_values[c] |
| 946 |
without changing the partition is better merged with the previous step, |
| 947 |
to avoid useless backtracking.) */ |
| 948 |
|
| 949 |
struct EquivalenceClass |
| 950 |
{ |
| 951 |
/* The keywords in this equivalence class. */ |
| 952 |
ArrayList<KeywordExt*> _keywords; |
| 953 |
|
| 954 |
/* Constructor. */ |
| 955 |
EquivalenceClass () : _keywords () {} |
| 956 |
}; |
| 957 |
|
| 958 |
struct Partition |
| 959 |
{ |
| 960 |
/* The equivalence classes in this partition. */ |
| 961 |
ArrayList<EquivalenceClass> _equclasses; |
| 962 |
|
| 963 |
/* Constructor. */ |
| 964 |
Partition () : _equclasses () {} |
| 965 |
}; |
| 966 |
|
| 967 |
struct Step |
| 968 |
{ |
| 969 |
/* The characters whose values are being determined in this step. */ |
| 970 |
unsigned int _changing_count; /* > 0 */ |
| 971 |
unsigned int * _changing; |
| 972 |
/* Exclusive upper bound for the _asso_values[c] of this step. |
| 973 |
A power of 2. */ |
| 974 |
unsigned int _asso_value_max; |
| 975 |
/* The characters whose values will be determined after this step. */ |
| 976 |
bool * _undetermined; |
| 977 |
/* The keyword set partition after this step. */ |
| 978 |
Partition * _partition; |
| 979 |
/* The expected number of iterations in this step. */ |
| 980 |
double _expected_lower; |
| 981 |
double _expected_upper; |
| 982 |
|
| 983 |
Step * _next; |
| 984 |
}; |
| 985 |
|
| 986 |
static inline bool |
| 987 |
equals (const unsigned int *ptr1, const unsigned int *ptr2, unsigned int len) |
| 988 |
{ |
| 989 |
while (len > 0) |
| 990 |
{ |
| 991 |
if (*ptr1 != *ptr2) |
| 992 |
return false; |
| 993 |
ptr1++; |
| 994 |
ptr2++; |
| 995 |
len--; |
| 996 |
} |
| 997 |
return true; |
| 998 |
} |
| 999 |
|
| 1000 |
static bool |
| 1001 |
undetermined_equals (KeywordExt *key1, KeywordExt *key2) |
| 1002 |
{ |
| 1003 |
return (key1->_undetermined_chars_length == key2 ->_undetermined_chars_length) |
| 1004 |
&& equals (key1->_undetermined_chars, key2->_undetermined_chars, |
| 1005 |
key1->_undetermined_chars_length); |
| 1006 |
} |
| 1007 |
|
| 1008 |
static size_t |
| 1009 |
undetermined_hashcode (KeywordExt *key) |
| 1010 |
{ |
| 1011 |
return key->_undetermined_chars_hashcode; |
| 1012 |
} |
| 1013 |
|
| 1014 |
/* Compares the equivalence classes cls1, cls2 pointed to by ptr1 and ptr2 |
| 1015 |
and returns |
| 1016 |
< 0 if cls1 is larger than cls2, |
| 1017 |
0 if cls1 and cls2 have the same size, |
| 1018 |
> 0 if cls1 is smaller than cls1. */ |
| 1019 |
static int |
| 1020 |
cmp_equiv_classes (void const *ptr1, void const *ptr2) |
| 1021 |
{ |
| 1022 |
EquivalenceClass const *cls1 = static_cast<EquivalenceClass const *>(ptr1); |
| 1023 |
EquivalenceClass const *cls2 = static_cast<EquivalenceClass const *>(ptr2); |
| 1024 |
return _GL_CMP (cls2->_keywords.size(), cls1->_keywords.size()); |
| 1025 |
} |
| 1026 |
|
| 1027 |
Partition * |
| 1028 |
Search::compute_partition (bool *undetermined) const |
| 1029 |
{ |
| 1030 |
/* Prepare the use of the hash-map: For each keyword, |
| 1031 |
compute the undetermined characters and their hash code. */ |
| 1032 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 1033 |
{ |
| 1034 |
KeywordExt *keyword = temp->first(); |
| 1035 |
|
| 1036 |
/* This scratch memory, an array of length keyword->_selchars_length, |
| 1037 |
was allocated earlier. */ |
| 1038 |
unsigned int *undetermined_chars = keyword->_undetermined_chars; |
| 1039 |
unsigned int undetermined_chars_length = 0; |
| 1040 |
|
| 1041 |
for (int i = 0; i < keyword->_selchars_length; i++) |
| 1042 |
if (undetermined[keyword->_selchars[i]]) |
| 1043 |
undetermined_chars[undetermined_chars_length++] = keyword->_selchars[i]; |
| 1044 |
keyword->_undetermined_chars_length = undetermined_chars_length; |
| 1045 |
|
| 1046 |
{ |
| 1047 |
const int SIZE_BITS = sizeof (size_t) * CHAR_BIT; |
| 1048 |
size_t h = undetermined_chars_length; |
| 1049 |
for (unsigned int i = 0; i < undetermined_chars_length; i++) |
| 1050 |
h = undetermined_chars[i] * 641 + ((h << 9) | (h >> (SIZE_BITS - 9))); |
| 1051 |
keyword->_undetermined_chars_hashcode = h; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
Partition *partition = new Partition (); |
| 1056 |
{ |
| 1057 |
/* A hash-map that maps each keyword to the EquivalenceClass that contains |
| 1058 |
it. */ |
| 1059 |
gl_Map<KeywordExt *, void const *> |
| 1060 |
map (GL_HASH_MAP, undetermined_equals, undetermined_hashcode, NULL, NULL); |
| 1061 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 1062 |
{ |
| 1063 |
KeywordExt *keyword = temp->first(); |
| 1064 |
|
| 1065 |
/* Look up the equivalence class to which this keyword belongs. */ |
| 1066 |
size_t pindex = reinterpret_cast<size_t>(map.get(keyword)) - 1; |
| 1067 |
if (pindex == (size_t)-1) |
| 1068 |
{ |
| 1069 |
/* Allocate a new EquivalenceClass and add it as the last element to |
| 1070 |
the partition. */ |
| 1071 |
char temp_storage[sizeof (EquivalenceClass)]; |
| 1072 |
EquivalenceClass * temp_equclass = new (temp_storage) EquivalenceClass (); |
| 1073 |
pindex = partition->_equclasses.add_last (* temp_equclass); |
| 1074 |
|
| 1075 |
/* Map this keyword (and all equivalent ones that will be seen later) |
| 1076 |
to the equivalence class number pindex. */ |
| 1077 |
map.put(keyword, reinterpret_cast<void *>(pindex + 1)); |
| 1078 |
} |
| 1079 |
|
| 1080 |
/* Add the keyword to the equivalence class number pindex. */ |
| 1081 |
partition->_equclasses.get_at(pindex)._keywords.add_last (keyword); |
| 1082 |
} |
| 1083 |
map.free (); |
| 1084 |
} |
| 1085 |
|
| 1086 |
/* Sort the equivalence classes according to decreasing size. |
| 1087 |
This results in a speedup of find_asso_values, because on average, a large |
| 1088 |
equivalence class has a higher probability for a collision than a smaller |
| 1089 |
equivalence class. (An equivalence class with just 1 keyword never has |
| 1090 |
a collision.) */ |
| 1091 |
if (partition->_equclasses.size() > 1) |
| 1092 |
qsort (&partition->_equclasses.get_at(0), partition->_equclasses.size(), |
| 1093 |
sizeof (EquivalenceClass), cmp_equiv_classes); |
| 1094 |
|
| 1095 |
return partition; |
| 1096 |
} |
| 1097 |
|
| 1098 |
static void |
| 1099 |
delete_partition (Partition *partition) |
| 1100 |
{ |
| 1101 |
delete partition; |
| 1102 |
} |
| 1103 |
|
| 1104 |
/* Compute the possible number of collisions when _asso_values[c] is |
| 1105 |
chosen, leading to the given partition. */ |
| 1106 |
unsigned int |
| 1107 |
Search::count_possible_collisions (Partition *partition, unsigned int c) const |
| 1108 |
{ |
| 1109 |
/* Every equivalence class p is split according to the frequency of |
| 1110 |
occurrence of c, leading to equivalence classes p1, p2, ... |
| 1111 |
This leads to (|p|^2 - |p1|^2 - |p2|^2 - ...)/2 possible collisions. |
| 1112 |
Return the sum of this expression over all equivalence classes. */ |
| 1113 |
unsigned int sum = 0; |
| 1114 |
unsigned int m = _max_selchars_length; |
| 1115 |
DYNAMIC_ARRAY (split_cardinalities, unsigned int, m + 1); |
| 1116 |
size_t partition_size = partition->_equclasses.size(); |
| 1117 |
for (size_t pindex = 0; pindex < partition_size; pindex++) |
| 1118 |
{ |
| 1119 |
EquivalenceClass *cls = & partition->_equclasses.get_at(pindex); |
| 1120 |
|
| 1121 |
for (unsigned int i = 0; i <= m; i++) |
| 1122 |
split_cardinalities[i] = 0; |
| 1123 |
|
| 1124 |
size_t cls_size = cls->_keywords.size(); |
| 1125 |
for (size_t index = 0; index < cls_size; index++) |
| 1126 |
{ |
| 1127 |
KeywordExt *keyword = cls->_keywords.get_at(index); |
| 1128 |
|
| 1129 |
unsigned int count = 0; |
| 1130 |
for (int i = 0; i < keyword->_selchars_length; i++) |
| 1131 |
if (keyword->_selchars[i] == c) |
| 1132 |
count++; |
| 1133 |
|
| 1134 |
split_cardinalities[count]++; |
| 1135 |
} |
| 1136 |
|
| 1137 |
sum += cls->_keywords.size() * cls->_keywords.size(); |
| 1138 |
for (unsigned int i = 0; i <= m; i++) |
| 1139 |
sum -= split_cardinalities[i] * split_cardinalities[i]; |
| 1140 |
} |
| 1141 |
FREE_DYNAMIC_ARRAY (split_cardinalities); |
| 1142 |
return sum; |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* Test whether adding c to the undetermined characters changes the given |
| 1146 |
partition. */ |
| 1147 |
bool |
| 1148 |
Search::unchanged_partition (Partition *partition, unsigned int c) const |
| 1149 |
{ |
| 1150 |
size_t partition_size = partition->_equclasses.size(); |
| 1151 |
for (size_t pindex = 0; pindex < partition_size; pindex++) |
| 1152 |
{ |
| 1153 |
EquivalenceClass *cls = & partition->_equclasses.get_at(pindex); |
| 1154 |
|
| 1155 |
unsigned int first_count = UINT_MAX; |
| 1156 |
|
| 1157 |
size_t cls_size = cls->_keywords.size(); |
| 1158 |
for (size_t index = 0; index < cls_size; index++) |
| 1159 |
{ |
| 1160 |
KeywordExt *keyword = cls->_keywords.get_at(index); |
| 1161 |
|
| 1162 |
unsigned int count = 0; |
| 1163 |
for (int i = 0; i < keyword->_selchars_length; i++) |
| 1164 |
if (keyword->_selchars[i] == c) |
| 1165 |
count++; |
| 1166 |
|
| 1167 |
if (index == 0) |
| 1168 |
first_count = count; |
| 1169 |
else if (count != first_count) |
| 1170 |
/* c would split this equivalence class. */ |
| 1171 |
return false; |
| 1172 |
} |
| 1173 |
} |
| 1174 |
return true; |
| 1175 |
} |
| 1176 |
|
| 1177 |
void |
| 1178 |
Search::find_asso_values () |
| 1179 |
{ |
| 1180 |
Step *steps; |
| 1181 |
|
| 1182 |
/* Determine the steps, starting with the last one. */ |
| 1183 |
{ |
| 1184 |
bool *undetermined; |
| 1185 |
bool *determined; |
| 1186 |
|
| 1187 |
steps = NULL; |
| 1188 |
|
| 1189 |
undetermined = new bool[_alpha_size]; |
| 1190 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1191 |
undetermined[c] = false; |
| 1192 |
|
| 1193 |
determined = new bool[_alpha_size]; |
| 1194 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1195 |
determined[c] = true; |
| 1196 |
|
| 1197 |
for (;;) |
| 1198 |
{ |
| 1199 |
/* Compute the partition that needs to be refined. */ |
| 1200 |
Partition *partition = compute_partition (undetermined); |
| 1201 |
|
| 1202 |
/* Determine the main character to be chosen in this step. |
| 1203 |
Choosing such a character c has the effect of splitting every |
| 1204 |
equivalence class (according the the frequency of occurrence of c). |
| 1205 |
We choose the c with the minimum number of possible collisions, |
| 1206 |
so that characters which lead to a large number of collisions get |
| 1207 |
handled early during the search. */ |
| 1208 |
unsigned int chosen_c; |
| 1209 |
unsigned int chosen_possible_collisions; |
| 1210 |
{ |
| 1211 |
unsigned int best_c = 0; |
| 1212 |
unsigned int best_possible_collisions = UINT_MAX; |
| 1213 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1214 |
if (_occurrences[c] > 0 && determined[c]) |
| 1215 |
{ |
| 1216 |
unsigned int possible_collisions = |
| 1217 |
count_possible_collisions (partition, c); |
| 1218 |
if (possible_collisions < best_possible_collisions) |
| 1219 |
{ |
| 1220 |
best_c = c; |
| 1221 |
best_possible_collisions = possible_collisions; |
| 1222 |
} |
| 1223 |
} |
| 1224 |
if (best_possible_collisions == UINT_MAX) |
| 1225 |
{ |
| 1226 |
/* All c with _occurrences[c] > 0 are undetermined. We are |
| 1227 |
at the starting situation and don't need any more step. */ |
| 1228 |
delete_partition (partition); |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
chosen_c = best_c; |
| 1232 |
chosen_possible_collisions = best_possible_collisions; |
| 1233 |
} |
| 1234 |
|
| 1235 |
/* We need one more step. */ |
| 1236 |
Step *step = new Step(); |
| 1237 |
|
| 1238 |
step->_undetermined = new bool[_alpha_size]; |
| 1239 |
memcpy (step->_undetermined, undetermined, _alpha_size*sizeof(bool)); |
| 1240 |
|
| 1241 |
step->_partition = partition; |
| 1242 |
|
| 1243 |
/* Now determine how the equivalence classes will be before this |
| 1244 |
step. */ |
| 1245 |
undetermined[chosen_c] = true; |
| 1246 |
Partition *partition_before = compute_partition (undetermined); |
| 1247 |
|
| 1248 |
/* Now determine which other characters should be determined in this |
| 1249 |
step, because they will not change the equivalence classes at |
| 1250 |
this point. It is the set of all c which, for all equivalence |
| 1251 |
classes, have the same frequency of occurrence in every keyword |
| 1252 |
of the equivalence class. */ |
| 1253 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1254 |
if (_occurrences[c] > 0 && determined[c] |
| 1255 |
&& unchanged_partition (partition_before, c)) |
| 1256 |
{ |
| 1257 |
undetermined[c] = true; |
| 1258 |
determined[c] = false; |
| 1259 |
} |
| 1260 |
|
| 1261 |
/* chosen_c must be one of these. */ |
| 1262 |
if (determined[chosen_c]) |
| 1263 |
abort (); |
| 1264 |
|
| 1265 |
/* Now the set of changing characters of this step. */ |
| 1266 |
unsigned int changing_count; |
| 1267 |
|
| 1268 |
changing_count = 0; |
| 1269 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1270 |
if (undetermined[c] && !step->_undetermined[c]) |
| 1271 |
changing_count++; |
| 1272 |
|
| 1273 |
unsigned int *changing = new unsigned int[changing_count]; |
| 1274 |
changing_count = 0; |
| 1275 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1276 |
if (undetermined[c] && !step->_undetermined[c]) |
| 1277 |
changing[changing_count++] = c; |
| 1278 |
|
| 1279 |
/* The set of changing characters is non-empty, since it contains |
| 1280 |
chosen_c. */ |
| 1281 |
if (changing_count == 0) |
| 1282 |
abort (); |
| 1283 |
|
| 1284 |
step->_changing = changing; |
| 1285 |
step->_changing_count = changing_count; |
| 1286 |
|
| 1287 |
step->_asso_value_max = _asso_value_max; |
| 1288 |
|
| 1289 |
step->_expected_lower = |
| 1290 |
exp (static_cast<double>(chosen_possible_collisions) |
| 1291 |
/ static_cast<double>(_max_hash_value)); |
| 1292 |
step->_expected_upper = |
| 1293 |
exp (static_cast<double>(chosen_possible_collisions) |
| 1294 |
/ static_cast<double>(_asso_value_max)); |
| 1295 |
|
| 1296 |
delete_partition (partition_before); |
| 1297 |
|
| 1298 |
step->_next = steps; |
| 1299 |
steps = step; |
| 1300 |
} |
| 1301 |
|
| 1302 |
delete[] determined; |
| 1303 |
delete[] undetermined; |
| 1304 |
} |
| 1305 |
|
| 1306 |
if (option[DEBUG]) |
| 1307 |
{ |
| 1308 |
unsigned int stepno = 0; |
| 1309 |
for (Step *step = steps; step; step = step->_next) |
| 1310 |
{ |
| 1311 |
stepno++; |
| 1312 |
fprintf (stderr, "Step %u chooses _asso_values[", stepno); |
| 1313 |
for (unsigned int i = 0; i < step->_changing_count; i++) |
| 1314 |
{ |
| 1315 |
if (i > 0) |
| 1316 |
fprintf (stderr, ","); |
| 1317 |
fprintf (stderr, "'%c'", step->_changing[i]); |
| 1318 |
} |
| 1319 |
fprintf (stderr, "], expected number of iterations between %g and %g.\n", |
| 1320 |
step->_expected_lower, step->_expected_upper); |
| 1321 |
fprintf (stderr, "Keyword equivalence classes:\n"); |
| 1322 |
Partition *partition = step->_partition; |
| 1323 |
size_t partition_size = partition->_equclasses.size(); |
| 1324 |
for (size_t pindex = 0; pindex < partition_size; pindex++) |
| 1325 |
{ |
| 1326 |
EquivalenceClass *cls = & partition->_equclasses.get_at(pindex); |
| 1327 |
fprintf (stderr, "\n"); |
| 1328 |
size_t cls_size = cls->_keywords.size(); |
| 1329 |
for (size_t index = 0; index < cls_size; index++) |
| 1330 |
{ |
| 1331 |
KeywordExt *keyword = cls->_keywords.get_at(index); |
| 1332 |
fprintf (stderr, " %.*s\n", |
| 1333 |
keyword->_allchars_length, keyword->_allchars); |
| 1334 |
} |
| 1335 |
} |
| 1336 |
fprintf (stderr, "\n"); |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
/* Initialize _asso_values[]. (The value given here matters only |
| 1341 |
for those c which occur in all keywords with equal multiplicity.) */ |
| 1342 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1343 |
_asso_values[c] = 0; |
| 1344 |
|
| 1345 |
unsigned int stepno = 0; |
| 1346 |
for (Step *step = steps; step; step = step->_next) |
| 1347 |
{ |
| 1348 |
stepno++; |
| 1349 |
|
| 1350 |
/* Initialize the asso_values[]. */ |
| 1351 |
unsigned int k = step->_changing_count; |
| 1352 |
if (k == 0) |
| 1353 |
abort (); |
| 1354 |
for (unsigned int i = 0; i < k; i++) |
| 1355 |
{ |
| 1356 |
unsigned int c = step->_changing[i]; |
| 1357 |
_asso_values[c] = |
| 1358 |
(_initial_asso_value < 0 ? rand () : _initial_asso_value) |
| 1359 |
& (step->_asso_value_max - 1); |
| 1360 |
} |
| 1361 |
|
| 1362 |
unsigned int iterations = 0; |
| 1363 |
DYNAMIC_ARRAY (iter, unsigned int, k); |
| 1364 |
for (unsigned int i = 0; i < k; i++) |
| 1365 |
iter[i] = 0; |
| 1366 |
unsigned int ii = (_jump != 0 ? k - 1 : 0); |
| 1367 |
|
| 1368 |
for (;;) |
| 1369 |
{ |
| 1370 |
/* Cache some values in local variables, for speed. */ |
| 1371 |
bool hash_includes_len = _hash_includes_len; |
| 1372 |
bool *undetermined = step->_undetermined; |
| 1373 |
int *asso_values = _asso_values; |
| 1374 |
|
| 1375 |
/* Test whether these asso_values[] lead to collisions among |
| 1376 |
the equivalence classes that should be collision-free. */ |
| 1377 |
bool has_collision = false; |
| 1378 |
Partition *partition = step->_partition; |
| 1379 |
size_t partition_size = partition->_equclasses.size(); |
| 1380 |
for (size_t pindex = 0; pindex < partition_size; pindex++) |
| 1381 |
{ |
| 1382 |
EquivalenceClass *cls = & partition->_equclasses.get_at(pindex); |
| 1383 |
|
| 1384 |
/* Iteration Number array is a win, O(1) initialization time! */ |
| 1385 |
_collision_detector->clear (); |
| 1386 |
|
| 1387 |
size_t cls_size = cls->_keywords.size(); |
| 1388 |
for (size_t index = 0; index < cls_size; index++) |
| 1389 |
{ |
| 1390 |
KeywordExt *keyword = cls->_keywords.get_at(index); |
| 1391 |
|
| 1392 |
/* Compute the new hash code for the keyword, leaving apart |
| 1393 |
the yet undetermined asso_values[]. */ |
| 1394 |
int hashcode; |
| 1395 |
{ |
| 1396 |
int sum = hash_includes_len ? keyword->_allchars_length : 0; |
| 1397 |
const unsigned int *p = keyword->_selchars; |
| 1398 |
int i = keyword->_selchars_length; |
| 1399 |
for (; i > 0; p++, i--) |
| 1400 |
if (!undetermined[*p]) |
| 1401 |
sum += asso_values[*p]; |
| 1402 |
hashcode = sum; |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* See whether it collides with another keyword's hash code, |
| 1406 |
from the same equivalence class. */ |
| 1407 |
if (_collision_detector->set_bit (hashcode)) |
| 1408 |
{ |
| 1409 |
has_collision = true; |
| 1410 |
break; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
/* Don't need to continue looking at the other equivalence |
| 1415 |
classes if we already have found a collision. */ |
| 1416 |
if (has_collision) |
| 1417 |
break; |
| 1418 |
} |
| 1419 |
|
| 1420 |
iterations++; |
| 1421 |
if (!has_collision) |
| 1422 |
break; |
| 1423 |
|
| 1424 |
/* Try other asso_values[]. */ |
| 1425 |
if (_jump != 0) |
| 1426 |
{ |
| 1427 |
/* The way we try various values for |
| 1428 |
asso_values[step->_changing[0],...step->_changing[k-1]] |
| 1429 |
is like this: |
| 1430 |
for (bound = 0,1,...) |
| 1431 |
for (ii = 0,...,k-1) |
| 1432 |
iter[ii] := bound |
| 1433 |
iter[0..ii-1] := values <= bound |
| 1434 |
iter[ii+1..k-1] := values < bound |
| 1435 |
and |
| 1436 |
asso_values[step->_changing[i]] = |
| 1437 |
_initial_asso_value + iter[i] * _jump. |
| 1438 |
This makes it more likely to find small asso_values[]. |
| 1439 |
*/ |
| 1440 |
unsigned int bound = iter[ii]; |
| 1441 |
unsigned int i = 0; |
| 1442 |
while (i < ii) |
| 1443 |
{ |
| 1444 |
unsigned int c = step->_changing[i]; |
| 1445 |
iter[i]++; |
| 1446 |
_asso_values[c] = |
| 1447 |
(_asso_values[c] + _jump) & (step->_asso_value_max - 1); |
| 1448 |
if (iter[i] <= bound) |
| 1449 |
goto found_next; |
| 1450 |
_asso_values[c] = |
| 1451 |
(_asso_values[c] - iter[i] * _jump) |
| 1452 |
& (step->_asso_value_max - 1); |
| 1453 |
iter[i] = 0; |
| 1454 |
i++; |
| 1455 |
} |
| 1456 |
i = ii + 1; |
| 1457 |
while (i < k) |
| 1458 |
{ |
| 1459 |
unsigned int c = step->_changing[i]; |
| 1460 |
iter[i]++; |
| 1461 |
_asso_values[c] = |
| 1462 |
(_asso_values[c] + _jump) & (step->_asso_value_max - 1); |
| 1463 |
if (iter[i] < bound) |
| 1464 |
goto found_next; |
| 1465 |
_asso_values[c] = |
| 1466 |
(_asso_values[c] - iter[i] * _jump) |
| 1467 |
& (step->_asso_value_max - 1); |
| 1468 |
iter[i] = 0; |
| 1469 |
i++; |
| 1470 |
} |
| 1471 |
/* Switch from one ii to the next. */ |
| 1472 |
{ |
| 1473 |
unsigned int c = step->_changing[ii]; |
| 1474 |
_asso_values[c] = |
| 1475 |
(_asso_values[c] - bound * _jump) |
| 1476 |
& (step->_asso_value_max - 1); |
| 1477 |
iter[ii] = 0; |
| 1478 |
} |
| 1479 |
/* Here all iter[i] == 0. */ |
| 1480 |
ii++; |
| 1481 |
if (ii == k) |
| 1482 |
{ |
| 1483 |
ii = 0; |
| 1484 |
bound++; |
| 1485 |
if (bound == step->_asso_value_max) |
| 1486 |
{ |
| 1487 |
/* Out of search space! We can either backtrack, or |
| 1488 |
increase the available search space of this step. |
| 1489 |
It seems simpler to choose the latter solution. */ |
| 1490 |
step->_asso_value_max = 2 * step->_asso_value_max; |
| 1491 |
if (step->_asso_value_max > _asso_value_max) |
| 1492 |
{ |
| 1493 |
_asso_value_max = step->_asso_value_max; |
| 1494 |
/* Reinitialize _max_hash_value. */ |
| 1495 |
_max_hash_value = |
| 1496 |
(_hash_includes_len ? _max_key_len : 0) |
| 1497 |
+ (_asso_value_max - 1) * _max_selchars_length; |
| 1498 |
/* Reinitialize _collision_detector. */ |
| 1499 |
delete _collision_detector; |
| 1500 |
_collision_detector = |
| 1501 |
new Bool_Array (_max_hash_value + 1); |
| 1502 |
} |
| 1503 |
} |
| 1504 |
} |
| 1505 |
{ |
| 1506 |
unsigned int c = step->_changing[ii]; |
| 1507 |
iter[ii] = bound; |
| 1508 |
_asso_values[c] = |
| 1509 |
(_asso_values[c] + bound * _jump) |
| 1510 |
& (step->_asso_value_max - 1); |
| 1511 |
} |
| 1512 |
found_next: ; |
| 1513 |
} |
| 1514 |
else |
| 1515 |
{ |
| 1516 |
/* Random. */ |
| 1517 |
unsigned int c = step->_changing[ii]; |
| 1518 |
_asso_values[c] = |
| 1519 |
(_asso_values[c] + rand ()) & (step->_asso_value_max - 1); |
| 1520 |
/* Next time, change the next c. */ |
| 1521 |
ii++; |
| 1522 |
if (ii == k) |
| 1523 |
ii = 0; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
FREE_DYNAMIC_ARRAY (iter); |
| 1527 |
|
| 1528 |
if (option[DEBUG]) |
| 1529 |
{ |
| 1530 |
fprintf (stderr, "Step %u chose _asso_values[", stepno); |
| 1531 |
for (unsigned int i = 0; i < step->_changing_count; i++) |
| 1532 |
{ |
| 1533 |
if (i > 0) |
| 1534 |
fprintf (stderr, ","); |
| 1535 |
fprintf (stderr, "'%c'", step->_changing[i]); |
| 1536 |
} |
| 1537 |
fprintf (stderr, "] in %u iterations.\n", iterations); |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
/* Free allocated memory. */ |
| 1542 |
while (steps != NULL) |
| 1543 |
{ |
| 1544 |
Step *step = steps; |
| 1545 |
steps = step->_next; |
| 1546 |
delete[] step->_changing; |
| 1547 |
delete[] step->_undetermined; |
| 1548 |
delete_partition (step->_partition); |
| 1549 |
delete step; |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
/* Computes a keyword's hash value, relative to the current _asso_values[], |
| 1554 |
and stores it in keyword->_hash_value. */ |
| 1555 |
|
| 1556 |
inline int |
| 1557 |
Search::compute_hash (KeywordExt *keyword) const |
| 1558 |
{ |
| 1559 |
int sum = _hash_includes_len ? keyword->_allchars_length : 0; |
| 1560 |
|
| 1561 |
const unsigned int *p = keyword->_selchars; |
| 1562 |
int i = keyword->_selchars_length; |
| 1563 |
for (; i > 0; p++, i--) |
| 1564 |
sum += _asso_values[*p]; |
| 1565 |
|
| 1566 |
return keyword->_hash_value = sum; |
| 1567 |
} |
| 1568 |
|
| 1569 |
/* Finds good _asso_values[]. */ |
| 1570 |
|
| 1571 |
void |
| 1572 |
Search::find_good_asso_values () |
| 1573 |
{ |
| 1574 |
prepare_asso_values (); |
| 1575 |
|
| 1576 |
/* Search for good _asso_values[]. */ |
| 1577 |
int asso_iteration; |
| 1578 |
if ((asso_iteration = option.get_asso_iterations ()) == 0) |
| 1579 |
/* Try only the given _initial_asso_value and _jump. */ |
| 1580 |
find_asso_values (); |
| 1581 |
else |
| 1582 |
{ |
| 1583 |
/* Try different pairs of _initial_asso_value and _jump, in the |
| 1584 |
following order: |
| 1585 |
(0, 1) |
| 1586 |
(1, 1) |
| 1587 |
(2, 1) (0, 3) |
| 1588 |
(3, 1) (1, 3) |
| 1589 |
(4, 1) (2, 3) (0, 5) |
| 1590 |
(5, 1) (3, 3) (1, 5) |
| 1591 |
..... */ |
| 1592 |
KeywordExt_List *saved_head = _head; |
| 1593 |
int best_initial_asso_value = 0; |
| 1594 |
int best_jump = 1; |
| 1595 |
int *best_asso_values = new int[_alpha_size]; |
| 1596 |
int best_collisions = INT_MAX; |
| 1597 |
int best_max_hash_value = INT_MAX; |
| 1598 |
|
| 1599 |
_initial_asso_value = 0; _jump = 1; |
| 1600 |
for (;;) |
| 1601 |
{ |
| 1602 |
/* Restore the keyword list in its original order. */ |
| 1603 |
_head = copy_list (saved_head); |
| 1604 |
/* Find good _asso_values[]. */ |
| 1605 |
find_asso_values (); |
| 1606 |
/* Test whether it is the best solution so far. */ |
| 1607 |
int collisions = 0; |
| 1608 |
int max_hash_value = INT_MIN; |
| 1609 |
_collision_detector->clear (); |
| 1610 |
for (KeywordExt_List *ptr = _head; ptr; ptr = ptr->rest()) |
| 1611 |
{ |
| 1612 |
KeywordExt *keyword = ptr->first(); |
| 1613 |
int hashcode = compute_hash (keyword); |
| 1614 |
if (max_hash_value < hashcode) |
| 1615 |
max_hash_value = hashcode; |
| 1616 |
if (_collision_detector->set_bit (hashcode)) |
| 1617 |
collisions++; |
| 1618 |
} |
| 1619 |
if (collisions < best_collisions |
| 1620 |
|| (collisions == best_collisions |
| 1621 |
&& max_hash_value < best_max_hash_value)) |
| 1622 |
{ |
| 1623 |
memcpy (best_asso_values, _asso_values, |
| 1624 |
_alpha_size * sizeof (_asso_values[0])); |
| 1625 |
best_collisions = collisions; |
| 1626 |
best_max_hash_value = max_hash_value; |
| 1627 |
} |
| 1628 |
/* Delete the copied keyword list. */ |
| 1629 |
delete_list (_head); |
| 1630 |
|
| 1631 |
if (--asso_iteration == 0) |
| 1632 |
break; |
| 1633 |
/* Prepare for next iteration. */ |
| 1634 |
if (_initial_asso_value >= 2) |
| 1635 |
_initial_asso_value -= 2, _jump += 2; |
| 1636 |
else |
| 1637 |
_initial_asso_value += _jump, _jump = 1; |
| 1638 |
} |
| 1639 |
_head = saved_head; |
| 1640 |
/* Install the best found asso_values. */ |
| 1641 |
_initial_asso_value = best_initial_asso_value; |
| 1642 |
_jump = best_jump; |
| 1643 |
memcpy (_asso_values, best_asso_values, |
| 1644 |
_alpha_size * sizeof (_asso_values[0])); |
| 1645 |
delete[] best_asso_values; |
| 1646 |
/* The keywords' _hash_value fields are recomputed below. */ |
| 1647 |
} |
| 1648 |
|
| 1649 |
/* Memory deallocation in each Keyword. */ |
| 1650 |
for (KeywordExt_List *temp = _head; temp; temp = temp->rest()) |
| 1651 |
{ |
| 1652 |
KeywordExt *keyword = temp->first(); |
| 1653 |
delete[] keyword->_undetermined_chars; |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
/* ========================================================================= */ |
| 1658 |
|
| 1659 |
/* Comparison function for sorting by increasing _hash_value. */ |
| 1660 |
static bool |
| 1661 |
less_by_hash_value (KeywordExt *keyword1, KeywordExt *keyword2) |
| 1662 |
{ |
| 1663 |
return keyword1->_hash_value < keyword2->_hash_value; |
| 1664 |
} |
| 1665 |
|
| 1666 |
/* Sorts the keyword list by hash value. */ |
| 1667 |
|
| 1668 |
void |
| 1669 |
Search::sort () |
| 1670 |
{ |
| 1671 |
_head = mergesort_list (_head, less_by_hash_value); |
| 1672 |
} |
| 1673 |
|
| 1674 |
void |
| 1675 |
Search::optimize () |
| 1676 |
{ |
| 1677 |
/* Preparations. */ |
| 1678 |
prepare (); |
| 1679 |
|
| 1680 |
/* Step 1: Finding good byte positions. */ |
| 1681 |
find_positions (); |
| 1682 |
|
| 1683 |
/* Step 2: Finding good alpha increments. */ |
| 1684 |
find_alpha_inc (); |
| 1685 |
|
| 1686 |
/* Step 3: Finding good asso_values. */ |
| 1687 |
find_good_asso_values (); |
| 1688 |
|
| 1689 |
/* Make one final check, just to make sure nothing weird happened.... */ |
| 1690 |
_collision_detector->clear (); |
| 1691 |
for (KeywordExt_List *curr_ptr = _head; curr_ptr; curr_ptr = curr_ptr->rest()) |
| 1692 |
{ |
| 1693 |
KeywordExt *curr = curr_ptr->first(); |
| 1694 |
unsigned int hashcode = compute_hash (curr); |
| 1695 |
if (_collision_detector->set_bit (hashcode)) |
| 1696 |
{ |
| 1697 |
/* This shouldn't happen. proj1, proj2, proj3 must have been |
| 1698 |
computed to be injective on the given keyword set. */ |
| 1699 |
fprintf (stderr, |
| 1700 |
"\nInternal error, unexpected duplicate hash code\n"); |
| 1701 |
if (option[POSITIONS]) |
| 1702 |
fprintf (stderr, "try options -m or -r, or use new key positions.\n\n"); |
| 1703 |
else |
| 1704 |
fprintf (stderr, "try options -m or -r.\n\n"); |
| 1705 |
exit (1); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
/* Sorts the keyword list by hash value. */ |
| 1710 |
sort (); |
| 1711 |
|
| 1712 |
/* Set unused asso_values[c] to max_hash_value + 1. This is not absolutely |
| 1713 |
necessary, but speeds up the lookup function in many cases of lookup |
| 1714 |
failure: no string comparison is needed once the hash value of a string |
| 1715 |
is larger than the hash value of any keyword. */ |
| 1716 |
int max_hash_value; |
| 1717 |
{ |
| 1718 |
KeywordExt_List *temp; |
| 1719 |
for (temp = _head; temp->rest(); temp = temp->rest()) |
| 1720 |
; |
| 1721 |
max_hash_value = temp->first()->_hash_value; |
| 1722 |
} |
| 1723 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1724 |
if (_occurrences[c] == 0) |
| 1725 |
_asso_values[c] = max_hash_value + 1; |
| 1726 |
|
| 1727 |
/* Propagate unified asso_values. */ |
| 1728 |
if (_alpha_unify) |
| 1729 |
{ |
| 1730 |
for (unsigned int c = 0; c < _alpha_size; c++) |
| 1731 |
if (_alpha_unify[c] != c) |
| 1732 |
_asso_values[c] = _asso_values[_alpha_unify[c]]; |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
/* Prints out some diagnostics upon completion. */ |
| 1737 |
|
| 1738 |
Search::~Search () |
| 1739 |
{ |
| 1740 |
delete _collision_detector; |
| 1741 |
if (option[DEBUG]) |
| 1742 |
{ |
| 1743 |
fprintf (stderr, "\ndumping occurrence and associated values tables\n"); |
| 1744 |
|
| 1745 |
for (unsigned int i = 0; i < _alpha_size; i++) |
| 1746 |
if (_occurrences[i]) |
| 1747 |
fprintf (stderr, "asso_values[%c] = %6d, occurrences[%c] = %6d\n", |
| 1748 |
i, _asso_values[i], i, _occurrences[i]); |
| 1749 |
|
| 1750 |
fprintf (stderr, "end table dumping\n"); |
| 1751 |
|
| 1752 |
fprintf (stderr, "\nDumping key list information:\ntotal non-static linked keywords = %d" |
| 1753 |
"\ntotal keywords = %d\ntotal duplicates = %d\nmaximum key length = %d\n", |
| 1754 |
_list_len, _total_keys, _total_duplicates, _max_key_len); |
| 1755 |
|
| 1756 |
int field_width = _max_selchars_length; |
| 1757 |
fprintf (stderr, "\nList contents are:\n(hash value, key length, index, %*s, keyword):\n", |
| 1758 |
field_width, "selchars"); |
| 1759 |
for (KeywordExt_List *ptr = _head; ptr; ptr = ptr->rest()) |
| 1760 |
{ |
| 1761 |
fprintf (stderr, "%11d,%11d,%6d, ", |
| 1762 |
ptr->first()->_hash_value, ptr->first()->_allchars_length, ptr->first()->_final_index); |
| 1763 |
if (field_width > ptr->first()->_selchars_length) |
| 1764 |
fprintf (stderr, "%*s", field_width - ptr->first()->_selchars_length, ""); |
| 1765 |
for (int j = 0; j < ptr->first()->_selchars_length; j++) |
| 1766 |
putc (ptr->first()->_selchars[j], stderr); |
| 1767 |
fprintf (stderr, ", %.*s\n", |
| 1768 |
ptr->first()->_allchars_length, ptr->first()->_allchars); |
| 1769 |
} |
| 1770 |
|
| 1771 |
fprintf (stderr, "End dumping list.\n\n"); |
| 1772 |
} |
| 1773 |
delete[] _asso_values; |
| 1774 |
delete[] _occurrences; |
| 1775 |
delete[] _alpha_unify; |
| 1776 |
delete[] _alpha_inc; |
| 1777 |
} |