123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)moduletypeCOLLECTION_BARE=sig(** A mutable structure that holds at most a fixed number of values of a same
type. Values are never removed, once the limit is reached, adding a value
replaces the oldest-promoted one in the buffer.
The function [promote] (see below) allows to pull a node to the front of
the buffer. *)(** 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(** [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. The replacement policy
of the cache determines which value is dropped, and the overflow policy
determines if it is dropped immediately or at some point in the future.
[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], oldest to newest.
*)valfold:'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->'anodelistvalelements_data:'at->'alist(** [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(** [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->unitendmoduletypeUNBOXED_COLLECTION=sig(** 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. *)(** 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(** [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(** [clear b] removes all values from the buffer [b]. *)valclear:'at->unit(** [fold b ~init ~f] folds over the value of the buffer [b], oldest to
newest. *)valfold:'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->'alistendmoduletypeHS=sig(** A subset of [Hashtbl.S]. *)typekeytype'atvalcreate:int->'atvalremove:'at->key->unitvalfind_opt:'at->key->'aoptionvalreplace:'at->key->'a->unitvallength:'at->intvalclear:'at->unitvalfold_v:('a->'b->'b)->'at->'b->'bendmoduletypeTABLER=functor(H:Hashtbl.HashedType)->(HSwithtypekey=H.t)(** # Caches
Ringo is a cache library. Below are signatures for caches.
A [CACHE_MAP] is a collection of kay-value bindings. A [CACHE_SET] is a
simple value store.
*)moduletypeCACHE_MAP=sig(** A Mutable structure akin to a hash-table, but with a size bound. Note
that, different caches have different policies towards the size bounds:
some uphold the bound strictly, some treat the bound as a suggestion. In
addition, some caches count their elements somewhat sloppily.
In general, the caches of ringo are intended to be used in settings that
do not require strict, by-the-number, extremely-predictable behaviors.
See [Ringo] (or [Functors]) for more information. *)(** The type of keys on which values in the cache are indexed. *)typekey(** The type of caches holding bindings from [key] to ['a] *)type'at(** [create n] creates a cache with a size-bound of [n]. Remember that the
size-bound is not upheld strictly by all caches. *)valcreate:int->'at(** [replace c k v] binds the key [k] to the value [v] in the cache [c]. This
may or may not cause another binding to be removed from the cache,
depending on the number of bindings already present in the cache [c], the
size-bound of the cache [c], and the policy of the cache [c] towards its
size-bound.
If [k] is already bound to a value in [c], the previous binding disappears
and is replaced by the new binding to [v].
Note that in caches with a [Sloppy] accounting policy, the old binding is
erased but may still count towards the size bound for some time. In other
words: apart for size bound consideration, the following sequences of
operations are indistinguishable:
[replace c k v; replace c k u]
[replace c k v; remove c k; replace c k u] *)valreplace:'at->key->'a->unit(** [fold f c init] folds the function [f] and value [init] over the bindings
of [c].
Note that for caches with a [Weak] overflow policy, this function may fold
over a subset of the bindings of [c]. See [Ringo] (or [Functors]) for more
details. *)valfold:(key->'a->'b->'b)->'at->'b->'b(** [fold_v f c init] folds the function [f] and value [init] over the
values held by the bindings of [c].
It is less powerful than [fold] in that it does not grant access to the
bindings' keys, but it does fold over all the values bound in [c], even
when the [c] has a [Weak] overflow policy. *)valfold_v:('a->'b->'b)->'at->'b->'b(** [find_opt c k] is [Some v] if [k] is bound to [v] in [c]. It is [None]
otherwise.
Note that the in caches with a non-[FIFO] replacement policy, this may
have a side effect on the [k]-to-[v] binding. Specifically, in those
caches, it might make it less likely to be removed when supernumerary
bindings are inserted. *)valfind_opt:'at->key->'aoption(** [remove c k] removes the binding from [k] in [c]. If [k] is not bound in
[c], it does nothing.
Note that in caches with a [Sloppy] accounting policy, removed bindings
can still count towards the size bound for some time. *)valremove:'at->key->unit(** [length c] is the number of bindings held by [c]. *)vallength:'at->int(** [capacity c] is the number of bindings [c] can hold:
[capacity (create n) = n] *)valcapacity:'at->int(** [clear c] removes all bindings from [c]. *)valclear:'at->unitmoduleH:Hashtbl.HashedTypewithtypet=keyendmoduletypeCACHE_SET=sig(** A Mutable structure akin to a set, but with a size bound. Note that,
different caches have different policies towards the size bounds: some
uphold the bound strictly, some treat the bound as a suggestion. In
addition, some caches count their elements somewhat sloppily.
In general, the caches of ringo are intended to be used in settings that
do not require strict, by-the-number, extremely-predictable behaviors.
See [Ringo] (or [Functors]) for more information. *)(** The type of values held by the cache. *)typeelt(** The type of caches holding values of type [elt]. *)typet(** [create n] creates a unit-cache with a size-bound of [n]. Remember that
the size-bound is not upheld strictly by all caches. *)valcreate:int->t(** [add c v] adds the value [v] to the cache [c]. This may or may not cause
another element to be removed from the cache, depending on the number of
elements already present in the cache [c], the size-bound of the cache
[c], and the policy of the cache [c] towards its size-bound.
If [v] is already present in [c], the element may count twice towards the
size bound for some time. In other words: apart for size bound in some
cases, the following sequences of operations are indistinguishable:
[add c v; add c u] and [add c v; remove c v; add c u] *)valadd:t->elt->unit(** [fold f c init] folds the function [f] and value [init] over the elements
of [c].
Note that for caches with a [Weak] overflow policy, this function may fold
over a subset of the elements of [c]. See [Ringo] (or [Functors]) for more
details. *)valfold:(elt->'a->'a)->t->'a->'a(** [mem c v] is [true] if [v] is present in [c]. It is [false] otherwise.
Note that the in caches with a non-[FIFO] replacement policy, this may
have a side effect on the [v] element. Specifically, in those caches, it
might make it less likely to be removed when supernumerary elements are
inserted. *)valmem:t->elt->bool(** [remove c v] removes the element [v] from [c]. If [v] is not present in
[c], it does nothing.
Note that in caches with a [Sloppy] accounting policy, removed elements
can still count towards the size bound for some time. *)valremove:t->elt->unit(** [length c] is the number of elements present in [c]. *)vallength:t->int(** [capacity c] is the number of bindings [c] can hold:
[capacity (create n) = n] *)valcapacity:t->int(** [clear c] removes all elements from [c]. *)valclear:t->unitend