add Wheel.iter for arbitrary order iteration

This commit is contained in:
tali 2024-02-01 19:01:09 -05:00
parent 69f182d0c2
commit 04478ffa0e
2 changed files with 10 additions and 0 deletions

View File

@ -25,6 +25,11 @@ let%expect_test _ =
Wheel.add wh 6 |> ignore; Wheel.add wh 6 |> ignore;
(* t=3 *) (* t=3 *)
print_ints_nl (Wheel.tick wh); [%expect {| [] |}]; print_ints_nl (Wheel.tick wh); [%expect {| [] |}];
let every = ref [] in
Wheel.iter (fun x -> every := x :: !every) wh;
print_ints_nl (List.sort compare !every); [%expect {| [1;2;3;4;5;6] |}];
(* t=0 *) (* t=0 *)
print_ints_nl (Wheel.tick wh); [%expect {| [1;2;3] |}]; print_ints_nl (Wheel.tick wh); [%expect {| [1;2;3] |}];
(* t=1 *) (* t=1 *)

View File

@ -22,3 +22,8 @@ let[@tail_mod_cons] rec empty t =
let tick t = let tick t =
t.index <- (t.index + 1) mod Array.length t.entries; t.index <- (t.index + 1) mod Array.length t.entries;
empty t empty t
let iter f t =
for i = 0 to Array.length t.entries - 1 do
Dllist.iter_l f t.entries.(i)
done