[m-rev.] diff: fix hash_table initialization bug
Simon Taylor
stayl at cs.mu.OZ.AU
Thu Mar 13 12:09:45 AEDT 2003
Estimated hours taken: 1
Branches: main, release
library/hash_table.m:
The arrays containing the keys and values are initialized
using the key and value of the first insertion. This was not
being done if the first insertion was done with
hash_table__det_insert rather than hash_table__set.
tests/hard_coded/Mmakefile:
tests/hard_coded/hash_init_bug.{m,exp}:
Test case.
Index: library//hash_table.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/hash_table.m,v
retrieving revision 1.4
diff -u -u -r1.4 hash_table.m
--- library//hash_table.m 29 Oct 2002 16:51:27 -0000 1.4
+++ library//hash_table.m 13 Mar 2003 00:32:12 -0000
@@ -342,6 +342,12 @@
throw(software_error("hash_table__det_insert: key already present"))
else if HT ^ num_occupants = HT ^ max_occupants then
set(expand(HT), K, V)
+ else if array__size(HT ^ values) = 0 then
+ % If this is the first entry in the hash table, then we use it to
+ % set up the hash table (the arrays are currently empty because we
+ % need values to initialise them with).
+ %
+ set(HT, K, V)
else
(((( HT
^ num_occupants := HT ^ num_occupants + 1 )
Index: tests/hard_coded//Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.193
diff -u -u -r1.193 Mmakefile
--- tests/hard_coded//Mmakefile 3 Mar 2003 03:25:14 -0000 1.193
+++ tests/hard_coded//Mmakefile 13 Mar 2003 00:33:07 -0000
@@ -70,6 +70,7 @@
func_test \
getopt_test \
ground_dd \
+ hash_init_bug \
higher_order_func_test \
higher_order_syntax \
higher_order_syntax2 \
Index: tests/hard_coded//hash_init_bug.exp
===================================================================
RCS file: tests/hard_coded//hash_init_bug.exp
diff -N tests/hard_coded//hash_init_bug.exp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded//hash_init_bug.exp 13 Mar 2003 01:07:08 -0000
@@ -0,0 +1 @@
+search failed as expected
Index: tests/hard_coded//hash_init_bug.m
===================================================================
RCS file: tests/hard_coded//hash_init_bug.m
diff -N tests/hard_coded//hash_init_bug.m
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded//hash_init_bug.m 13 Mar 2003 01:07:03 -0000
@@ -0,0 +1,68 @@
+:- module hash_init_bug.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- import_module assoc_list, exception, hash_table, list.
+:- import_module map, require, std_util, string.
+
+main(!IO) :-
+ HashPred = (pred(Name::in, Hash1::out, Hash2::out) is det :-
+ sym_name_to_string(Name, ".", Str),
+ Hash1 = hash(Str),
+ Hash2 = hash(from_rev_char_list(to_char_list(Str)))
+ ),
+ HT0 = new(HashPred, 10, 0.8),
+ build_table(entries, HT0, HT),
+ (
+ hash_table__search(HT,
+ qualified(unqualified("io"), "read_word"), _)
+ ->
+ io__write_string("error: search succeeded\n", !IO)
+ ;
+ io__write_string("search failed as expected\n", !IO)
+ ).
+
+:- pred build_table(list(sym_name)::in, name_ht::hash_table_di,
+ name_ht::hash_table_uo) is det.
+
+build_table([], HT, HT).
+build_table([K | T], HT0, HT) :-
+ build_table(T, det_insert(HT0, K, 1), HT).
+
+:- type name_ht == hash_table(sym_name, int).
+
+:- type sym_name
+ ---> unqualified(string)
+ ; qualified(sym_name, string).
+
+ % sym_name_to_string(SymName, Separator, String):
+ % convert a symbol name to a string,
+ % with module qualifiers separated by Separator.
+:- pred sym_name_to_string(sym_name, string, string).
+:- mode sym_name_to_string(in, in, out) is det.
+
+sym_name_to_string(SymName, Separator, String) :-
+ sym_name_to_string_2(SymName, Separator, Parts, []),
+ string__append_list(Parts, String).
+
+:- pred sym_name_to_string_2(sym_name, string,
+ list(string), list(string)).
+:- mode sym_name_to_string_2(in, in, out, in) is det.
+
+sym_name_to_string_2(qualified(ModuleSpec,Name), Separator) -->
+ sym_name_to_string_2(ModuleSpec, Separator),
+ [Separator, Name].
+sym_name_to_string_2(unqualified(Name), _) -->
+ [Name].
+
+:- func entries = list(sym_name).
+
+entries = [
+ unqualified("main")
+].
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list