[m-dev.] for review: two useful additions to map.m
Ralph Becket
rbeck at microsoft.com
Mon May 29 23:17:13 AEST 2000
I end up coding these functions so often I thought they should go
into map.m.
Ralph
estimated hours taken: 0.25
Added two functions to the map module that I often end up having to
code elsewhere.
library/map.m
Added func map__add_to_list(Map, K, V). V is consed onto the
list value mapped to by K in Map (if such a mapping exists)
or the mapping for K is set as [V] (otherwise).
Added func map__add_to_set(Map, K, V) which is similar to the
above, except that the value type for the mapping is set/1
and the corresponding operations are set__insert/2 and
set__make_singleton_list/1 respectively.
Index: map.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/map.m,v
retrieving revision 1.73
diff -u -u -r1.73 map.m
--- map.m 2000/03/31 01:05:47 1.73
+++ map.m 2000/05/29 13:11:54
@@ -106,6 +106,15 @@
:- mode map__set(di, di, di, uo) is det.
:- mode map__set(in, in, in, out) is det.
+ % Cons a new member onto the list value for the key or
+ % set the value for the key as the singleton list containing
+ % the new member if no mapping already exists.
+:- func map__add_to_list(map(K, list(V)), K, V) = map(K, list(V)).
+
+ % Similar to the above, except that the value type is a set
+ % rather than a list.
+:- func map__add_to_set(map(K, set(V)), K, V) = map(K, set(V)).
+
% Given a map, return a list of all the keys in the map.
:- pred map__keys(map(K, _V), list(K)).
:- mode map__keys(in, out) is det.
@@ -767,4 +776,28 @@
map__det_union(F, M1, M2) = M3 :-
P = ( pred(X::in, Y::in, Z::out) is semidet :- Z = F(X, Y) ),
map__det_union(P, M1, M2, M3).
+
+%
----------------------------------------------------------------------------
%
+
+map__add_to_list(Map, K, V) =
+ ( if
+ map__search(Map, K, Vs)
+ then
+ map__set(Map, K, [V | Vs])
+ else
+ map__set(Map, K, [V])
+ ).
+
+%
----------------------------------------------------------------------------
%
+
+map__add_to_set(Map, K, V) =
+ ( if
+ map__search(Map, K, Vs)
+ then
+ map__set(Map, K, set__insert(Vs, V))
+ else
+ map__set(Map, K, set__make_singleton_set(V))
+ ).
+
--
Ralph Becket | MSR Cambridge | rbeck at microsoft.com
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list