package ringo

  1. Overview
  2. Docs

Module Ringo.Weighted_DllSource

Weighted_Dll is similar to Dll but comes with a notion of data weight as capacity instead of being based on the number of elements. The documentation is available in UNBOXED_WEIGHTED_COLLECTION.

By contrast to Dll, elements require their weight to be provided in order to be inserted in the doubly-linked list.

Sourcetype 'a t

The type of bounded-size buffers.

Sourceval 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.

Sourceval capacity : 'a t -> int

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

Sourceval length : 'a t -> int

length b is the number of elements that are currently in b.

Sourceval total_weight : 'a t -> int

total_weight b is the summed weight of all elements that are currently in b.

Sourceval add : 'a t -> ('a * int) -> unit

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.

Sourceval add_and_return_erased : 'a t -> ('a * int) -> 'a list

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

Sourceval add_list : 'a t -> ('a * int) list -> unit

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.

Sourceval clear : 'a t -> unit

clear b removes all values from the buffer b.

Sourceval 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.

Sourceval 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.

Sourceval elements : 'a t -> 'a list

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

Sourceval oldest_element : 'a t -> 'a option

oldest_element b returns the oldest inserted element from the buffer b if any.

Sourceval newest_element : 'a t -> 'a option

newest_element b returns the oldest inserted element from the buffer b if any.

Sourceval 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.

Sourceval 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.

OCaml

Innovation. Community. Security.