core: Allow non-string hash keys.

This merely allows adding non-string keys to a hash table and puts them
non-string keys in bucket 0.  Efficiency is lost.  TODO: calculate a
proper hash.

* src/hash.c (hash_): Allow non-string keys in bucket 0.
* tests/hash.test: Add a test.

Co-authored-by: Timothy Sample <samplet@ngyro.com>
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2019-11-13 08:16:04 +01:00 committed by Timothy Sample
parent 0a905d54d7
commit 9a5de02f42
2 changed files with 10 additions and 7 deletions

View File

@ -46,13 +46,9 @@ hashq_ (struct scm *x, long size)
int
hash_ (struct scm *x, long size)
{
if (x->type != TSTRING)
{
eputs ("hash_ failed, not a string:");
display_error_ (x);
assert_msg (0, "0");
}
return hash_cstring (cell_bytes (x->string), size);
if (x->type == TSTRING)
return hash_cstring (cell_bytes (x->string), size);
return 0;
}
struct scm *

View File

@ -80,4 +80,11 @@ exec ${MES-bin/mes} --no-auto-compile -L ${0%/*} -L module -C module -e '(tests
(= (assoc-ref a "second") 2)
(= (assoc-ref a "third") 3)))))
(pass-if "integer keys"
(let ((t (make-hash-table)))
(hash-set! t 21 'a)
(hash-set! t 42 'b)
(and (eq? (hash-ref t 42) 'b)
(eq? (hash-ref t 21) 'a))))
(result 'report)