[m-rev.] diff: fix multi_map__from_corresponding_lists

Mark Brown dougl at cs.mu.OZ.AU
Tue Aug 20 10:41:08 AEST 2002


Estimated hours taken: 0.25
Branches: main, release

library/multi_map.m:
	Fix multi_map__from_corresponding_lists so that it works as
	expected if a key appears more than once in the key list.

tests/hard_coded/Mmakefile:
tests/hard_coded/multi_map_test.exp:
tests/hard_coded/multi_map_test.m:
	Test case.

Index: library/multi_map.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/multi_map.m,v
retrieving revision 1.8
diff -u -r1.8 multi_map.m
--- library/multi_map.m	2000/03/28 03:41:10	1.8
+++ library/multi_map.m	2002/08/19 20:52:08
@@ -1,5 +1,5 @@
 %---------------------------------------------------------------------------%
-% Copyright (C) 1995, 1997, 2000 The University of Melbourne.
+% Copyright (C) 1995, 1997, 2000, 2002 The University of Melbourne.
 % This file may only be copied under the terms of the GNU Library General
 % Public License - see the file COPYING.LIB in the Mercury distribution.
 %-----------------------------------------------------------------------------%
@@ -189,7 +189,7 @@
 
 :- implementation.
 
-:- import_module std_util, int.
+:- import_module std_util, int, require.
 
 %-----------------------------------------------------------------------------%
 
@@ -321,9 +321,28 @@
 
 %-----------------------------------------------------------------------------%
 
-multi_map__from_corresponding_lists(Keys, Values0, MultiMap) :-
-	list__chunk(Values0, 1, Values),
-	map__from_corresponding_lists(Keys, Values, MultiMap).
+multi_map__from_corresponding_lists(Keys, Values, MultiMap) :-
+	multi_map__init(MultiMap0),
+	(
+		multi_map__from_corresponding_lists_2(MultiMap0, Keys, Values,
+			MultiMap1)
+	->
+		MultiMap = MultiMap1
+	;
+		error("multi_map__from_corresponding_lists: list length mismatch")
+	).
+
+:- pred multi_map__from_corresponding_lists_2(multi_map(K, V), list(K), list(V),
+	multi_map(K, V)).
+:- mode multi_map__from_corresponding_lists_2(in, in, in, out) is semidet.
+
+multi_map__from_corresponding_lists_2(MultiMap, [], [], MultiMap).
+multi_map__from_corresponding_lists_2(MultiMap0, [Key | Keys], [Value | Values],
+	MultiMap) :-
+
+	multi_map__set(MultiMap0, Key, Value, MultiMap1),
+	multi_map__from_corresponding_lists_2(MultiMap1, Keys, Values,
+		MultiMap).
 
 multi_map__from_corresponding_list_lists(Keys, Values, MultiMap) :-
 	map__from_corresponding_lists(Keys, Values, MultiMap).
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.163
diff -u -r1.163 Mmakefile
--- tests/hard_coded/Mmakefile	2002/08/17 13:52:10	1.163
+++ tests/hard_coded/Mmakefile	2002/08/19 15:36:23
@@ -90,6 +90,7 @@
 	merge_and_remove_dups \
 	minint_bug \
 	mode_choice \
+	multi_map_test \
 	multimode \
 	myset_test \
 	name_mangling \
Index: tests/hard_coded/multi_map_test.exp
===================================================================
RCS file: multi_map_test.exp
diff -N multi_map_test.exp
--- /dev/null	Mon Dec 17 21:20:53 2001
+++ multi_map_test.exp	Tue Aug 20 07:06:59 2002
@@ -0,0 +1 @@
+[11, 13]
Index: tests/hard_coded/multi_map_test.m
===================================================================
RCS file: multi_map_test.m
diff -N multi_map_test.m
--- /dev/null	Mon Dec 17 21:20:53 2001
+++ multi_map_test.m	Tue Aug 20 07:03:48 2002
@@ -0,0 +1,14 @@
+:- module multi_map_test.
+:- interface.
+:- import_module io.
+:- pred main(io__state::di, io__state::uo) is det.
+:- implementation.
+:- import_module multi_map, list, std_util.
+
+main -->
+	{ multi_map__from_corresponding_lists([1,  2,  1], [11, 22, 13], M) },
+	{ multi_map__lookup(M, 1, Vs0) },
+	{ list__sort_and_remove_dups(Vs0, Vs) },
+	io__write(Vs),
+	io__nl.
+
--------------------------------------------------------------------------
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