package ringo

  1. Overview
  2. Docs

A mutable structure that holds at most a fixed number of values of a same type. Values are not removed by hand, instead, once the limit is reached, adding a value replaces the oldest one in the buffer.

type 'a t

The type of bounded-size buffers.

val create : int -> 'a t

create n allocates a ring buffer that can hold up to n values.

  • raises [Invalid_argument]

    if n is 0 or less.

val capacity : 'a t -> int

capacity b is the number of elements that b can hold.

val add : 'a t -> 'a -> unit

add b v adds the value v to the buffer b. If the buffer b already has capacity b values, the oldest of its values is dropped.

val add_and_return_erased : 'a t -> 'a -> 'a option

add_and_return_erased b v has the same effect as add b v but it returns the dropped value when applicable.

val add_list : 'a t -> 'a list -> unit

add_list b vs adds each element of the list vs in the order they appear in the list. Note that if List.length vs > capacity b, then only the last capacity b elements of the list remain in b at the end.

val clear : 'a t -> unit

clear b removes all values from the buffer b.

val fold : 'a t -> init:'b -> f:('b -> 'a -> 'b) -> 'b

fold b ~init ~f folds over the value of the buffer b, newest to oldest.

val fold_oldest_first : 'a t -> init:'b -> f:('b -> 'a -> 'b) -> 'b

fold_oldest_first b ~init ~f folds over the value of the buffer b, oldest to newest.

val elements : 'a t -> 'a list

elements b is a list that contains the same elements as the buffer b, oldest first, newest last.

OCaml

Innovation. Community. Security.