123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(** A mutable structure that holds at most a fixed number of values of a same
type. Values are automatically removed to make room for new values when
needs be, and they can also be removed by hand if needed.
The function [promote] (see below) allows to pull a node to the front of
the buffer. *)moduletypeCOLLECTION_BARE=sig(** The type of bounded-size buffers. *)type'at(** [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]. *)type'anode(** [data n] is the value contained in the node [n]. *)valdata:'anode->'a(** [create n] allocates a buffer that can hold up to [n] elements.
@raise [Invalid_argument] if [n] is 0 or less. *)valcreate:int->'at(** [capacity b] is the number of elements that [b] can hold. *)valcapacity:'at->int(** [length b] is the number of elements that are currently in [b]. *)vallength:'at->int(** [add b v] adds the value [v] to the buffer [b]. If the buffer [b] already
has [capacity b] values, then a value is dropped.
[adds b v] returns the node containing the value [v]. This node can be
used to [promote] or [remove] the value [v] from the buffer [b]. *)valadd:'at->'a->'anode(** [add_and_return_erased b v] has the same effect as [add b v] but it
returns the dropped value when applicable (and [None] otherwise). *)valadd_and_return_erased:'at->'a->('anode*'aoption)(** [add_list b vs] adds each element of the list [vs] in the order they
appear in the list. It returns a list of nodes, for each of the inserted
elements.
If [length vs > capacity b], then each value from [vs] is added, but the
ones at the front of the list are popped. In this case, [add_list b vs]
returns a list of [capacity b] nodes only. *)valadd_list:'at->'alist->'anodelist(** [clear b] removes all values from the buffer [b]. *)valclear:'at->unit(** [fold b ~init ~f] folds over the value of the buffer [b], newest to oldest. *)valfold:'at->init:'b->f:('b->'anode->'b)->'b(** [fold_oldest_first b ~init ~f] folds over the value of the buffer [b], oldest to newest. *)valfold_oldest_first:'at->init:'b->f:('b->'anode->'b)->'b(** [elements b] is a list of nodes from [b]. They appear oldest first, newest
last. *)valelements:'at->'anodelist(** [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. *)valelements_data:'at->'alist(** [oldest_element b] returns the oldest inserted element
from the buffer [b] if any. *)valoldest_element:'at->'anodeoption(** [newest_element b] returns the oldest inserted element
from the buffer [b] if any. *)valnewest_element:'at->'anodeoption(** [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. *)valremove:'at->'anode->unit(** [remove_oldest b] removes and returns the oldest inserted element
from the buffer [b] or [None] if the buffer is empty. *)valremove_oldest:'at->'anodeoption(** [remove_newest b] removes and returns the most recently inserted
element from the buffer [b] or [None] if the buffer is empty. *)valremove_newest:'at->'anodeoption(** [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]. *)valpromote:'at->'anode->unitendmoduletypeCOLLECTION=sigincludeCOLLECTION_BAREvalpromote_read:'at->'anode->unitvalpromote_write:'at->'anode->unitendmoduletypeWEIGHTED_COLLECTION_BARE=sigincludeCOLLECTION_BARE(** [capacity b] is the maximum weight that [b] can hold. *)valcapacity:'at->int(** [weight e] returns the weight of the element [e]. *)valweight:'anode->int(** [total_weight b] is the summed weight of all elements that are
currently in [b]. *)valtotal_weight:'at->int(** [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]. *)valadd:'at->'a*int->'anodeoption(** [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. *)valadd_and_return_erased:'at->'a*int->'anodeoption*'alist(** [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. *)valadd_list:'at->('a*int)list->'anodelistendmoduletypeWEIGHTED_COLLECTION=sigincludeWEIGHTED_COLLECTION_BAREvalpromote_read:'at->'anode->unitvalpromote_write:'at->'anode->unitend(** A mutable structure that holds at most a fixed number of values of a same
type. Values are automatically removed to make room for new values when
needs be, and they can also be removed by hand if needed. *)moduletypeUNBOXED_COLLECTION=sig(** The type of bounded-size buffers. *)type'at(** [create n] allocates a ring buffer that can hold up to [n] values.
@raise [Invalid_argument] if [n] is 0 or less. *)valcreate:int->'at(** [capacity b] is the number of elements that [b] can hold. *)valcapacity:'at->int(** [length b] is the number of elements that are currently in [b]. *)vallength:'at->int(** [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. *)valadd:'at->'a->unit(** [add_and_return_erased b v] has the same effect as [add b v] but it
returns the dropped value when applicable. *)valadd_and_return_erased:'at->'a->'aoption(** [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. *)valadd_list:'at->'alist->unit(** [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. *)valremove_oldest:'at->'aoption(** [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. *)valremove_newest:'at->'aoption(** [clear b] removes all values from the buffer [b]. *)valclear:'at->unit(** [fold b ~init ~f] folds over the value of the buffer [b], newest to oldest. *)valfold:'at->init:'b->f:('b->'a->'b)->'b(** [fold_oldest_first b ~init ~f] folds over the value of the buffer [b],
oldest to newest. *)valfold_oldest_first:'at->init:'b->f:('b->'a->'b)->'b(** [elements b] is a list that contains the same elements as the buffer [b],
oldest first, newest last. *)valelements:'at->'alist(** [oldest_element b] returns the oldest inserted element
from the buffer [b] if any. *)valoldest_element:'at->'aoption(** [newest_element b] returns the oldest inserted element
from the buffer [b] if any. *)valnewest_element:'at->'aoptionendmoduletypeUNBOXED_WEIGHTED_COLLECTION=sig(** The type of bounded-size buffers. *)type'at(** [create n] allocates a ring buffer that can hold up to [n] values.
@raise [Invalid_argument] if [n] is 0 or less. *)valcreate:int->'at(** [capacity b] is the number of elements that [b] can hold. *)valcapacity:'at->int(** [length b] is the number of elements that are currently in [b]. *)vallength:'at->int(** [total_weight b] is the summed weight of all elements that are
currently in [b]. *)valtotal_weight:'at->int(** [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. *)valadd:'at->'a*int->unit(** [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 *)valadd_and_return_erased:'at->'a*int->'alist(** [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. *)valadd_list:'at->('a*int)list->unit(** [clear b] removes all values from the buffer [b]. *)valclear:'at->unit(** [fold b ~init ~f] folds over the value of the buffer [b], newest to oldest. *)valfold:'at->init:'b->f:('b->'a->'b)->'b(** [fold_oldest_first b ~init ~f] folds over the value of the buffer [b],
oldest to newest. *)valfold_oldest_first:'at->init:'b->f:('b->'a->'b)->'b(** [elements b] is a list that contains the same elements as the buffer [b],
oldest first, newest last. *)valelements:'at->'alist(** [oldest_element b] returns the oldest inserted element
from the buffer [b] if any. *)valoldest_element:'at->'aoption(** [newest_element b] returns the oldest inserted element
from the buffer [b] if any. *)valnewest_element:'at->'aoption(** [remove_oldest b] removes and returns the oldest inserted element
from the buffer [b] or [None] if the buffer is empty. *)valremove_oldest:'at->'aoption(** [remove_newest b] removes and returns the most recently inserted
element from the buffer [b] or [None] if the buffer is empty. *)valremove_newest:'at->'aoptionend