Skip to content

Commit

Permalink
Merge 6f922dd into ba5b84e
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Jan 26, 2018
2 parents ba5b84e + 6f922dd commit 72acc6b
Show file tree
Hide file tree
Showing 36 changed files with 906 additions and 1,176 deletions.
52 changes: 16 additions & 36 deletions jscomp/others/bs_HashMap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,24 @@ let has0 ~hash ~eq h key =
memInBucket ~eq key bucket


let create0 = C.create0
let clear0 = C.clear0
let size0 = C.size
let forEach0 = N.forEach0
let reduce0 = N.reduce0
let logStats0 = N.logStats0
let filterMapDone0 = N.filterMapInplace0
let toArray0 = N.toArray0


let create initialize_size ~dict =
B.bag ~data:(create0 initialize_size) ~dict
let clear h = clear0 (B.data h)


let make initialize_size ~dict =
B.bag ~data:(C.create0 initialize_size) ~dict
let clear h = C.clear0 (B.data h)
let size h = C.size (B.data h)
let forEach h f = N.forEach0 (B.data h) f
let reduce h init f = N.reduce0 (B.data h) init f
let logStats h = logStats0 (B.data h)
let logStats h = N.logStats0 (B.data h)

let setDone (type a) (type id) (h : (a,_,id) t) (key:a) info =
let set (type a) (type id) (h : (a,_,id) t) (key:a) info =
let module M = (val B.dict h) in
setDone0 ~hash:M.hash ~eq:M.eq (B.data h) key info

let set h key info = setDone h key info; h

let removeDone (type a) (type id) (h : (a,_,id) t) (key : a) =
let remove (type a) (type id) (h : (a,_,id) t) (key : a) =
let module M = (val B.dict h) in
remove0 ~hash:M.hash ~eq:M.eq (B.data h) key

let remove h key = removeDone h key; h

let get (type a) (type id) (h : (a, _, id) t) (key : a) =
let module M = (val B.dict h) in
get0 ~hash:M.hash ~eq:M.eq (B.data h) key
Expand All @@ -212,15 +200,14 @@ let has (type a) (type id) (h : (a,_,id) t) (key : a) =
let module M = (val B.dict h) in
has0 ~hash:M.hash ~eq:M.eq (B.data h) key

let filterMapDone h f = filterMapDone0 (B.data h) f
let keepMapInPlace h f = N.filterMapInplace0 (B.data h) f

let filterMap h f = filterMapDone h f; h

let toArray h = toArray0 (B.data h)
let toArray h = N.toArray0 (B.data h)

let ofArray0 arr ~hash ~eq =
let len = A.length arr in
let v = create0 len in
let v = C.create0 len in
for i = 0 to len - 1 do
let key,value = (A.getUnsafe arr i) in
setDone0 ~eq ~hash v key value
Expand All @@ -234,28 +221,21 @@ let mergeArrayDone0 h arr ~hash ~eq =
let key,value = (A.getUnsafe arr i) in
setDone0 h ~eq ~hash key value
done

let mergeArray0 h arr ~hash ~eq = mergeArrayDone0 h arr ~hash ~eq; h


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

let mergeArrayDone (type a) (type id) (h : (a,_,id) t) arr =
let mergeMany (type a) (type id) (h : (a,_,id) t) arr =
let module M = (val B.dict h) in
mergeArrayDone0 ~hash:M.hash ~eq:M.eq (B.data h) arr

let mergeArray h arr =
mergeArrayDone h arr;
h

let copy h = B.bag ~dict:(B.dict h) ~data:(N.copy (B.data h))

let keysToArray0 = N.keys0
let keysToArray h = N.keys0 (B.data h)
let valuesToArray0 = N.values0

let valuesToArray h = N.values0 (B.data h)
let getBucketHistogram h = N.getBucketHistogram (B.data h)
let getData = B.data
let getDict = B.dict
let packDictData = B.bag

let isEmpty h = C.size (B.data h) = 0
130 changes: 27 additions & 103 deletions jscomp/others/bs_HashMap.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,41 @@



type ('a,'b,'id) t

(** The type of hash tables from type ['a] to type ['b]. *)
type ('key,'value,'id) t
(** The type of hash tables from type ['key] to type ['value]. *)

val create: int -> dict:('a,'id) Bs_Hash.t -> ('a,'b,'id) t
val make: int -> dict:('key, 'id) Bs_Hash.t -> ('key,'value,'id) t
(*TODO: allow randomization for security *)

val clear: ('a, 'b, 'id) t -> unit
(** Empty a hash table. Use [reset] instead of [clear] to shrink the
size of the bucket table to its initial size. *)

val clear: ('key, 'value, 'id ) t -> unit
(** Empty a hash table. *)

val isEmpty: _ t -> bool

val setDone: ('a, 'b, 'id) t -> 'a -> 'b -> unit
(** [setDone tbl k v] if [k] does not exist,
val set: ('key, 'value, 'id ) t -> 'key -> 'value -> unit
(** [set tbl k v] if [k] does not exist,
add the binding [k,v], otherwise, update the old value with the new
[v]
*)

val set: ('a, 'b, 'id) t -> 'a -> 'b -> ('a, 'b, 'id) t
(** [set tbl k v]
*)
val copy: ('a, 'b, 'id) t -> ('a, 'b, 'id) t
val copy: ('key, 'value, 'id ) t -> ('key, 'value, 'id ) t

val get:
('a, 'b, 'id) t -> 'a -> 'b option
val get: ('key, 'value, 'id ) t -> 'key -> 'value option


val has: ('a, 'b, 'id) t -> 'a -> bool
val has: ('key, 'value, 'id ) t -> 'key -> bool
(** [has tbl x] checks if [x] is bound in [tbl]. *)

val removeDone:
('a, 'b, 'id) t -> 'a ->
unit
val remove:
('a, 'b, 'id) t -> 'a ->
('a, 'b, 'id) t
val remove: ('key, 'value, 'id ) t -> 'key -> unit

val forEach: ('a, 'b, 'id) t -> ('a -> 'b -> unit [@bs]) -> unit
val forEach: ('key, 'value, 'id ) t -> ('key -> 'value -> unit [@bs]) -> unit
(** [forEach tbl f] applies [f] to all bindings in table [tbl].
[f] receives the key as first argument, and the associated value
as second argument. Each binding is presented exactly once to [f].
If the hash table was created in non-randomized mode, the order
in which the bindings are enumerated is reproducible between
successive runs of the program. *)
*)


val reduce: ('a, 'b, 'id) t -> 'c -> ('c -> 'a -> 'b -> 'c [@bs]) -> 'c
val reduce: ('key, 'value, 'id ) t -> 'c -> ('c -> 'key -> 'value -> 'c [@bs]) -> 'c
(** [reduce tbl init f] computes
[(f kN dN ... (f k1 d1 init)...)],
where [k1 ... kN] are the keys of all bindings in [tbl],
Expand All @@ -84,19 +70,13 @@ val reduce: ('a, 'b, 'id) t -> 'c -> ('c -> 'a -> 'b -> 'c [@bs]) -> 'c
However, if the table contains several bindings for the same key,
they are passed to [f] in reverse order of introduction, that is,
the most recent binding is passed first.
If the hash table was created in non-randomized mode, the order
in which the bindings are enumerated is reproducible between
successive runs of the program, and even between minor versions
of OCaml. For randomized hash tables, the order of enumeration
is entirely random. *)
*)


val filterMapDone: ('a, 'b, 'id) t -> ('a -> 'b -> 'b option [@bs]) -> unit
val filterMap: ('a, 'b, 'id) t -> ('a -> 'b -> 'b option [@bs]) -> ('a, 'b, 'id) t
val keepMapInPlace: ('key, 'value, 'id ) t -> ('key -> 'value -> 'value option [@bs]) -> unit


val size: ('a, 'b, 'id) t -> int
val size: _ t -> int
(** [size tbl] returns the number of bindings in [tbl].
It takes constant time. *)

Expand All @@ -111,79 +91,23 @@ val logStats: _ t -> unit



val toArray: ('a, 'b, 'id) t -> ('a * 'b) array
val toArray: ('key, 'value, 'id ) t -> ('key * 'value) array




val ofArray:
('a * 'b) array ->
dict:('a,'id) Bs_Hash.t ->
('a, 'b, 'id) t
val mergeArrayDone: ('a, 'b, 'id) t -> ('a * 'b) array -> unit
val mergeArray: ('a, 'b, 'id) t -> ('a * 'b) array -> ('a, 'b, 'id) t
val ofArray: ('key * 'value) array -> dict:('key,'id) Bs_Hash.t -> ('key, 'value, 'id ) t

val mergeMany: ('key, 'value, 'id ) t -> ('key * 'value) array -> unit

val keysToArray:
('a,'b,'id) t -> 'a array
('key, _, _) t -> 'key array
val valuesToArray:
('a,'b,'id) t -> 'b array
(_,'value,_) t -> 'value array

val getBucketHistogram: _ t -> int array
(****************************************************************************)

type ('a, 'b, 'id) t0
val getData: ('k,'v,'id) t -> ('k,'v,'id) t0
val getDict: ('k,'v,'id) t -> ('k,'id) Bs_Hash.t
val packDictData: dict:('k, 'id) Bs_Hash.t -> data:('k, 'v, 'id) t0 -> ('k, 'v, 'id) t
val create0: int -> ('a, 'b, 'id) t0
val clear0: ('a, 'b, 'id) t0 -> unit
val setDone0 :
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a,'b,'id) t0 -> 'a ->
'b -> unit
val get0:
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a, 'b, 'id) t0 ->
'a ->
'b option

val has0:
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a, 'b, 'id) t0 ->
'a ->
bool

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

val forEach0: ('a, 'b, 'id) t0 -> ('a -> 'b -> unit [@bs]) -> unit
val reduce0: ('a, 'b, 'id) t0 -> 'c -> ('c -> 'a -> 'b -> 'c [@bs]) -> 'c
val filterMapDone0: ('a, 'b, 'id) t0 -> ('a -> 'b -> 'b option [@bs]) -> unit
val size0: ('a, 'b, 'id) t0 -> int
val logStats0: ('a, 'b, 'id) t0 -> unit
val toArray0: ('a, 'b, 'id) t0 -> ('a * 'b) array
val ofArray0:
('a * 'b) array ->
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a, 'b, 'id) t0
val mergeArray0:
('a, 'b, 'id) t0 ->
('a * 'b) array ->
hash:('a,'id) Bs_Hash.hash ->
eq:('a,'id) Bs_Hash.eq ->
('a, 'b, 'id) t0

val keysToArray0:
('a,'b,'id) t0 -> 'a array
val valuesToArray0:
('a,'b,'id) t0 -> 'b array





35 changes: 17 additions & 18 deletions jscomp/others/bs_HashMapInt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let rec replaceInBucket (key : key) info cell =
| Some cell ->
replaceInBucket key info cell

let setDone h (key : key) value =
let set h (key : key) value =
let h_buckets = C.buckets h in
let i = hash key land (Array.length h_buckets - 1) in
let l = Array.unsafe_get h_buckets i in
Expand All @@ -89,14 +89,14 @@ let setDone h (key : key) value =
if C.size h > Array.length (C.buckets h) lsl 1 then resize h
| Some bucket ->
begin
if replaceInBucket key value bucket then begin
A.setUnsafe h_buckets i (C.return (N.bucket ~key ~value ~next:l));
C.sizeSet h (C.size h + 1);
if C.size h > Array.length (C.buckets h) lsl 1 then resize h
end
if replaceInBucket key value bucket then begin
A.setUnsafe h_buckets i (C.return (N.bucket ~key ~value ~next:l));
C.sizeSet h (C.size h + 1);
if C.size h > Array.length (C.buckets h) lsl 1 then resize h
end
end

let set h key value = setDone h key value ; h


let rec removeInBucket h h_buckets i (key : key) prec buckets =
match C.toOpt buckets with
Expand All @@ -111,7 +111,7 @@ let rec removeInBucket h h_buckets i (key : key) prec buckets =
end
else removeInBucket h h_buckets i key cell cell_next

let removeDone h key =
let remove h key =
let h_buckets = C.buckets h in
let i = hash key land (Array.length h_buckets - 1) in
let bucket = (A.getUnsafe h_buckets i) in
Expand All @@ -126,7 +126,7 @@ let removeDone h key =
else
removeInBucket h h_buckets i key cell (N.next cell)

let remove h key = removeDone h key; h


let rec findAux (key : key) buckets =
match C.toOpt buckets with
Expand Down Expand Up @@ -173,37 +173,36 @@ let has h key =
memInBucket key bucket


let create = C.create0
let make = C.create0
let clear = C.clear0
let size = C.size
let forEach = N.forEach0
let reduce = N.reduce0
let logStats = N.logStats0
let filterMapDone = N.filterMapInplace0
let filterMap h f = filterMapDone h f; h
let keepMapInPlace = N.filterMapInplace0
let toArray = N.toArray0

let ofArray arr =
let len = A.length arr in
let v = create len in
let v = make len in
for i = 0 to len - 1 do
let k,value = (A.getUnsafe arr i) in
setDone v k value
set v k value
done ;
v

(* TOOD: optimize heuristics for resizing *)
let mergeArrayDone h arr =
let mergeMany h arr =
let len = A.length arr in
for i = 0 to len - 1 do
let k,v = (A.getUnsafe arr i) in
setDone h k v
set h k v
done

let mergeArray h arr = mergeArrayDone h arr; h

let copy = N.copy

let keysToArray = N.keys0
let valuesToArray = N.values0
let getBucketHistogram = N.getBucketHistogram

let isEmpty h = C.size h = 0
Loading

0 comments on commit 72acc6b

Please sign in to comment.