Skip to content

Commit 1edd728

Browse files
committed
more tests
1 parent f0e7b37 commit 1edd728

35 files changed

+531
-210
lines changed

jscomp/others/.depend

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ js_console.cmj :
1717
js_result.cmj : js_result.cmi
1818
js_mapperRt.cmj : js_mapperRt.cmi
1919
bs_Array.cmj : js_math.cmj bs_Array.cmi
20-
bs_internalAVLset.cmj :
20+
bs_internalAVLset.cmj : bs_Array.cmj bs.cmj
2121
bs_internalAVLtree.cmj :
2222
bs_internalMutableAVLSet.cmj : bs_internalAVLset.cmj
2323
bs_Hash.cmj : bs_Hash.cmi
2424
bs_Queue.cmj : bs_Array.cmj bs_Queue.cmi
2525
bs_internalBucketsType.cmj : bs_Array.cmj
26-
bs_internalSetBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj
26+
bs_internalSetBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj bs.cmj
2727
bs_internalBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj
2828
bs_HashMap.cmj : bs_internalBucketsType.cmj bs_internalBuckets.cmj \
2929
bs_Hash.cmj bs_Bag.cmj bs_Array.cmj bs_HashMap.cmi
3030
bs_HashSet.cmj : bs_internalSetBuckets.cmj bs_internalBucketsType.cmj \
3131
bs_Hash.cmj bs_Bag.cmj bs_Array.cmj bs_HashSet.cmi
3232
bs_HashSetString.cmj : bs_internalSetBuckets.cmj bs_internalBucketsType.cmj \
33-
bs_Array.cmj bs_HashSetString.cmi
33+
bs_Array.cmj bs.cmj bs_HashSetString.cmi
3434
bs_HashSetInt.cmj : bs_internalSetBuckets.cmj bs_internalBucketsType.cmj \
35-
bs_Array.cmj bs_HashSetInt.cmi
35+
bs_Array.cmj bs.cmj bs_HashSetInt.cmi
3636
bs_Bag.cmj :
3737
bs_Cmp.cmj : bs_Cmp.cmi
3838
bs_Map.cmj : bs_internalAVLtree.cmj bs_Cmp.cmj bs_Bag.cmj bs_Array.cmj \

jscomp/others/bs_HashSet.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ let length0 = C.length0
163163
let iter0 = N.iter0
164164
let fold0 = N.fold0
165165
let logStats0 = N.logStats0
166-
167-
166+
let toArray0 = N.toArray0
167+
let toArray h = toArray0 (B.data h)
168168
(* Wrapper *)
169169
let create dict initialize_size =
170170
B.bag ~data:(create0 initialize_size)

jscomp/others/bs_HashSet.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,5 @@ val logStats : _ t -> unit
178178
[IntHashtbl.length].
179179
*)
180180

181-
182-
181+
val toArray0 : ('a,'id) t0 -> 'a array
182+
val toArray : ('a,'id) t -> 'a array

jscomp/others/bs_HashSetInt.ml

+28-19
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,35 @@ let remove h (key : key)=
8484

8585

8686

87-
let rec addBucket h (key : key) cell =
88-
if (N.key cell) = key
89-
then
90-
N.keySet cell key
91-
else
87+
let rec addBucket h buckets_len (key : key) cell =
88+
if N.key cell <> key then
9289
let n = N.next cell in
9390
match C.toOpt n with
9491
| None ->
9592
C.sizeSet h (C.size h + 1);
96-
N.nextSet cell (C.return @@ N.bucket ~key ~next:n)
97-
| Some n -> addBucket h key n
93+
N.nextSet cell (C.return @@ N.bucket ~key ~next:n);
94+
if C.size h > buckets_len lsl 1 then resize h
95+
| Some n -> addBucket h buckets_len key n
9896

9997
let add h key =
10098
let h_buckets = C.buckets h in
101-
let i = hash key land (Array.length h_buckets - 1) in
99+
let buckets_len = Array.length h_buckets in
100+
let i = hash key land (buckets_len - 1) in
102101
let l = Array.unsafe_get h_buckets i in
103-
(match C.toOpt l with
104-
| None ->
105-
C.sizeSet h (C.size h + 1);
106-
Bs_Array.unsafe_set h_buckets i
107-
(C.return @@ N.bucket ~key ~next:C.emptyOpt)
108-
| Some cell ->
109-
addBucket h key cell);
110-
if C.size h > Array.length (C.buckets h) lsl 1 then resize h
102+
match C.toOpt l with
103+
| None ->
104+
Bs_Array.unsafe_set h_buckets i
105+
(C.return @@ N.bucket ~key ~next:C.emptyOpt);
106+
C.sizeSet h (C.size h + 1);
107+
if C.size h > buckets_len lsl 1 then resize h
108+
| Some cell ->
109+
addBucket h buckets_len key cell
110+
111111

112112

113113
let rec mem_in_bucket (key : key) cell =
114-
115-
(N.key cell) = key ||
114+
115+
(N.key cell) = key ||
116116
(match C.toOpt (N.next cell) with
117117
| None -> false
118118
| Some nextCell ->
@@ -134,4 +134,13 @@ let reset = C.reset0
134134
let length = C.length0
135135
let iter = N.iter0
136136
let fold = N.fold0
137-
let logStats = N.logStats0
137+
let logStats = N.logStats0
138+
let toArray = N.toArray0
139+
140+
let ofArray arr =
141+
let len = Bs.Array.length arr in
142+
let v = create len in
143+
for i = 0 to len - 1 do
144+
add v (Bs.Array.unsafe_get arr i)
145+
done ;
146+
v

jscomp/others/bs_HashSetInt.mli

+3
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,6 @@ val logStats : t -> unit
168168

169169

170170

171+
val toArray : t -> key array
172+
173+
val ofArray : key array -> t

jscomp/others/bs_HashSetString.ml

+28-19
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,35 @@ let remove h (key : key)=
8484

8585

8686

87-
let rec addBucket h (key : key) cell =
88-
if (N.key cell) = key
89-
then
90-
N.keySet cell key
91-
else
87+
let rec addBucket h buckets_len (key : key) cell =
88+
if N.key cell <> key then
9289
let n = N.next cell in
9390
match C.toOpt n with
9491
| None ->
9592
C.sizeSet h (C.size h + 1);
96-
N.nextSet cell (C.return @@ N.bucket ~key ~next:n)
97-
| Some n -> addBucket h key n
93+
N.nextSet cell (C.return @@ N.bucket ~key ~next:n);
94+
if C.size h > buckets_len lsl 1 then resize h
95+
| Some n -> addBucket h buckets_len key n
9896

9997
let add h key =
10098
let h_buckets = C.buckets h in
101-
let i = hash key land (Array.length h_buckets - 1) in
99+
let buckets_len = Array.length h_buckets in
100+
let i = hash key land (buckets_len - 1) in
102101
let l = Array.unsafe_get h_buckets i in
103-
(match C.toOpt l with
104-
| None ->
105-
C.sizeSet h (C.size h + 1);
106-
Bs_Array.unsafe_set h_buckets i
107-
(C.return @@ N.bucket ~key ~next:C.emptyOpt)
108-
| Some cell ->
109-
addBucket h key cell);
110-
if C.size h > Array.length (C.buckets h) lsl 1 then resize h
102+
match C.toOpt l with
103+
| None ->
104+
Bs_Array.unsafe_set h_buckets i
105+
(C.return @@ N.bucket ~key ~next:C.emptyOpt);
106+
C.sizeSet h (C.size h + 1);
107+
if C.size h > buckets_len lsl 1 then resize h
108+
| Some cell ->
109+
addBucket h buckets_len key cell
110+
111111

112112

113113
let rec mem_in_bucket (key : key) cell =
114-
115-
(N.key cell) = key ||
114+
115+
(N.key cell) = key ||
116116
(match C.toOpt (N.next cell) with
117117
| None -> false
118118
| Some nextCell ->
@@ -134,4 +134,13 @@ let reset = C.reset0
134134
let length = C.length0
135135
let iter = N.iter0
136136
let fold = N.fold0
137-
let logStats = N.logStats0
137+
let logStats = N.logStats0
138+
let toArray = N.toArray0
139+
140+
let ofArray arr =
141+
let len = Bs.Array.length arr in
142+
let v = create len in
143+
for i = 0 to len - 1 do
144+
add v (Bs.Array.unsafe_get arr i)
145+
done ;
146+
v

jscomp/others/bs_HashSetString.mli

+3
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,6 @@ val logStats : t -> unit
168168

169169

170170

171+
val toArray : t -> key array
172+
173+
val ofArray : key array -> t

jscomp/others/bs_Set.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let filter0 = N.filter0
2424
let partition0 = N.partition0
2525
let cardinal0 = N.cardinal0
2626
let elements0 = N.elements0
27-
27+
let toArray0 = N.toArray0
2828
(* Insertion of one element *)
2929

3030
let rec add0 ~cmp x (t : _ t0) : _ t0 =
@@ -278,7 +278,7 @@ let partition f m =
278278
let cardinal m = cardinal0 (B.data m)
279279

280280
let elements m = elements0 (B.data m)
281-
281+
let toArray m = toArray0 (B.data m)
282282
let min m = min0 (B.data m)
283283

284284
let max m = max0 (B.data m)

jscomp/others/bs_Set.mli

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ val elements: ('elt, 'id) t -> 'elt list
140140
The returned list is sorted in increasing order with respect
141141
to the ordering [Ord.compare], where [Ord] is the argument
142142
given to {!Set.Make}. *)
143+
val toArray0: ('elt, 'id) t0 -> 'elt array
144+
val toArray: ('elt, 'id) t -> 'elt array
143145

144146
val min0: ('elt, 'id) t0 -> 'elt option
145147
val min: ('elt, 'id) t -> 'elt option

jscomp/others/bs_SetInt.ml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let filter = N.filter0
2929
let partition = N.partition0
3030
let cardinal = N.cardinal0
3131
let elements = N.elements0
32+
let toArray = N.toArray0
3233
let checkInvariant = N.checkInvariant
3334
(* Insertion of one element *)
3435

jscomp/others/bs_SetInt.mli

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ val elements: t -> elt list
8484
to the ordering [Ord.compare], where [Ord] is the argument
8585
given to {!Set.Make}. *)
8686

87+
val toArray: t -> elt array
88+
8789
val min: t -> elt option
8890
(** Return the smallest element of the given set
8991
(with respect to the [Ord.compare] ordering), or raise

jscomp/others/bs_SetString.ml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let filter = N.filter0
2929
let partition = N.partition0
3030
let cardinal = N.cardinal0
3131
let elements = N.elements0
32+
let toArray = N.toArray0
3233
let checkInvariant = N.checkInvariant
3334
(* Insertion of one element *)
3435

jscomp/others/bs_SetString.mli

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ val elements: t -> elt list
8484
to the ordering [Ord.compare], where [Ord] is the argument
8585
given to {!Set.Make}. *)
8686

87+
val toArray: t -> elt array
88+
8789
val min: t -> elt option
8890
(** Return the smallest element of the given set
8991
(with respect to the [Ord.compare] ordering), or raise

jscomp/others/bs_internalAVLset.ml

+20-4
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,28 @@ let rec checkInvariant (v : _ t0) =
252252
diff <=2 && diff >= -2 && checkInvariant l && checkInvariant r
253253

254254

255+
let rec fillArray n i arr =
256+
let l,v,r = left n, key n, right n in
257+
let next =
258+
match toOpt l with
259+
| None -> i
260+
| Some l ->
261+
fillArray l i arr in
262+
Bs_Array.unsafe_set arr next v ;
263+
let rnext = next + 1 in
264+
match toOpt r with
265+
| None -> rnext
266+
| Some r ->
267+
fillArray r rnext arr
268+
255269
(* TODO: binary search tree to array efficiency
256-
let toArray n =
270+
*)
271+
let toArray0 n =
257272
match toOpt n with
258273
| None -> [||]
259274
| Some n ->
260275
let size = cardinalAux n in
261-
let v = Bs.Array.makeUninitialized size in
262-
let l,v,r = left n, value n, right n in
263-
*)
276+
let v = Bs.Array.makeUninitializedUnsafe size in
277+
ignore (fillArray n 0 v : int); (* may add assertion *)
278+
v
279+

jscomp/others/bs_internalSetBuckets.ml

+21-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
(* We do dynamic hashing, and resize the table and rehash the elements
25+
(* We do dynamic hashing, and resize the table and rehash the elements
2626
when buckets become too long. *)
2727
module C = Bs_internalBucketsType
2828
(* TODO:
@@ -59,6 +59,26 @@ let iter0 f h =
5959
do_bucket_iter f (Bs_Array.unsafe_get d i)
6060
done
6161

62+
let rec fillArray i arr cell =
63+
Bs_Array.unsafe_set arr i (key cell);
64+
match C.toOpt (next cell) with
65+
| None -> i + 1
66+
| Some v -> fillArray (i + 1) arr v
67+
68+
let toArray0 h =
69+
let d = C.buckets h in
70+
let current = ref 0 in
71+
let arr = Bs.Array.makeUninitializedUnsafe (C.size h) in
72+
for i = 0 to Bs_Array.length d - 1 do
73+
let cell = Bs_Array.unsafe_get d i in
74+
match C.toOpt cell with
75+
| None -> ()
76+
| Some cell ->
77+
current := fillArray !current arr cell
78+
done;
79+
arr
80+
81+
6282

6383
let rec do_bucket_fold ~f b accu =
6484
match C.toOpt b with

0 commit comments

Comments
 (0)