[m-rev.] for review: add io.write_array/[56] and array2d.is_empty/1

Julien Fischer jfischer at opturion.com
Fri Mar 18 12:03:26 AEDT 2016


For review by anyone.

------------------------

Add io.write_array/[56] and array2d.is_empty/1.

library/io.m:
     Add predicates io.write_array/[56] which are are similar to
     io.write_list but work on arrays and do _not_ require converting
     the array into a list first.

library/array2d.m:
     Add array2d.is_empty/1.

NEWS:
     Announce the above additions.

tests/hard_coded/Mmakefile:
tests/hard_coded/write_array.{m,exp}:
     Add a test for write_array.

tests/hard_coded/test_array2d.{m,exp}:
     Extend this test to cover is_empty/1.

Julien.

diff --git a/NEWS b/NEWS
index e2fded5..ca54e01 100644
--- a/NEWS
+++ b/NEWS
@@ -115,6 +115,11 @@ Changes to the Mercury standard library:
    io module.  These behave like the print and write predicates, but also
    write a terminating newline.

+* We have added the predicates write_array/5 and write_array/6 to the io
+  module; these predicates write arrays using a user-specified procedure
+  to write the elements and separating the elements with a user-specified
+  separator string.
+
  * io.print and string_writer.print now print arbitrary precision integers
    in their decimal form instead of printing their underlying representation.

@@ -283,6 +288,10 @@ Changes to the Mercury standard library:
     - to_list/1     (synonym for the existing list/1 function)
     - to_rev_list/1 (synonym for the existing rev_list/1 function)

+* The following function has been added to the array2d module
+
+   - is_empty/1
+
  Changes to the Mercury compiler:

  * We have added a new option --warn-dead-preds. While the existing option
diff --git a/library/array2d.m b/library/array2d.m
index 259acea..d13c7fa 100644
--- a/library/array2d.m
+++ b/library/array2d.m
@@ -54,6 +54,13 @@
  :- func array2d(list(list(T))) = array2d(T).
  :- mode array2d(in) = array2d_uo is det.

+    % is_empty(Array):
+    % True iff Array contains zero elements.
+    %
+:- pred is_empty(array2d(T)).
+%:- mode is_empty(array2d_ui) is semidet.
+:- mode is_empty(in) is semidet.
+
      % A synonym for the above.
      %
  :- func from_lists(list(list(T))) = array2d(T).
@@ -163,6 +170,9 @@ array2d(Xss @ [Xs | _]) = T :-
            else  func_error("array2d.array2d/1: non-rectangular list of lists")
          ).

+is_empty(array2d(_, _, A)) :-
+    array.is_empty(A).
+
  from_lists(Xss) = array2d(Xss).

  %---------------------------------------------------------------------------%
diff --git a/library/io.m b/library/io.m
index c7a8116..bcc4b20 100644
--- a/library/io.m
+++ b/library/io.m
@@ -32,6 +32,7 @@
  :- module io.
  :- interface.

+:- import_module array.
  :- import_module bitmap.
  :- import_module bool.
  :- import_module char.
@@ -578,6 +579,26 @@
  :- mode write_list(in, in, in, pred(in, di, uo) is cc_multi, di, uo)
      is cc_multi.

+    % write_array(Array, Separator, OutputPred, !IO):
+    % Applies OutputPred to each element of Array, printing Separator
+    % between each element. Outputs to the current output stream.
+    %
+:- pred write_array(array(T), string, pred(T, io, io), io, io).
+:- mode write_array(in, in, pred(in, di, uo) is det, di, uo) is det.
+%:- mode write_array(array_ui, in, pred(in, di, uo) is det, di uo) is det.
+:- mode write_array(in, in, pred(in, di, uo) is cc_multi, di, uo) is cc_multi.
+%:- mode write_array(array_ui, in, pred(in, di, uo) is cc_multi, di uo) is cc_multi.
+
+    % write_array(Stream, Array, Separator, OutputPred, !IO):
+    % Applies OutputPred to each element of Array, printing Separator
+    % between each element. Outputs to Stream.
+    %
+:- pred write_array(text_output_stream, array(T), string, pred(T, io, io), io, io).
+:- mode write_array(in, in, in, pred(in, di, uo) is det, di, uo) is det.
+%:- mode write_array(in, array_ui, in, pred(in, di, uo) is det, di uo) is det.
+:- mode write_array(in, in, in, pred(in, di, uo) is cc_multi, di, uo) is cc_multi.
+%:- mode write_array(in, array_ui, in, pred(in, di, uo) is cc_multi, di uo) is cc_multi.
+
      % Flush the output buffer of the current output stream.
      %
  :- pred flush_output(io::di, io::uo) is det.
@@ -4851,6 +4872,36 @@ io.write_list(Stream, List, Separator, OutputPred, !IO) :-

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

+write_array(Array, Separator, OutputPred, !IO) :-
+    array.bounds(Array, Lo, Hi),
+    do_write_array(Array, Separator, OutputPred, Lo, Hi, !IO).
+
+:- pred do_write_array(array(T), string, pred(T, io, io), int, int, io, io).
+:- mode do_write_array(in, in, pred(in, di, uo) is det, in, in, di, uo)
+    is det.
+:- mode do_write_array(in, in, pred(in, di, uo) is cc_multi, in, in, di, uo)
+    is cc_multi.
+
+do_write_array(Array, Separator, OutputPred, I, Hi, !IO) :-
+    ( if I =< Hi then
+        array.unsafe_lookup(Array, I, E),
+        OutputPred(E, !IO),
+        ( if I < Hi
+        then io.write_string(Separator, !IO)
+        else true
+        ),
+        do_write_array(Array, Separator, OutputPred, I + 1, Hi, !IO)
+    else
+        true
+    ).
+
+write_array(Stream, Array, Separator, OutputPred, !IO) :-
+    io.set_output_stream(Stream, OrigStream, !IO),
+    io.write_array(Array, Separator, OutputPred, !IO),
+    io.set_output_stream(OrigStream, _, !IO).
+
+%---------------------------------------------------------------------------%
+
  io.write_binary(Stream, Term, !IO) :-
      io.set_binary_output_stream(Stream, OrigStream, !IO),
      io.write_binary(Term, !IO),
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 9e55e2c3..be0059d 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -365,6 +365,7 @@ ORDINARY_PROGS =	\
  	word_aligned_pointer \
  	words_separator \
  	write \
+	write_array \
  	write_float_special \
  	write_reg1 \
  	write_reg2 \
diff --git a/tests/hard_coded/test_array2d.exp b/tests/hard_coded/test_array2d.exp
index 3e556d9..aa2749d 100644
--- a/tests/hard_coded/test_array2d.exp
+++ b/tests/hard_coded/test_array2d.exp
@@ -13,6 +13,10 @@ Two ^ elem(0, 1) = 0
  Two ^ elem(1, 0) = 0
  Two ^ elem(1, 1) = 2

+Empty is empty
+One is not empty
+Two is not empty
+
  Two_a =
  [[1, 3]
   [0, 2]]
diff --git a/tests/hard_coded/test_array2d.m b/tests/hard_coded/test_array2d.m
index 2c71e68..34debe3 100644
--- a/tests/hard_coded/test_array2d.m
+++ b/tests/hard_coded/test_array2d.m
@@ -46,6 +46,23 @@ main(!IO) :-
      write_array2d_elem("Two", Two, 1, 1, !IO),
      io.nl(!IO),

+    ( if array2d.is_empty(Empty) then
+        io.write_string("Empty is empty\n", !IO)
+    else
+        io.write_string("Empty is not empty\n", !IO)
+    ),
+    ( if array2d.is_empty(One) then
+        io.write_string("One is empty\n", !IO)
+    else
+        io.write_string("One is not empty\n", !IO)
+    ),
+    ( if array2d.is_empty(Two) then
+        io.write_string("Two is empty\n", !IO)
+    else
+        io.write_string("Two is not empty\n", !IO)
+    ),
+    io.nl(!IO),
+
      Two_a = Two ^ elem(0, 1) := 3,
      write_array2d("Two_a", Two_a, !IO),
      io.nl(!IO),
diff --git a/tests/hard_coded/write_array.exp b/tests/hard_coded/write_array.exp
index e69de29..2c9bbba 100644
--- a/tests/hard_coded/write_array.exp
+++ b/tests/hard_coded/write_array.exp
@@ -0,0 +1,6 @@
+Array: 
+List: 
+Array: 1
+List: 1
+Array: 1, 2, 3
+List: 1, 2, 3
diff --git a/tests/hard_coded/write_array.m b/tests/hard_coded/write_array.m
index e69de29..59a693d 100644
--- a/tests/hard_coded/write_array.m
+++ b/tests/hard_coded/write_array.m
@@ -0,0 +1,29 @@
+%---------------------------------------------------------------------------%
+% vim: ts=4 sw=4 et ft=mercury
+%---------------------------------------------------------------------------%
+
+:- module write_array.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module array.
+:- import_module list.
+
+main(!IO) :-
+    list.foldl(run_test, [[], [1], [1, 2, 3]], !IO).
+
+:- pred run_test(list(int)::in, io::di, io::uo) is det.
+
+run_test(List, !IO) :-
+    Array = array.from_list(List),
+    io.write_string("Array: ", !IO),
+    io.write_array(Array, ", ", io.write_int, !IO),
+    io.nl(!IO),
+    io.write_string("List: ", !IO),
+    io.write_list(List, ", ", io.write_int, !IO),
+    io.nl(!IO).



More information about the reviews mailing list