diff --git a/collects/scribblings/reference/hashes.scrbl b/collects/scribblings/reference/hashes.scrbl index 4555878bae..2819a19f76 100644 --- a/collects/scribblings/reference/hashes.scrbl +++ b/collects/scribblings/reference/hashes.scrbl @@ -103,53 +103,60 @@ Returns @scheme[#t] if @scheme[hash] retains its keys weakly, @scheme[#f] if it retains keys strongly.} -@defproc[(make-hash) hash?]{ +@defproc[(make-hash [assocs (listof pair?) null]) hash?]{ -Creates an empty mutable hash table that holds keys strongly and that -uses @scheme[equal?] to compare keys. See also -@scheme[make-custom-hash].} +Creates a mutable hash table that holds keys strongly and that uses +@scheme[equal?] to compare keys. See also @scheme[make-custom-hash]. + +The table is initialized with the content of @scheme[assocs]. In each +element of @scheme[assocs], the @scheme[car] is a key, and the +@scheme[cdr] is the corresponding value. The mappings are added to the +table in the order that they appear in @scheme[assocs], so later +mappings can hide earlier mappings.} -@defproc[(make-hasheqv) (and/c hash? hash-eqv?)]{ +@defproc[(make-hasheqv [assocs (listof pair?) null]) (and/c hash? hash-eqv?)]{ -Creates an empty mutable hash table that holds keys strongly and that -uses @scheme[eqv?] to compare keys.} +Creates a mutable hash table that holds keys strongly and that +uses @scheme[eqv?] to compare keys. The table is initialized with the +content of @scheme[assocs] as in @scheme[make-hash].} -@defproc[(make-hasheq) (and/c hash? hash-eq?)]{ +@defproc[(make-hasheq [assocs (listof pair?) null]) (and/c hash? hash-eq?)]{ -Creates an empty mutable hash table that holds keys strongly and that -uses @scheme[eq?] to compare keys.} +Creates a mutable hash table that holds keys strongly and that +uses @scheme[eq?] to compare keys. The table is initialized with the +content of @scheme[assocs] as in @scheme[make-hash].} -@defproc[(make-weak-hash) (and/c hash? hash-weak?)]{ +@defproc[(make-weak-hash [assocs (listof pair?) null]) (and/c hash? hash-weak?)]{ -Creates an empty mutable hash table that holds keys weakly and that -uses @scheme[equal?] to compare keys. See also -@scheme[make-weak-custom-hash].} +Creates a mutable hash table that holds keys weakly and that +uses @scheme[equal?] to compare keys; see also +@scheme[make-weak-custom-hash]. The table is initialized with the +content of @scheme[assocs] as in @scheme[make-hash].} -@defproc[(make-weak-hasheqv) (and/c hash? hash-eqv? hash-weak?)]{ +@defproc[(make-weak-hasheqv [assocs (listof pair?) null]) (and/c hash? hash-eqv? hash-weak?)]{ -Creates an empty mutable hash table that holds keys weakly and that -uses @scheme[eqv?] to compare keys.} +Creates a mutable hash table that holds keys weakly and that +uses @scheme[eqv?] to compare keys. The table is initialized with the +content of @scheme[assocs] as in @scheme[make-hash].} -@defproc[(make-weak-hasheq) (and/c hash? hash-eq? hash-weak?)]{ +@defproc[(make-weak-hasheq [assocs (listof pair?) null]) (and/c hash? hash-eq? hash-weak?)]{ -Creates an empty mutable hash table that holds keys weakly and that -uses @scheme[eq?] to compare keys.} +Creates a mutable hash table that holds keys weakly and that +uses @scheme[eq?] to compare keys. The table is initialized with the +content of @scheme[assocs] as in @scheme[make-hash].} @defproc[(make-immutable-hash [assocs (listof pair?)]) (and/c hash? immutable?)]{ Creates an immutable hash table that compares keys with -@scheme[equal?]. In each element of @scheme[assocs], the @scheme[car] -of each pair is a key, and the @scheme[cdr] is the corresponding -value. The mappings are added to the table in the order that they -appear in @scheme[assocs], so later mappings can hide earlier -mappings.} +@scheme[equal?]. The table is created with the content of +@scheme[assocs] as in @scheme[make-hash].} @defproc[(make-immutable-hasheqv [assocs (listof pair?)]) (and/c hash? hash-eqv? immutable?)]{ diff --git a/collects/tests/mzscheme/basic.ss b/collects/tests/mzscheme/basic.ss index 7aaf513a2b..c465b489e9 100644 --- a/collects/tests/mzscheme/basic.ss +++ b/collects/tests/mzscheme/basic.ss @@ -1967,12 +1967,12 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; hash tables -(arity-test make-hash 0 0) -(arity-test make-hasheq 0 0) -(arity-test make-hasheqv 0 0) -(arity-test make-weak-hash 0 0) -(arity-test make-weak-hasheq 0 0) -(arity-test make-weak-hasheqv 0 0) +(arity-test make-hash 0 1) +(arity-test make-hasheq 0 1) +(arity-test make-hasheqv 0 1) +(arity-test make-weak-hash 0 1) +(arity-test make-weak-hasheq 0 1) +(arity-test make-weak-hasheqv 0 1) (define (hash-tests make-hash make-hasheq make-hasheqv make-weak-hash make-weak-hasheq make-weak-hasheqv diff --git a/src/mzscheme/src/list.c b/src/mzscheme/src/list.c index 2963747aee..d7ab8d0772 100644 --- a/src/mzscheme/src/list.c +++ b/src/mzscheme/src/list.c @@ -459,32 +459,32 @@ scheme_init_list (Scheme_Env *env) scheme_add_global_constant("make-hash", scheme_make_immed_prim(make_hash, "make-hash", - 0, 0), + 0, 1), env); scheme_add_global_constant("make-hasheq", scheme_make_immed_prim(make_hasheq, "make-hasheq", - 0, 0), + 0, 1), env); scheme_add_global_constant("make-hasheqv", scheme_make_immed_prim(make_hasheqv, "make-hasheqv", - 0, 0), + 0, 1), env); scheme_add_global_constant("make-weak-hash", scheme_make_immed_prim(make_weak_hash, "make-weak-hash", - 0, 0), + 0, 1), env); scheme_add_global_constant("make-weak-hasheq", scheme_make_immed_prim(make_weak_hasheq, "make-weak-hasheq", - 0, 0), + 0, 1), env); scheme_add_global_constant("make-weak-hasheqv", scheme_make_immed_prim(make_weak_hasheqv, "make-weak-hasheqv", - 0, 0), + 0, 1), env); scheme_add_global_constant("make-immutable-hash", scheme_make_immed_prim(make_immutable_hash, @@ -1555,34 +1555,77 @@ Scheme_Bucket_Table *scheme_make_weak_eqv_table(void) return t; } +static Scheme_Object *fill_table(Scheme_Object *ht, const char *who, + int argc, Scheme_Object **argv) +{ + Scheme_Object *l, *a, *args[3]; + + if (argc) { + l = argv[0]; + if (scheme_proper_list_length(l) >= 0) { + for (; SCHEME_PAIRP(l); l = SCHEME_CDR(l)) { + a = SCHEME_CAR(l); + if (!SCHEME_PAIRP(a)) + break; + } + } + + if (!SCHEME_NULLP(l)) + scheme_wrong_type(who, "list of pairs", 0, argc, argv); + + args[0] = ht; + + for (l = argv[0]; SCHEME_PAIRP(l); l = SCHEME_CDR(l)) { + a = SCHEME_CAR(l); + args[1] = SCHEME_CAR(a); + args[2] = SCHEME_CDR(a); + hash_table_put_bang(3, args); + } + } + + return ht; +} + static Scheme_Object *make_hash(int argc, Scheme_Object *argv[]) { - return (Scheme_Object *)scheme_make_hash_table_equal(); + Scheme_Object *ht; + ht = (Scheme_Object *)scheme_make_hash_table_equal(); + return fill_table(ht, "make-hash", argc, argv); } static Scheme_Object *make_hasheq(int argc, Scheme_Object *argv[]) { - return (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr); + Scheme_Object *ht; + ht = (Scheme_Object *)scheme_make_hash_table(SCHEME_hash_ptr); + return fill_table(ht, "make-hasheq", argc, argv); } static Scheme_Object *make_hasheqv(int argc, Scheme_Object *argv[]) { - return (Scheme_Object *)scheme_make_hash_table_eqv(); + Scheme_Object *ht; + ht = (Scheme_Object *)scheme_make_hash_table_eqv(); + return fill_table(ht, "make-hasheqv", argc, argv); } static Scheme_Object *make_weak_hash(int argc, Scheme_Object *argv[]) { - return (Scheme_Object *)scheme_make_weak_equal_table(); + Scheme_Object *ht; + ht = (Scheme_Object *)scheme_make_weak_equal_table(); + return fill_table(ht, "make-weak-hash", argc, argv); } static Scheme_Object *make_weak_hasheq(int argc, Scheme_Object *argv[]) { - return (Scheme_Object *)scheme_make_bucket_table(20, SCHEME_hash_weak_ptr); + Scheme_Object *ht; + ht = (Scheme_Object *)scheme_make_bucket_table(20, SCHEME_hash_weak_ptr); + return fill_table(ht, "make-weak-hasheq", argc, argv); } static Scheme_Object *make_weak_hasheqv(int argc, Scheme_Object *argv[]) { - return (Scheme_Object *)scheme_make_weak_eqv_table(); + Scheme_Object *ht; + ht = (Scheme_Object *)scheme_make_weak_eqv_table(); + return fill_table(ht, "make-weak-hasheqv", argc, argv); } static Scheme_Object *make_immutable_table(const char *who, int kind, int argc, Scheme_Object *argv[])