Skip to content

Commit a084c99

Browse files
committed
tweak -- less pattern match for mutable data structure
1 parent 3505e66 commit a084c99

File tree

3 files changed

+11
-20
lines changed

3 files changed

+11
-20
lines changed

jscomp/others/belt_internalAVLset.ml

+3-8
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,21 @@ let bal l v r =
8080
let hl = match l with None -> 0 | Some n -> n.height in
8181
let hr = match r with None -> 0 | Some n -> n.height in
8282
if hl > hr + 2 then begin
83-
(* [l] could not be None *)
8483
match l with None -> assert false
8584
| Some {left = ll; value = lv; right = lr} ->
8685
if heightGe ll lr then
8786
create ll lv (create lr v r)
8887
else begin
89-
(* [lr] could not be None*)
9088
match lr with None -> assert false
9189
| Some lr ->
9290
create (create ll lv lr.left) lr.value (create lr.right v r)
9391
end
9492
end else if hr > hl + 2 then begin
95-
(* [r] could not be None *)
9693
match r with None -> assert false
9794
| Some {left = rl; value = rv; right = rr} ->
9895
if heightGe rr rl then
9996
create (create l v rl) rv rr
10097
else begin
101-
(* [rl] could not be None *)
10298
match rl with None -> assert false
10399
| Some rl ->
104100
create (create l v rl.left) rl.value (create rl.right rv rr)
@@ -140,10 +136,9 @@ let maxUndefined n =
140136
| Some n -> Js.Undefined.return (max0Aux n)
141137

142138
let rec removeMinAuxWithRef n v =
143-
let {left = ln; right = rn; value = kn} = n in
144-
match ln with
145-
| None -> v.contents<- kn ; rn
146-
| Some ln -> bal (removeMinAuxWithRef ln v) kn rn
139+
match n.left with
140+
| None -> v.contents<- n.value ; n.right
141+
| Some ln -> bal (removeMinAuxWithRef ln v) n.value n.right
147142

148143

149144

lib/es6/belt_internalAVLset.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,12 @@ function maxUndefined(n) {
141141
}
142142

143143
function removeMinAuxWithRef(n, v) {
144-
var kn = n.value;
145144
var ln = n.left;
146-
var rn = n.right;
147145
if (ln !== undefined) {
148-
return bal(removeMinAuxWithRef(ln, v), kn, rn);
146+
return bal(removeMinAuxWithRef(ln, v), n.value, n.right);
149147
} else {
150-
v.contents = kn;
151-
return rn;
148+
v.contents = n.value;
149+
return n.right;
152150
}
153151
}
154152

@@ -374,7 +372,7 @@ function checkInvariantInternal(_v) {
374372
var r = v.right;
375373
var diff = treeHeight(l) - treeHeight(r) | 0;
376374
if (!(diff <= 2 && diff >= -2)) {
377-
throw new Error("File \"belt_internalAVLset.ml\", line 299, characters 6-12");
375+
throw new Error("File \"belt_internalAVLset.ml\", line 294, characters 6-12");
378376
}
379377
checkInvariantInternal(l);
380378
_v = r;

lib/js/belt_internalAVLset.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,12 @@ function maxUndefined(n) {
141141
}
142142

143143
function removeMinAuxWithRef(n, v) {
144-
var kn = n.value;
145144
var ln = n.left;
146-
var rn = n.right;
147145
if (ln !== undefined) {
148-
return bal(removeMinAuxWithRef(ln, v), kn, rn);
146+
return bal(removeMinAuxWithRef(ln, v), n.value, n.right);
149147
} else {
150-
v.contents = kn;
151-
return rn;
148+
v.contents = n.value;
149+
return n.right;
152150
}
153151
}
154152

@@ -374,7 +372,7 @@ function checkInvariantInternal(_v) {
374372
var r = v.right;
375373
var diff = treeHeight(l) - treeHeight(r) | 0;
376374
if (!(diff <= 2 && diff >= -2)) {
377-
throw new Error("File \"belt_internalAVLset.ml\", line 299, characters 6-12");
375+
throw new Error("File \"belt_internalAVLset.ml\", line 294, characters 6-12");
378376
}
379377
checkInvariantInternal(l);
380378
_v = r;

0 commit comments

Comments
 (0)