package aches
Install
Dune Dependency
Authors
Maintainers
Sources
md5=c4bfe8506ee67b82bf5a4f5a989711d3
sha512=4c06df137173a605f14d1bf06193e591b02bd61518669f2d77513e7cd9ad7b660d5ea913cbb079eef8ac17246a71422827594dfe5ffaec032284e0de7e660305
doc/aches.rache/Rache/module-type-BORROW/index.html
Module type Rache.BORROW
Source
BORROW
are caches in which resources can be borrowed but never transferred. In other words, the cache retains ownership of all resources.
Check the documentation of the interface for more details.
A BORROW
is similar to a TRANSFER
except that:
- It only allows borrowing of resources. All resources are under the ownership of the cache. Always.
- Resources are created by the cache, on-demand. This allows the cache to have ownership of the resources from the beginning of their lifetime.
It is always unsafe to clean-up resources obtained from a BORROW
.
See Rache
(or Functors
) for more information.
create destroy n
creates a cache with a size-bound of n
. Remember that the size-bound is not upheld strictly by all caches.
Accessing (and implicitely adding elements)
borrow_or_make c k mk f
If k
is bound to r
in c
then it calls f r
.
Otherwise, it generates r
using mk
, then inserts the binding k
-to-r
in c
, then calls f r
.
Note that inserting the binding in c
may cause another binding to be removed and its associated resource to be cleaned-up.
It is unsafe to make use of c
during the evaluation of f
.
It is unsafe to clean-up r
.
Note that the in caches with a non-FIFO
replacement policy, this may have a side effect on the k
-to-r
binding. Specifically, in those caches, it might make it less likely to be removed when supernumerary bindings are inserted.
borrow c k f
calls f
with r
if k
is bound to r
in c
. This does not remove the resource from the cache: the cache is still responsible for cleaning-up the resource.
It is unsafe to use the cache from within the function f
.
It is unsafe to clean-up r
.
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.
fold f c init
folds the function f
and value init
over the bindings of c
from newest to oldest.
At each called to f
, the resource of the traversed binding is borrowed by f
. Consequently, the same limitations apply for fold
as for borrow
.
It is unsafe to clean-up any of the borrowed resources.
It is unsafe to use the cache from within f
.
fold_oldest_first
is like fold
but in reversed order: the elements that would be the first to be removed are traversed first. In a FIFO
cache, it is oldest-first traversal.
The same limitations and warning applies as for fold
.
Removing elements from the cache
The removal functions (remove
, clear
, and filter
) remove the specified elements from the cache. In all cases, the resources are cleaned-up by the cache.
remove c k
removes and cleans-up the binding from k
in c
. If k
is not bound in c
, it does nothing.
val clear : 'resource t -> unit
clear c
removes and cleans-up all bindings from c
.
filter c f
removes and cleans-up all the bindings (k, v)
such that f k v = false
.
Introspecting the cache's state
val length : 'resource t -> int
length c
is the number of bindings held by c
.
val capacity : 'resource t -> int
capacity c
is the number of bindings c
can hold: capacity (create n) = n
module H : Hashtbl.HashedType with type t = key