ocaml-containers/2.1/containers.data/CCCache/index.html
2018-03-28 20:36:52 -05:00

38 lines
No EOL
12 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>CCCache (containers.data.CCCache)</title><link rel="stylesheet" href="../../odoc.css"/><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><meta name="generator" content="doc-ock-html v1.0.0-1-g1fc9bf0"/></head><body><nav id="top"><a href="../index.html">Up</a> &mdash; <span class="package">package <a href="../index.html">containers.data</a></span></nav><header><h1><span class="keyword">Module</span> <span class="module-path">CCCache</span></h1></header><h2>Caches</h2><p>Particularly useful for memoization. See <a href="index.html#val-with_cache">with_cache</a> and <a href="index.html#val-with_cache_rec">with_cache_rec</a>
for more details.</p><ul class="at-tag"><li><span class="at-tag since">Since</span>: 0.6</li></ul><div class="spec type" id="type-equal"><a href="#type-equal" class="anchor"></a><div class="def type"><code><span class="keyword">type </span>'a equal</code><code><span class="keyword"> = </span><span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> bool</code><code></code></div><div class="doc"></div></div><div class="spec type" id="type-hash"><a href="#type-hash" class="anchor"></a><div class="def type"><code><span class="keyword">type </span>'a hash</code><code><span class="keyword"> = </span><span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> int</code><code></code></div><div class="doc"></div></div><h3>Value interface</h3><p>Typical use case: one wants to memoize a function <code class="code">f : 'a -&gt; 'b</code>. Code sample:
</p><pre><code class="code"> let f x =
print_endline &quot;call f&quot;;
x + 1;;
let f' = with_cache (lru 256) f;;
f' 0;; (* prints *)
f' 1;; (* prints *)
f' 0;; (* doesn't print, returns cached value *)</code></pre><ul class="at-tag"><li><span class="at-tag since">Since</span>: 0.6</li></ul><div class="spec type" id="type-t"><a href="#type-t" class="anchor"></a><div class="def type"><code><span class="keyword">type </span>('a, 'b) t</code><code></code><code></code></div><div class="doc"></div></div><div class="spec val" id="val-clear"><a href="#val-clear" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>clear : (<span class="type-var">_</span>, <span class="type-var">_</span>) <a href="index.html#type-t">t</a> <span class="keyword">&#8209;&gt;</span> unit</code></div><div class="doc"><p>Clear the content of the cache.</p></div></div><div class="spec type" id="type-callback"><a href="#type-callback" class="anchor"></a><div class="def type"><code><span class="keyword">type </span>('a, 'b) callback</code><code><span class="keyword"> = </span>in_cache:bool <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span> <span class="keyword">&#8209;&gt;</span> unit</code><code></code></div><div class="doc"><p>Type of the callback that is called once a cached value is found
or not.
Should never raise.</p><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">in_cache</span>: is <code class="code">true</code> if the value was in cache, <code class="code">false</code>
if the value was just produced.</li><li><span class="at-tag since">Since</span>: 1.3</li></ul></div></div><div class="spec val" id="val-with_cache"><a href="#val-with_cache" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>with_cache : ?&#8288;cb:(<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-callback">callback</a> <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a> <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span>) <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span></code></div><div class="doc"><p><code class="code">with_cache c f</code> behaves like <code class="code">f</code>, but caches calls to <code class="code">f</code> in the
cache <code class="code">c</code>. It always returns the same value as
<code class="code">f x</code>, if <code class="code">f x</code> returns, or raise the same exception.
However, <code class="code">f</code> may not be called if <code class="code">x</code> is in the cache.</p><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">cb</span>: called after the value is generated or retrieved.</li></ul></div></div><div class="spec val" id="val-with_cache_rec"><a href="#val-with_cache_rec" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>with_cache_rec : ?&#8288;cb:(<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-callback">callback</a> <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a> <span class="keyword">&#8209;&gt;</span> ((<span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span>) <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span>) <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span></code></div><div class="doc"><p><code class="code">with_cache_rec c f</code> is a function that first, applies <code class="code">f</code> to
some <code class="code">f' = fix f</code>, such that recursive calls to <code class="code">f'</code> are cached in <code class="code">c</code>.
It is similar to <a href="index.html#val-with_cache">with_cache</a> but with a function that takes as
first argument its own recursive version.
Example (memoized Fibonacci function):
</p><pre><code class="code"> let fib = with_cache_rec (lru 256)
(fun fib' n -&gt; match n with
| 1 | 2 -&gt; 1
| _ -&gt; fib' (n-1) + fib' (n-2)
);;
fib 70;;</code></pre><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">cb</span>: called after the value is generated or retrieved.</li></ul></div></div><div class="spec val" id="val-size"><a href="#val-size" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>size : (<span class="type-var">_</span>, <span class="type-var">_</span>) <a href="index.html#type-t">t</a> <span class="keyword">&#8209;&gt;</span> int</code></div><div class="doc"><p>Size of the cache (number of entries). At most linear in the number
of entries.</p></div></div><div class="spec val" id="val-iter"><a href="#val-iter" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>iter : (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a> <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span> <span class="keyword">&#8209;&gt;</span> unit) <span class="keyword">&#8209;&gt;</span> unit</code></div><div class="doc"><p>Iterate on cached values. Should yield <code class="code">size cache</code> pairs.</p></div></div><div class="spec val" id="val-add"><a href="#val-add" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>add : (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'a</span> <span class="keyword">&#8209;&gt;</span> <span class="type-var">'b</span> <span class="keyword">&#8209;&gt;</span> bool</code></div><div class="doc"><p>Manually add a cached value. Return <code class="code">true</code> if the value has successfully
been added, and <code class="code">false</code> if the value was already bound.</p><ul class="at-tag"><li><span class="at-tag since">Since</span>: 1.5</li></ul></div></div><div class="spec val" id="val-dummy"><a href="#val-dummy" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>dummy : (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a></code></div><div class="doc"><p>Dummy cache, never stores any value.</p></div></div><div class="spec val" id="val-linear"><a href="#val-linear" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>linear : eq:<span class="type-var">'a</span> <a href="index.html#type-equal">equal</a> <span class="keyword">&#8209;&gt;</span> int <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a></code></div><div class="doc"><p>Linear cache with the given size. It stores key/value pairs in
an array and does linear search at every call, so it should only be used
with small size.</p><ul class="at-tag"><li><span class="at-tag parameter">Parameter</span> <span class="module-path">eq</span>: optional equality predicate for keys.</li></ul></div></div><div class="spec val" id="val-replacing"><a href="#val-replacing" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>replacing : eq:<span class="type-var">'a</span> <a href="index.html#type-equal">equal</a> <span class="keyword">&#8209;&gt;</span> ?&#8288;hash:<span class="type-var">'a</span> <a href="index.html#type-hash">hash</a> <span class="keyword">&#8209;&gt;</span> int <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a></code></div><div class="doc"><p>Replacing cache of the given size. Equality and hash functions can be
parametrized. It's a hash table that handles collisions by replacing
the old value with the new (so a cache entry is evicted when another
entry with the same hash (modulo size) is added).
Never grows wider than the given size.</p></div></div><div class="spec val" id="val-lru"><a href="#val-lru" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>lru : eq:<span class="type-var">'a</span> <a href="index.html#type-equal">equal</a> <span class="keyword">&#8209;&gt;</span> ?&#8288;hash:<span class="type-var">'a</span> <a href="index.html#type-hash">hash</a> <span class="keyword">&#8209;&gt;</span> int <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a></code></div><div class="doc"><p>LRU cache of the given size (&quot;Least Recently Used&quot;: keys that have not been
used recently are deleted first). Never grows wider than the given size.</p></div></div><div class="spec val" id="val-unbounded"><a href="#val-unbounded" class="anchor"></a><div class="def val"><code><span class="keyword">val </span>unbounded : eq:<span class="type-var">'a</span> <a href="index.html#type-equal">equal</a> <span class="keyword">&#8209;&gt;</span> ?&#8288;hash:<span class="type-var">'a</span> <a href="index.html#type-hash">hash</a> <span class="keyword">&#8209;&gt;</span> int <span class="keyword">&#8209;&gt;</span> (<span class="type-var">'a</span>, <span class="type-var">'b</span>) <a href="index.html#type-t">t</a></code></div><div class="doc"><p>Unbounded cache, backed by a Hash table. Will grow forever
unless <a href="index.html#val-clear">clear</a> is called manually.</p></div></div></body></html>