Legend:
Library
Module
Module type
Parameter
Class
Class type
Library
Module
Module type
Parameter
Class
Class type
node
s are boxes that contain data. Boxes are never meant to be returned to the end user (they can be unsafe), they are meant to build some abstraction on top of the buffer.
In order to make the module safe and remove all notion of box, use the functor Misc.Unbox
.
val data : 'a node -> 'a
data n
is the value contained in the node n
.
val create : int -> 'a t
create n
allocates a buffer that can hold up to n
elements.
val length : 'a t -> int
length b
is the number of elements that are currently in b
.
val clear : 'a t -> unit
clear b
removes all values from the buffer b
.
fold b ~init ~f
folds over the value of the buffer b
, newest to oldest.
fold_oldest_first b ~init ~f
folds over the value of the buffer b
, oldest to newest.
elements b
is a list of nodes from b
. They appear oldest first, newest last.
val elements_data : 'a t -> 'a list
elements_data b
is a list of the data content of the nodes of b
. It is equivalent to List.map data (elements b)
but with better performance.
oldest_element b
returns the oldest inserted element from the buffer b
if any.
newest_element b
returns the oldest inserted element from the buffer b
if any.
remove b n
removes the node n
from the buffer b
.
The behavior of this function is undefined if n
is not part of b
, i.e., if List.exists ((==) n) (elements b)
is false
.
It is the responsibility of the user of this library (presumably, another library wrapping the primitives of this one) to ensure this is never the case.
remove_oldest b
removes and returns the oldest inserted element from the buffer b
or None
if the buffer is empty.
remove_newest b
removes and returns the most recently inserted element from the buffer b
or None
if the buffer is empty.
promote b n
places the node n
to the front of the buffer b
, making the node n
the newest of the nodes of the buffer b
.
promote b n
is similar to remove b n; ignore (add b @@ data n)
except that: it is more efficient, and it keeps the value data n
in the same node it was originally inserted in.
The behavior of this function is undefined if n
is not part of b
, i.e., if List.exists ((==) n) (elements b)
is false
.
val capacity : 'a t -> int
capacity b
is the maximum weight that b
can hold.
val weight : 'a node -> int
weight e
returns the weight of the element e
.
val total_weight : 'a t -> int
total_weight b
is the summed weight of all elements that are currently in b
.
add b (v, w)
adds the value v
of weight w
to the buffer b
. If the buffer b
cannot hold this value, i.e., w >
capacity b
then this value is not added. And, if by adding this value, the buffer would get too full, older values will be dropped until the v
fits.
adds b (v, w)
returns the node containing the value v
. This node can be used to promote
or remove
the value v
from the buffer b
.
add_and_return_erased b (v, w)
has the same effect as add b
(v,w)
but it also returns the dropped values when applicable (and None
otherwise). Dropped values are ordered from the oldest to the most recently inserted.
add_list b vs
adds each element (with their weight) of the list vs
in the order they appear in the list. It returns a list of nodes, for each of the inserted elements. Elements that are too large in vs
w.r.t to capacity b
are filtered out.
If the total weight of vs
is larger than capacity b
then a first-fit strategy is used and the last elements in vs
are prioritized.
For instance: add_list (create 5) [(x, 1); (y, 3); (z, 4)]
will successfully return [x ; z]
which are added in the buffer.