File tree Expand file tree Collapse file tree 1 file changed +31
-11
lines changed Expand file tree Collapse file tree 1 file changed +31
-11
lines changed Original file line number Diff line number Diff line change @@ -1092,10 +1092,11 @@ struct
1092
1092
| [] , _ -> size, ys
1093
1093
| _ , [] -> size, xs
1094
1094
| x :: tx , y :: ty ->
1095
- if Ord. compare x y = 0
1095
+ let comp = Ord. compare x y in
1096
+ if comp = 0
1096
1097
then let s, rs = merge (pred size) tx ty in
1097
1098
s, x :: rs
1098
- else if Ord. compare x y < 0
1099
+ else if comp < 0
1099
1100
then let s, rs = merge size tx ys in
1100
1101
s, x :: rs
1101
1102
else let s, rs = merge size xs ty in
@@ -1253,19 +1254,38 @@ struct
1253
1254
s) (1 , [] ) s
1254
1255
in treeify n l
1255
1256
1257
+ let rec distinct size xs ys =
1258
+ match xs, ys with
1259
+ [] , _ -> size, []
1260
+ | _ , [] -> size, []
1261
+ | x :: tx , y :: ty ->
1262
+ let comp = Ord. compare x y in
1263
+ if comp = 0
1264
+ then let s, rs = distinct (succ size) tx ty in
1265
+ s, x :: rs
1266
+ else if comp < 0
1267
+ then distinct size tx ys
1268
+ else distinct size xs ty
1269
+
1256
1270
let inter s1 s2 =
1257
1271
let size1 = cardinality s1 in
1258
1272
let size2 = cardinality s2 in
1259
- let s1, s2 =
1260
- if size1 < size2 then
1261
- s1, s2
1273
+ let comp = compare_height size1 size2 in
1274
+ let n, l =
1275
+ if comp = 0 then
1276
+ distinct 1 (to_list s1) (to_list s2)
1262
1277
else
1263
- s2, s1 in
1264
- let n, l = fold_r (fun s3 x ->
1265
- if mem s2 x then
1266
- add_item x s3
1267
- else
1268
- s3) (1 , [] ) s1
1278
+ let s1, s2 =
1279
+ if comp < 0 then
1280
+ s1, s2
1281
+ else
1282
+ s2, s1
1283
+ in
1284
+ fold_r (fun s3 x ->
1285
+ if mem s2 x then
1286
+ add_item x s3
1287
+ else
1288
+ s3) (1 , [] ) s1
1269
1289
in treeify n l
1270
1290
1271
1291
let partition pred s =
You can’t perform that action at this time.
0 commit comments