[m-rev.] for review: fix behaviour of bimap.from_assoc_list

Julien Fischer juliensf at cs.mu.OZ.AU
Mon Mar 7 15:16:28 AEDT 2005


For review by anyone.

Estimated hours taken: 0.5
Branches: main, release

Change the implementation of bimap.from_assoc_list so that
it throws an exception if the association list does not
implicitly define a bijection.  At the moment it is possible
to use it to create bimaps that are not bijections.

library/bimap.m:
	Make bimap.from_assoc_list work only with association
	lists that implicitly define bijections.

Julien.

Index: library/bimap.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/bimap.m,v
retrieving revision 1.16.2.2
diff -u -r1.16.2.2 bimap.m
--- library/bimap.m	7 Mar 2005 03:02:25 -0000	1.16.2.2
+++ library/bimap.m	7 Mar 2005 04:10:53 -0000
@@ -136,7 +136,10 @@
 :- func bimap__to_assoc_list(bimap(K, V)) = assoc_list(K, V).

 	% Convert an association list to a bimap.
-	%
+	% Throws an exception if the association list does not implicitly
+	% define a bijection, i.e a key or value occurs multiple times in
+	% the association list.
+	%
 :- pred bimap__from_assoc_list(assoc_list(K, V)::in, bimap(K, V)::out) is det.
 :- func bimap__from_assoc_list(assoc_list(K, V)) = bimap(K, V).

@@ -332,10 +335,8 @@
 bimap__to_assoc_list(bimap(Forward, _), L) :-
 	map__to_assoc_list(Forward, L).

-bimap__from_assoc_list(L, bimap(Forward, Reverse)) :-
-	map__from_assoc_list(L, Forward),
-	assoc_list__reverse_members(L, L1),
-	map__from_assoc_list(L1, Reverse).
+bimap__from_assoc_list(L, Bimap) :-
+	bimap__det_insert_from_assoc_list(L, bimap.init, Bimap).

 bimap__from_corresponding_lists(Ks, Vs, BM) :-
 	assoc_list__from_corresponding_lists(Ks, Vs, L),


--------------------------------------------------------------------------
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