Skip to content

Commit 16ae9ad

Browse files
committedJan 23, 2018
finalize array api
1 parent cc5bbca commit 16ae9ad

29 files changed

+198
-141
lines changed
 

‎jscomp/others/bs_Array.ml

+33-14
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ external blitUnsafe :
3131
(*DOC: when l < 0 raise RangeError js excpetion *)
3232
(* See #6575. We could also check for maximum array size, but this depends
3333
on whether we create a float array or a regular one... *)
34-
let init l f =
34+
let initExn l f =
3535
[%assert l >= 0];
3636
let res = makeUninitializedUnsafe l in
3737
for i = 0 to l - 1 do
@@ -53,7 +53,7 @@ let shuffleDone xs =
5353

5454
let shuffle xs = shuffleDone xs; xs
5555

56-
let makeMatrix sx sy init =
56+
let makeMatrixExn sx sy init =
5757
[%assert sx >=0 && sy >=0 ];
5858
let res = makeUninitializedUnsafe sx in
5959
for x = 0 to sx - 1 do
@@ -91,29 +91,31 @@ let append a1 a2 =
9191
else if length a2 = 0 then unsafe_sub a1 0 l1
9292
else append_prim a1 a2
9393

94-
let sub a ofs len =
94+
let subExn a ofs len =
9595
if len < 0 || ofs > length a - len
9696
then
9797
(* invalid_arg *)
98-
[%assert "Array.sub"]
98+
[%assert "subExn"]
9999
else unsafe_sub a ofs len
100100

101101
let fill a ofs len v =
102102
if ofs < 0 || len < 0 || ofs > length a - len
103103
then
104-
(* invalid_arg *)
105-
[%assert "Array.fill"]
106-
else for i = ofs to ofs + len - 1 do unsafe_set a i v done
104+
false
105+
else
106+
begin
107+
for i = ofs to ofs + len - 1 do unsafe_set a i v done;
108+
true
109+
end
107110

108111
let blit a1 ofs1 a2 ofs2 len =
109112
if len < 0 || ofs1 < 0 || ofs1 > length a1 - len
110113
|| ofs2 < 0 || ofs2 > length a2 - len
111114
then
112-
(* invalid_arg *)
113-
[%assert "Array.blit"]
114-
else blitUnsafe a1 ofs1 a2 ofs2 len
115+
false
116+
else (blitUnsafe a1 ofs1 a2 ofs2 len; true)
115117

116-
let iter a f =
118+
let forEach a f =
117119
for i = 0 to length a - 1 do f(unsafe_get a i) [@bs] done
118120

119121
let map a f =
@@ -125,7 +127,7 @@ let map a f =
125127
r
126128

127129

128-
let iteri a f=
130+
let forEachi a f=
129131
for i = 0 to length a - 1 do f i (unsafe_get a i) [@bs] done
130132

131133
let mapi a f =
@@ -159,14 +161,14 @@ let ofList xs =
159161
fillAUx a 0 xs;
160162
a
161163

162-
let foldLeft a x f =
164+
let reduce a x f =
163165
let r = ref x in
164166
for i = 0 to length a - 1 do
165167
r := f !r (unsafe_get a i) [@bs]
166168
done;
167169
!r
168170

169-
let foldRight a x f =
171+
let reduceFromTail a x f =
170172
let r = ref x in
171173
for i = length a - 1 downto 0 do
172174
r := f !r (unsafe_get a i) [@bs]
@@ -200,6 +202,23 @@ let forAll2 a b p =
200202
else
201203
forAllAux2 a b 0 p lena
202204

205+
let eq = forAll2
206+
207+
let rec forAllCmpAux2 arr1 arr2 i b len =
208+
if i = len then 0
209+
else
210+
let c = b (unsafe_get arr1 i) (unsafe_get arr2 i) [@bs] in
211+
if c = 0 then
212+
forAllCmpAux2 arr1 arr2 (i + 1) b len
213+
else c
214+
215+
let cmp a b p =
216+
let lena = length a in
217+
let lenb = length b in
218+
if lena > lenb then 1
219+
else if lena < lenb then -1
220+
else forAllCmpAux2 a b 0 p lena
221+
203222
external truncateToLengthUnsafe : 'a array -> int -> unit = "length" [@@bs.set]
204223

205224

‎jscomp/others/bs_Array.mli

+35-33
Original file line numberDiff line numberDiff line change
@@ -37,79 +37,81 @@ external set : 'a array -> int -> 'a -> unit = "%array_safe_set"
3737
external makeUninitialized : int -> 'a Js.undefined array = "Array" [@@bs.new]
3838
external makeUninitializedUnsafe : int -> 'a array = "Array" [@@bs.new]
3939

40-
val init : int -> (int -> 'a [@bs]) -> 'a array
40+
val initExn: int -> (int -> 'a [@bs]) -> 'a array
4141

42-
val shuffleDone : 'a array -> unit
42+
val shuffleDone: 'a array -> unit
4343

44-
val shuffle :'a array -> 'a array
44+
val shuffle: 'a array -> 'a array
4545
(** [shuffle xs] it mutates [xs] and return
4646
[xs] for chaining
4747
*)
48-
val zip : 'a array -> 'b array -> ('a * 'b) array
48+
val zip: 'a array -> 'b array -> ('a * 'b) array
4949
(** [zip a b] stop with the shorter array *)
5050

51-
val makeMatrix : int -> int -> 'a -> 'a array array
51+
val makeMatrixExn: int -> int -> 'a -> 'a array array
5252

5353

54-
val append : 'a array -> 'a array -> 'a array
54+
val append: 'a array -> 'a array -> 'a array
5555
(** Note it returns a fresh array containing the
5656
concatenation of the arrays [v1] and [v2], so even if [v1] or [v2]
5757
is empty, it can not be shared
5858
*)
5959

60-
val concat : 'a array list -> 'a array
61-
(** Same as [Array.append], but concatenates a list of arrays. *)
60+
val concat: 'a array list -> 'a array
6261

63-
val sub : 'a array -> int -> int -> 'a array
6462

63+
val subExn: 'a array -> int -> int -> 'a array
6564

66-
val copy : 'a array -> 'a array
67-
(** [Array.copy a] returns a copy of [a], that is, a fresh array
65+
66+
val copy: 'a array -> 'a array
67+
(** [.copy a] returns a copy of [a], that is, a fresh array
6868
containing the same elements as [a]. *)
6969

70-
val fill : 'a array -> int -> int -> 'a -> unit
71-
(** [Array.fill a ofs len x] modifies the array [a] in place,
72-
storing [x] in elements number [ofs] to [ofs + len - 1].
70+
val fill: 'a array -> int -> int -> 'a -> bool
71+
(** [fill a ofs len x] modifies the array [a] in place,
72+
storing [x] in elements number [ofs] to [ofs + len - 1].
7373
74-
Raise [Invalid_argument "Array.fill"] if [ofs] and [len] do not
75-
designate a valid subarray of [a]. *)
74+
return false means the input is invalid, the array is unchanged
75+
*)
7676

77-
val blit :
78-
'a array -> int -> 'a array -> int -> int -> unit
77+
val blit:
78+
'a array -> int -> 'a array -> int -> int -> bool
7979
(** [blit v1 o1 v2 o2 len] copies [len] elements
8080
from array [v1], starting at element number [o1], to array [v2],
8181
starting at element number [o2]. It works correctly even if
8282
[v1] and [v2] are the same array, and the source and
8383
destination chunks overlap.
8484
85-
Raise [Invalid_argument "Array.blit"] if [o1] and [len] do not
86-
designate a valid subarray of [v1], or if [o2] and [len] do not
87-
designate a valid subarray of [v2]. *)
88-
external blitUnsafe :
85+
return false means the input is invalid, the array is unchnaged
86+
*)
87+
external blitUnsafe:
8988
'a array -> int -> 'a array -> int -> int -> unit = "caml_array_blit"
9089

91-
val toList : 'a array -> 'a list
90+
val toList: 'a array -> 'a list
9291

9392

94-
val ofList : 'a list -> 'a array
93+
val ofList: 'a list -> 'a array
9594

96-
val iter : 'a array -> ('a -> unit [@bs]) -> unit
95+
val forEach: 'a array -> ('a -> unit [@bs]) -> unit
9796

98-
val map : 'a array -> ('a -> 'b [@bs]) -> 'b array
97+
val map: 'a array -> ('a -> 'b [@bs]) -> 'b array
9998

100-
val iteri : 'a array -> (int -> 'a -> unit [@bs]) -> unit
99+
val forEachi: 'a array -> (int -> 'a -> unit [@bs]) -> unit
101100

102-
val mapi : 'a array -> (int -> 'a -> 'b [@bs]) -> 'b array
101+
val mapi: 'a array -> (int -> 'a -> 'b [@bs]) -> 'b array
103102

104-
val foldLeft : 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) ->'a
103+
val reduce: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) ->'a
105104

106-
val foldRight : 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) -> 'a
105+
val reduceFromTail: 'b array -> 'a -> ('a -> 'b -> 'a [@bs]) -> 'a
107106

108107
val forAll: 'a array -> ('a -> bool [@bs]) -> bool
109108

110109
(** [forAll2 a b] return false when [length a <> length b] *)
111110
val forAll2: 'a array -> 'b array -> ('a -> 'b -> bool [@bs]) -> bool
112111

113-
external unsafe_get : 'a array -> int -> 'a = "%array_unsafe_get"
114-
external unsafe_set : 'a array -> int -> 'a -> unit = "%array_unsafe_set"
115-
external truncateToLengthUnsafe : 'a array -> int -> unit = "length" [@@bs.set]
112+
val cmp: 'a array -> 'a array -> ('a -> 'a -> int [@bs]) -> int
113+
val eq: 'a array -> 'a array -> ('a -> 'a -> bool [@bs]) -> bool
114+
115+
external unsafe_get: 'a array -> int -> 'a = "%array_unsafe_get"
116+
external unsafe_set: 'a array -> int -> 'a -> unit = "%array_unsafe_set"
117+
external truncateToLengthUnsafe: 'a array -> int -> unit = "length" [@@bs.set]

‎jscomp/others/bs_internalBuckets.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ let fold0 h init f =
116116

117117
let logStats0 h =
118118
let mbl =
119-
Bs_Array.foldLeft (C.buckets h) 0 (fun[@bs] m b ->
119+
Bs_Array.reduce (C.buckets h) 0 (fun[@bs] m b ->
120120
let len = (bucket_length 0 b) in
121121
Pervasives.max m len) in
122-
let histo = Bs_Array.init (mbl + 1) (fun[@bs] _ -> 0) in
123-
Bs_Array.iter (C.buckets h)
122+
let histo = Bs_Array.initExn (mbl + 1) (fun[@bs] _ -> 0) in
123+
Bs_Array.forEach (C.buckets h)
124124
(fun[@bs] b ->
125125
let l = bucket_length 0 b in
126126
Bs_Array.unsafe_set histo l (Bs_Array.unsafe_get histo l + 1)

‎jscomp/others/bs_internalSetBuckets.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ let fold0 h init f =
131131

132132
let logStats0 h =
133133
let mbl =
134-
Bs_Array.foldLeft (C.buckets h) 0 (fun[@bs] m b ->
134+
Bs_Array.reduce (C.buckets h) 0 (fun[@bs] m b ->
135135
let len = (bucketLength 0 b) in
136136
max m len) in
137-
let histo = Bs_Array.init (mbl + 1) (fun[@bs] _ -> 0) in
138-
Bs_Array.iter (C.buckets h)
137+
let histo = Bs_Array.initExn (mbl + 1) (fun[@bs] _ -> 0) in
138+
Bs_Array.forEach (C.buckets h)
139139
(fun[@bs] b ->
140140
let l = bucketLength 0 b in
141141
Bs_Array.unsafe_set histo l (Bs_Array.unsafe_get histo l + 1)

‎jscomp/test/array_data_util.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
var Bs_Array = require("../../lib/js/bs_Array.js");
44

55
function range(i, j) {
6-
return Bs_Array.init((j - i | 0) + 1 | 0, (function (k) {
6+
return Bs_Array.initExn((j - i | 0) + 1 | 0, (function (k) {
77
return k + i | 0;
88
}));
99
}
1010

1111
function randomRange(i, j) {
12-
return Bs_Array.shuffle(Bs_Array.init((j - i | 0) + 1 | 0, (function (k) {
12+
return Bs_Array.shuffle(Bs_Array.initExn((j - i | 0) + 1 | 0, (function (k) {
1313
return k + i | 0;
1414
})));
1515
}

‎jscomp/test/array_data_util.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module A = Bs_Array
33

44
(* []*)
55
let range i j =
6-
A.init (j - i + 1) (fun[@bs] k -> k + i )
6+
A.initExn (j - i + 1) (fun[@bs] k -> k + i )
77

88
let randomRange i j =
9-
A.shuffle (A.init (j - i + 1) (fun[@bs] k -> k + i ))
9+
A.shuffle (A.initExn (j - i + 1) (fun[@bs] k -> k + i ))
1010

1111

‎jscomp/test/bs_array_test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function add(x, y) {
172172
return x + y | 0;
173173
}
174174

175-
var v = Bs_Array.init(3000, (function (i) {
175+
var v = Bs_Array.initExn(3000, (function (i) {
176176
return i;
177177
}));
178178

@@ -182,25 +182,25 @@ Bs_Array.shuffleDone(u);
182182

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

185-
eq("File \"bs_array_test.ml\", line 65, characters 5-12", Bs_Array.foldLeft(u, 0, add), Bs_Array.foldLeft(v, 0, add));
185+
eq("File \"bs_array_test.ml\", line 65, characters 5-12", Bs_Array.reduce(u, 0, add), Bs_Array.reduce(v, 0, add));
186186

187187
function addone(x) {
188188
return x + 1 | 0;
189189
}
190190

191-
eq("File \"bs_array_test.ml\", line 69, characters 5-12", Bs_Array.init(0, (function () {
191+
eq("File \"bs_array_test.ml\", line 69, characters 5-12", Bs_Array.initExn(0, (function () {
192192
return 1;
193193
})), /* int array */[]);
194194

195-
eq("File \"bs_array_test.ml\", line 70, characters 5-12", Bs_Array.init(3, (function (i) {
195+
eq("File \"bs_array_test.ml\", line 70, characters 5-12", Bs_Array.initExn(3, (function (i) {
196196
return i;
197197
})), /* int array */[
198198
0,
199199
1,
200200
2
201201
]);
202202

203-
eq("File \"bs_array_test.ml\", line 71, characters 5-12", Bs_Array.makeMatrix(3, 4, 1), /* array */[
203+
eq("File \"bs_array_test.ml\", line 71, characters 5-12", Bs_Array.makeMatrixExn(3, 4, 1), /* array */[
204204
/* int array */[
205205
1,
206206
1,
@@ -221,15 +221,15 @@ eq("File \"bs_array_test.ml\", line 71, characters 5-12", Bs_Array.makeMatrix(3,
221221
]
222222
]);
223223

224-
eq("File \"bs_array_test.ml\", line 74, characters 5-12", Bs_Array.makeMatrix(3, 0, 0), /* array */[
224+
eq("File \"bs_array_test.ml\", line 74, characters 5-12", Bs_Array.makeMatrixExn(3, 0, 0), /* array */[
225225
/* int array */[],
226226
/* int array */[],
227227
/* int array */[]
228228
]);
229229

230-
eq("File \"bs_array_test.ml\", line 75, characters 5-12", Bs_Array.makeMatrix(0, 3, 1), /* array */[]);
230+
eq("File \"bs_array_test.ml\", line 75, characters 5-12", Bs_Array.makeMatrixExn(0, 3, 1), /* array */[]);
231231

232-
eq("File \"bs_array_test.ml\", line 76, characters 5-12", Bs_Array.makeMatrix(1, 1, 1), /* array */[/* int array */[1]]);
232+
eq("File \"bs_array_test.ml\", line 76, characters 5-12", Bs_Array.makeMatrixExn(1, 1, 1), /* array */[/* int array */[1]]);
233233

234234
eq("File \"bs_array_test.ml\", line 77, characters 5-12", Bs_Array.copy(/* array */[]), /* array */[]);
235235

‎jscomp/test/bs_array_test.ml

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ let () =
5757
module A = Bs.Array
5858
let add = fun [@bs] x y -> x + y
5959
let () =
60-
let v = Bs.Array.init 3000 (fun[@bs] i -> i) in
60+
let v = Bs.Array.initExn 3000 (fun[@bs] i -> i) in
6161
let u = Bs.Array.copy v in
6262
Bs.Array.shuffleDone u ;
6363
neq __LOC__ u v (* unlikely*);
64-
let sum x = Bs.Array.foldLeft x 0 add in
64+
let sum x = Bs.Array.reduce x 0 add in
6565
eq __LOC__ ( sum u) (sum v)
6666
let addone = fun [@bs] x -> x + 1
6767

6868
let () =
69-
eq __LOC__ (A.init 0 begin fun[@bs] _ ->1 end ) [||];
70-
eq __LOC__ (A.init 3 begin fun [@bs] i -> i end) [|0;1;2|];
71-
eq __LOC__ (A.makeMatrix 3 4 1
69+
eq __LOC__ (A.initExn 0 begin fun[@bs] _ ->1 end ) [||];
70+
eq __LOC__ (A.initExn 3 begin fun [@bs] i -> i end) [|0;1;2|];
71+
eq __LOC__ (A.makeMatrixExn 3 4 1
7272

7373
) [| [|1;1;1;1|]; [|1;1;1;1|]; [|1;1;1;1|]|];
74-
eq __LOC__ (A.makeMatrix 3 0 0 ) [| [||] ; [||]; [||] |];
75-
eq __LOC__ (A.makeMatrix 0 3 1 ) [||];
76-
eq __LOC__ (A.makeMatrix 1 1 1) [| [|1 |] |];
74+
eq __LOC__ (A.makeMatrixExn 3 0 0 ) [| [||] ; [||]; [||] |];
75+
eq __LOC__ (A.makeMatrixExn 0 3 1 ) [||];
76+
eq __LOC__ (A.makeMatrixExn 1 1 1) [| [|1 |] |];
7777
eq __LOC__ (A.copy [||]) [||];
7878
eq __LOC__ (A.map [||] addone) [||];
7979
eq __LOC__ (A.mapi [||] add) [||];

‎jscomp/test/bs_hashmap_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ Bs_HashMap.mergeArrayDone(v$1, Bs_Array.zip(u$1, u$1));
8989
eqx("File \"bs_hashmap_test.ml\", line 47, characters 6-13", v$1.data.size, 100001);
9090

9191
for(var i = 0; i <= 1000; ++i){
92-
Bs_HashMap.remove(v$1, i);
92+
Bs_HashMap.removeDone(v$1, i);
9393
}
9494

9595
eqx("File \"bs_hashmap_test.ml\", line 51, characters 6-13", v$1.data.size, 99000);
9696

9797
for(var i$1 = 0; i$1 <= 2000; ++i$1){
98-
Bs_HashMap.remove(v$1, i$1);
98+
Bs_HashMap.removeDone(v$1, i$1);
9999
}
100100

101101
eqx("File \"bs_hashmap_test.ml\", line 55, characters 6-13", v$1.data.size, 98000);

‎jscomp/test/bs_hashmap_test.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ let () =
3838
let v = A.zip u u in
3939
let xx = N.ofArray (module Y) v in
4040
eqx __LOC__ (N.size xx) 91;
41-
eqx __LOC__ (So.sortByCont (N.keys xx) cmp) (I.range 30 120)
41+
eqx __LOC__ (So.sortByCont (N.keysToArray xx) cmp) (I.range 30 120)
4242

4343
let () =
4444
let u = I.randomRange 0 100_000 ++ I.randomRange 0 100 in
4545
let v = N.create (module Y) 40 in
4646
N.mergeArrayDone v (A.zip u u);
4747
eqx __LOC__ (N.size v) 100_001;
4848
for i = 0 to 1_000 do
49-
N.remove v i
49+
N.removeDone v i
5050
done;
5151
eqx __LOC__ (N.size v) 99_000;
5252
for i = 0 to 2_000 do
53-
N.remove v i
53+
N.removeDone v i
5454
done ;
5555
eqx __LOC__ (N.size v) 98_000;
5656
b __LOC__ (A.forAll (I.range 2_001 100_000) (fun [@bs] x -> N.has v x ))

‎jscomp/test/bs_map_int_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function b(loc, v) {
4040
return /* () */0;
4141
}
4242

43-
var v = Bs_Array.shuffle(Bs_Array.init(1000000, (function (i) {
43+
var v = Bs_Array.shuffle(Bs_Array.initExn(1000000, (function (i) {
4444
return /* tuple */[
4545
i,
4646
i
@@ -51,9 +51,9 @@ var u = Bs_MapInt.ofArray(v);
5151

5252
b("File \"bs_map_int_test.ml\", line 20, characters 4-11", Bs_MapInt.checkInvariant(u));
5353

54-
var firstHalf = Bs_Array.sub(v, 0, 2000);
54+
var firstHalf = Bs_Array.subExn(v, 0, 2000);
5555

56-
Bs_Array.foldLeft(firstHalf, u, (function (acc, param) {
56+
Bs_Array.reduce(firstHalf, u, (function (acc, param) {
5757
return Bs_MapInt.remove(acc, param[0]);
5858
}));
5959

‎jscomp/test/bs_map_int_test.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ module N = Bs.MapInt
1515
module A = Bs.Array
1616
let () =
1717
let v =
18-
A.shuffle (A.init 1_000_000 (fun[@bs] i -> (i,i))) in
18+
A.shuffle (A.initExn 1_000_000 (fun[@bs] i -> (i,i))) in
1919
let u = N.ofArray v in
2020
b __LOC__ (N.checkInvariant u);
21-
let firstHalf = Bs.Array.sub v 0 2_000 in
22-
let xx = Bs.Array.foldLeft firstHalf u
21+
let firstHalf = Bs.Array.subExn v 0 2_000 in
22+
let xx = Bs.Array.reduce firstHalf u
2323
(fun[@bs] acc (x,_) -> N.remove acc x) in
2424
b __LOC__ (N.checkInvariant u);
2525

‎jscomp/test/bs_map_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ eq("File \"bs_map_test.ml\", line 93, characters 5-12", Bs_Map.get(u0, 39), /* S
137137

138138
eq("File \"bs_map_test.ml\", line 94, characters 5-12", Bs_Map.get(u1, 39), /* Some */[120]);
139139

140-
var u = f(Bs_Array.shuffle(Bs_Array.init(10000, (function (x) {
140+
var u = f(Bs_Array.shuffle(Bs_Array.initExn(10000, (function (x) {
141141
return /* tuple */[
142142
x,
143143
x
144144
];
145145
}))));
146146

147-
eq("File \"bs_map_test.ml\", line 100, characters 4-11", Bs_Array.init(10000, (function (x) {
147+
eq("File \"bs_map_test.ml\", line 100, characters 4-11", Bs_Array.initExn(10000, (function (x) {
148148
return /* tuple */[
149149
x,
150150
x

‎jscomp/test/bs_map_test.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ let () =
9696

9797
let () =
9898
let u = f
99-
(A.shuffle (A.init 10_000 (fun[@bs] x -> (x,x)))) in
99+
(A.shuffle (A.initExn 10_000 (fun[@bs] x -> (x,x)))) in
100100
eq __LOC__
101-
(A.init 10_000 (fun[@bs] x -> (x,x)))
101+
(A.initExn 10_000 (fun[@bs] x -> (x,x)))
102102
(M.toArray u)
103103

104104

‎jscomp/test/bs_mutable_set_test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ eq("File \"bs_mutable_set_test.ml\", line 48, characters 5-12", Bs_internalAVLse
7575

7676
b("File \"bs_mutable_set_test.ml\", line 49, characters 4-11", Bs_internalAVLset.isEmpty0(v$2.data));
7777

78-
var xs = Bs_Array.init(30, (function (i) {
78+
var xs = Bs_Array.initExn(30, (function (i) {
7979
return i;
8080
}));
8181

@@ -220,7 +220,7 @@ for(var i$4 = 0; i$4 <= 200; ++i$4){
220220

221221
eq("File \"bs_mutable_set_test.ml\", line 92, characters 5-12", Bs_internalAVLset.length0(copyV.data), 126);
222222

223-
eq("File \"bs_mutable_set_test.ml\", line 93, characters 5-12", Bs_internalAVLset.toArray0(copyV.data), Bs_Array.init(126, (function (i) {
223+
eq("File \"bs_mutable_set_test.ml\", line 93, characters 5-12", Bs_internalAVLset.toArray0(copyV.data), Bs_Array.initExn(126, (function (i) {
224224
return (i << 3);
225225
})));
226226

@@ -264,15 +264,15 @@ var match$3 = Bs_SetIntM.split(d, 1001);
264264

265265
var match$4 = match$3[0];
266266

267-
var xs$6 = Bs_Array.init(501, (function (x) {
267+
var xs$6 = Bs_Array.initExn(501, (function (x) {
268268
return (x << 1);
269269
}));
270270

271271
b("File \"bs_mutable_set_test.ml\", line 105, characters 4-11", Bs_SetIntM.eq(match$4[0], {
272272
data: Bs_internalSetInt.ofArray(xs$6)
273273
}));
274274

275-
var xs$7 = Bs_Array.init(500, (function (x) {
275+
var xs$7 = Bs_Array.initExn(500, (function (x) {
276276
return 1002 + (x << 1) | 0;
277277
}));
278278

‎jscomp/test/bs_mutable_set_test.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let () =
4949
b __LOC__ (N.isEmpty v )
5050

5151
let () =
52-
let v = N.ofArray (A.init 30 (fun [@bs]i -> i)) in
52+
let v = N.ofArray (A.initExn 30 (fun [@bs]i -> i)) in
5353
N.removeDone v 30;
5454
N.removeDone v 29 ;
5555
b __LOC__ (Js.eqNull 28 (N.maxNull v ));
@@ -90,7 +90,7 @@ let () =
9090
N.removeDone v i
9191
done ;
9292
eq __LOC__ (N.size copyV) 126;
93-
eq __LOC__ (N.toArray copyV) (A.init 126 (fun[@bs] i -> i * 8));
93+
eq __LOC__ (N.toArray copyV) (A.initExn 126 (fun[@bs] i -> i * 8));
9494
eq __LOC__ (N.size v ) 800;
9595
b __LOC__ (N.eq copyV aa);
9696
b __LOC__ (N.eq cc bb)
@@ -102,8 +102,8 @@ let () =
102102
b __LOC__ (N.eq bb (N.ofArray (I.randomRange 401 1000)));
103103
let d = N.ofArray (A.map (I.randomRange 0 1000) (fun[@bs] x -> x * 2)) in
104104
let ((cc,dd), _) = N.split d 1001 in
105-
b __LOC__ (N.eq cc (N.ofArray (A.init 501 (fun[@bs] x -> x * 2))));
106-
b __LOC__ (N.eq dd (N.ofArray (A.init 500 (fun [@bs] x -> 1002 + x * 2))))
105+
b __LOC__ (N.eq cc (N.ofArray (A.initExn 501 (fun[@bs] x -> x * 2))));
106+
b __LOC__ (N.eq dd (N.ofArray (A.initExn 500 (fun [@bs] x -> 1002 + x * 2))))
107107

108108

109109
let (++) = N.union

‎jscomp/test/bs_poly_map_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ eq("File \"bs_poly_map_test.ml\", line 97, characters 5-12", Bs_Map.get(u1$1, 3)
179179
eq("File \"bs_poly_map_test.ml\", line 98, characters 5-12", Bs_Map.get(u0$1, 3), /* Some */[3]);
180180

181181
function acc(m, is) {
182-
return Bs_Array.foldLeft(is, m, (function (a, i) {
182+
return Bs_Array.reduce(is, m, (function (a, i) {
183183
var m = a;
184184
var i$1 = i;
185185
return Bs_Map.update(m, i$1, (function (n) {
@@ -199,7 +199,7 @@ var m = {
199199

200200
var m1 = acc(m, Bs_Array.append(Array_data_util.randomRange(0, 20), Array_data_util.randomRange(10, 30)));
201201

202-
b("File \"bs_poly_map_test.ml\", line 110, characters 4-11", Bs_Map.eq(m1, Bs_Map.ofArray(Bs_Array.init(31, (function (i) {
202+
b("File \"bs_poly_map_test.ml\", line 110, characters 4-11", Bs_Map.eq(m1, Bs_Map.ofArray(Bs_Array.initExn(31, (function (i) {
203203
return /* tuple */[
204204
i,
205205
i >= 10 && i <= 20 ? 2 : 1

‎jscomp/test/bs_poly_map_test.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ let acc m i =
102102
M.update m i (fun[@bs] n -> match n with None -> Some 1 | Some acc -> Some (acc + 1))
103103

104104
let acc m is : _ M.t =
105-
A.foldLeft is m (fun[@bs] a i -> acc a i)
105+
A.reduce is m (fun[@bs] a i -> acc a i)
106106

107107
let () =
108108
let m = M.empty (module Icmp) in
109109
let m1 = acc m (A.append (I.randomRange 0 20) (I.randomRange 10 30)) in
110110
b __LOC__
111111
(M.eq m1
112-
(M.ofArray ~dict:(module Icmp) (A.init 31 (fun[@bs] i -> i, if i >= 10 && i <= 20 then 2 else 1 )))
112+
(M.ofArray ~dict:(module Icmp) (A.initExn 31 (fun[@bs] i -> i, if i >= 10 && i <= 20 then 2 else 1 )))
113113
(fun[@bs] x y -> x = y)
114114
)
115115

‎jscomp/test/bs_poly_set_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ var match$2 = match$1[0];
291291

292292
b("File \"bs_poly_set_test.ml\", line 119, characters 4-11", match$1[1]);
293293

294-
eq("File \"bs_poly_set_test.ml\", line 120, characters 5-12", Bs_internalAVLset.toArray0(match$2[0].data), Bs_Array.init(200, (function (i) {
294+
eq("File \"bs_poly_set_test.ml\", line 120, characters 5-12", Bs_internalAVLset.toArray0(match$2[0].data), Bs_Array.initExn(200, (function (i) {
295295
return i;
296296
})));
297297

@@ -311,7 +311,7 @@ var a8 = match$4[0];
311311

312312
b("File \"bs_poly_set_test.ml\", line 124, characters 4-11", 1 - match$3[1]);
313313

314-
eq("File \"bs_poly_set_test.ml\", line 125, characters 5-12", Bs_internalAVLset.toArray0(a8.data), Bs_Array.init(200, (function (i) {
314+
eq("File \"bs_poly_set_test.ml\", line 125, characters 5-12", Bs_internalAVLset.toArray0(a8.data), Bs_Array.initExn(200, (function (i) {
315315
return i;
316316
})));
317317

‎jscomp/test/bs_poly_set_test.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ let () =
117117
b __LOC__ (not @@ N.isEmpty a0);
118118
let (a5,a6), pres = N.split a0 200 in
119119
b __LOC__ pres ;
120-
eq __LOC__ (N.toArray a5) (A.init 200 (fun[@bs] i -> i));
120+
eq __LOC__ (N.toArray a5) (A.initExn 200 (fun[@bs] i -> i));
121121
eq __LOC__ (N.toList a6) (L.init 800 (fun[@bs] i -> i + 201));
122122
let a7 = N.remove a0 200 in
123123
let (a8,a9), pres = N.split a7 200 in
124124
b __LOC__ (not pres) ;
125-
eq __LOC__ (N.toArray a8) (A.init 200 (fun[@bs] i -> i));
125+
eq __LOC__ (N.toArray a8) (A.initExn 200 (fun[@bs] i -> i));
126126
eq __LOC__ (N.toList a9) (L.init 800 (fun[@bs] i -> i + 201));
127127
eq __LOC__ (N.minimum a8) (Some 0);
128128
eq __LOC__ (N.minimum a9) (Some 201);

‎jscomp/test/bs_queue_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ if (!Caml_obj.caml_equal(Bs_Queue.toArray(q2$4), v)) {
10691069

10701070
if (Bs_Queue.reduce(q2$4, 0, (function (x, y) {
10711071
return x - y | 0;
1072-
})) !== Bs_Array.foldLeft(v, 0, (function (x, y) {
1072+
})) !== Bs_Array.reduce(v, 0, (function (x, y) {
10731073
return x - y | 0;
10741074
}))) {
10751075
throw [

‎jscomp/test/bs_queue_test.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ let () =
136136
assert (Q.length q2 = 8); assert (Q.toArray q2 = v );
137137

138138
assert (Q.reduce q2 0 (fun[@bs] x y -> x - y ) =
139-
Bs.Array.foldLeft v 0 (fun [@bs] x y -> x - y) )
139+
Bs.Array.reduce v 0 (fun [@bs] x y -> x - y) )
140140

141141
;;
142142

‎jscomp/test/bs_set_int_test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function approx(loc, x, y) {
155155

156156
eq("File \"bs_set_int_test.ml\", line 74, characters 5-12", Bs_SetInt.reduce(v$1, 0, (function (x, y) {
157157
return x + y | 0;
158-
})), Bs_Array.foldLeft(ss, 0, (function (x, y) {
158+
})), Bs_Array.reduce(ss, 0, (function (x, y) {
159159
return x + y | 0;
160160
})));
161161

@@ -207,17 +207,17 @@ var v$10 = Bs_SetInt.remove(v$9, 1);
207207

208208
b("File \"bs_set_int_test.ml\", line 95, characters 4-11", Bs_SetInt.isEmpty(v$10));
209209

210-
var v$11 = Bs_Array.shuffle(Bs_Array.init(1000000, (function (i) {
210+
var v$11 = Bs_Array.shuffle(Bs_Array.initExn(1000000, (function (i) {
211211
return i;
212212
})));
213213

214214
var u$1 = Bs_SetInt.ofArray(v$11);
215215

216216
b("File \"bs_set_int_test.ml\", line 102, characters 4-11", Bs_SetInt.checkInvariant(u$1));
217217

218-
var firstHalf = Bs_Array.sub(v$11, 0, 2000);
218+
var firstHalf = Bs_Array.subExn(v$11, 0, 2000);
219219

220-
var xx = Bs_Array.foldLeft(firstHalf, u$1, Bs_SetInt.remove);
220+
var xx = Bs_Array.reduce(firstHalf, u$1, Bs_SetInt.remove);
221221

222222
b("File \"bs_set_int_test.ml\", line 106, characters 4-11", Bs_SetInt.checkInvariant(u$1));
223223

‎jscomp/test/bs_set_int_test.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let () =
7171
let minv, maxv = N.minNull v, N.maxNull v in
7272
let approx loc (x : int) y =
7373
b loc (Js.eqNull x y) in
74-
eq __LOC__ (N.reduce v 0 (fun [@bs] x y -> x + y) ) (A.foldLeft ss 0 (fun [@bs] x y -> x + y) ) ;
74+
eq __LOC__ (N.reduce v 0 (fun [@bs] x y -> x + y) ) (A.reduce ss 0 (fun [@bs] x y -> x + y) ) ;
7575
approx __LOC__ (-1) minv ;
7676
approx __LOC__ 222 maxv;
7777
let v = N.remove v 3 in
@@ -97,11 +97,11 @@ let () =
9797

9898
let () =
9999
let count = 1_000_000 in
100-
let v = (A.shuffle (A.init count (fun [@bs] i -> i))) in
100+
let v = (A.shuffle (A.initExn count (fun [@bs] i -> i))) in
101101
let u = N.ofArray v in
102102
b __LOC__ (N.checkInvariant u );
103-
let firstHalf = Bs.Array.sub v 0 2_000 in
104-
let xx = Bs.Array.foldLeft firstHalf u
103+
let firstHalf = Bs.Array.subExn v 0 2_000 in
104+
let xx = Bs.Array.reduce firstHalf u
105105
(fun[@bs] acc x -> N.remove acc x ) in
106106
b __LOC__ (N.checkInvariant u);
107107
b __LOC__ N.(eq (union (ofArray firstHalf) xx) u)

‎jscomp/test/imm_map_bench.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function should(b) {
2323
}
2424
}
2525

26-
var shuffledDataAdd = Bs_Array.shuffle(Bs_Array.init(1000001, (function (i) {
26+
var shuffledDataAdd = Bs_Array.shuffle(Bs_Array.initExn(1000001, (function (i) {
2727
return /* tuple */[
2828
i,
2929
i

‎jscomp/test/imm_map_bench.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let should b =
2121

2222
let count = 1_000_000
2323

24-
let shuffledDataAdd = A.shuffle (A.init (count + 1) (fun[@bs] i -> (i,i)))
24+
let shuffledDataAdd = A.shuffle (A.initExn (count + 1) (fun[@bs] i -> (i,i)))
2525

2626

2727

‎lib/js/bs_Array.js

+55-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var Js_math = require("./js_math.js");
44
var Caml_array = require("./caml_array.js");
55

6-
function init(l, f) {
6+
function initExn(l, f) {
77
if (l < 0) {
88
throw new Error("File \"bs_Array.ml\", line 35, characters 4-10");
99
}
@@ -34,7 +34,7 @@ function shuffle(xs) {
3434
return xs;
3535
}
3636

37-
function makeMatrix(sx, sy, init) {
37+
function makeMatrixExn(sx, sy, init) {
3838
if (!(sx >= 0 && sy >= 0)) {
3939
throw new Error("File \"bs_Array.ml\", line 57, characters 4-10");
4040
}
@@ -85,34 +85,35 @@ function append(a1, a2) {
8585
}
8686
}
8787

88-
function sub(a, ofs, len) {
88+
function subExn(a, ofs, len) {
8989
if (len < 0 || ofs > (a.length - len | 0)) {
90-
throw new Error("Array.sub");
90+
throw new Error("subExn");
9191
} else {
9292
return Caml_array.caml_array_sub(a, ofs, len);
9393
}
9494
}
9595

9696
function fill(a, ofs, len, v) {
9797
if (ofs < 0 || len < 0 || ofs > (a.length - len | 0)) {
98-
throw new Error("Array.fill");
98+
return /* false */0;
9999
} else {
100100
for(var i = ofs ,i_finish = (ofs + len | 0) - 1 | 0; i <= i_finish; ++i){
101101
a[i] = v;
102102
}
103-
return /* () */0;
103+
return /* true */1;
104104
}
105105
}
106106

107107
function blit(a1, ofs1, a2, ofs2, len) {
108108
if (len < 0 || ofs1 < 0 || ofs1 > (a1.length - len | 0) || ofs2 < 0 || ofs2 > (a2.length - len | 0)) {
109-
throw new Error("Array.blit");
109+
return /* false */0;
110110
} else {
111-
return Caml_array.caml_array_blit(a1, ofs1, a2, ofs2, len);
111+
Caml_array.caml_array_blit(a1, ofs1, a2, ofs2, len);
112+
return /* true */1;
112113
}
113114
}
114115

115-
function iter(a, f) {
116+
function forEach(a, f) {
116117
for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){
117118
f(a[i]);
118119
}
@@ -128,7 +129,7 @@ function map(a, f) {
128129
return r;
129130
}
130131

131-
function iteri(a, f) {
132+
function forEachi(a, f) {
132133
for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){
133134
f(i, a[i]);
134135
}
@@ -202,15 +203,15 @@ function ofList(xs) {
202203
return a;
203204
}
204205

205-
function foldLeft(a, x, f) {
206+
function reduce(a, x, f) {
206207
var r = x;
207208
for(var i = 0 ,i_finish = a.length - 1 | 0; i <= i_finish; ++i){
208209
r = f(r, a[i]);
209210
}
210211
return r;
211212
}
212213

213-
function foldRight(a, x, f) {
214+
function reduceFromTail(a, x, f) {
214215
var r = x;
215216
for(var i = a.length - 1 | 0; i >= 0; --i){
216217
r = f(r, a[i]);
@@ -264,27 +265,62 @@ function forAll2(a, b, p) {
264265
}
265266
}
266267

268+
function cmp(a, b, p) {
269+
var lena = a.length;
270+
var lenb = b.length;
271+
if (lena > lenb) {
272+
return 1;
273+
} else if (lena < lenb) {
274+
return -1;
275+
} else {
276+
var arr1 = a;
277+
var arr2 = b;
278+
var _i = 0;
279+
var b$1 = p;
280+
var len = lena;
281+
while(true) {
282+
var i = _i;
283+
if (i === len) {
284+
return 0;
285+
} else {
286+
var c = b$1(arr1[i], arr2[i]);
287+
if (c) {
288+
return c;
289+
} else {
290+
_i = i + 1 | 0;
291+
continue ;
292+
293+
}
294+
}
295+
};
296+
}
297+
}
298+
267299
var concat = Caml_array.caml_array_concat;
268300

269-
exports.init = init;
301+
var eq = forAll2;
302+
303+
exports.initExn = initExn;
270304
exports.shuffleDone = shuffleDone;
271305
exports.shuffle = shuffle;
272306
exports.zip = zip;
273-
exports.makeMatrix = makeMatrix;
307+
exports.makeMatrixExn = makeMatrixExn;
274308
exports.append = append;
275309
exports.concat = concat;
276-
exports.sub = sub;
310+
exports.subExn = subExn;
277311
exports.copy = copy;
278312
exports.fill = fill;
279313
exports.blit = blit;
280314
exports.toList = toList;
281315
exports.ofList = ofList;
282-
exports.iter = iter;
316+
exports.forEach = forEach;
283317
exports.map = map;
284-
exports.iteri = iteri;
318+
exports.forEachi = forEachi;
285319
exports.mapi = mapi;
286-
exports.foldLeft = foldLeft;
287-
exports.foldRight = foldRight;
320+
exports.reduce = reduce;
321+
exports.reduceFromTail = reduceFromTail;
288322
exports.forAll = forAll;
289323
exports.forAll2 = forAll2;
324+
exports.cmp = cmp;
325+
exports.eq = eq;
290326
/* No side effect */

‎lib/js/bs_internalBuckets.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,18 @@ function fold0(h, init, f) {
117117
}
118118

119119
function logStats0(h) {
120-
var mbl = Bs_Array.foldLeft(h.buckets, 0, (function (m, b) {
120+
var mbl = Bs_Array.reduce(h.buckets, 0, (function (m, b) {
121121
var len = bucket_length(0, b);
122122
if (m > len) {
123123
return m;
124124
} else {
125125
return len;
126126
}
127127
}));
128-
var histo = Bs_Array.init(mbl + 1 | 0, (function () {
128+
var histo = Bs_Array.initExn(mbl + 1 | 0, (function () {
129129
return 0;
130130
}));
131-
Bs_Array.iter(h.buckets, (function (b) {
131+
Bs_Array.forEach(h.buckets, (function (b) {
132132
var l = bucket_length(0, b);
133133
histo[l] = histo[l] + 1 | 0;
134134
return /* () */0;

‎lib/js/bs_internalSetBuckets.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ function fold0(h, init, f) {
146146
}
147147

148148
function logStats0(h) {
149-
var mbl = Bs_Array.foldLeft(h.buckets, 0, (function (m, b) {
149+
var mbl = Bs_Array.reduce(h.buckets, 0, (function (m, b) {
150150
var len = bucketLength(0, b);
151151
if (m > len) {
152152
return m;
153153
} else {
154154
return len;
155155
}
156156
}));
157-
var histo = Bs_Array.init(mbl + 1 | 0, (function () {
157+
var histo = Bs_Array.initExn(mbl + 1 | 0, (function () {
158158
return 0;
159159
}));
160-
Bs_Array.iter(h.buckets, (function (b) {
160+
Bs_Array.forEach(h.buckets, (function (b) {
161161
var l = bucketLength(0, b);
162162
histo[l] = histo[l] + 1 | 0;
163163
return /* () */0;

0 commit comments

Comments
 (0)
Please sign in to comment.