Skip to content

Commit

Permalink
Merge 3ac010b into 2b34a26
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Jan 13, 2018
2 parents 2b34a26 + 3ac010b commit 7669d9d
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 128 deletions.
4 changes: 3 additions & 1 deletion jscomp/others/bs_Array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ let swapUnsafe xs i j =
unsafe_set xs j tmp


let shuffleInPlace xs =
let shuffleOnly xs =
let len = length xs in
for i = 0 to len - 1 do
swapUnsafe xs i (Js_math.random_int i len) (* [i,len)*)
done

let shuffle xs = shuffleOnly xs; xs

let makeMatrix sx sy init =
[%assert sx >=0 && sy >=0 ];
let res = makeUninitializedUnsafe sx in
Expand Down
6 changes: 5 additions & 1 deletion jscomp/others/bs_Array.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ external makeUninitializedUnsafe : int -> 'a array = "Array" [@@bs.new]

val init : int -> (int -> 'a [@bs]) -> 'a array

val shuffleInPlace : 'a array -> unit
val shuffleOnly : 'a array -> unit

val shuffle :'a array -> 'a array
(** [shuffle xs] it mutates [xs] and return
[xs] for chaining
*)
val zip : 'a array -> 'b array -> ('a * 'b) array
(** [zip a b] stop with the shorter array *)

Expand Down
12 changes: 7 additions & 5 deletions jscomp/others/bs_Map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@ let length map =

let toList map =
toList0 (B.data map)

let minBinding map =
minBinding0 (B.data map)
let maxBinding map =
maxBinding0 (B.data map)
let toArray m =
N.toArray0 (B.data m)
let minKVOpt m = N.minKVOpt0 (B.data m)
let minKVNull m = N.minKVNull0 (B.data m)
let maxKVOpt m = N.maxKVOpt0 (B.data m)
let maxKVNull m = N.maxKVNull0 (B.data m)


let map m f =
let dict, map = B.(dict m, data m) in
Expand Down
24 changes: 8 additions & 16 deletions jscomp/others/bs_Map.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(** Adapted by authors of BuckleScript without using functors *)
(** The type of the map keys. *)
type ('k, 'a, 'id) t0
(** [('k, 'a, id) t]
(**
['k] the key type
['a] the value type
['id] is a unique type for each keyed module
Expand All @@ -23,6 +23,7 @@ type ('k, 'a, 'id) t0
type ('k,'v,'id) t =
(('k,'id) Bs_Cmp.t,
('k,'v, 'id) t0 ) Bs_Bag.bag
(** The data associated with a comparison function *)

(*
How we remain soundness:
Expand Down Expand Up @@ -128,22 +129,13 @@ val length: ('k, 'a, 'id) t -> int


val toList: ('k, 'a, 'id) t -> ('k * 'a) list
(** Return the list of all bindings of the given map.
The returned list is sorted in increasing order with respect
to the ordering [Ord.compare], where [Ord] is the argument
given to {!Map.Make}.
*)

val minBinding: ('k, 'a, 'id) t -> ('k * 'a) option
(** Return the smallest binding of the given map
(with respect to the [Ord.compare] ordering), or raise
[Not_found] if the map is empty.
*)
(** In increasing order*)
val toArray : ('k, 'a, 'id) t -> ('k * 'a) array

val maxBinding: ('k, 'a, 'id) t -> ('k * 'a) option
(** Same as {!Map.S.min_binding}, but returns the largest binding
of the given map.
*)
val minKVOpt: ('k, 'a, _) t -> ('k * 'a) option
val minKVNull: ('k, 'a, _) t -> ('k * 'a) Js.null
val maxKVOpt: ('k, 'a, _) t -> ('k * 'a) option
val maxKVNull:('k, 'a, _) t -> ('k * 'a) Js.null

val split: 'k -> ('k, 'a, 'id) t -> ('k, 'a, 'id) t * 'a option * ('k, 'a, 'id) t
(** [split x m] returns a triple [(l, data, r)], where
Expand Down
8 changes: 5 additions & 3 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ arith_parser.cmj : ../stdlib/parsing.cmj ../stdlib/obj.cmj \
../stdlib/lexing.cmj arith_syntax.cmj
arith_syntax.cmj :
arity_deopt.cmj : mt.cmj
array_data_util.cmj : ../others/bs.cmj ../stdlib/array.cmj
array_data_util.cmj : ../others/bs_Array.cmj
array_safe_get.cmj : ../stdlib/array.cmj
array_subtle_test.cmj : mt.cmj ../runtime/js.cmj ../stdlib/array.cmj
array_test.cmj : ../stdlib/pervasives.cmj mt.cmj ../stdlib/list.cmj \
Expand Down Expand Up @@ -99,7 +99,8 @@ bs_ignore_effect.cmj : mt.cmj
bs_ignore_test.cmj : ../runtime/js.cmj
bs_list_test.cmj : mt.cmj ../runtime/js.cmj ../others/bs.cmj
bs_map_int_test.cmj : mt.cmj ../others/bs.cmj
bs_map_test.cmj : ../runtime/js.cmj ../others/bs.cmj
bs_map_test.cmj : mt.cmj ../runtime/js.cmj ../others/bs_Array.cmj \
../others/bs.cmj array_data_util.cmj
bs_min_max_test.cmj : ../stdlib/pervasives.cmj mt.cmj
bs_mutable_set_test.cmj : mt.cmj ../runtime/js.cmj ../others/bs.cmj \
array_data_util.cmj ../stdlib/array.cmj
Expand All @@ -114,7 +115,8 @@ bs_rbset_int_bench.cmj : rbset.cmj
bs_rest_test.cmj :
bs_set_bench.cmj : ../others/bs.cmj
bs_set_int_test.cmj : mt.cmj ../stdlib/list.cmj ../runtime/js.cmj \
../others/bs.cmj array_data_util.cmj ../stdlib/array.cmj
../others/bs_Array.cmj ../others/bs.cmj array_data_util.cmj \
../stdlib/array.cmj
bs_sort_test.cmj : mt.cmj ../others/bs_Range.cmj ../others/bs_Array.cmj \
../others/bs.cmj array_data_util.cmj
bs_splice_partial.cmj : ../runtime/js.cmj
Expand Down
14 changes: 7 additions & 7 deletions jscomp/test/array_data_util.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';

var $$Array = require("../../lib/js/array.js");
var Bs_Array = require("../../lib/js/bs_Array.js");

function range(i, j) {
return $$Array.init((j - i | 0) + 1 | 0, (function (k) {
return Bs_Array.init((j - i | 0) + 1 | 0, (function (k) {
return k + i | 0;
}));
}

function randomRange(i, j) {
var v = Bs_Array.init((j - i | 0) + 1 | 0, (function (k) {
return k + i | 0;
}));
Bs_Array.shuffleInPlace(v);
return v;
return Bs_Array.shuffle(Bs_Array.init((j - i | 0) + 1 | 0, (function (k) {
return k + i | 0;
})));
}

var A = 0;

exports.A = A;
exports.range = range;
exports.randomRange = randomRange;
/* No side effect */
10 changes: 6 additions & 4 deletions jscomp/test/array_data_util.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

module A = Bs_Array

(* []*)
let range i j =
Array.init (j - i + 1) (fun k -> k + i )
A.init (j - i + 1) (fun[@bs] k -> k + i )

let randomRange i j =
let v = Bs.Array.init (j - i + 1) (fun[@bs] k -> k + i ) in
Bs.Array.shuffleInPlace v ;
v
A.shuffle (A.init (j - i + 1) (fun[@bs] k -> k + i ))


2 changes: 1 addition & 1 deletion jscomp/test/bs_array_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ var v = Bs_Array.init(3000, (function (i) {

var u = Bs_Array.copy(v);

Bs_Array.shuffleInPlace(u);
Bs_Array.shuffleOnly(u);

neq("File \"bs_array_test.ml\", line 63, characters 6-13", u, v);

Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/bs_array_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ let add = fun [@bs] x y -> x + y
let () =
let v = Bs.Array.init 3000 (fun[@bs] i -> i) in
let u = Bs.Array.copy v in
Bs.Array.shuffleInPlace u ;
Bs.Array.shuffleOnly u ;
neq __LOC__ u v (* unlikely*);
let sum x = Bs.Array.foldLeft x 0 add in
eq __LOC__ ( sum u) (sum v)
Expand Down
17 changes: 9 additions & 8 deletions jscomp/test/bs_map_int_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ function b(loc, v) {
return /* () */0;
}

var v = Bs_Array.init(1000000, (function (i) {
return /* tuple */[
i,
i
];
}));

Bs_Array.shuffleInPlace(v);
var v = Bs_Array.shuffle(Bs_Array.init(1000000, (function (i) {
return /* tuple */[
i,
i
];
})));

var u = Bs_MapInt.ofArray(v);

Expand All @@ -65,9 +63,12 @@ Mt.from_pair_suites("bs_map_int_test.ml", suites[0]);

var N = 0;

var A = 0;

exports.suites = suites;
exports.test_id = test_id;
exports.eq = eq;
exports.b = b;
exports.N = N;
exports.A = A;
/* v Not a pure module */
6 changes: 3 additions & 3 deletions jscomp/test/bs_map_int_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ let b loc v =
(fun _ -> Mt.Ok v)) :: !suites

module N = Bs.MapInt

module A = Bs.Array
let () =
let v = Bs.Array.init 1_000_000 (fun[@bs] i -> (i,i)) in
Bs.Array.shuffleInPlace v ;
let v =
A.shuffle (A.init 1_000_000 (fun[@bs] i -> (i,i))) in
let u = N.ofArray v in
b __LOC__ (N.checkInvariant u);
let firstHalf = Bs.Array.sub v 0 2_000 in
Expand Down
Loading

0 comments on commit 7669d9d

Please sign in to comment.