[m-rev.] diff: add map.keys_and_values/3

Julien Fischer juliensf at csse.unimelb.edu.au
Mon Jan 16 01:48:52 AEDT 2012


Branches: main, 11.07

Add the predicate map.keys_and_values/3 to the standrad library.

library/map.m:
library/tree234.m:
 	Add the new predicate.

NEWS:
 	Announce the new predicate.

tests/hard_coded/Mmakefile:
tests/hard_coded/test_keys_and_values.{m,exp}:
 	Test the new predicate.

Julien.

Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.598
diff -u -r1.598 NEWS
--- NEWS	9 Jan 2012 15:57:12 -0000	1.598
+++ NEWS	15 Jan 2012 14:37:47 -0000
@@ -4,6 +4,14 @@
  * There is no news yet.


+NEWS for Mercury 11.07.1-beta
+-----------------------------
+
+Changes to the Mercury standrad library:
+
+* We have added the predicate map.keys_and_values/3.
+
+
  NEWS for Mercury 11.07
  ----------------------

Index: library/map.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/map.m,v
retrieving revision 1.127
diff -u -r1.127 map.m
--- library/map.m	21 Jul 2011 06:58:34 -0000	1.127
+++ library/map.m	14 Jan 2012 09:07:57 -0000
@@ -187,6 +187,8 @@
  :- func map.values(map(_K, V)) = list(V).
  :- pred map.values(map(_K, V)::in, list(V)::out) is det.

+:- pred map.keys_and_values(map(K, V)::in, list(K)::out, list(V)::out) is det.
+
      % Convert a map to an association list.
      %
  :- func map.to_assoc_list(map(K, V)) = assoc_list(K, V).
@@ -897,6 +899,9 @@
  map.values(Map, KeyList) :-
      tree234.values(Map, KeyList).

+map.keys_and_values(Map, KeyList, ValueList) :-
+    tree234.keys_and_values(Map, KeyList, ValueList).
+
  map.to_assoc_list(M) = AL :-
      map.to_assoc_list(M, AL).

Index: library/tree234.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/tree234.m,v
retrieving revision 1.73
diff -u -r1.73 tree234.m
--- library/tree234.m	18 May 2011 15:40:24 -0000	1.73
+++ library/tree234.m	15 Jan 2012 14:33:36 -0000
@@ -99,6 +99,9 @@
  :- func tree234.values(tree234(K, V)) = list(V).
  :- pred tree234.values(tree234(K, V)::in, list(V)::out) is det.

+:- pred tree234.keys_and_values(tree234(K, V)::in, list(K)::out, list(V)::out)
+    is det.
+
  :- pred tree234.update(K::in, V::in, tree234(K, V)::in, tree234(K, V)::out)
      is semidet.

@@ -2595,6 +2598,41 @@

  %------------------------------------------------------------------------------%

+tree234.keys_and_values(Tree, Keys, Values) :-
+    tree234.keys_and_values_2(Tree, [], Keys, [], Values).
+
+:- pred tree234.keys_and_values_2(tree234(K, V)::in, list(K)::in, list(K)::out,
+    list(V)::in, list(V)::out) is det.
+
+tree234.keys_and_values_2(empty, !Keys, !Values).
+tree234.keys_and_values_2(two(K0, V0, T0, T1), !Keys, !Values) :-
+    tree234.keys_and_values_2(T1, !Keys, !Values),
+    !:Keys = [K0 | !.Keys],
+    !:Values = [V0 | !.Values],
+    tree234.keys_and_values_2(T0, !Keys, !Values).
+tree234.keys_and_values_2(three(K0, V0, K1, V1, T0, T1, T2), !Keys, !Values) :-
+    tree234.keys_and_values_2(T2, !Keys, !Values),
+    !:Keys = [K1 | !.Keys],
+    !:Values = [V1 | !.Values],
+    tree234.keys_and_values_2(T1, !Keys, !Values),
+    !:Keys = [K0 | !.Keys],
+    !:Values = [V0 | !.Values],
+    tree234.keys_and_values_2(T0, !Keys, !Values).
+tree234.keys_and_values_2(four(K0, V0, K1, V1, K2, V2, T0, T1, T2, T3),
+        !Keys, !Values) :- 
+    tree234.keys_and_values_2(T3, !Keys, !Values),
+    !:Keys = [K2 | !.Keys],
+    !:Values = [V2 | !.Values],
+    tree234.keys_and_values_2(T2, !Keys, !Values),
+    !:Keys = [K1 | !.Keys],
+    !:Values = [V1 | !.Values],
+    tree234.keys_and_values_2(T1, !Keys, !Values),
+    !:Keys = [K0 | !.Keys],
+    !:Values = [V0 | !.Values],
+    tree234.keys_and_values_2(T0, !Keys, !Values).
+
+%------------------------------------------------------------------------------%
+
  tree234.assoc_list_to_tree234(AssocList, Tree) :-
      tree234.assoc_list_to_tree234_2(AssocList, empty, Tree).

Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.421
diff -u -r1.421 Mmakefile
--- tests/hard_coded/Mmakefile	5 Dec 2011 09:08:28 -0000	1.421
+++ tests/hard_coded/Mmakefile	15 Jan 2012 14:36:41 -0000
@@ -289,6 +289,7 @@
  	test_bitset \
  	test_cord \
  	test_imported_no_tag \
+	test_keys_and_values \
  	test_pretty_printer \
  	test_pretty_printer_defaults \
  	test_semaphore \
Index: tests/hard_coded/test_keys_and_values.exp
===================================================================
RCS file: tests/hard_coded/test_keys_and_values.exp
diff -N tests/hard_coded/test_keys_and_values.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/test_keys_and_values.exp	15 Jan 2012 14:35:53 -0000
@@ -0,0 +1,10 @@
+KeysPrime = []
+ValuesPrime = []
+KeysPrime = ["one"]
+ValuesPrime = [1]
+KeysPrime = ["one", "two"]
+ValuesPrime = [1, 2]
+KeysPrime = ["one", "three", "two"]
+ValuesPrime = [1, 3, 2]
+KeysPrime = ["four", "one", "three", "two"]
+ValuesPrime = [4, 1, 3, 2]
Index: tests/hard_coded/test_keys_and_values.m
===================================================================
RCS file: tests/hard_coded/test_keys_and_values.m
diff -N tests/hard_coded/test_keys_and_values.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/test_keys_and_values.m	15 Jan 2012 14:35:53 -0000
@@ -0,0 +1,47 @@
+% vim: ft=mercury ts=4 sw=4 et
+%
+% Test map.keys_and_values/3.
+%
+:- module test_keys_and_values.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module assoc_list.
+:- import_module list.
+:- import_module map.
+:- import_module pair.
+
+main(!IO) :-
+    map.init(Map0),
+    Map1 = map.singleton("one", 1),
+    Map2 = map.from_assoc_list(["one" - 1, "two" - 2]),
+    Map3 = map.from_assoc_list(["one" - 1, "two" - 2, "three" - 3]),
+    Map4 = map.from_assoc_list(["one" - 1, "two" - 2, "three" - 3, "four" - 4]),
+    do_test(Map0, !IO),
+    do_test(Map1, !IO),
+    do_test(Map2, !IO),
+    do_test(Map3, !IO),
+    do_test(Map4, !IO).
+
+:- pred do_test(map(string, int)::in, io::di, io::uo) is det.
+
+do_test(Map, !IO) :-
+    Keys = map.keys(Map),
+    Values = map.values(Map),
+    map.keys_and_values(Map, KeysPrime, ValuesPrime),
+    ( if Keys = KeysPrime, Values = ValuesPrime then
+        io.write_string("KeysPrime = ", !IO),
+        io.write(KeysPrime, !IO),
+        io.write_string("\nValuesPrime = ", !IO),
+        io.write(ValuesPrime, !IO),
+        io.nl(!IO)
+      else
+        io.write_string("ERROR: Map = ", !IO),
+        io.write(Map, !IO),
+        io.nl(!IO)
+    ).

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list