22
22
* You should have received a copy of the GNU Lesser General Public License
23
23
* along with this program; if not, write to the Free Software
24
24
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
25
-
26
-
25
+ [@@@ bs.config {flags = [|" -bs-noassertfalse" |] }]
27
26
type 'value node = {
28
27
mutable value : 'value ;
29
28
mutable height : int ;
@@ -36,9 +35,6 @@ and 'value t = 'value node option
36
35
module A = Belt_Array
37
36
module S = Belt_SortArray
38
37
39
-
40
- external unsafeCoerce : 'a option -> 'a = " %identity"
41
-
42
38
type ('a, 'b) cmp = ('a , 'b ) Belt_Id .cmp
43
39
44
40
(* Sets are represented by balanced binary trees (the heights of the
@@ -85,22 +81,26 @@ let bal l v r =
85
81
let hr = match r with None -> 0 | Some n -> n.height in
86
82
if hl > hr + 2 then begin
87
83
(* [l] could not be None *)
88
- let {left = ll; value = lv; right = lr} = l |. unsafeCoerce in
84
+ match l with None -> assert false
85
+ | Some {left = ll ; value = lv ; right = lr } ->
89
86
if heightGe ll lr then
90
87
create ll lv (create lr v r)
91
88
else begin
92
89
(* [lr] could not be None*)
93
- let lr = lr |. unsafeCoerce in
90
+ match lr with None -> assert false
91
+ | Some lr ->
94
92
create (create ll lv lr.left) lr.value (create lr.right v r)
95
93
end
96
94
end else if hr > hl + 2 then begin
97
95
(* [r] could not be None *)
98
- let {left = rl; value = rv; right = rr} = r |. unsafeCoerce in
96
+ match r with None -> assert false
97
+ | Some {left = rl ; value = rv ; right = rr } ->
99
98
if heightGe rr rl then
100
99
create (create l v rl) rv rr
101
100
else begin
102
101
(* [rl] could not be None *)
103
- let rl = rl |. unsafeCoerce in
102
+ match rl with None -> assert false
103
+ | Some rl ->
104
104
create (create l v rl.left) rl.value (create rl.right rv rr)
105
105
end
106
106
end else
@@ -553,20 +553,23 @@ let rec getExn (n : _ t) x ~cmp =
553
553
L rotation, Some root node
554
554
*)
555
555
let rotateWithLeftChild k2 =
556
- let k1 = k2 .left|. unsafeCoerce in
557
- k2 .left < - (k1 .right);
558
- k1 .right < - Some k2 ;
559
- let hlk2, hrk2 = k2 .left|. treeHeight , k2 .right |. treeHeight in
560
- k2 .height < - (Pervasives. max hlk2 hrk2 + 1 );
561
- let hlk1, hk2 = k1 .left|. treeHeight , k2 .height in
562
- k1 .height < - (Pervasives. max hlk1 hk2 + 1 );
563
- k1
556
+ match k2 .left with
557
+ | None -> assert false
558
+ | Some k1 ->
559
+ k2 .left < - k1 .right;
560
+ k1 .right < - Some k2 ;
561
+ let hlk2, hrk2 = k2 .left|. treeHeight , k2 .right |. treeHeight in
562
+ k2 .height < - (Pervasives. max hlk2 hrk2 + 1 );
563
+ let hlk1, hk2 = k1 .left|. treeHeight , k2 .height in
564
+ k1 .height < - (Pervasives. max hlk1 hk2 + 1 );
565
+ k1
564
566
(* right rotation *)
565
567
let rotateWithRightChild k1 =
566
- let k2 = k1 .right |. unsafeCoerce in
567
- k1 .right < - (k2 .left);
568
+ match k1 .right with None -> assert false
569
+ | Some k2 ->
570
+ k1 .right < - k2 .left;
568
571
k2 .left < - Some k1;
569
- let hlk1, hrk1 = k1 .left|. treeHeight, k1 .right |. treeHeight in
572
+ let hlk1, hrk1 = k1.left |. treeHeight, k1 .right |. treeHeight in
570
573
k1 .height < - (Pervasives. max hlk1 hrk1 + 1 );
571
574
let hrk2, hk1 = k2 .right |. treeHeight, k1 .height in
572
575
k2 .height < - (Pervasives. max hrk2 hk1 + 1 );
@@ -576,15 +579,21 @@ let rotateWithRightChild k1 =
576
579
double l rotation
577
580
*)
578
581
let doubleWithLeftChild k3 =
579
- let v = k3 .left|. unsafeCoerce |. rotateWithRightChild |. Some in
580
- k3 .left < - v;
581
- k3 |. rotateWithLeftChild
582
- (* * *)
582
+ match k3.left with
583
+ | None -> assert false
584
+ | Some k3l ->
585
+ let v = k3l |. rotateWithRightChild |. Some in
586
+ k3 .left < - v;
587
+ k3 |. rotateWithLeftChild
588
+ (* * *)
583
589
584
590
let doubleWithRightChild k2 =
585
- let v = k2 .right |. unsafeCoerce |. rotateWithLeftChild |. Some in
586
- k2 .right < - v;
587
- rotateWithRightChild k2
591
+ match k2.right with
592
+ | None -> assert false
593
+ | Some k2r ->
594
+ let v = k2r |. rotateWithLeftChild |. Some in
595
+ k2 .right < - v;
596
+ rotateWithRightChild k2
588
597
589
598
let heightUpdateMutate t =
590
599
let hlt, hrt = t .left|. treeHeight, t .right |. treeHeight in
@@ -595,15 +604,17 @@ let balMutate nt =
595
604
let {left = l; right = r} = nt in
596
605
let hl, hr = (treeHeight l, treeHeight r) in
597
606
if hl > 2 + hr then
598
- let {left = ll; right = lr} = l |. unsafeCoerce in
607
+ match l with None -> assert false
608
+ | Some {left = ll ; right = lr } ->
599
609
(if heightGe ll lr then
600
610
heightUpdateMutate (rotateWithLeftChild nt)
601
611
else
602
612
heightUpdateMutate (doubleWithLeftChild nt)
603
613
)
604
614
else
605
615
if hr > 2 + hl then
606
- let {left = rl; right = rr} = r |. unsafeCoerce in
616
+ match r with None -> assert false
617
+ | Some {left = rl ; right = rr } ->
607
618
(if heightGe rr rl then
608
619
heightUpdateMutate (rotateWithRightChild nt)
609
620
else
0 commit comments