Skip to content

Commit

Permalink
Merge pull request #2415 from BuckleScript/assemble_and_share_mutable…
Browse files Browse the repository at this point in the history
…_methods

assemble and share mutable methods
  • Loading branch information
bobzhang committed Dec 30, 2017
2 parents b9cce02 + 49de845 commit 009f726
Show file tree
Hide file tree
Showing 37 changed files with 2,259 additions and 1,433 deletions.
14 changes: 10 additions & 4 deletions jscomp/others/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ js_mapperRt.cmj : js_mapperRt.cmi
bs_Array.cmj : js_math.cmj bs_Array.cmi
bs_internalAVLset.cmj : bs_Array.cmj bs.cmj
bs_internalAVLtree.cmj :
bs_internalMutableAVLSet.cmj : bs_internalAVLset.cmj
bs_SetIntM.cmj : bs_internalSetInt.cmj bs_internalAVLset.cmj bs_SetIntM.cmi
bs_Hash.cmj : bs_Hash.cmi
bs_Queue.cmj : bs_Array.cmj bs_Queue.cmi
bs_List.cmj : js_json.cmj bs_Array.cmj bs_List.cmi
Expand All @@ -42,12 +42,16 @@ bs_Bag.cmj :
bs_Cmp.cmj : bs_Cmp.cmi
bs_Map.cmj : bs_internalAVLtree.cmj bs_Cmp.cmj bs_Bag.cmj bs_Array.cmj \
bs_Map.cmi
bs_Set.cmj : bs_internalAVLset.cmj bs_Cmp.cmj bs_Bag.cmj bs_Array.cmj \
bs_Set.cmj : bs_internalAVLset.cmj bs_Cmp.cmj bs_Bag.cmj bs_Array.cmj bs.cmj \
bs_Set.cmi
bs_MapString.cmj : bs_internalAVLtree.cmj bs_Array.cmj bs_MapString.cmi
bs_MapInt.cmj : bs_internalAVLtree.cmj bs_Array.cmj bs_MapInt.cmi
bs_SetInt.cmj : bs_internalAVLset.cmj bs_Array.cmj bs_SetInt.cmi
bs_SetString.cmj : bs_internalAVLset.cmj bs_Array.cmj bs_SetString.cmi
bs_internalSetInt.cmj : bs_internalAVLset.cmj bs_Array.cmj
bs_internalSetString.cmj : bs_internalAVLset.cmj bs_Array.cmj
bs_SetInt.cmj : bs_internalSetInt.cmj bs_internalAVLset.cmj bs_SetInt.cmi
bs_SetIntM.cmj : bs_internalSetInt.cmj bs_internalAVLset.cmj bs_SetIntM.cmi
bs_SetString.cmj : bs_internalSetString.cmj bs_internalAVLset.cmj \
bs_SetString.cmi
node_child_process.cmj : node.cmj
js_boolean.cmj : js_boolean.cmi
js_math.cmj :
Expand All @@ -71,6 +75,7 @@ js_option.cmi :
js_result.cmi :
js_mapperRt.cmi :
bs_Array.cmi :
bs_SetIntM.cmi :
bs_Hash.cmi :
bs_Queue.cmi :
bs_List.cmi : js_json.cmi
Expand All @@ -86,6 +91,7 @@ bs_Set.cmi : bs_Cmp.cmi bs_Bag.cmj
bs_MapString.cmi :
bs_MapInt.cmi :
bs_SetInt.cmi :
bs_SetIntM.cmi :
bs_SetString.cmi :
js_boolean.cmi :
js_dict.cmi :
Expand Down
12 changes: 8 additions & 4 deletions jscomp/others/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string
bs_Array\
bs_internalAVLset\
bs_internalAVLtree\
bs_internalMutableAVLSet\
bs_SetIntM\
bs_internalMutableAVL\
bs_Hash\
bs_Queue\
Expand All @@ -33,7 +33,11 @@ SOURCE_LIST= node_path node_fs node_process dict node_module js_array js_string
bs_Map\
bs_Set\
bs_MapString bs_MapInt\
bs_SetInt bs_SetString\
bs_internalSetInt\
bs_internalSetString\
bs_SetInt\
bs_SetIntM\
bs_SetString\
node_child_process js_boolean js_math\
js_dict js_date js_global js_cast js_promise\
dom dom_storage\
Expand Down Expand Up @@ -93,11 +97,11 @@ bs_MapString.mli: map.cppo.mli
cppo -D TYPE_STRING $^ > $@
bs_MapInt.mli: map.cppo.mli
cppo -D TYPE_INT $^ > $@
bs_SetInt.ml: set.cppo.ml
bs_internalSetInt.ml: set.cppo.ml
cppo -D TYPE_INT $^ > $@
bs_SetInt.mli: set.cppo.mli
cppo -D TYPE_INT $^ > $@
bs_SetString.ml: set.cppo.ml
bs_internalSetString.ml: set.cppo.ml
cppo -D TYPE_STRING $^ > $@
bs_SetString.mli: set.cppo.mli
cppo -D TYPE_STRING $^ > $@
Expand Down
1 change: 1 addition & 0 deletions jscomp/others/bs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module Set = Bs_Set
module MapInt = Bs_MapInt
module MapString = Bs_MapString
module SetInt = Bs_SetInt
module SetIntM = Bs_SetIntM
module SetString = Bs_SetString
module List = Bs_List

28 changes: 19 additions & 9 deletions jscomp/others/bs_Set.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ let forAll0 = N.forAll0
let exists0 = N.exists0
let filter0 = N.filter0
let partition0 = N.partition0
let cardinal0 = N.cardinal0
let length0 = N.length0
let elements0 = N.elements0
let toArray0 = N.toArray0
(* Insertion of one element *)

let rec add0 ~cmp x (t : _ t0) : _ t0 =
let rec add0 ~cmp (t : _ t0) x : _ t0 =
match N.toOpt t with
None -> N.(return @@ node ~left:empty ~right:empty ~key:x ~h:1)
| Some nt (* Node(l, v, r, _) as t *) ->
let l,v,r = N.(left nt, key nt, right nt) in
let c = (Bs_Cmp.getCmp cmp) x v [@bs] in
if c = 0 then t else
if c < 0 then N.bal (add0 ~cmp x l) v r else N.bal l v (add0 ~cmp x r)
if c < 0 then N.bal (add0 ~cmp l x ) v r else N.bal l v (add0 ~cmp r x )


(* Splitting. split x s returns a triple (l, present, r) where
Expand Down Expand Up @@ -90,13 +90,13 @@ let rec union0 ~cmp (s1 : _ t0) (s2 : _ t0) : _ t0=
| Some n1, Some n2 (* (Node(l1, v1, r1, h1), Node(l2, v2, r2, h2)) *) ->
let h1, h2 = N.(h n1 , h n2) in
if h1 >= h2 then
if h2 = 1 then add0 ~cmp (N.key n2) s1 else begin
if h2 = 1 then add0 ~cmp s1 (N.key n2) else begin
let l1, v1, r1 = N.(left n1, key n1, right n1) in
let (l2, _, r2) = split0 ~cmp v1 s2 in
N.join (union0 ~cmp l1 l2) v1 (union0 ~cmp r1 r2)
end
else
if h1 = 1 then add0 ~cmp (N.key n1) s2 else begin
if h1 = 1 then add0 s2 ~cmp (N.key n1) else begin
let l2, v2, r2 = N.(left n2 , key n2, right n2) in
let (l1, _, r1) = split0 ~cmp v2 s1 in
N.join (union0 ~cmp l1 l2) v2 (union0 ~cmp r1 r2)
Expand Down Expand Up @@ -184,10 +184,20 @@ let rec findAssert0 ~cmp x (n : _ t0) =
let ofArray0 ~cmp (xs : _ array) : _ t0 =
let result = ref N.empty in
for i = 0 to Array.length xs - 1 do
result := add0 ~cmp (Bs_Array.unsafe_get xs i) !result
result := add0 ~cmp !result (Bs_Array.unsafe_get xs i)
done ;
!result

(* TOOD: optimize heuristics for resizing *)
let addArray0 ~cmp h arr =
let len = Bs.Array.length arr in
let v = ref empty0 in
for i = 0 to len - 1 do
let key = (Bs_Array.unsafe_get arr i) in
v := add0 !v ~cmp key
done ;
!v

let empty dict =
B.bag
~dict
Expand All @@ -207,11 +217,11 @@ let mem (type elt) (type id) e (m : (elt,id) t) =
let module M = (val dict) in
mem0 ~cmp:(M.cmp) e data

let add (type elt) (type id) e (m : (elt,id) t) =
let add (type elt) (type id) (m : (elt,id) t) e =
let dict, data = B.(dict m, data m) in
let module M = (val dict) in
B.bag ~dict
~data:(add0 ~cmp:(M.cmp) e data)
~data:(add0 ~cmp:(M.cmp) data e)

let singleton dict e =
B.bag ~dict
Expand Down Expand Up @@ -275,7 +285,7 @@ let partition f m =
let l,r = partition0 f mdata in
B.bag ~data:l ~dict, B.bag ~data:r ~dict

let cardinal m = cardinal0 (B.data m)
let length m = length0 (B.data m)

let elements m = elements0 (B.data m)
let toArray m = toArray0 (B.data m)
Expand Down
8 changes: 4 additions & 4 deletions jscomp/others/bs_Set.mli
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ val mem:

val add0:
cmp: ('elt,'id) Bs_Cmp.cmp ->
'elt -> ('elt, 'id) t0 -> ('elt, 'id) t0
('elt, 'id) t0 -> 'elt -> ('elt, 'id) t0
val add:
'elt -> ('elt, 'id) t -> ('elt, 'id) t
('elt, 'id) t -> 'elt -> ('elt, 'id) t
(** [add x s] returns a set containing all elements of [s],
plus [x]. If [x] was already in [s], [s] is returned unchanged. *)

Expand Down Expand Up @@ -130,8 +130,8 @@ val partition: ('elt -> bool [@bs]) -> ('elt, 'id) t -> ('elt, 'id) t * ('elt, '
predicate [p], and [s2] is the set of all the elements of
[s] that do not satisfy [p]. *)

val cardinal0: ('elt, 'id) t0 -> int
val cardinal:('elt, 'id) t -> int
val length0: ('elt, 'id) t0 -> int
val length: ('elt, 'id) t -> int
(** Return the number of elements of a set. *)

val elements0: ('elt, 'id) t0 -> 'elt list
Expand Down
Loading

0 comments on commit 009f726

Please sign in to comment.