[m-rev.] diff: updates to random modules
Mark Brown
mark at mercurylang.org
Fri Sep 6 20:23:25 AEST 2019
diff is attached
On Fri, Sep 6, 2019 at 7:39 PM Mark Brown <mark at mercurylang.org> wrote:
>
> Hi,
>
> On Fri, Sep 6, 2019 at 7:32 PM Julien Fischer <jfischer at opturion.com> wrote:
> >
> >
> > Hi Mark,
> >
> > On Fri, 6 Sep 2019, Mark Brown wrote:
> >
> > > This addresses the discussion on the users list.
> >
> > That looks fine, with the exception of the comment below.
> >
> > > diff --git a/library/random.m b/library/random.m
> > > index 3b9873f..98322ca 100644
> > > --- a/library/random.m
> > > +++ b/library/random.m
> >
> > ...
> >
> > > @@ -511,6 +535,30 @@ normal_floats(U, V, !R) :-
> > > normal_floats(U, V, !R)
> > > ).
> > >
> > > +shuffle_list(L0, L, !R) :-
> > > + A0 = array(L0),
> > > + shuffle_array(A0, A, !R),
> > > + L = array.to_list(A).
> > > +
> > > +shuffle_array(A0, A, !R) :-
> > > + Lo = array.min(A0),
> > > + Hi = array.max(A0),
> > > + Sz = array.size(A0),
> > > + shuffle_2(Lo, Lo, Hi, Sz, A0, A, !R).
> > > +
> > > +:- pred shuffle_2(int::in, int::in, int::in, int::in,
> > > + array(T)::array_di, array(T)::array_uo, R::in, R::out) is det
> > > + <= random(R).
> > > +
> > > +shuffle_2(I, Lo, Hi, Sz, !A, !R) :-
> > > + ( if I > Hi then
> > > + true
> > > + else
> > > + uniform_int_in_range(Lo, Sz, J, !R),
> > > + swap_elems(I, J, !A),
> >
> > The array module now exports array.swap/4 and array.unsafe_swap/4, the
> > latter of which you should be using here and in the unique version.
> > swap_elems/4 can be deleted.
>
> Thanks, I will do so.
>
> Mark
-------------- next part --------------
commit 6e7b45404032eab93c36a2f3b8957734da1507eb
Author: Mark Brown <mark at mercurylang.org>
Date: Fri Sep 6 19:45:33 2019 +1000
Use array.unsafe_swap in library/random.m
diff --git a/library/random.m b/library/random.m
index 98322ca..4794c82 100644
--- a/library/random.m
+++ b/library/random.m
@@ -555,7 +555,7 @@ shuffle_2(I, Lo, Hi, Sz, !A, !R) :-
true
else
uniform_int_in_range(Lo, Sz, J, !R),
- swap_elems(I, J, !A),
+ array.unsafe_swap(I, J, !A),
shuffle_2(I + 1, Lo, Hi, Sz, !A, !R)
).
@@ -635,7 +635,7 @@ shuffle_2(P, I, Lo, Hi, Sz, !A, !S) :-
true
else
uniform_int_in_range(P, Lo, Sz, J, !S),
- swap_elems(I, J, !A),
+ array.unsafe_swap(I, J, !A),
shuffle_2(P, I + 1, Lo, Hi, Sz, !A, !S)
).
@@ -652,15 +652,6 @@ uniform_to_normal(X, Y, U, V) :-
U = X * Fac,
V = Y * Fac.
-:- pred swap_elems(int::in, int::in, array(T)::array_di, array(T)::array_uo)
- is det.
-
-swap_elems(I, J, !A) :-
- array.lookup(!.A, I, XI),
- array.lookup(!.A, J, XJ),
- array.unsafe_set(I, XJ, !A),
- array.unsafe_set(J, XI, !A).
-
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
More information about the reviews
mailing list