[m-rev.] for review: fix a bug in bimap.set/4

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


Estimated hours taken: 2
Branches: main, release

The current implementation of of bimap.set/4 breaks if you try
to set a Key-Value pair that have already been mapped to each other.

library/bimap.m:
	Fix the implementation of bimap.set/4.

tests/hard_coded/bimap_set_bug.m:
tests/hard_coded/bimap_set_bug.exp:
	Add a test for the above bug.

Julien.

Workspace:/home/earth/juliensf/mercury-0.12
Index: library/bimap.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/bimap.m,v
retrieving revision 1.16.2.1
diff -u -r1.16.2.1 bimap.m
--- library/bimap.m	2 Feb 2005 04:44:34 -0000	1.16.2.1
+++ library/bimap.m	7 Mar 2005 00:59:50 -0000
@@ -233,20 +233,30 @@

 bimap__set(bimap(Forward0, Reverse0), K, V, bimap(Forward, Reverse)) :-
 	( map__search(Forward0, K, KVal) ->
-		map__det_update(Forward0, K, V, Forward1),
-		map__delete(Reverse0, KVal, Reverse1)
+		( V \= KVal ->
+			map__det_update(Forward0, K, V, Forward1),
+			map__delete(Reverse0, KVal, Reverse1)
+		;
+			Forward1 = Forward0,
+			Reverse1 = Reverse0
+		)
 	;
 		map__det_insert(Forward0, K, V, Forward1),
-		Reverse1 = Reverse0
+		Reverse0 = Reverse1
 	),
 	( map__search(Reverse0, V, VKey) ->
-		map__det_update(Reverse1, V, K, Reverse),
-		map__delete(Forward1, VKey, Forward)
+		( K \= VKey ->
+			map__det_update(Reverse1, V, K, Reverse),
+			map__delete(Forward1, VKey, Forward)
+		;
+			Forward = Forward1,
+			Reverse = Reverse1
+		)
 	;
 		map__det_insert(Reverse1, V, K, Reverse),
 		Forward = Forward1
 	).
-
+
 bimap__det_insert_from_assoc_list([], !BM).
 bimap__det_insert_from_assoc_list([Key - Value | KeysValues], !BM) :-
 	bimap__det_insert(!.BM, Key, Value, !:BM),
Index: tests/hard_coded/bimap_set_bug.exp
===================================================================
RCS file: tests/hard_coded/bimap_set_bug.exp
diff -N tests/hard_coded/bimap_set_bug.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/bimap_set_bug.exp	7 Mar 2005 01:01:22 -0000
@@ -0,0 +1,24 @@
+Forward map is:
+map([
+ hydrogen ->
+   h,
+ helium ->
+   he,
+ lithium ->
+   li,
+ beryllium ->
+   be,
+ sodium ->
+   na])
+Reverse map is:
+map([
+ h ->
+   hydrogen,
+ he ->
+   helium,
+ li ->
+   lithium,
+ be ->
+   beryllium,
+ na ->
+   sodium])
Index: tests/hard_coded/bimap_set_bug.m
===================================================================
RCS file: tests/hard_coded/bimap_set_bug.m
diff -N tests/hard_coded/bimap_set_bug.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/bimap_set_bug.m	7 Mar 2005 01:01:22 -0000
@@ -0,0 +1,59 @@
+:- module bimap_set_bug.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module bimap, pprint.
+
+:- type element
+	---> 	hydrogen
+	;	helium
+	;	lithium
+	;	beryllium
+	;	boron
+	;	carbon
+	;	nitrogen
+	;	oxygen
+	;	fluorine
+	;	neon
+	;	sodium.
+
+:- type symbol
+	--->	h
+	;	he
+	;	li
+	;	be
+	;	b
+	;	c
+	;	n
+	;	o
+	;	f
+	;	ne
+	;	na.
+
+main(!IO) :-
+	some [!Bimap] (
+		bimap.init(!:Bimap),
+		bimap.set(!.Bimap, hydrogen,   na,  !:Bimap),
+		bimap.set(!.Bimap, helium,     he,  !:Bimap),
+		bimap.set(!.Bimap, lithium,    li,  !:Bimap),
+		bimap.set(!.Bimap, beryllium,  be,  !:Bimap),
+		bimap.set(!.Bimap, hydrogen,   na,  !:Bimap),
+		bimap.set(!.Bimap, hydrogen,   h,   !:Bimap),
+		bimap.set(!.Bimap, sodium,     h,   !:Bimap),
+		bimap.set(!.Bimap, sodium,     na,  !:Bimap),
+		bimap.set(!.Bimap, hydrogen,   h,   !:Bimap),
+		Forward = to_doc(bimap.forward_map(!.Bimap)),
+		Reverse = to_doc(bimap.reverse_map(!.Bimap)),
+		io.write_string("Forward map is:\n", !IO),
+		pprint.write(4, Forward, !IO),
+		io.nl(!IO),
+		io.write_string("Reverse map is:\n", !IO),
+		pprint.write(4, Reverse, !IO),
+		io.nl(!IO)
+	).
--------------------------------------------------------------------------
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