Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jscomp/others/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bs_internalBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj
bs_HashMap.cmj : bs_internalBucketsType.cmj bs_internalBuckets.cmj \
bs_Hash.cmj bs_Bag.cmj bs_Array.cmj bs_HashMap.cmi
bs_HashSet.cmj : bs_internalSetBuckets.cmj bs_internalBucketsType.cmj \
bs_Hash.cmj bs_Bag.cmj bs_Array.cmj bs_HashSet.cmi
bs_Hash.cmj bs_Bag.cmj bs_Array.cmj bs.cmj bs_HashSet.cmi
bs_HashSetString.cmj : bs_internalSetBuckets.cmj bs_internalBucketsType.cmj \
bs_Array.cmj bs.cmj bs_HashSetString.cmi
bs_HashSetInt.cmj : bs_internalSetBuckets.cmj bs_internalBucketsType.cmj \
Expand Down
27 changes: 27 additions & 0 deletions jscomp/others/bs_HashSet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,30 @@ let mem (type a) (type id) (h : (a,id) t) (key : a) =
let module M = (val dict) in
mem0 ~hash:M.hash ~eq:M.eq data key

let ofArray0 ~hash ~eq arr =
let len = Bs.Array.length arr in
let v = create0 len in
for i = 0 to len - 1 do
add0 ~eq ~hash v (Bs.Array.unsafe_get arr i)
done ;
v

(* TOOD: optimize heuristics for resizing *)
let addArray0 ~hash ~eq h arr =
let len = Bs.Array.length arr in
for i = 0 to len - 1 do
add0 h ~eq ~hash (Bs_Array.unsafe_get arr i)
done

let ofArray (type a) (type id)
~dict:(dict:(a,id) Bs_Hash.t) arr =
let module M = (val dict) in
B.bag ~dict
~data:M.(ofArray0 ~eq~hash arr)

let addArray (type a) (type id)
(h : (a,id) t) arr =
let dict,data = B.(dict h, data h) in
let module M = (val dict) in
M.(addArray0 ~hash ~eq data arr)

39 changes: 28 additions & 11 deletions jscomp/others/bs_HashSet.mli
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@


type ('a, 'id) t0

type ('a, 'id) t =
(('a, 'id) Bs_Hash.t,
('a, 'id) t0) Bs_Bag.bag
(('a, 'id) Bs_Hash.t,
('a, 'id) t0) Bs_Bag.bag

(** The type of hash tables from type ['a] to type ['b]. *)

val create0 : int -> ('a, 'id) t0
Expand Down Expand Up @@ -62,9 +60,9 @@ val reset : ('a, 'id) t -> unit


val add0 :
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a,'id) t0 -> 'a -> unit
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a,'id) t0 -> 'a -> unit
val add : ('a, 'id) t -> 'a -> unit
(** [Hashtbl.add tbl x y] adds a binding of [x] to [y] in table [tbl].
Previous bindings for [x] are not removed, but simply
Expand All @@ -85,13 +83,13 @@ val remove0:
eq:('a,'id) Bs_Hash.eq ->
('a, 'id) t0 -> 'a -> unit
val remove:
('a, 'id) t -> 'a -> unit
('a, 'id) t -> 'a -> unit
(** [Hashtbl.remove tbl x] removes the current binding of [x] in [tbl],
restoring the previous binding if it exists.
It does nothing if [x] is not bound in [tbl]. *)




val iter0 : ('a -> unit [@bs]) -> ('a, 'id) t0 -> unit
val iter : ('a -> unit [@bs]) -> ('a, 'id) t -> unit
Expand Down Expand Up @@ -129,7 +127,7 @@ val fold : ('a -> 'c -> 'c [@bs]) -> ('a, 'id) t -> 'c -> 'c
of OCaml. For randomized hash tables, the order of enumeration
is entirely random. *)


val length0 : ('a, 'id) t0 -> int
val length : ('a, 'id) t -> int
(** [Hashtbl.length tbl] returns the number of bindings in [tbl].
Expand Down Expand Up @@ -180,3 +178,22 @@ val logStats : _ t -> unit

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

val ofArray0 :
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
'a array ->
('a, 'id) t0

val ofArray :
dict:('a,'id) Bs_Hash.t ->
'a array ->
('a,'id) t

val addArray0 :
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a,'id) t0 -> 'a array -> unit

val addArray:
('a,'id) t -> 'a array -> unit
9 changes: 8 additions & 1 deletion jscomp/others/bs_HashSetInt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ let ofArray arr =
for i = 0 to len - 1 do
add v (Bs.Array.unsafe_get arr i)
done ;
v
v

(* TOOD: optimize heuristics for resizing *)
let addArray h arr =
let len = Bs.Array.length arr in
for i = 0 to len - 1 do
add h (Bs_Array.unsafe_get arr i)
done
4 changes: 3 additions & 1 deletion jscomp/others/bs_HashSetInt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,6 @@ val logStats : t -> unit

val toArray : t -> key array

val ofArray : key array -> t
val ofArray : key array -> t

val addArray : t -> key array -> unit
9 changes: 8 additions & 1 deletion jscomp/others/bs_HashSetString.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ let ofArray arr =
for i = 0 to len - 1 do
add v (Bs.Array.unsafe_get arr i)
done ;
v
v

(* TOOD: optimize heuristics for resizing *)
let addArray h arr =
let len = Bs.Array.length arr in
for i = 0 to len - 1 do
add h (Bs_Array.unsafe_get arr i)
done
4 changes: 3 additions & 1 deletion jscomp/others/bs_HashSetString.mli
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,6 @@ val logStats : t -> unit

val toArray : t -> key array

val ofArray : key array -> t
val ofArray : key array -> t

val addArray : t -> key array -> unit
11 changes: 9 additions & 2 deletions jscomp/others/hashset.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let hash (s : key) =
final_mix (caml_hash_mix_int 0 s)
#else
[%error "unknown type"]
#endif
#endif

module N = Bs_internalSetBuckets
module C = Bs_internalBucketsType
Expand Down Expand Up @@ -152,4 +152,11 @@ let ofArray arr =
for i = 0 to len - 1 do
add v (Bs.Array.unsafe_get arr i)
done ;
v
v

(* TOOD: optimize heuristics for resizing *)
let addArray h arr =
let len = Bs.Array.length arr in
for i = 0 to len - 1 do
add h (Bs_Array.unsafe_get arr i)
done
4 changes: 3 additions & 1 deletion jscomp/others/hashset.cppo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,6 @@ val logStats : t -> unit

val toArray : t -> key array

val ofArray : key array -> t
val ofArray : key array -> t

val addArray : t -> key array -> unit
20 changes: 20 additions & 0 deletions jscomp/test/bs_hashset_int_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ var xs = Bs_SetInt.toArray(Bs_SetInt.ofArray(Bs_HashSetInt.toArray(v)));

eq("File \"bs_hashset_int_test.ml\", line 19, characters 5-12", xs, Array_data_util.range(30, 120));

var u$1 = Bs_Array.append(Array_data_util.randomRange(0, 100000), Array_data_util.randomRange(0, 100));

var v$1 = Bs_HashSetInt.create(40);

Bs_HashSetInt.addArray(v$1, u$1);

eq("File \"bs_hashset_int_test.ml\", line 25, characters 5-12", Bs_HashSetInt.length(v$1), 100001);

for(var i = 0; i <= 1000; ++i){
Bs_HashSetInt.remove(v$1, i);
}

eq("File \"bs_hashset_int_test.ml\", line 29, characters 5-12", Bs_HashSetInt.length(v$1), 99000);

for(var i$1 = 0; i$1 <= 2000; ++i$1){
Bs_HashSetInt.remove(v$1, i$1);
}

eq("File \"bs_hashset_int_test.ml\", line 33, characters 5-12", Bs_HashSetInt.length(v$1), 98000);

Mt.from_pair_suites("bs_hashset_int_test.ml", suites[0]);

var N = 0;
Expand Down
15 changes: 14 additions & 1 deletion jscomp/test/bs_hashset_int_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,18 @@ let () =
eq __LOC__ (N.length v) 91 ;
let xs = S.toArray (S.ofArray (N.toArray v)) in
eq __LOC__ xs (I.range 30 120)

let () =
let u = I.randomRange 0 100_000 ++ I.randomRange 0 100 in
let v = N.create 40 in
N.addArray v u ;
eq __LOC__ (N.length v) 100_001;
for i = 0 to 1_000 do
N.remove v i
done ;
eq __LOC__ (N.length v ) 99_000;
for i = 0 to 2_000 do
N.remove v i
done ;
eq __LOC__ (N.length v ) 98_000
let () = Mt.from_pair_suites __FILE__ !suites

34 changes: 34 additions & 0 deletions lib/js/bs_HashSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,36 @@ function mem(h, key) {
return mem0(dict[/* hash */0], dict[/* eq */1], data, key);
}

function ofArray0(hash, eq, arr) {
var len = arr.length;
var v = Bs_internalBucketsType.create0(len);
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
add0(hash, eq, v, arr[i]);
}
return v;
}

function addArray0(hash, eq, h, arr) {
var len = arr.length;
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
add0(hash, eq, h, arr[i]);
}
return /* () */0;
}

function ofArray(dict, arr) {
return {
dict: dict,
data: ofArray0(dict[/* hash */0], dict[/* eq */1], arr)
};
}

function addArray(h, arr) {
var dict = h.dict;
var data = h.data;
return addArray0(dict[/* hash */0], dict[/* eq */1], data, arr);
}

var create0 = Bs_internalBucketsType.create0;

var clear0 = Bs_internalBucketsType.clear0;
Expand Down Expand Up @@ -250,4 +280,8 @@ exports.logStats0 = logStats0;
exports.logStats = logStats;
exports.toArray0 = toArray0;
exports.toArray = toArray;
exports.ofArray0 = ofArray0;
exports.ofArray = ofArray;
exports.addArray0 = addArray0;
exports.addArray = addArray;
/* Bs_internalBucketsType Not a pure module */
9 changes: 9 additions & 0 deletions lib/js/bs_HashSetInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ function ofArray(arr) {
return v;
}

function addArray(h, arr) {
var len = arr.length;
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
add(h, arr[i]);
}
return /* () */0;
}

var create = Bs_internalBucketsType.create0;

var clear = Bs_internalBucketsType.clear0;
Expand Down Expand Up @@ -202,4 +210,5 @@ exports.length = length;
exports.logStats = logStats;
exports.toArray = toArray;
exports.ofArray = ofArray;
exports.addArray = addArray;
/* Bs_internalBucketsType Not a pure module */
9 changes: 9 additions & 0 deletions lib/js/bs_HashSetString.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ function ofArray(arr) {
return v;
}

function addArray(h, arr) {
var len = arr.length;
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
add(h, arr[i]);
}
return /* () */0;
}

var create = Bs_internalBucketsType.create0;

var clear = Bs_internalBucketsType.clear0;
Expand Down Expand Up @@ -202,4 +210,5 @@ exports.length = length;
exports.logStats = logStats;
exports.toArray = toArray;
exports.ofArray = ofArray;
exports.addArray = addArray;
/* Bs_internalBucketsType Not a pure module */