summaryrefslogtreecommitdiff
path: root/lib/splay/splay.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-04-30 09:16:03 +0200
committerSébastien Dailly <sebastien@dailly.me>2026-05-06 16:45:25 +0200
commit960d5b8f2728f71dd9ce038d527844d82ca2f0f2 (patch)
tree9a15dc43f91a0b58a3aaa10223bba35abf7ef5f2 /lib/splay/splay.ml
parenta5efd7d15b4d049a4ed539ed5d9c2f1037d00973 (diff)
Allow updates for existing contents
Diffstat (limited to 'lib/splay/splay.ml')
-rw-r--r--lib/splay/splay.ml44
1 files changed, 22 insertions, 22 deletions
diff --git a/lib/splay/splay.ml b/lib/splay/splay.ml
index bf0a2ac..979d017 100644
--- a/lib/splay/splay.ml
+++ b/lib/splay/splay.ml
@@ -28,40 +28,40 @@ module Make (El : KEY) = struct
let (Node (l, y, r)) = t in
begin match El.compare x (fst y) with
| 0 -> t
- | v when v < 0 -> begin
- match l with
+ | v when v < 0 ->
+ begin match l with
| Leaf -> t
- | Node (ll, z, rr) -> begin
- match El.compare x (fst z) with
+ | Node (ll, z, rr) ->
+ begin match El.compare x (fst z) with
| 0 -> Node (ll, z, Node (rr, y, r))
- | v when v < 0 -> begin
- match ll with
+ | v when v < 0 ->
+ begin match ll with
| Leaf -> Node (ll, z, Node (rr, y, r))
| Node _ as ll ->
let (Node (newL, newV, newR)) = splay x ll in
Node (newL, newV, Node (newR, z, Node (rr, y, r)))
- end
+ end
| _ -> (
match rr with
| Leaf -> Node (ll, z, Node (rr, y, r))
| Node _ as rr ->
let (Node (newL, newV, newR)) = splay x rr in
Node (Node (ll, z, newL), newV, Node (newR, y, r)))
- end
- end
+ end
+ end
| _ -> (
match r with
| Leaf -> t
| Node (ll, z, rr) -> (
match El.compare x (fst z) with
| 0 -> Node (Node (l, y, ll), z, rr)
- | v when v < 0 -> begin
- match ll with
+ | v when v < 0 ->
+ begin match ll with
| Leaf -> Node (Node (l, y, ll), z, rr)
| Node _ as ll ->
let (Node (newL, newV, newR)) = splay x ll in
Node (Node (l, y, newL), newV, Node (newR, z, rr))
- end
+ end
| _ -> (
match rr with
| Leaf -> Node (Node (l, y, ll), z, rr)
@@ -128,8 +128,8 @@ module Make (El : KEY) = struct
let root' = splay key root in
let (Node (l, c', r)) = root' in
begin match El.compare (fst c') key with
- | 0 -> begin
- match _subtree_maximum l with
+ | 0 ->
+ begin match _subtree_maximum l with
| Node (l, c, Leaf) -> T (ref (Node (l, c, r)))
| Node (_, _, _) -> raise Not_found
| Leaf -> (
@@ -137,7 +137,7 @@ module Make (El : KEY) = struct
| Leaf -> empty ()
| Node (Leaf, c, r) -> T (ref (Node (l, c, r)))
| Node (_, _, _) -> raise Not_found)
- end
+ end
(* The key is not present, return the splayed tree *)
| _ -> T (ref root')
end
@@ -155,11 +155,11 @@ module Make (El : KEY) = struct
let c = (key, value) in
(_fold [@tailcall]) ((c, Branch right) :: acc) v left
(* We have nothing left, we process the values delayed *)
- | Leaf -> begin
- match acc with
+ | Leaf ->
+ begin match acc with
| [] -> v
| (c, Branch right) :: tl -> (_fold [@tailcall]) tl (f v c) right
- end
+ end
in
_fold [] init !t
@@ -174,14 +174,14 @@ module Make (El : KEY) = struct
let pp formatter (T t) =
let leaf_id = ref 0 in
let repr_edge from formatter dest =
- Format.fprintf formatter "\"%a\" -> \"%a\"@;" El.pp from El.pp dest
+ Format.fprintf formatter "@,\"%a\" -> \"%a\"" El.pp from El.pp dest
in
let rec repr' : type b. El.t -> Format.formatter -> (b, _) branch -> unit =
fun parent formatter -> function
| Leaf ->
- Format.fprintf formatter "leaf_%d [shape=point]@;" !leaf_id;
- Format.fprintf formatter "\"%a\" -> leaf_%d@;" El.pp parent !leaf_id;
+ Format.fprintf formatter "@,leaf_%d [shape=point]" !leaf_id;
+ Format.fprintf formatter "@,\"%a\" -> leaf_%d" El.pp parent !leaf_id;
incr leaf_id
| Node (l, c, r) ->
let key = fst c in
@@ -193,6 +193,6 @@ module Make (El : KEY) = struct
| Leaf -> Format.fprintf formatter "digraph G {}"
| Node (l, c, r) ->
let key = fst c in
- Format.fprintf formatter "@[<v 2>digraph G {@;%a%a@]}" (repr' key) l
+ Format.fprintf formatter "@[<v 2>digraph G {%a%a@]@,}" (repr' key) l
(repr' key) r
end