diff options
Diffstat (limited to 'lib/splay')
| -rw-r--r-- | lib/splay/dune | 2 | ||||
| -rw-r--r-- | lib/splay/splay.ml | 198 | ||||
| -rw-r--r-- | lib/splay/splay.mli | 36 | ||||
| -rw-r--r-- | lib/splay/tests/dune | 11 | ||||
| -rw-r--r-- | lib/splay/tests/splay.t | 93 |
5 files changed, 340 insertions, 0 deletions
diff --git a/lib/splay/dune b/lib/splay/dune new file mode 100644 index 0000000..34da79e --- /dev/null +++ b/lib/splay/dune @@ -0,0 +1,2 @@ +(library + (name splay)) diff --git a/lib/splay/splay.ml b/lib/splay/splay.ml new file mode 100644 index 0000000..bf0a2ac --- /dev/null +++ b/lib/splay/splay.ml @@ -0,0 +1,198 @@ +module type KEY = sig + type t + + val compare : t -> t -> int + val pp : Format.formatter -> t -> unit +end + +module Make (El : KEY) = struct + type leaf + (** Fantom type for typing the tree *) + + type node + (** Fantom type for typing the tree *) + + type ('a, 'v) branch = + | Leaf : (leaf, 'v) branch + | Node : (_, 'v) branch * (El.t * 'v) * (_, 'v) branch -> (node, 'v) branch + + type 'v t = T : ('a, 'v) branch ref -> 'v t [@@unboxed] + + let empty () = T (ref Leaf) + + let isEmpty : type a. a t -> bool = + fun (T tree) -> match !tree with Leaf -> true | _ -> false + + let rec splay : El.t -> (node, 'any) branch -> (node, 'any) branch = + fun x t -> + 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 + | Leaf -> t + | 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 + | 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 + | _ -> ( + 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 + | _ -> ( + 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 + | 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 + | _ -> ( + match rr with + | Leaf -> Node (Node (l, y, ll), z, rr) + | Node _ as rr -> + let (Node (newL, newV, newR)) = splay x rr in + Node (Node (Node (l, y, ll), z, newL), newV, newR)))) + end + + let member : type a. El.t -> a t -> bool = + fun x (T t) -> + match !t with + | Leaf -> false + | Node _ as root -> ( + let root' = splay x root in + t := root'; + let (Node (_, c', _)) = root' in + match El.compare (fst c') x with 0 -> true | _ -> false) + + let find : type a. El.t -> a t -> a option = + fun x (T t) -> + match !t with + | Leaf -> None + | Node _ as root -> ( + let root' = splay x root in + t := root'; + let (Node (_, c', _)) = root' in + match El.compare (fst c') x with 0 -> Some (snd c') | _ -> None) + + let add : type a. El.t -> a -> a t -> a t = + fun key value (T t) -> + match !t with + | Leaf -> T (ref (Node (Leaf, (key, value), Leaf))) + | Node _ as root -> ( + let root' = splay key root in + let (Node (l, y, r)) = root' in + match El.compare key (fst y) with + | 0 -> T (ref (Node (l, (key, value), r))) + | v when v < 0 -> T (ref (Node (l, (key, value), Node (Leaf, y, r)))) + | _ -> T (ref (Node (Node (l, y, Leaf), (key, value), r)))) + + let rec _subtree_maximum : type a. (a, 'any) branch -> (a, 'any) branch = + fun t -> + match t with + | Leaf -> Leaf + | Node (_, _, (Node (_, _, _) as x)) -> _subtree_maximum x + | Node (_, (key, value), Leaf) -> + ignore value; + splay key t + + let rec _subtree_minimum : type a. (a, 'any) branch -> (a, 'any) branch = + fun t -> + match t with + | Leaf -> Leaf + | Node ((Node (_, _, _) as x), _, _) -> _subtree_minimum x + | Node (Leaf, (key, value), _) -> + ignore value; + splay key t + + let remove : type a. El.t -> a t -> a t = + fun key (T t) -> + match !t with + | Leaf -> empty () + | Node _ as root -> + 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 + | Node (l, c, Leaf) -> T (ref (Node (l, c, r))) + | Node (_, _, _) -> raise Not_found + | Leaf -> ( + match _subtree_minimum r with + | Leaf -> empty () + | Node (Leaf, c, r) -> T (ref (Node (l, c, r))) + | Node (_, _, _) -> raise Not_found) + end + (* The key is not present, return the splayed tree *) + | _ -> T (ref root') + end + + (** Existencial type for the branches *) + type 'a exBranch = Branch : (_, 'a) branch -> 'a exBranch [@@unboxed] + + let fold f init (T t) = + let rec _fold : type b. + ((El.t * 'a) * 'a exBranch) list -> 'acc -> (b, 'a) branch -> 'acc = + fun acc v -> function + (* We have a node : we accumulate the right part, and process the left + branch *) + | Node (left, (key, value), right) -> + 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 + | [] -> v + | (c, Branch right) :: tl -> (_fold [@tailcall]) tl (f v c) right + end + in + _fold [] init !t + + let choose (T tree) = + match _subtree_minimum !tree with + | Leaf -> None + | Node (left, (key, value), right) -> + ignore left; + ignore right; + Some (key, value) + + 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 + 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; + incr leaf_id + | Node (l, c, r) -> + let key = fst c in + Format.fprintf formatter "%a%a%a" (repr_edge parent) key (repr' key) + l (repr' key) r + in + + match !t with + | 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 + (repr' key) r +end diff --git a/lib/splay/splay.mli b/lib/splay/splay.mli new file mode 100644 index 0000000..f484651 --- /dev/null +++ b/lib/splay/splay.mli @@ -0,0 +1,36 @@ +module type KEY = sig + type t + + val compare : t -> t -> int + val pp : Format.formatter -> t -> unit +end + +module Make (El : KEY) : sig + type 'a t + + val empty : unit -> 'a t + (** Create an empty tree *) + + val isEmpty : 'a t -> bool + + val find : El.t -> 'a t -> 'a option + (** Return the element in the tree with the given key *) + + val add : El.t -> 'a -> 'a t -> 'a t + (** Add one element in the tree, if the key is already present, the associated + element is replaced. *) + + val member : El.t -> 'a t -> bool + (** Check if the key exists *) + + val remove : El.t -> 'a t -> 'a t + val fold : ('a -> El.t * 'b -> 'a) -> 'a -> 'b t -> 'a + + val choose : 'a t -> (El.t * 'a) option + (** Return one element of the given tree, or raise Not_found if the tree is + empty. Which element is chosen is unspecified, but equal elements will be + chosen for equal trees. *) + + val pp : Format.formatter -> 'a t -> unit + (** Represent the content in dot syntax *) +end diff --git a/lib/splay/tests/dune b/lib/splay/tests/dune new file mode 100644 index 0000000..c5913da --- /dev/null +++ b/lib/splay/tests/dune @@ -0,0 +1,11 @@ +; Add the test library in the path + +(env + (_ + (binaries %{project_root}/test/tests.exe))) + +; And depend of it. The binary will be available in the +; cram tests under the name "tests" + +(cram + (deps %{bin:tests})) diff --git a/lib/splay/tests/splay.t b/lib/splay/tests/splay.t new file mode 100644 index 0000000..628ccaa --- /dev/null +++ b/lib/splay/tests/splay.t @@ -0,0 +1,93 @@ +Tests for the splay tree. The tree representation used in the tests follow the +dot syntax + +See https://en.wikipedia.org/wiki/Splay_tree + + +The root is the latest inserted node: + + $ tests -splay_tree \ + > -add 5 \ + > -add 3 + digraph G { + leaf_0 [shape=point] + "3" -> leaf_0 + "3" -> "5" + leaf_1 [shape=point] + "5" -> leaf_1 + leaf_2 [shape=point] + "5" -> leaf_2 + } + + $ tests -splay_tree \ + > -add 3 \ + > -add 5 + digraph G { + "5" -> "3" + leaf_0 [shape=point] + "3" -> leaf_0 + leaf_1 [shape=point] + "3" -> leaf_1 + leaf_2 [shape=point] + "5" -> leaf_2 + } + +Collecting an element in the tree will push this node at the root: + + $ tests -splay_tree \ + > -add 1 \ + > -add 2 \ + > -add 3 \ + > -get 2 + digraph G { + "2" -> "1" + leaf_0 [shape=point] + "1" -> leaf_0 + leaf_1 [shape=point] + "1" -> leaf_1 + "2" -> "3" + leaf_2 [shape=point] + "3" -> leaf_2 + leaf_3 [shape=point] + "3" -> leaf_3 + } + + $ tests -splay_tree \ + > -add 1 \ + > -add 2 \ + > -add 3 \ + > -add 4 \ + > -get 1 + digraph G { + leaf_0 [shape=point] + "1" -> leaf_0 + "1" -> "3" + "3" -> "2" + leaf_1 [shape=point] + "2" -> leaf_1 + leaf_2 [shape=point] + "2" -> leaf_2 + "3" -> "4" + leaf_3 [shape=point] + "4" -> leaf_3 + leaf_4 [shape=point] + "4" -> leaf_4 + } + +Removing an element from the tree modify the structure + + $ tests -splay_tree \ + > -add 1 \ + > -add 2 \ + > -add 3 \ + > -del 2 + digraph G { + leaf_0 [shape=point] + "1" -> leaf_0 + "1" -> "3" + leaf_1 [shape=point] + "3" -> leaf_1 + leaf_2 [shape=point] + "3" -> leaf_2 + } + |
