[m-rev.] diff: add version_array.from_reverse_list/1

Julien Fischer jfischer at opturion.com
Fri Jul 22 13:50:34 AEST 2016


Add version_array.from_reverse_list/1.

library/version_array.m:
      Add the above function.

      Minor fixes to some comments.

tests/hard_coded/version_array_test.{m,exp}:
      Extend this test to cover from_reverse_list/1 and
      from_list/1.

NEWS:
      Announce the addition.

Julien.

diff --git a/NEWS b/NEWS
index 3853213..23a1a10 100644
--- a/NEWS
+++ b/NEWS
@@ -169,6 +169,8 @@ Changes to the Mercury standard library:
  * We have added thread.spawn_native/4 to dedicate an OS thread to a Mercury
    thread. thread.spawn/4 was added as well.

+* We have added the function from_reverse_list/1 to the version_array module.
+
  * We have added thread.num_processors/3 which returns the number of
    processors available for parallel work.

diff --git a/library/version_array.m b/library/version_array.m
index afd744d..b8fe3b3 100644
--- a/library/version_array.m
+++ b/library/version_array.m
@@ -114,6 +114,11 @@
      %
  :- func from_list(list(T)) = version_array(T).

+    % from_reverse_list(Xs) returns an array constructed from the items in the
+    % list Xs in reverse order.
+    %
+:- func from_reverse_list(list(T)) = version_array(T).
+
      % A ^ elem(I) = X iff the I'th member of A is X (the first item has
      % index 0).
      %
@@ -281,6 +286,20 @@ version_array_2(I, [X | Xs], !VA) :-

  from_list(Xs) = version_array(Xs).

+from_reverse_list([]) = version_array.empty.
+from_reverse_list([X | Xs]) = VA :-
+    NumElems = 1 + list.length(Xs),
+    VA0 = version_array.init(NumElems, X),
+    from_reverse_list_2(NumElems - 2, Xs, VA0, VA).
+
+:- pred from_reverse_list_2(int::in, list(T)::in,
+    version_array(T)::in, version_array(T)::out) is det.
+
+from_reverse_list_2(_, [], !VA).
+from_reverse_list_2(I, [X | Xs], !VA) :-
+    set(I, X, !VA),
+    from_reverse_list_2(I - 1, Xs, !VA).
+
  %---------------------------------------------------------------------------%

  :- pragma inline(version_array.elem/2).
@@ -288,7 +307,7 @@ from_list(Xs) = version_array(Xs).
  lookup(VA, I) = X :-
      ( if get_if_in_range(VA, I, X0) then
          X = X0
-      else
+    else
          out_of_bounds_error(I, max(VA), "version_array.lookup")
      ).

@@ -941,7 +960,7 @@ extern MR_Integer
  ML_va_size_dolock(ML_const_va_ptr);

  /*
-** If I is in range then ML_va_get(VA, I, &X) sets X to the Ith item
+** If I is in range then ML_va_get(VA, I, &X) sets X to the I'th item
  ** in VA (counting from zero) and returns MR_TRUE. Otherwise it
  ** returns MR_FALSE.
  */
@@ -950,7 +969,7 @@ ML_va_get_dolock(ML_const_va_ptr, MR_Integer, MR_Word *);

  /*
  ** If I is in range then ML_va_set(VA0, I, X, VA) sets VA to be VA0
-** updated with the Ith item as X (counting from zero) and
+** updated with the I'th item as X (counting from zero) and
  ** returns MR_TRUE. Otherwise it returns MR_FALSE.
  */
  extern MR_bool
@@ -984,7 +1003,7 @@ static MR_Integer
  ML_va_size(ML_const_va_ptr);

  /*
-** If I is in range then ML_va_get(VA, I, &X) sets X to the Ith item
+** If I is in range then ML_va_get(VA, I, &X) sets X to the I'th item
  ** in VA (counting from zero) and returns MR_TRUE. Otherwise it
  ** returns MR_FALSE.
  */
@@ -993,7 +1012,7 @@ ML_va_get(ML_const_va_ptr VA, MR_Integer I, MR_Word *Xptr);

  /*
  ** If I is in range then ML_va_set(VA0, I, X, VA) sets VA to be VA0
-** updated with the Ith item as X (counting from zero) and
+** updated with the I'th item as X (counting from zero) and
  ** returns MR_TRUE. Otherwise it returns MR_FALSE.
  */
  static MR_bool
diff --git a/tests/hard_coded/version_array_test.exp b/tests/hard_coded/version_array_test.exp
index 5dbe9b6..7e9dfdd 100644
--- a/tests/hard_coded/version_array_test.exp
+++ b/tests/hard_coded/version_array_test.exp
@@ -25,3 +25,9 @@ all_true(int.even, A0): passed
  all_false(int.even, A0): passed
  not all_true(int.odd, A8): passed
  not all_false(int.even, A8): passed
+from_list([]): [] (size 0)
+from_list([1]): [1] (size 1)
+from_list([1, 2]): [1, 2] (size 2)
+from_reverse_list([]): [] (size 0)
+from_reverse_list([1]): [1] (size 1)
+from_reverse_list([2, 1]): [1, 2] (size 2)
diff --git a/tests/hard_coded/version_array_test.m b/tests/hard_coded/version_array_test.m
index ab578a0..172b18a 100644
--- a/tests/hard_coded/version_array_test.m
+++ b/tests/hard_coded/version_array_test.m
@@ -132,6 +132,20 @@ main(!IO) :-
      else io.write_string("failed\n", !IO)
      ),

+    FromList1 = version_array.from_list([] : list(int)),
+    write_array("from_list([])", FromList1, !IO),
+    FromList2 = version_array.from_list([1]),
+    write_array("from_list([1])", FromList2, !IO),
+    FromList3 = version_array.from_list([1, 2]),
+    write_array("from_list([1, 2])", FromList3, !IO),
+
+    FromRevList1 = version_array.from_reverse_list([] : list(int)),
+    write_array("from_reverse_list([])", FromRevList1, !IO),
+    FromRevList2 = version_array.from_reverse_list([1]),
+    write_array("from_reverse_list([1])", FromRevList2, !IO),
+    FromRevList3 = version_array.from_reverse_list([2, 1]),
+    write_array("from_reverse_list([2, 1])", FromRevList3, !IO),
+
      true.

  :- pred test_exception((pred)::in((pred) is semidet),


More information about the reviews mailing list