2017-05-26 14:30:34 +00:00
|
|
|
(* (c) 2017 Hannes Mehnert, all rights reserved *)
|
|
|
|
|
|
|
|
(* a ring buffer with N strings, dropping old ones *)
|
|
|
|
|
2018-10-25 14:02:04 +00:00
|
|
|
type 'a t = {
|
|
|
|
data : (Ptime.t * 'a) array ;
|
2017-05-26 14:30:34 +00:00
|
|
|
mutable write : int ;
|
|
|
|
size : int ;
|
|
|
|
}
|
|
|
|
|
2018-10-25 14:02:04 +00:00
|
|
|
let create ?(size = 1024) neutral () =
|
2019-01-27 12:02:10 +00:00
|
|
|
{ data = Array.make size (Ptime.min, neutral) ; write = 0 ; size }
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let inc t = (succ t.write) mod t.size
|
|
|
|
|
2018-10-25 14:02:04 +00:00
|
|
|
let write t entry =
|
|
|
|
Array.set t.data t.write entry ;
|
2017-05-26 14:30:34 +00:00
|
|
|
t.write <- inc t
|
|
|
|
|
|
|
|
let dec t n = (pred n + t.size) mod t.size
|
|
|
|
|
2019-10-29 19:31:25 +00:00
|
|
|
let get_valid t idx =
|
|
|
|
let our = Array.get t.data idx in
|
|
|
|
if Ptime.equal (fst our) Ptime.min then
|
|
|
|
None
|
|
|
|
else
|
|
|
|
Some our
|
|
|
|
|
2019-10-29 18:42:55 +00:00
|
|
|
let read_last t ?(tst = fun _ -> true) n =
|
|
|
|
let rec one idx count acc =
|
2019-10-29 19:31:25 +00:00
|
|
|
if count = 0 then
|
|
|
|
acc
|
|
|
|
else match get_valid t idx with
|
|
|
|
| None -> acc
|
|
|
|
| Some our ->
|
|
|
|
if tst (snd our) then
|
|
|
|
one (dec t idx) (pred count) (our :: acc)
|
|
|
|
else
|
|
|
|
one (dec t idx) count acc
|
2019-10-29 18:42:55 +00:00
|
|
|
in
|
|
|
|
one (dec t t.write) n []
|
2019-01-27 12:10:09 +00:00
|
|
|
|
2019-10-29 18:42:55 +00:00
|
|
|
let read_history t ?(tst = fun _ -> true) since =
|
2019-10-29 19:31:25 +00:00
|
|
|
let rec go idx acc =
|
|
|
|
match get_valid t idx with
|
|
|
|
| None -> acc
|
|
|
|
| Some entry ->
|
2019-10-29 18:42:55 +00:00
|
|
|
if Ptime.is_earlier (fst entry) ~than:since then
|
|
|
|
acc
|
2019-10-29 19:31:25 +00:00
|
|
|
else if tst (snd entry) then
|
|
|
|
go (dec t idx) (entry :: acc)
|
2019-10-29 18:42:55 +00:00
|
|
|
else
|
2019-10-29 19:31:25 +00:00
|
|
|
go (dec t idx) acc
|
2018-09-19 19:16:44 +00:00
|
|
|
in
|
2019-10-29 19:31:25 +00:00
|
|
|
go (dec t t.write) []
|