Legend:
Library
Module
Module type
Parameter
Class
Class type
Library
Module
Module type
Parameter
Class
Class type
Dll
is a potentially useful module that is used internally to manage bounded, LRU collections of items. The documentation is available in UNBOXED_COLLECTION
.
It is implemented as an abstraction over a doubly-linked list.
The implementations of Ring
and Dll
are functionally indistinguishable. However, their memory consumptions differ. On the one hand, with a Ring
, the whole structure is allocated in its entirety as soon as a single element is add
ed. Afterwards, there are no more allocations.
On the other hand, with a Dll
, cells holding the add
ed values are allocated on a by-need basis. Inserting a supernumerary element renders one single cell garbage-collectible.
In other words, Ring
allocates a bigger chunk of data in one go but is stable afterwards, whereas Dll
allocates small chunks of data one-by-one.
val create : int -> 'a t
create n
allocates a ring buffer that can hold up to n
values.
val capacity : 'a t -> int
capacity b
is the number of elements that b
can hold.
val length : 'a t -> int
length b
is the number of elements that are currently in b
.
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 remove_oldest : 'a t -> 'a option
remove_oldest b
removes and returns the oldest inserted element from the buffer b
or None
if the buffer is empty.
Note that for some collections, the removed element might still be held in memory. It will be removed eventually after other elements are added.
val remove_newest : 'a t -> 'a option
remove_newest b
removes and returns the most recently inserted element from the buffer b
or None
if the buffer is empty.
Note that for some collections, the removed element might still be held in memory. It will be removed eventually after other elements are added.
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.
val oldest_element : 'a t -> 'a option
oldest_element b
returns the oldest inserted element from the buffer b
if any.
val newest_element : 'a t -> 'a option
newest_element b
returns the oldest inserted element from the buffer b
if any.