Skip to content

Commit

Permalink
Merge d8e7e00 into 6f549db
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Jan 22, 2018
2 parents 6f549db + d8e7e00 commit eb8c5ec
Show file tree
Hide file tree
Showing 37 changed files with 1,233 additions and 768 deletions.
2 changes: 1 addition & 1 deletion jscomp/others/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ bs_internalBucketsType.cmi :
bs_internalSetBuckets.cmi : bs_internalBucketsType.cmi
bs_HashMap.cmi : bs_Hash.cmi
bs_HashMultiMap.cmi : bs_Hash.cmi bs_Bag.cmj
bs_HashSet.cmi : bs_Hash.cmi bs_Bag.cmj
bs_HashSet.cmi : bs_Hash.cmi
bs_HashSetString.cmi :
bs_HashSetInt.cmi :
bs_Cmp.cmi :
Expand Down
90 changes: 51 additions & 39 deletions jscomp/others/bs_HashMap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let rec replace_in_bucket ~eq key info cell =
(* if [key] already exists, replace it, otherwise add it
Here we add it to the head, it could be tail
*)
let add0 ~hash ~eq h key value =
let setDone0 ~hash ~eq h key value =
let h_buckets = C.buckets h in
let i = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
let l = Array.unsafe_get h_buckets i in
Expand Down Expand Up @@ -137,7 +137,7 @@ let rec findAux ~eq key buckets =
if (Bs_Hash.getEq eq) key (N.key cell) [@bs] then Some (N.value cell)
else findAux ~eq key (N.next cell)

let findOpt0 ~hash ~eq h key =
let get0 ~hash ~eq h key =
let h_buckets = C.buckets h in
let nid = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
match C.toOpt @@ Bs_Array.unsafe_get h_buckets nid with
Expand Down Expand Up @@ -173,7 +173,7 @@ let rec mem_in_bucket ~eq key cell =
| Some nextCell ->
mem_in_bucket ~eq key nextCell)

let mem0 ~hash ~eq h key =
let has0 ~hash ~eq h key =
let h_buckets = C.buckets h in
let nid = (Bs_Hash.getHash hash) key [@bs] land (Array.length h_buckets - 1) in
let bucket = Bs_Array.unsafe_get h_buckets nid in
Expand All @@ -186,11 +186,11 @@ let mem0 ~hash ~eq h key =
let create0 = C.create0
let clear0 = C.clear0
let reset0 = C.reset0
let length0 = C.length0
let iter0 = N.iter0
let fold0 = N.fold0
let size0 = C.length0
let forEach0 = N.iter0
let reduce0 = N.fold0
let logStats0 = N.logStats0
let filterMapInplace0 = N.filterMapInplace0
let filterMapDone0 = N.filterMapInplace0
let toArray0 = N.toArray0

(* Wrapper *)
Expand All @@ -199,73 +199,85 @@ let create dict initialize_size =
~dict
let clear h = clear0 (B.data h)
let reset h = reset0 (B.data h)
let length h = length0 (B.data h)
let iter h f = iter0 (B.data h) f
let fold h init f = fold0 (B.data h) init f
let size h = size0 (B.data h)
let forEach h f = N.iter0 (B.data h) f
let reduce h init f = N.fold0 (B.data h) init f
let logStats h = logStats0 (B.data h)

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

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

let removeDone (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 findOpt (type a) (type b) (type id) (h : (a,b,id) t) (key : a) =
let dict,data = B.(dict h, data h) in
let module M = (val dict) in
findOpt0 ~hash:M.hash ~eq:M.eq data key
let remove h key = removeDone h key; h

let get (type a) (type b) (type id) (h : (a,b,id) t) (key : a) =
let module M = (val B.dict h) in
get0 ~hash:M.hash ~eq:M.eq (B.data h) key




let mem (type a) (type b) (type id) (h : (a,b,id) t) (key : a) =
let has (type a) (type b) (type id) (h : (a,b,id) t) (key : a) =
let dict,data = B.(dict h, data h) in
let module M = (val dict) in
mem0 ~hash:M.hash ~eq:M.eq data key
has0 ~hash:M.hash ~eq:M.eq data key

let filterMapInplace h f =
filterMapInplace0 (B.data h) f
let filterMapDone h f =
filterMapDone0 (B.data h) f
let filterMap h f = filterMapDone h f ; h

let toArray (type a) (type b) (type id) (h : (a,b,id) t) =
toArray0 (B.data h)
let ofArray0 ~hash ~eq arr =
let len = Bs.Array.length arr in
let v = create0 len in
for i = 0 to len - 1 do
let key,value = (Bs.Array.unsafe_get arr i) in
add0 ~eq ~hash v key value
setDone0 ~eq ~hash v key value
done ;
v

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

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)
~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 b) (type id)
let mergeArrayDone (type a) (type b) (type id)
(h : (a,b,id) t) arr =
let dict,data = B.(dict h, data h) in
let module M = (val dict) in
M.(addArray0 ~hash ~eq data arr)

let keys0 = N.keys0
let keys h =
keys0 (B.data h)
let values0 = N.values0
let values h = N.values0 (B.data h)
let data = B.data h in
let module M = (val B.dict h) in
mergeArrayDone0 ~hash:M.hash ~eq:M.eq data arr


let mergeArray (type a) (type b) (type id)
(h : (a,b,id) t) 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 getData = B.data
let getDict = B.dict
Expand Down
Loading

0 comments on commit eb8c5ec

Please sign in to comment.