comparison src/elhash.c @ 20:859a2309aef8 r19-15b93

Import from CVS: tag r19-15b93
author cvs
date Mon, 13 Aug 2007 08:50:05 +0200
parents 9ee227acff29
children 25f70ba0133c
comparison
equal deleted inserted replaced
19:ac1f612d5250 20:859a2309aef8
174 ht->harray = Qnil; /* Let GC do its job */ 174 ht->harray = Qnil; /* Let GC do its job */
175 return; 175 return;
176 } 176 }
177 177
178 178
179 DEFUN ("hashtablep", Fhashtablep, Shashtablep, 1, 1, 0 /* 179 DEFUN ("hashtablep", Fhashtablep, 1, 1, 0, /*
180 Return t if OBJ is a hashtable, else nil. 180 Return t if OBJ is a hashtable, else nil.
181 */ ) 181 */
182 (obj) 182 (obj))
183 Lisp_Object obj;
184 { 183 {
185 return ((HASHTABLEP (obj)) ? Qt : Qnil); 184 return ((HASHTABLEP (obj)) ? Qt : Qnil);
186 } 185 }
187 186
188 187
309 308
310 signal_simple_error ("Invalid hashtable test fun", sym); 309 signal_simple_error ("Invalid hashtable test fun", sym);
311 return HASHTABLE_EQ; /* not reached */ 310 return HASHTABLE_EQ; /* not reached */
312 } 311 }
313 312
314 DEFUN ("make-hashtable", Fmake_hashtable, Smake_hashtable, 1, 2, 0 /* 313 DEFUN ("make-hashtable", Fmake_hashtable, 1, 2, 0, /*
315 Make a hashtable of initial size SIZE. 314 Make a hashtable of initial size SIZE.
316 Comparison between keys is done with TEST-FUN, which must be one of 315 Comparison between keys is done with TEST-FUN, which must be one of
317 `eq', `eql', or `equal'. The default is `eql'; i.e. two keys must 316 `eq', `eql', or `equal'. The default is `eql'; i.e. two keys must
318 be the same object (or have the same floating-point value, for floats) 317 be the same object (or have the same floating-point value, for floats)
319 to be considered equivalent. 318 to be considered equivalent.
320 319
321 See also `make-weak-hashtable', `make-key-weak-hashtable', and 320 See also `make-weak-hashtable', `make-key-weak-hashtable', and
322 `make-value-weak-hashtable'. 321 `make-value-weak-hashtable'.
323 */ ) 322 */
324 (size, test_fun) 323 (size, test_fun))
325 Lisp_Object size, test_fun;
326 { 324 {
327 CHECK_NATNUM (size); 325 CHECK_NATNUM (size);
328 return make_lisp_hashtable (XINT (size), HASHTABLE_NONWEAK, 326 return make_lisp_hashtable (XINT (size), HASHTABLE_NONWEAK,
329 decode_hashtable_test_fun (test_fun)); 327 decode_hashtable_test_fun (test_fun));
330 } 328 }
331 329
332 DEFUN ("copy-hashtable", Fcopy_hashtable, Scopy_hashtable, 1, 1, 0 /* 330 DEFUN ("copy-hashtable", Fcopy_hashtable, 1, 1, 0, /*
333 Make a new hashtable which contains the same keys and values 331 Make a new hashtable which contains the same keys and values
334 as the given table. The keys and values will not themselves be copied. 332 as the given table. The keys and values will not themselves be copied.
335 */ ) 333 */
336 (old_table) 334 (old_table))
337 Lisp_Object old_table;
338 { 335 {
339 struct _C_hashtable old_htbl; 336 struct _C_hashtable old_htbl;
340 struct _C_hashtable new_htbl; 337 struct _C_hashtable new_htbl;
341 struct hashtable_struct *old_ht; 338 struct hashtable_struct *old_ht;
342 struct hashtable_struct *new_ht; 339 struct hashtable_struct *new_ht;
371 368
372 return (result); 369 return (result);
373 } 370 }
374 371
375 372
376 DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0 /* 373 DEFUN ("gethash", Fgethash, 2, 3, 0, /*
377 Find hash value for KEY in TABLE. 374 Find hash value for KEY in TABLE.
378 If there is no corresponding value, return DEFAULT (defaults to nil). 375 If there is no corresponding value, return DEFAULT (defaults to nil).
379 */ ) 376 */
380 (key, table, defalt) 377 (key, table, defalt))
381 Lisp_Object key, table, defalt; /* One can't even spell correctly in C */
382 { 378 {
383 CONST void *vval; 379 CONST void *vval;
384 struct _C_hashtable htbl; 380 struct _C_hashtable htbl;
385 if (!gc_in_progress) 381 if (!gc_in_progress)
386 CHECK_HASHTABLE (table); 382 CHECK_HASHTABLE (table);
394 else 390 else
395 return defalt; 391 return defalt;
396 } 392 }
397 393
398 394
399 DEFUN ("remhash", Fremhash, Sremhash, 2, 2, 0 /* 395 DEFUN ("remhash", Fremhash, 2, 2, 0, /*
400 Remove hash value for KEY in TABLE. 396 Remove hash value for KEY in TABLE.
401 */ ) 397 */
402 (key, table) 398 (key, table))
403 Lisp_Object key, table;
404 { 399 {
405 struct _C_hashtable htbl; 400 struct _C_hashtable htbl;
406 CHECK_HASHTABLE (table); 401 CHECK_HASHTABLE (table);
407 402
408 ht_copy_to_c (XHASHTABLE (table), &htbl); 403 ht_copy_to_c (XHASHTABLE (table), &htbl);
410 ht_copy_from_c (&htbl, XHASHTABLE (table)); 405 ht_copy_from_c (&htbl, XHASHTABLE (table));
411 return Qnil; 406 return Qnil;
412 } 407 }
413 408
414 409
415 DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0 /* 410 DEFUN ("puthash", Fputhash, 3, 3, 0, /*
416 Hash KEY to VAL in TABLE. 411 Hash KEY to VAL in TABLE.
417 */ ) 412 */
418 (key, val, table) 413 (key, val, table))
419 Lisp_Object key, val, table;
420 { 414 {
421 struct hashtable_struct *ht; 415 struct hashtable_struct *ht;
422 void *vkey = LISP_TO_VOID (key); 416 void *vkey = LISP_TO_VOID (key);
423 417
424 CHECK_HASHTABLE (table); 418 CHECK_HASHTABLE (table);
437 UNGCPRO; 431 UNGCPRO;
438 } 432 }
439 return (val); 433 return (val);
440 } 434 }
441 435
442 DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0 /* 436 DEFUN ("clrhash", Fclrhash, 1, 1, 0, /*
443 Flush TABLE. 437 Flush TABLE.
444 */ ) 438 */
445 (table) 439 (table))
446 Lisp_Object table;
447 { 440 {
448 struct _C_hashtable htbl; 441 struct _C_hashtable htbl;
449 CHECK_HASHTABLE (table); 442 CHECK_HASHTABLE (table);
450 ht_copy_to_c (XHASHTABLE (table), &htbl); 443 ht_copy_to_c (XHASHTABLE (table), &htbl);
451 clrhash (&htbl); 444 clrhash (&htbl);
452 ht_copy_from_c (&htbl, XHASHTABLE (table)); 445 ht_copy_from_c (&htbl, XHASHTABLE (table));
453 return Qnil; 446 return Qnil;
454 } 447 }
455 448
456 DEFUN ("hashtable-fullness", Fhashtable_fullness, Shashtable_fullness, 1, 1, 0 /* 449 DEFUN ("hashtable-fullness", Fhashtable_fullness, 1, 1, 0, /*
457 Return number of entries in TABLE. 450 Return number of entries in TABLE.
458 */ ) 451 */
459 (table) 452 (table))
460 Lisp_Object table;
461 { 453 {
462 struct _C_hashtable htbl; 454 struct _C_hashtable htbl;
463 CHECK_HASHTABLE (table); 455 CHECK_HASHTABLE (table);
464 ht_copy_to_c (XHASHTABLE (table), &htbl); 456 ht_copy_to_c (XHASHTABLE (table), &htbl);
465 return (make_int (htbl.fullness)); 457 return (make_int (htbl.fullness));
504 VOID_TO_LISP (fn, void_fn); 496 VOID_TO_LISP (fn, void_fn);
505 call2 (fn, key, val); 497 call2 (fn, key, val);
506 } 498 }
507 499
508 500
509 DEFUN ("maphash", Fmaphash, Smaphash, 2, 2, 0 /* 501 DEFUN ("maphash", Fmaphash, 2, 2, 0, /*
510 Map FUNCTION over entries in TABLE, calling it with two args, 502 Map FUNCTION over entries in TABLE, calling it with two args,
511 each key and value in the table. 503 each key and value in the table.
512 */ ) 504 */
513 (function, table) 505 (function, table))
514 Lisp_Object function, table;
515 { 506 {
516 struct _C_hashtable htbl; 507 struct _C_hashtable htbl;
517 struct gcpro gcpro1, gcpro2; 508 struct gcpro gcpro1, gcpro2;
518 509
519 verify_function (function, GETTEXT ("hashtable mapping function")); 510 verify_function (function, GETTEXT ("hashtable mapping function"));
565 } 556 }
566 #endif /* 0 */ 557 #endif /* 0 */
567 558
568 559
569 560
570 DEFUN ("make-weak-hashtable", Fmake_weak_hashtable, Smake_weak_hashtable, 561 DEFUN ("make-weak-hashtable", Fmake_weak_hashtable, 1, 2, 0, /*
571 1, 2, 0 /*
572 Make a fully weak hashtable of initial size SIZE. 562 Make a fully weak hashtable of initial size SIZE.
573 A weak hashtable is one whose pointers do not count as GC referents: 563 A weak hashtable is one whose pointers do not count as GC referents:
574 for any key-value pair in the hashtable, if the only remaining pointer 564 for any key-value pair in the hashtable, if the only remaining pointer
575 to either the key or the value is in a weak hash table, then the pair 565 to either the key or the value is in a weak hash table, then the pair
576 will be removed from the table, and the key and value collected. A 566 will be removed from the table, and the key and value collected. A
577 non-weak hash table (or any other pointer) would prevent the object 567 non-weak hash table (or any other pointer) would prevent the object
578 from being collected. 568 from being collected.
579 569
580 You can also create semi-weak hashtables; see `make-key-weak-hashtable' 570 You can also create semi-weak hashtables; see `make-key-weak-hashtable'
581 and `make-value-weak-hashtable'. 571 and `make-value-weak-hashtable'.
582 */ ) 572 */
583 (size, test_fun) 573 (size, test_fun))
584 Lisp_Object size, test_fun;
585 { 574 {
586 CHECK_NATNUM (size); 575 CHECK_NATNUM (size);
587 return make_lisp_hashtable (XINT (size), HASHTABLE_WEAK, 576 return make_lisp_hashtable (XINT (size), HASHTABLE_WEAK,
588 decode_hashtable_test_fun (test_fun)); 577 decode_hashtable_test_fun (test_fun));
589 } 578 }
590 579
591 DEFUN ("make-key-weak-hashtable", Fmake_key_weak_hashtable, 580 DEFUN ("make-key-weak-hashtable", Fmake_key_weak_hashtable, 1, 2, 0, /*
592 Smake_key_weak_hashtable, 1, 2, 0 /*
593 Make a key-weak hashtable of initial size SIZE. 581 Make a key-weak hashtable of initial size SIZE.
594 A key-weak hashtable is similar to a fully-weak hashtable (see 582 A key-weak hashtable is similar to a fully-weak hashtable (see
595 `make-weak-hashtable') except that a key-value pair will be removed 583 `make-weak-hashtable') except that a key-value pair will be removed
596 only if the key remains unmarked outside of weak hashtables. The pair 584 only if the key remains unmarked outside of weak hashtables. The pair
597 will remain in the hashtable if the key is pointed to by something other 585 will remain in the hashtable if the key is pointed to by something other
598 than a weak hashtable, even if the value is not. 586 than a weak hashtable, even if the value is not.
599 */ ) 587 */
600 (size, test_fun) 588 (size, test_fun))
601 Lisp_Object size, test_fun;
602 { 589 {
603 CHECK_NATNUM (size); 590 CHECK_NATNUM (size);
604 return make_lisp_hashtable (XINT (size), HASHTABLE_KEY_WEAK, 591 return make_lisp_hashtable (XINT (size), HASHTABLE_KEY_WEAK,
605 decode_hashtable_test_fun (test_fun)); 592 decode_hashtable_test_fun (test_fun));
606 } 593 }
607 594
608 DEFUN ("make-value-weak-hashtable", Fmake_value_weak_hashtable, 595 DEFUN ("make-value-weak-hashtable", Fmake_value_weak_hashtable, 1, 2, 0, /*
609 Smake_value_weak_hashtable, 1, 2, 0 /*
610 Make a value-weak hashtable of initial size SIZE. 596 Make a value-weak hashtable of initial size SIZE.
611 A value-weak hashtable is similar to a fully-weak hashtable (see 597 A value-weak hashtable is similar to a fully-weak hashtable (see
612 `make-weak-hashtable') except that a key-value pair will be removed only 598 `make-weak-hashtable') except that a key-value pair will be removed only
613 if the value remains unmarked outside of weak hashtables. The pair will 599 if the value remains unmarked outside of weak hashtables. The pair will
614 remain in the hashtable if the value is pointed to by something other 600 remain in the hashtable if the value is pointed to by something other
615 than a weak hashtable, even if the key is not. 601 than a weak hashtable, even if the key is not.
616 */ ) 602 */
617 (size, test_fun) 603 (size, test_fun))
618 Lisp_Object size, test_fun;
619 { 604 {
620 CHECK_NATNUM (size); 605 CHECK_NATNUM (size);
621 return make_lisp_hashtable (XINT (size), HASHTABLE_VALUE_WEAK, 606 return make_lisp_hashtable (XINT (size), HASHTABLE_VALUE_WEAK,
622 decode_hashtable_test_fun (test_fun)); 607 decode_hashtable_test_fun (test_fun));
623 } 608 }
886 /************************************************************************/ 871 /************************************************************************/
887 872
888 void 873 void
889 syms_of_elhash (void) 874 syms_of_elhash (void)
890 { 875 {
891 defsubr (&Smake_hashtable); 876 DEFSUBR (Fmake_hashtable);
892 defsubr (&Scopy_hashtable); 877 DEFSUBR (Fcopy_hashtable);
893 defsubr (&Shashtablep); 878 DEFSUBR (Fhashtablep);
894 defsubr (&Sgethash); 879 DEFSUBR (Fgethash);
895 defsubr (&Sputhash); 880 DEFSUBR (Fputhash);
896 defsubr (&Sremhash); 881 DEFSUBR (Fremhash);
897 defsubr (&Sclrhash); 882 DEFSUBR (Fclrhash);
898 defsubr (&Smaphash); 883 DEFSUBR (Fmaphash);
899 defsubr (&Shashtable_fullness); 884 DEFSUBR (Fhashtable_fullness);
900 defsubr (&Smake_weak_hashtable); 885 DEFSUBR (Fmake_weak_hashtable);
901 defsubr (&Smake_key_weak_hashtable); 886 DEFSUBR (Fmake_key_weak_hashtable);
902 defsubr (&Smake_value_weak_hashtable); 887 DEFSUBR (Fmake_value_weak_hashtable);
903 defsymbol (&Qhashtablep, "hashtablep"); 888 defsymbol (&Qhashtablep, "hashtablep");
904 } 889 }
905 890
906 void 891 void
907 vars_of_elhash (void) 892 vars_of_elhash (void)