[m-dev.] diff: clean up map.m
Zoltan Somogyi
zs at cs.mu.OZ.AU
Mon Jan 12 17:03:54 AEDT 2004
library/map.m:
Bring this module up to date with respect to our coding guidelines:
use predmode syntax where relevant. There are no changes in algorithms.
Zoltan.
cvs diff: Diffing .
Index: map.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/map.m,v
retrieving revision 1.88
diff -u -b -b -r1.88 map.m
--- map.m 22 Dec 2003 11:21:48 -0000 1.88
+++ map.m 8 Jan 2004 05:17:59 -0000
@@ -10,7 +10,7 @@
%
% This file provides the 'map' ADT.
% A map (also known as a dictionary or an associative array) is a collection
-% of (Key,Data) pairs which allows you to look up any Data item given the
+% of (Key, Data) pairs which allows you to look up any Data item given the
% Key.
%
% The implementation is using balanced binary trees, as provided by
@@ -31,252 +31,198 @@
%-----------------------------------------------------------------------------%
% Initialize an empty map.
-:- pred map__init(map(_,_)).
-:- mode map__init(uo) is det.
-
-:- func map__init = map(K, V).
-:- mode map__init = uo is det.
+:- pred map__init(map(_, _)::uo) is det.
+:- func map__init = (map(K, V)::uo) is det.
% Check whether a map is empty.
-:- pred map__is_empty(map(_,_)).
-:- mode map__is_empty(in) is semidet.
+:- pred map__is_empty(map(_, _)::in) is semidet.
% Check whether map contains key
-:- pred map__contains(map(K,_V), K).
-:- mode map__contains(in, in) is semidet.
+:- pred map__contains(map(K, _V)::in, K::in) is semidet.
-:- pred map__member(map(K,V), K, V).
-:- mode map__member(in, out, out) is nondet.
+:- pred map__member(map(K, V)::in, K::out, V::out) is nondet.
% Search map for key.
-:- pred map__search(map(K,V), K, V).
-:- mode map__search(in, in, out) is semidet.
-
-:- func map__search(map(K,V), K) = V is semidet.
+:- pred map__search(map(K, V)::in, K::in, V::out) is semidet.
+:- func map__search(map(K, V), K) = V is semidet.
% Search map for key, but abort if search fails.
-:- pred map__lookup(map(K,V), K, V).
-:- mode map__lookup(in, in, out) is det.
-
-:- func map__lookup(map(K,V), K) = V.
+:- pred map__lookup(map(K, V)::in, K::in, V::out) is det.
+:- func map__lookup(map(K, V), K) = V.
% Search for a key-value pair using the key. If there is no entry
% for the given key, returns the pair for the next lower key instead.
% Fails if there is no key with the given or lower value.
-:- pred map__lower_bound_search(map(K,V), K, K, V).
-:- mode map__lower_bound_search(in, in, out, out) is semidet.
+:- pred map__lower_bound_search(map(K, V)::in, K::in, K::out, V::out)
+ is semidet.
% Search for a key-value pair using the key. If there is no entry
% for the given key, returns the pair for the next lower key instead.
% Aborts if there is no key with the given or lower value.
-:- pred map__lower_bound_lookup(map(K,V), K, K, V).
-:- mode map__lower_bound_lookup(in, in, out, out) is det.
+:- pred map__lower_bound_lookup(map(K, V)::in, K::in, K::out, V::out) is det.
% Search for a key-value pair using the key. If there is no entry
% for the given key, returns the pair for the next higher key instead.
% Fails if there is no key with the given or higher value.
-:- pred map__upper_bound_search(map(K,V), K, K, V).
-:- mode map__upper_bound_search(in, in, out, out) is semidet.
+:- pred map__upper_bound_search(map(K, V)::in, K::in, K::out, V::out)
+ is semidet.
% Search for a key-value pair using the key. If there is no entry
% for the given key, returns the pair for the next higher key instead.
% Aborts if there is no key with the given or higher value.
-:- pred map__upper_bound_lookup(map(K,V), K, K, V).
-:- mode map__upper_bound_lookup(in, in, out, out) is det.
+:- pred map__upper_bound_lookup(map(K, V)::in, K::in, K::out, V::out) is det.
% Search map for data.
-:- pred map__inverse_search(map(K,V), V, K).
-:- mode map__inverse_search(in, in, out) is nondet.
+:- pred map__inverse_search(map(K, V)::in, V::in, K::out) is nondet.
% Insert a new key and corresponding value into a map.
% Fail if the key already exists.
-:- pred map__insert(map(K,V), K, V, map(K,V)).
-:- mode map__insert(in, in, in, out) is semidet.
-
-:- func map__insert(map(K,V), K, V) = map(K,V) is semidet.
+:- pred map__insert(map(K, V)::in, K::in, V::in, map(K, V)::out) is semidet.
+:- func map__insert(map(K, V), K, V) = map(K, V) is semidet.
% Insert a new key and corresponding value into a map.
% Abort if the key already exists.
-:- pred map__det_insert(map(K,V), K, V, map(K,V)).
-:- mode map__det_insert(in, in, in, out) is det.
-
-:- func map__det_insert(map(K,V), K, V) = map(K,V).
+:- pred map__det_insert(map(K, V)::in, K::in, V::in, map(K, V)::out) is det.
+:- func map__det_insert(map(K, V), K, V) = map(K, V).
% Apply map__det_insert to key - value pairs from corresponding lists.
-:- pred map__det_insert_from_corresponding_lists(map(K,V), list(K),
- list(V), map(K,V)).
-:- mode map__det_insert_from_corresponding_lists(in, in, in, out) is det.
+:- pred map__det_insert_from_corresponding_lists(map(K, V)::in, list(K)::in,
+ list(V)::in, map(K, V)::out) is det.
-:- func map__det_insert_from_corresponding_lists(map(K,V), list(K), list(V)) =
- map(K,V).
+:- func map__det_insert_from_corresponding_lists(map(K, V), list(K), list(V))
+ = map(K, V).
% Apply map__det_insert to key - value pairs from the assoc_lists.
-:- pred map__det_insert_from_assoc_list(map(K,V), assoc_list(K, V),
- map(K,V)).
-:- mode map__det_insert_from_assoc_list(in, in, out) is det.
-
-:- func map__det_insert_from_assoc_list(map(K,V), assoc_list(K, V)) = map(K,V).
+:- pred map__det_insert_from_assoc_list(map(K, V)::in, assoc_list(K, V)::in,
+ map(K, V)::out) is det.
+:- func map__det_insert_from_assoc_list(map(K, V), assoc_list(K, V))
+ = map(K, V).
% Apply map__set to key - value pairs from corresponding lists.
-:- pred map__set_from_corresponding_lists(map(K,V), list(K),
- list(V), map(K,V)).
-:- mode map__set_from_corresponding_lists(in, in, in, out) is det.
-
-:- func map__set_from_corresponding_lists(map(K,V), list(K), list(V)) =
- map(K,V).
-
-:- pred map__set_from_assoc_list(map(K,V), assoc_list(K, V), map(K,V)).
-:- mode map__set_from_assoc_list(in, in, out) is det.
-
-:- func map__set_from_assoc_list(map(K,V), assoc_list(K, V)) = map(K,V).
+:- pred map__set_from_corresponding_lists(map(K, V)::in, list(K)::in,
+ list(V)::in, map(K, V)::out) is det.
+:- func map__set_from_corresponding_lists(map(K, V), list(K), list(V))
+ = map(K, V).
+
+:- pred map__set_from_assoc_list(map(K, V)::in, assoc_list(K, V)::in,
+ map(K, V)::out) is det.
+:- func map__set_from_assoc_list(map(K, V), assoc_list(K, V)) = map(K, V).
% Update the value corresponding to a given key
% Fail if the key doesn't already exist.
-:- pred map__update(map(K,V), K, V, map(K,V)).
-:- mode map__update(in, in, in, out) is semidet.
-
-:- func map__update(map(K,V), K, V) = map(K,V) is semidet.
+:- pred map__update(map(K, V)::in, K::in, V::in, map(K, V)::out) is semidet.
+:- func map__update(map(K, V), K, V) = map(K, V) is semidet.
% Update the value corresponding to a given key
% Abort if the key doesn't already exist.
-:- pred map__det_update(map(K,V), K, V, map(K,V)).
-:- mode map__det_update(in, in, in, out) is det.
-
-:- func map__det_update(map(K,V), K, V) = map(K,V).
+:- pred map__det_update(map(K, V)::in, K::in, V::in, map(K, V)::out) is det.
+:- func map__det_update(map(K, V), K, V) = map(K, V).
% Update value if the key is already present, otherwise
% insert new key and value.
-:- pred map__set(map(K,V), K, V, map(K,V)).
+:- pred map__set(map(K, V), K, V, map(K, V)).
:- mode map__set(di, di, di, uo) is det.
:- mode map__set(in, in, in, out) is det.
-:- func map__set(map(K,V), K, V) = map(K,V).
+:- func map__set(map(K, V), K, V) = map(K, 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.
-
+:- pred map__keys(map(K, _V)::in, list(K)::out) is det.
:- func map__keys(map(K, _V)) = list(K).
% Given a map, return a list of all the keys in the map,
% in sorted order.
-:- pred map__sorted_keys(map(K, _V), list(K)).
-:- mode map__sorted_keys(in, out) is det.
-
+:- pred map__sorted_keys(map(K, _V)::in, list(K)::out) is det.
:- func map__sorted_keys(map(K, _V)) = list(K).
% Given a map, return a list of all the data values in the map.
-:- pred map__values(map(_K, V), list(V)).
-:- mode map__values(in, out) is det.
-
+:- pred map__values(map(_K, V)::in, list(V)::out) is det.
:- func map__values(map(_K, V)) = list(V).
% Convert a map to an association list.
-:- pred map__to_assoc_list(map(K,V), assoc_list(K,V)).
-:- mode map__to_assoc_list(in, out) is det.
-
-:- func map__to_assoc_list(map(K,V)) = assoc_list(K,V).
+:- pred map__to_assoc_list(map(K, V)::in, assoc_list(K, V)::out) is det.
+:- func map__to_assoc_list(map(K, V)) = assoc_list(K, V).
% Convert a map to an association list which is sorted on the keys.
-:- pred map__to_sorted_assoc_list(map(K,V), assoc_list(K,V)).
-:- mode map__to_sorted_assoc_list(in, out) is det.
-
-:- func map__to_sorted_assoc_list(map(K,V)) = assoc_list(K,V).
+:- pred map__to_sorted_assoc_list(map(K, V)::in, assoc_list(K, V)::out) is det.
+:- func map__to_sorted_assoc_list(map(K, V)) = assoc_list(K, V).
% Convert an association list to a map.
-:- pred map__from_assoc_list(assoc_list(K,V), map(K,V)).
-:- mode map__from_assoc_list(in, out) is det.
-
-:- func map__from_assoc_list(assoc_list(K,V)) = map(K,V).
+:- pred map__from_assoc_list(assoc_list(K, V)::in, map(K, V)::out) is det.
+:- func map__from_assoc_list(assoc_list(K, V)) = map(K, V).
% Convert a sorted association list to a map.
-:- pred map__from_sorted_assoc_list(assoc_list(K,V), map(K,V)).
-:- mode map__from_sorted_assoc_list(in, out) is det.
-
-:- func map__from_sorted_assoc_list(assoc_list(K,V)) = map(K,V).
+:- pred map__from_sorted_assoc_list(assoc_list(K, V)::in, map(K, V)::out)
+ is det.
+:- func map__from_sorted_assoc_list(assoc_list(K, V)) = map(K, V).
% Delete a key-value pair from a map.
% If the key is not present, leave the map unchanged.
-:- pred map__delete(map(K,V), K, map(K,V)).
+:- pred map__delete(map(K, V), K, map(K, V)).
:- mode map__delete(di, in, uo) is det.
:- mode map__delete(in, in, out) is det.
-:- func map__delete(map(K,V), K) = map(K,V).
+:- func map__delete(map(K, V), K) = map(K, V).
% Apply map__delete/3 to a list of keys.
-:- pred map__delete_list(map(K,V), list(K), map(K,V)).
+:- pred map__delete_list(map(K, V), list(K), map(K, V)).
:- mode map__delete_list(di, in, uo) is det.
:- mode map__delete_list(in, in, out) is det.
-:- func map__delete_list(map(K,V), list(K)) = map(K,V).
+:- func map__delete_list(map(K, V), list(K)) = map(K, V).
% Delete a key-value pair from a map and return the value.
% Fail if the key is not present.
-:- pred map__remove(map(K,V), K, V, map(K,V)).
-:- mode map__remove(in, in, out, out) is semidet.
+:- pred map__remove(map(K, V)::in, K::in, V::out, map(K, V)::out) is semidet.
% Delete a key-value pair from a map and return the value.
% Abort if the key is not present.
-:- pred map__det_remove(map(K,V), K, V, map(K,V)).
-:- mode map__det_remove(in, in, out, out) is det.
+:- pred map__det_remove(map(K, V)::in, K::in, V::out, map(K, V)::out) is det.
% Count the number of elements in the map.
-:- pred map__count(map(K, V), int).
-:- mode map__count(in, out) is det.
-
+:- pred map__count(map(K, V)::in, int::out) is det.
:- func map__count(map(K, V)) = int.
% Convert a pair of lists (which must be of the same length)
% to a map.
-:- pred map__from_corresponding_lists(list(K), list(V), map(K, V)).
-:- mode map__from_corresponding_lists(in, in, out) is det.
-
+:- pred map__from_corresponding_lists(list(K)::in, list(V)::in, map(K, V)::out)
+ is det.
:- func map__from_corresponding_lists(list(K), list(V)) = map(K, V).
% For map__merge(MapA, MapB, Map), MapA and MapB must
% not both contain the same key.
-:- pred map__merge(map(K, V), map(K, V), map(K, V)).
-:- mode map__merge(in, in, out) is det.
-
+:- pred map__merge(map(K, V)::in, map(K, V)::in, map(K, V)::out) is det.
:- func map__merge(map(K, V), map(K, V)) = map(K, V).
% For map__overlay(MapA, MapB, Map), if MapA and MapB both
% contain the same key, then Map will map that key to
% the value from MapB. In otherwords, MapB takes precedence
% over MapA.
-:- pred map__overlay(map(K,V), map(K,V), map(K,V)).
-:- mode map__overlay(in, in, out) is det.
-
-:- func map__overlay(map(K,V), map(K,V)) = map(K,V).
+:- pred map__overlay(map(K, V)::in, map(K, V)::in, map(K, V)::out) is det.
+:- func map__overlay(map(K, V), map(K, V)) = map(K, V).
% map__select takes a map and a set of keys and returns
% a map containing the keys in the set and their corresponding
% values.
-:- pred map__select(map(K,V), set(K), map(K,V)).
-:- mode map__select(in, in, out) is det.
-
-:- func map__select(map(K,V), set(K)) = map(K,V).
+:- pred map__select(map(K, V)::in, set(K)::in, map(K, V)::out) is det.
+:- func map__select(map(K, V), set(K)) = map(K, V).
% Given a list of keys, produce a list of their corresponding
% values in a specified map.
-:- pred map__apply_to_list(list(K), map(K, V), list(V)).
-:- mode map__apply_to_list(in, in, out) is det.
-
+:- pred map__apply_to_list(list(K)::in, map(K, V)::in, list(V)::out) is det.
:- func map__apply_to_list(list(K), map(K, V)) = list(V).
% Declaratively, a NOP.
% Operationally, a suggestion that the implementation
% optimize the representation of the map in the expectation
% of a number of lookups but few or no modifications.
-:- pred map__optimize(map(K, V), map(K, V)).
-:- mode map__optimize(in, out) is det.
-
+:- pred map__optimize(map(K, V)::in, map(K, V)::out) is det.
:- func map__optimize(map(K, V)) = map(K, V).
% Remove the smallest item from the map, fail if
% the map is empty.
-:- pred map__remove_smallest(map(K, V), K, V, map(K, V)).
-:- mode map__remove_smallest(in, out, out, out) is semidet.
+:- pred map__remove_smallest(map(K, V)::in, K::out, V::out, map(K, V)::out)
+ is semidet.
% Perform an inorder traversal of the map, applying
% an accumulator predicate for each key-value pair.
@@ -416,7 +362,7 @@
:- import_module tree234.
:- import_module term. % for var/1.
-:- type map(K,V) == tree234(K,V).
+:- type map(K, V) == tree234(K, V).
%-----------------------------------------------------------------------------%
@@ -508,16 +454,21 @@
).
map__det_insert_from_corresponding_lists(Map0, Ks, Vs, Map) :-
- ( Ks = [Key | Keys], Vs = [Value | Values] ->
+ (
+ Ks = [Key | Keys],
+ Vs = [Value | Values]
+ ->
map__det_insert(Map0, Key, Value, Map1),
- map__det_insert_from_corresponding_lists(Map1, Keys,
- Values, Map)
+ map__det_insert_from_corresponding_lists(Map1, Keys, Values,
+ Map)
;
- Ks = [], Vs = []
+ Ks = [],
+ Vs = []
->
Map = Map0
;
- error("map__det_insert_from_corresponding_lists - lists do not correspond")
+ error("map__det_insert_from_corresponding_lists - " ++
+ "lists do not correspond")
).
map__det_insert_from_assoc_list(Map, [], Map).
@@ -526,16 +477,20 @@
map__det_insert_from_assoc_list(Map1, KVs, Map).
map__set_from_corresponding_lists(Map0, Ks, Vs, Map) :-
- ( Ks = [Key | Keys], Vs = [Value | Values] ->
+ (
+ Ks = [Key | Keys],
+ Vs = [Value | Values]
+ ->
map__set(Map0, Key, Value, Map1),
- map__set_from_corresponding_lists(Map1, Keys,
- Values, Map)
+ map__set_from_corresponding_lists(Map1, Keys, Values, Map)
;
- Ks = [], Vs = []
+ Ks = [],
+ Vs = []
->
Map = Map0
;
- error("map__set_from_corresponding_lists - lists do not correspond")
+ error("map__set_from_corresponding_lists - " ++
+ "lists do not correspond")
).
map__set_from_assoc_list(Map, [], Map).
@@ -631,8 +586,8 @@
map__to_assoc_list(Map1, AssocList),
map__overlay_2(AssocList, Map0, Map).
-:- pred map__overlay_2(assoc_list(K,V), map(K,V), map(K,V)).
-:- mode map__overlay_2(in, in, out) is det.
+:- pred map__overlay_2(assoc_list(K, V)::in, map(K, V)::in, map(K, V)::out)
+ is det.
:- pragma type_spec(map__overlay_2/3, K = var(_)).
map__overlay_2([], Map, Map).
@@ -647,15 +602,13 @@
map__init(NewMap0),
map__select_2(KeyList, Original, NewMap0, NewMap).
-:- pred map__select_2(list(K), map(K,V), map(K,V), map(K,V)).
-:- mode map__select_2(in, in, in, out) is det.
+:- pred map__select_2(list(K)::in, map(K, V)::in, map(K, V)::in,
+ map(K, V)::out) is det.
:- pragma type_spec(map__select_2/4, K = var(_)).
map__select_2([], _Original, New, New).
map__select_2([K|Ks], Original, New0, New) :-
- (
- map__search(Original, K, V)
- ->
+ ( map__search(Original, K, V) ->
map__set(New0, K, V, New1)
;
New1 = New0
--------------------------------------------------------------------------
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