ligo/src/lib_stdlib/ring.mli

43 lines
1.6 KiB
OCaml
Raw Normal View History

2017-01-14 13:13:19 +01:00
(**************************************************************************)
(* *)
2017-11-14 00:36:14 +01:00
(* Copyright (c) 2014 - 2017. *)
2017-01-14 13:13:19 +01:00
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
(** Imperative Ring Buffer *)
2017-11-29 13:46:36 +01:00
(** An imperative ring buffer: a mutable structure that holds at most
a fixed number of values of a same type. Values are never removed,
once the limit is reached, adding a value replaces the oldest one
in the ring buffer. *)
2017-01-14 13:13:19 +01:00
type 'a t
2017-11-29 13:46:36 +01:00
(** Allocates a ring buffer for a given number of values. *)
2017-01-14 13:13:19 +01:00
val create : int -> 'a t
2017-11-29 13:46:36 +01:00
(** Adds a value, dropping the oldest present one if full. *)
2017-01-14 13:13:19 +01:00
val add : 'a t -> 'a -> unit
2017-11-29 13:46:36 +01:00
(** Same as {!add}, but returns the dropped value if any. *)
val add_and_return_erased : 'a t -> 'a -> 'a option
(** Adds the values of a list, in order. *)
2017-01-14 13:13:19 +01:00
val add_list : 'a t -> 'a list -> unit
2017-11-29 13:46:36 +01:00
(** Retrieves the most recent value, or [None] when empty. *)
2017-01-14 13:13:19 +01:00
val last : 'a t -> 'a option
2017-11-29 13:46:36 +01:00
2017-01-14 13:13:19 +01:00
exception Empty
2017-11-29 13:46:36 +01:00
(** Same as {!last}, but raises {!Empty} when empty. *)
2017-01-14 13:13:19 +01:00
val last_exn : 'a t -> 'a
2017-11-29 13:46:36 +01:00
(** Iterates over the elements, oldest to newest. *)
2017-01-14 13:13:19 +01:00
val fold : 'a t -> init:'b -> f:('b -> 'a -> 'b) -> 'b
2017-11-29 13:46:36 +01:00
(** Retrieves the elements as a list, oldest first.. *)
2017-01-14 13:13:19 +01:00
val elements : 'a t -> 'a list