[m-rev.] New library module: default_map.m

Ralph Becket rbeck at microsoft.com
Thu Jul 12 20:56:56 AEST 2001


> From: Fergus Henderson [mailto:fjh at cs.mu.OZ.AU]
> Sent: 11 July 2001 14:03
> 
> Well, I just don't think that this is worth another module, and more
> importantly another type with its own set of procedures to go with it.

That sounds fair.  How's this:



Estimated hours taken: 0.5
Branches: main

Add "defaulty" versions of lookup to map.m and multi_map.m.

library/map.m:
	Added func default_lookup/3.

library/multi_map.m:
	Added pred default_lookup/3.

NEWS:
	Noted new additions.

Index: map.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/map.m,v
retrieving revision 1.80
diff -u -r1.80 map.m
--- map.m	2001/05/24 02:32:28	1.80
+++ map.m	2001/07/12 10:51:54
@@ -60,6 +60,10 @@
 
 :- func map__lookup(map(K,V), K) = V.
 
+	% Search map for key, but return default value if
+	% no binding found.
+:- func map__default_lookup(V, map(K, V), K) = V.
+
 :- pred map__lower_bound_search(map(K,V), K, K, V).
 :- mode map__lower_bound_search(in, in, out, out) is semidet.
 
@@ -792,6 +796,9 @@
 
 map__lookup(M, K) = V :-
 	map__lookup(M, K, V).
+
+map__default_lookup(DV, M, K) =
+	( if M ^ elem(K) = V then V else DV ).
 
 map__insert(M1, K, V) = M2 :-
 	map__insert(M1, K, V, M2).
Index: 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
--- multi_map.m	2000/03/28 03:41:10	1.8
+++ multi_map.m	2001/07/12 10:51:54
@@ -56,6 +56,11 @@
 :- pred multi_map__lookup(multi_map(K,V), K, list(V)).
 :- mode multi_map__lookup(in, in, out) is det.
 
+	% Search multi_map for key, but return the empty list
+	% if no binding found.
+:- pred multi_map__default_lookup(multi_map(K, V), K, list(V)).
+:- mode multi_map__default_lookup(in, in, out) is det.
+
 	% Search multi_map for key.
 :- pred multi_map__nondet_lookup(multi_map(K,V), K, V).
 :- mode multi_map__nondet_lookup(in, in, out) is nondet.
@@ -215,6 +220,9 @@
 
 multi_map__lookup(MultiMap, Key, Values) :-
 	map__lookup(MultiMap, Key, Values).
+
+multi_map__default_lookup(MultiMap, Key, Values) :-
+	Values = map__default_lookup([], MultiMap, Key).
 
 multi_map__nondet_lookup(MultiMap, Key, Value) :-
 	map__search(MultiMap, Key, Values),
Index: ../NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.213
diff -u -r1.213 NEWS
--- ../NEWS	2001/06/27 05:03:57	1.213
+++ ../NEWS	2001/07/12 10:51:54
@@ -42,6 +42,9 @@
 
 * We've added a predicate version of `set__fold'.
 
+* We've added function `map__default_lookup/3' and predicate
+  multi_map__default_lookup/3'.
+
 Changes to the Mercury implementation:
 * We've fixed a long-standing bug in the handling of module imports.
   Previously, if `module1' imported `module2' which imported `module3'
in
--------------------------------------------------------------------------
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