Skip to content
hash.d 38.1 KiB
Newer Older
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
/* -*- mode: c -*- */
/*
    hash.d  -- Hash tables.
*/
/*
    Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
    Copyright (c) 1990, Giuseppe Attardi.
    Copyright (c) 2001, Juan Jose Garcia Ripoll.
    Copyright (c) 2010-2011, Jean-Claude Beaudoin 

    MKCL is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    See file '../../Copyright' for full details.
*/

#include <mkcl/mkcl.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <mkcl/mkcl-inl.h>
#include <mkcl/internal.h>
#include <mkcl/mkcl-fenv.h>
#include "newhash.h"

static void corrupted_hash(MKCL, mkcl_object hashtable) /*__attribute__((noreturn))*/;

#define SYMBOL_NAME(x) (mkcl_Null(x)? mk_cl_Cnil_symbol->symbol.name : (x)->symbol.name)

static void
corrupted_hash(MKCL, mkcl_object hashtable)
{
  mkcl_FEerror(env, "internal error, corrupted hashtable ~S", 1, hashtable);
}

/* These do a peek inside GMP privates, brittle at best. JCB */
#define mkcl_big_size	big_num->_mp_size
#define mkcl_big_limbs	big_num->_mp_d

#if __i386 || __x86_64
# define MKCL_LONG_DOUBLE_REAL_SIZE 10
#else
# define MKCL_LONG_DOUBLE_REAL_SIZE sizeof(long double)
#endif

static mkcl_hashkey
_hash_eql(mkcl_hashkey h, mkcl_object x)
{
  switch (mkcl_type_of(x)) {
  case mkcl_t_bignum:
    return hash_string(h, (unsigned char*)x->big.mkcl_big_limbs,
		       labs(x->big.mkcl_big_size) * sizeof(mp_limb_t));
  case mkcl_t_ratio:
    h = _hash_eql(h, x->ratio.num);
    return _hash_eql(h, x->ratio.den);
  case mkcl_t_singlefloat:
    return hash_string(h, (unsigned char*)&mkcl_single_float(x), sizeof(mkcl_single_float(x)));
  case mkcl_t_doublefloat:
    return hash_string(h, (unsigned char*)&mkcl_double_float(x), sizeof(mkcl_double_float(x)));
#ifdef MKCL_LONG_FLOAT
  case mkcl_t_longfloat:
    return hash_string(h, (unsigned char*)&mkcl_long_float(x), MKCL_LONG_DOUBLE_REAL_SIZE);
#endif
  case mkcl_t_complex:
    h = _hash_eql(h, x->_complex.real);
    return _hash_eql(h, x->_complex.imag);
  case mkcl_t_character:
    return hash_word(h, MKCL_CHAR_CODE(x));
  default:
    return hash_word(h, ((mkcl_hashkey)x >> 2));
  }
}

static mkcl_hashkey
_hash_equal(int depth, mkcl_hashkey h, mkcl_object x)
{
  switch (mkcl_type_of(x)) {
  case mkcl_t_cons:
    if (--depth == 0) {
      return h;
    } else {
      h = _hash_equal(depth, h, MKCL_CONS_CAR(x));
      return _hash_equal(depth, h, MKCL_CONS_CDR(x));
    }
  case mkcl_t_symbol:
    if (mkcl_Null(x)) {
      return _hash_equal(depth, h, mk_cl_Cnil_symbol->symbol.name);
    }
    else
      return _hash_equal(depth, h, x->symbol.name);
  case mkcl_t_base_string:
    return hash_base_string((mkcl_base_char *)x->base_string.self, x->base_string.fillp, h);
  case mkcl_t_string:
    return hash_full_string(x->string.self, x->string.fillp, h);
  case mkcl_t_pathname:
    h = _hash_equal(0, h, x->pathname.directory);
    h = _hash_equal(0, h, x->pathname.name);
    h = _hash_equal(0, h, x->pathname.type);
    h = _hash_equal(0, h, x->pathname.host);
    h = _hash_equal(0, h, x->pathname.device);
    return _hash_equal(0, h, x->pathname.version);
  case mkcl_t_bitvector:
    /* Notice that we may round out some bits. We must do this
     * because the fill pointer may be set in the middle of a byte.
     * If so, the extra bits _must_ _not_ take part in the hash,
     * because otherwise two bit arrays which are EQUAL might
     * have different hash keys. */
    return hash_string(h, x->vector.self.bc, x->vector.fillp / 8);
  case mkcl_t_random:
    return _hash_equal(0, h, x->random.value);
#ifdef MKCL_SIGNED_ZERO
  case mkcl_t_singlefloat: {
    float f = mkcl_single_float(x);
    /* if (f == 0.0) f = 0.0; */
    return hash_string(h, (unsigned char*)&f, sizeof(f));
  }
  case mkcl_t_doublefloat: {
    double f = mkcl_double_float(x);
    /* if (f == 0.0) f = 0.0; */
    return hash_string(h, (unsigned char*)&f, sizeof(f));
  }
#ifdef MKCL_LONG_FLOAT
  case mkcl_t_longfloat: {
    long double f = mkcl_long_float(x);
    /* if (f == 0.0) f = 0.0; */
    return hash_string(h, (unsigned char*)&f, MKCL_LONG_DOUBLE_REAL_SIZE);
  }
#endif
  case mkcl_t_complex: {
    h = _hash_equal(depth, h, x->_complex.real);
    return _hash_equal(depth, h, x->_complex.imag);
  }
#endif
  default:
    return _hash_eql(h, x);
  }
}

static mkcl_hashkey
_hash_equalp(MKCL, int depth, mkcl_hashkey h, mkcl_object x)
{
  mkcl_index i, len;

  if (mkcl_Null(x)) {
    return _hash_equalp(env, depth, h, mk_cl_Cnil_symbol->symbol.name);
  }

  switch (mkcl_type_of(x)) {
  case mkcl_t_character:
    return hash_word(h, mkcl_char_upcase(MKCL_CHAR_CODE(x)));
  case mkcl_t_cons:
    if (--depth == 0) {
      return h;
    } else {
      h = _hash_equalp(env, depth, h, MKCL_CONS_CAR(x));
      return _hash_equalp(env, depth, h, MKCL_CONS_CDR(x));
    }
  case mkcl_t_string:
    len = x->string.fillp;
    goto SCAN;
  case mkcl_t_base_string:
    len = x->base_string.fillp;
    goto SCAN;
  case mkcl_t_vector:
  case mkcl_t_bitvector:
    len = x->vector.fillp;
    goto SCAN;
  case mkcl_t_array:
    len = x->array.dim;
  SCAN:	if (--depth) {
      for (i = 0; i < len; i++) {
	h = _hash_equalp(env, depth, h, mkcl_aref_index(env, x, i));
      }
    }
    return h;
  case mkcl_t_fixnum:
    return hash_word(h, mkcl_fixnum_to_word(x));
  case mkcl_t_singlefloat:
    /* FIXME! We should be more precise here! */
    return hash_word(h, (mkcl_index)mkcl_single_float(x));
  case mkcl_t_doublefloat:
    /* FIXME! We should be more precise here! */
    return hash_word(h, (mkcl_index)mkcl_double_float(x));
  case mkcl_t_bignum:
    /* FIXME! We should be more precise here! */
    return _hash_equal(depth, h, x);
  case mkcl_t_ratio:
    h = _hash_equalp(env, 0, h, x->ratio.num);
    return _hash_equalp(env, 0, h, x->ratio.den);
  case mkcl_t_complex:
    h = _hash_equalp(env, 0, h, x->_complex.real);
    return _hash_equalp(env, 0, h, x->_complex.imag);
  case mkcl_t_instance:
  case mkcl_t_hashtable:
    /* FIXME! We should be more precise here! */
    return hash_word(h, 42); /* Is this the meaning of life by any chance? JCB */
  default:
    return _hash_equal(depth, h, x);
  }
}


struct mkcl_hashtable_entry *
mkcl_search_hash_eq(MKCL, mkcl_object key, mkcl_object hashtable)
{
  struct mkcl_hashtable_entry *e;
  mkcl_hashkey h;
  mkcl_index hsize = hashtable->hash.size;

#ifdef HASHTABLE_STATS
  hashtable->hash.nb_searches++;
#endif

  h = (mkcl_hashkey)key >> 2;

  /* A power of 2 for hsize would not be good here! */
  e = hashtable->hash.data[h % hsize]; 
  
  if ( e != NULL )
    {
#ifdef HASHTABLE_STATS
      long probes = 0;
#endif

      do {
#ifdef HASHTABLE_STATS
	probes++;
#endif

	if ( key == e->key )
	  {
#ifdef HASHTABLE_STATS
	    hashtable->hash.probes += probes;
	    if ( probes < hashtable->hash.shortest_probe_chain )
	      hashtable->hash.shortest_probe_chain = probes;
	    if ( hashtable->hash.longest_probe_chain < probes )
	      hashtable->hash.longest_probe_chain = probes;
#endif
	    return(e);
	  }
	else
	  e = e->next;
      } while ( e != NULL );
#ifdef HASHTABLE_STATS
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      return(NULL); /* we got to the end of the chain without a match. */
    }
  else
    {
#ifdef HASHTABLE_STATS
      long probes = 1;
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      
      return(NULL); /* The chain was empty */
    }
}

struct mkcl_hashtable_entry *
mkcl_search_hash_eql(MKCL, mkcl_object key, mkcl_object hashtable)
{
  struct mkcl_hashtable_entry *e;
  mkcl_hashkey h;
  mkcl_index hsize = hashtable->hash.size;

#ifdef HASHTABLE_STATS
  hashtable->hash.nb_searches++;
#endif

  h = _hash_eql(0, key); 

  /* A power of 2 for hsize would not be good here! */
  e = hashtable->hash.data[h % hsize]; 
  
  if ( e != NULL )
    {
#ifdef HASHTABLE_STATS
      long probes = 0;
#endif

      do {
#ifdef HASHTABLE_STATS
	probes++;
#endif
	if ( mkcl_eql(env, key, e->key) )
	  {
#ifdef HASHTABLE_STATS
	    hashtable->hash.probes += probes;
	    if ( probes < hashtable->hash.shortest_probe_chain )
	      hashtable->hash.shortest_probe_chain = probes;
	    if ( hashtable->hash.longest_probe_chain < probes )
	      hashtable->hash.longest_probe_chain = probes;
#endif
	    return(e);
	  }
	else
	  e = e->next;
      } while ( e != NULL );
#ifdef HASHTABLE_STATS
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      return(NULL); /* we got to the end of the chain without a match. */
    }
  else
    {
#ifdef HASHTABLE_STATS
      long probes = 1;
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      
      return(NULL); /* The chain was empty */
    }
}

struct mkcl_hashtable_entry *
mkcl_search_hash_equal(MKCL, mkcl_object key, mkcl_object hashtable)
{
  struct mkcl_hashtable_entry *e;
  mkcl_hashkey h;
  mkcl_index hsize = hashtable->hash.size;

#ifdef HASHTABLE_STATS
  hashtable->hash.nb_searches++;
#endif

  h = _hash_equal(3, 0, key);

  /* A power of 2 for hsize would not be good here! */
  e = hashtable->hash.data[h % hsize]; 
  
  if ( e != NULL )
    {
#ifdef HASHTABLE_STATS
      long probes = 0;
#endif

      do {
#ifdef HASHTABLE_STATS
	probes++;
#endif

	if ( mkcl_equal(env, key, e->key) )
	  {
#ifdef HASHTABLE_STATS
	    hashtable->hash.probes += probes;
	    if ( probes < hashtable->hash.shortest_probe_chain )
	      hashtable->hash.shortest_probe_chain = probes;
	    if ( hashtable->hash.longest_probe_chain < probes )
	      hashtable->hash.longest_probe_chain = probes;
#endif
	    return(e);
	  }
	else
	  e = e->next;
      } while ( e != NULL );
#ifdef HASHTABLE_STATS
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      return(NULL); /* we got to the end of the chain without a match. */
    }
  else
    {
#ifdef HASHTABLE_STATS
      long probes = 1;
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      
      return(NULL); /* The chain was empty */
    }
}

struct mkcl_hashtable_entry *
mkcl_search_hash_equalp(MKCL, mkcl_object key, mkcl_object hashtable)
{
  struct mkcl_hashtable_entry *e;
  mkcl_hashkey h;
  mkcl_index hsize = hashtable->hash.size;

#ifdef HASHTABLE_STATS
  hashtable->hash.nb_searches++;
#endif

  h = _hash_equalp(env, 3, 0, key);

  /* A power of 2 for hsize would not be good here! */
  e = hashtable->hash.data[h % hsize]; 
  
  if ( e != NULL )
    {
#ifdef HASHTABLE_STATS
      long probes = 0;
#endif

      do {
#ifdef HASHTABLE_STATS
	probes++;
#endif

	if ( mkcl_equalp(env, key, e->key) )
	  {
#ifdef HASHTABLE_STATS
	    hashtable->hash.probes += probes;
	    if ( probes < hashtable->hash.shortest_probe_chain )
	      hashtable->hash.shortest_probe_chain = probes;
	    if ( hashtable->hash.longest_probe_chain < probes )
	      hashtable->hash.longest_probe_chain = probes;
#endif
	    return(e);
	  }
	else
	  e = e->next;
      } while ( e != NULL );
#ifdef HASHTABLE_STATS
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      return(NULL); /* we got to the end of the chain without a match. */
    }
  else
    {
#ifdef HASHTABLE_STATS
      long probes = 1;
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      
      return(NULL); /* The chain was empty */
    }
}

struct mkcl_hashtable_entry *
mkcl_search_hash_package(MKCL, mkcl_object key, mkcl_object hashtable)
{
  struct mkcl_hashtable_entry *e;
  mkcl_hashkey h;
  mkcl_object ho;
  mkcl_index hsize = hashtable->hash.size;

#ifdef HASHTABLE_STATS
  hashtable->hash.nb_searches++;
#endif

  h = _hash_equal(3, 0, key);
  ho = MKCL_MAKE_FIXNUM(h & 0xFFFFFFF);

  /* A power of 2 for hsize would not be good here! */
  e = hashtable->hash.data[h % hsize]; 
  
  if ( e != NULL )
    {
#ifdef HASHTABLE_STATS
      long probes = 0;
#endif

      do {
#ifdef HASHTABLE_STATS
	probes++;
#endif

	if ( (ho == e->key) && mkcl_string_E(env, key,SYMBOL_NAME(e->value)) )
	  {
#ifdef HASHTABLE_STATS
	    hashtable->hash.probes += probes;
	    if ( probes < hashtable->hash.shortest_probe_chain )
	      hashtable->hash.shortest_probe_chain = probes;
	    if ( hashtable->hash.longest_probe_chain < probes )
	      hashtable->hash.longest_probe_chain = probes;
#endif
	    return(e);
	  }
	else
	  e = e->next;
      } while ( e != NULL );
#ifdef HASHTABLE_STATS
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      return(NULL); /* we got to the end of the chain without a match. */
    }
  else
    {
#ifdef HASHTABLE_STATS
      long probes = 1;
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      
      return(NULL); /* The chain was empty */
    }
}

#if 0 /* inlined */
/* The content of mkcl_search_hash is largely duplicated inside
   mkcl_remhash. If you change one you need to also change the other. */
struct mkcl_hashtable_entry *
mkcl_search_hash(mkcl_object key, mkcl_object hashtable)
{
  struct mkcl_hashtable_entry *e;
  mkcl_hashkey h;
  mkcl_object ho;
  mkcl_index hsize = hashtable->hash.size;
  enum mkcl_httest htest = hashtable->hash.test;

#ifdef HASHTABLE_STATS
  hashtable->hash.nb_searches++;
#endif

  switch (htest) {
  case mkcl_htt_eq:	  h = (mkcl_hashkey)key >> 2; break; /* you must be kidding! */
  case mkcl_htt_eql:	  h = _hash_eql(0, key); break;
  case mkcl_htt_equal:	  h = _hash_equal(3, 0, key); break;
  case mkcl_htt_equalp:  h = _hash_equalp(3, 0, key); break;
  case mkcl_htt_package: h = _hash_equal(3, 0, key);
    ho = MKCL_MAKE_FIXNUM(h & 0xFFFFFFF);
    break;
  default:	  corrupted_hash(hashtable);
  }

  /* A power of 2 for hsize would not be good here! */
  e = hashtable->hash.data[h % hsize]; 
  
  if ( e != NULL )
    {
#ifdef HASHTABLE_STATS
      long probes = 0;
#endif

      do {
	bool b;
	mkcl_object hkey = e->key;
#ifdef HASHTABLE_STATS
	probes++;
#endif

	switch (htest)
	  {
	  case mkcl_htt_eq:	b = (key == hkey); break;
	  case mkcl_htt_eql:	b = mkcl_eql(key, hkey); break;
	  case mkcl_htt_equal: b = mkcl_equal(key, hkey); break;
	  case mkcl_htt_equalp:  b = mkcl_equalp(key, hkey); break;
	  case mkcl_htt_package: 
	    b = (ho == hkey) && mkcl_string_E(key,SYMBOL_NAME(e->value));
	    break;
	  }
	if ( b )
	  {
#ifdef HASHTABLE_STATS
	    hashtable->hash.probes += probes;
	    if ( probes < hashtable->hash.shortest_probe_chain )
	      hashtable->hash.shortest_probe_chain = probes;
	    if ( hashtable->hash.longest_probe_chain < probes )
	      hashtable->hash.longest_probe_chain = probes;
#endif
	    return(e);
	  }
	else
	  e = e->next;
      } while ( e != NULL );
#ifdef HASHTABLE_STATS
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      return(NULL); /* we got to the end of the chain without a match. */
    }
  else
    {
#ifdef HASHTABLE_STATS
      long probes = 1;
      hashtable->hash.probes += probes;
      if ( probes < hashtable->hash.shortest_failed_probe_chain )
	hashtable->hash.shortest_failed_probe_chain = probes;
      if ( hashtable->hash.longest_failed_probe_chain < probes )
	hashtable->hash.longest_failed_probe_chain = probes;
#endif
      
      return(NULL); /* The chain was empty */
    }
}
#else
extern inline struct mkcl_hashtable_entry *mkcl_search_hash(MKCL, mkcl_object key, mkcl_object hashtable);
#endif



mkcl_object
mkcl_gethash_safe(MKCL, mkcl_object key, mkcl_object hashtable, mkcl_object def)
{
  struct mkcl_hashtable_entry *e;

  mkcl_assert_type_hash_table(env, hashtable);
  e = mkcl_search_hash(env, key, hashtable);
  if (e != NULL)
    def = e->value;
  return def;
}

static void
add_new_to_hash(MKCL, mkcl_object key, mkcl_object hashtable, mkcl_object value)
{
  enum mkcl_httest htest;
  mkcl_hashkey h;
  mkcl_index hsize;
  struct mkcl_hashtable_entry ** root;

  /* INV: hashtable has the right type */
  htest = hashtable->hash.test;
  hsize = hashtable->hash.size;
  switch (htest) {
  case mkcl_htt_eq:	h = (mkcl_hashkey)key >> 2; break;
  case mkcl_htt_eql:	h = _hash_eql(0, key); break;
  case mkcl_htt_equal:	h = _hash_equal(3, 0, key); break;
  case mkcl_htt_equalp:  h = _hash_equalp(env, 3, 0, key); break;
  case mkcl_htt_package: h = _hash_equal(3, 0, key); break;
  default:	corrupted_hash(env, hashtable);
  }

  root = &(hashtable->hash.data[h % hsize]);
  
  {
    struct mkcl_hashtable_entry * e;
    struct mkcl_hashtable_entry * free_bucket = hashtable->hash.free_bucket;

    if (free_bucket)
      { e = free_bucket; hashtable->hash.free_bucket = e->next; /* e->next = NULL; */ }
    else
      e = (struct mkcl_hashtable_entry *)
	mkcl_alloc(env, sizeof(struct mkcl_hashtable_entry));

    hashtable->hash.entries++;

    if (htest == mkcl_htt_package)
      e->key = MKCL_MAKE_FIXNUM(h & 0xFFFFFFF);
    else
      e->key = key;
    e->value = value;
    e->next = *root;

    *root = e;
  }
}

void
mkcl_sethash(MKCL, mkcl_object key, mkcl_object hashtable, mkcl_object value)
{
  mkcl_index i;
  struct mkcl_hashtable_entry *e;

  mkcl_assert_type_hash_table(env, hashtable);
  e = mkcl_search_hash(env, key, hashtable);
  if (e != NULL) {
    e->value = value;
    return;
  }

  i = hashtable->hash.entries + 1;
  if (i >= hashtable->hash.size ||
      /* This version is all integral ops. */
      (!(i & (((mkcl_word) -1) << (MKCL_WORD_BITS - 4))) /* make sure i will not overflow. */
       && (i * 16) >= (hashtable->hash.size * hashtable->hash.factor_of_16th)))
    mkcl_extend_hashtable(env, hashtable);

  add_new_to_hash(env, key, hashtable, value);
  return;
}

void
mkcl_extend_hashtable(MKCL, mkcl_object hashtable)
{
  mkcl_object old, key;
  mkcl_index old_size, new_size, i;
  mkcl_object new_size_obj;

  mkcl_assert_type_hash_table(env, hashtable);
  old_size = hashtable->hash.size;
  /* We do the computation with lisp datatypes, just in case the sizes contain
   * weird numbers */
  if (MKCL_FIXNUMP(hashtable->hash.rehash_size)) {
    new_size_obj = mkcl_plus(env, hashtable->hash.rehash_size,
			     MKCL_MAKE_FIXNUM(old_size));
  } else {
    /* if we get here rehash_size is most likely a real (float) number. */
    bool fe_inexact_on = FE_INEXACT & fegetexcept();

    if (fe_inexact_on)
      {
	fedisableexcept(FE_INEXACT);
      }
    
    new_size_obj = mkcl_times(env, hashtable->hash.rehash_size, MKCL_MAKE_FIXNUM(old_size));
    new_size_obj = mkcl_ceiling1(env, new_size_obj);

    feclearexcept(FE_INEXACT); /* Clear leftovers from casting. */ /* should it be FE_ALL_EXCEPT? JCB*/
    if (fe_inexact_on)
      {
	feenableexcept(FE_INEXACT);
      }
  }
  if (!MKCL_FIXNUMP(new_size_obj)) {
    /* New size is too large */
    new_size = old_size * 2;
  } else {
    new_size = mkcl_fixnum_to_word(new_size_obj);
  }
  old = mkcl_alloc_raw_hashtable(env);

  old->hash = hashtable->hash; /* To copy entries, size, data. */

  hashtable->hash.entries = 0;
  hashtable->hash.size = new_size;
  hashtable->hash.data = (struct mkcl_hashtable_entry **)
    mkcl_alloc(env, new_size * sizeof(struct mkcl_hashtable_entry *));

  for (i = 0;  i < new_size;  i++) {
    hashtable->hash.data[i] = NULL;
  }

  for (i = 0;  i < old_size;  i++)
    {
      struct mkcl_hashtable_entry * e = old->hash.data[i];

      for (; e != NULL; e = e->next)
	{
	  if ( hashtable->hash.test == mkcl_htt_package )
	    key = SYMBOL_NAME(e->value);
	  else
	    key = e->key;
	  add_new_to_hash(env, key, hashtable, e->value);
	}
    }
}


@(defun make_hash_table (&key 
			 (test @'eql')
			 (size MKCL_MAKE_FIXNUM(1024))
			 (rehash_size mkcl_make_singlefloat(env, 1.5))
			 (rehash_threshold mkcl_make_singlefloat(env, 0.7))
			 )
@
  @(return mk_cl__make_hash_table(env, test, size, rehash_size, rehash_threshold));
@)

static void
do_clrhash(mkcl_object ht)
{
  /*
   * Fill a hash with null pointers and ensure it does not have
   * any entry. We separate this routine because it is needed
   * both by clrhash and hash table initialization.
   */
  mkcl_index i;
  const mkcl_index hsize = ht->hash.size;
  struct mkcl_hashtable_entry ** data = ht->hash.data;

  ht->hash.entries = 0;
  for(i = 0; i < hsize; i++) {
    data[i] = NULL;
  }
}

mkcl_object
mk_cl__make_hash_table(MKCL, mkcl_object test, mkcl_object size,
		       mkcl_object rehash_size, mkcl_object rehash_threshold)
{
  enum mkcl_httest htt;
  struct mkcl_hashtable_entry * (*search_fun)(MKCL, mkcl_object key, mkcl_object hashtable);  
  mkcl_index hsize;
  mkcl_object h;
  /*
   * Argument checking
   */
  if (test == @'eq' || test == MKCL_SYM_FUN(@'eq'))
    { htt = mkcl_htt_eq; search_fun = mkcl_search_hash_eq; }
  else if (test == @'eql' || test == MKCL_SYM_FUN(@'eql'))
    { htt = mkcl_htt_eql; search_fun = mkcl_search_hash_eql; }
  else if (test == @'equal' || test == MKCL_SYM_FUN(@'equal'))
    { htt = mkcl_htt_equal; search_fun = mkcl_search_hash_equal; }
  else if (test == @'equalp' || test == MKCL_SYM_FUN(@'equalp'))
    { htt = mkcl_htt_equalp; search_fun = mkcl_search_hash_equalp; }
  else
    mkcl_FEerror(env, "~S is an illegal hash-table test function.", 1, test);

  hsize = mkcl_fixnum_in_range(env, @'make-hash-table', "size", size, 0, MKCL_ATOTLIM);;
  if (hsize < 16) {
    hsize = 16;
  }
 AGAIN:
  if (mkcl_minusp(env, rehash_size)) {
  ERROR1:
    rehash_size =
      mkcl_type_error(env, @'make-hash-table',"rehash-size",
		      rehash_size,
		      mkcl_fast_read_from_cstring(env, "(OR (INTEGER 1 *) (FLOAT 0 (1)))"));
    goto AGAIN;
  }
  if (mkcl_floatp(env, rehash_size)) {
    if (mkcl_number_compare(env, rehash_size, MKCL_MAKE_FIXNUM(1)) < 0 ||
	mkcl_minusp(env, rehash_size)) {
      goto ERROR1;
    }
    rehash_size = mkcl_make_doublefloat(env, mkcl_to_double(env, rehash_size));
  } else if (!MKCL_FIXNUMP(rehash_size)) {
    goto ERROR1;
  }
  while (!mkcl_numberp(env, rehash_threshold) ||
	 mkcl_minusp(env, rehash_threshold) ||
	 mkcl_number_compare(env, rehash_threshold, MKCL_MAKE_FIXNUM(1)) > 0)
    {
      rehash_threshold =
	mkcl_type_error(env, @'make-hash-table',"rehash-threshold",
			rehash_threshold,
			mkcl_fast_read_from_cstring(env, "(REAL 0 1)"));
    }
  /*
   * Build actual hash.
   */

  h = mkcl_alloc_raw_hashtable(env);
  h->hash.test = htt;
  h->hash.search_fun = search_fun;
  h->hash.size = hsize;
  h->hash.entries = 0;
  h->hash.data = NULL;	/* for GC sake */

  h->hash.data = (struct mkcl_hashtable_entry **) mkcl_alloc(env, hsize * sizeof(struct mkcl_hashtable_entry *));
  do_clrhash(h);

  h->hash.free_bucket = NULL;

  h->hash.rehash_size = rehash_size;
  h->hash.threshold = rehash_threshold;
  {
    fenv_t old_fenv;

    bool fe_inexact_on = FE_INEXACT & fegetexcept();
    if (fe_inexact_on)
      {
	/* printf("\nwrite_long_double: turning off FE_INEXACT!\n"); fflush(NULL); */
	fedisableexcept(FE_INEXACT);
      }

    /* fegetenv(&old_fenv); */ /* should this be feholdexcept() instead? JCB */
    /* fedisableexcept(FE_INEXACT); */

    mkcl_object factor = mkcl_round1(env,
				     mkcl_times(env,
						rehash_threshold,
						MKCL_MAKE_FIXNUM(16)));
    /* fesetenv(&old_fenv); */
    feclearexcept(FE_INEXACT); /* Clear leftovers from rounding. */
    if (fe_inexact_on)
      {
	/* printf("\nwrite_long_double: turning on FE_INEXACT!\n"); fflush(NULL); */
	feenableexcept(FE_INEXACT);
      }

    h->hash.factor_of_16th = mkcl_integer_to_index(env, factor);
  }
  if (h->hash.factor_of_16th == 0)
    h->hash.factor_of_16th = 1;


#ifdef HASHTABLE_STATS
  h->hash.nb_searches = 0;
  h->hash.probes = 0;
  h->hash.shortest_probe_chain = LONG_MAX;
  h->hash.longest_probe_chain = 0;
  h->hash.shortest_failed_probe_chain = LONG_MAX;
  h->hash.longest_failed_probe_chain = 0;
  h->hash.longest_static_chain = 0;

  mkcl_core.hashtables[htt] = mkcl_cons(h, mkcl_core.hashtables[htt]);
#endif

  return h;
}

static void
print_stats_for_hset(mkcl_object hset)
{
#ifdef HASHTABLE_STATS
  mkcl_loop_for_in(hset) {
    mkcl_object h = MKCL_CONS_CAR(hset);
    long chain_count[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

    printf("Hash table size = %d, entries = %d, alpha = %f\n",
	   h->hash.size, h->hash.entries, (float) h->hash.entries / (float)h->hash.size);
    printf("searches = %d\n", h->hash.nb_searches);
    printf("total probes = %d\n", h->hash.probes);
    printf("average probe chain = %f\n",
	   (float) h->hash.probes / (float) h->hash.nb_searches);
    printf("shortest probe chain = %d\n", h->hash.shortest_probe_chain);
    printf("longest probe chain = %d\n", h->hash.longest_probe_chain);
    printf("shortest failed probe chain = %d\n", h->hash.shortest_failed_probe_chain);
    printf("longest failed probe chain = %d\n", h->hash.longest_failed_probe_chain);

    {
      long size = h->hash.size;
      long i;

      for (i = 0; i < size; i++)
	{
	  long count = 0;
	  struct mkcl_hashtable_entry * e = h->hash.data[i];
	  
	  for (; e != NULL; e = e->next)
	    count++;

	  if ( h->hash.longest_static_chain < count )
	    h->hash.longest_static_chain = count;

	  if ( count >= 7 )
	    chain_count[7]++;
	  else
	    chain_count[count]++;
	}
    }
    printf("longest static chain = %d\n", h->hash.longest_static_chain);
    if ( h->hash.longest_static_chain > 2 )
      {
	printf("\tchains of length 0: %d\n", chain_count[0]);
	printf("\tchains of length 1: %d\n", chain_count[1]);
	printf("\tchains of length 2: %d\n", chain_count[2]);
	printf("\tchains of length 3: %d\n", chain_count[3]);
	printf("\tchains of length 4: %d\n", chain_count[4]);
	printf("\tchains of length 5: %d\n", chain_count[5]);
	printf("\tchains of length 6: %d\n", chain_count[6]);
	printf("\tchains of length 7: %d\n", chain_count[7]);
      }
    printf("\n");
  } mkcl_end_loop_for_in;
#endif
}

mkcl_object
mk_si_hash_tables_statistics(MKCL)
{
  mkcl_call_stack_check(env);
#ifdef HASHTABLE_STATS
  printf("\nHash tables of type EQ:\n");
  print_stats_for_hset(mkcl_core.hashtables[mkcl_htt_eq]);
  printf("\nHash tables of type EQL:\n");
  print_stats_for_hset(mkcl_core.hashtables[mkcl_htt_eql]);
  printf("\nHash tables of type EQUAL:\n");
  print_stats_for_hset(mkcl_core.hashtables[mkcl_htt_equal]);
  printf("\nHash tables of type EQUALP:\n");
  print_stats_for_hset(mkcl_core.hashtables[mkcl_htt_equalp]);
  printf("\nHash tables of type PACKAGE:\n");
  print_stats_for_hset(mkcl_core.hashtables[mkcl_htt_package]);
#else
  printf("\nThere is no hashtable statistics!\n");
#endif
  @(return mk_cl_Cnil);
}

mkcl_object
mk_cl_hash_table_p(MKCL, mkcl_object ht)