From 69f182d0c20af511e0edd363790f4f2185839bf6 Mon Sep 17 00:00:00 2001 From: tali Date: Thu, 1 Feb 2024 19:00:59 -0500 Subject: [PATCH] add Dllist.iter_l, reset --- lib/data/dllist.ml | 15 +++++++++++++++ lib/data/dllist.mli | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/lib/data/dllist.ml b/lib/data/dllist.ml index 7941f83..cdb3152 100644 --- a/lib/data/dllist.ml +++ b/lib/data/dllist.ml @@ -25,6 +25,12 @@ let create () = let is_empty seq = seq.next == seq +let reset seq = + begin + seq.next <- seq; + seq.prev <- seq; + end + let remove node = if node.node_active then begin node.node_active <- false; @@ -74,6 +80,15 @@ let fold_r f seq acc = in loop seq.prev acc +let iter_l f seq = + let rec loop curr = + if curr != seq then + let node = node_of_seq curr in + if node.node_active then f node.node_data; + loop node.node_next + in + loop seq.next + let find f seq = let rec loop curr = if curr == seq then diff --git a/lib/data/dllist.mli b/lib/data/dllist.mli index a88ff8d..59b2485 100644 --- a/lib/data/dllist.mli +++ b/lib/data/dllist.mli @@ -21,6 +21,9 @@ val create : unit -> 'a t val is_empty : 'a t -> bool (** Returns [true] iff the given sequence is empty *) +val reset : 'a t -> unit + (** [reset ()] is a lazy way to remove all the elements from the sequence *) + val add_l : 'a -> 'a t -> 'a node (** [add_l x s] adds [x] to the left of the sequence [s] *) @@ -57,6 +60,10 @@ val fold_r : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b where [e1], [e2], ..., [en] are the elements of [s] *) +val iter_l : ('a -> unit) -> 'a t -> unit + (** [iter_l f s] applies [f] on all elements of [s] starting from + the left *) + val find : ('a -> bool) -> 'a t -> 'a (** [find_node_l f s] returns the first element of [s] starting from the left that satisfies [f] or raises [Not_found] if none exists. *)