ocaml-containers/3.10/containers-data/CCCache/index.html
Simon Cruanes fc810ce03e
add 3.10
2022-11-16 13:25:28 -05:00

25 lines
No EOL
13 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. 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="generator" content="odoc 2.1.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../index.html">containers-data</a> &#x00BB; CCCache</nav><header class="odoc-preamble"><h1>Module <code><span>CCCache</span></code></h1><p>Caches Utils</p><p>Particularly useful for memoization. See <a href="#val-with_cache"><code>with_cache</code></a> and <a href="#val-with_cache_rec"><code>with_cache_rec</code></a> for more details.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.6</li></ul></header><nav class="odoc-toc"><ul><li><a href="#value-interface">Value interface</a></li></ul></nav><div class="odoc-content"><div class="odoc-spec"><div class="spec type" id="type-equal" class="anchored"><a href="#type-equal" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a equal</span></span><span> = <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> bool</span></code></div></div><div class="odoc-spec"><div class="spec type" id="type-hash" class="anchored"><a href="#type-hash" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a hash</span></span><span> = <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> int</span></code></div></div><h3 id="value-interface"><a href="#value-interface" class="anchor"></a>Value interface</h3><p>Typical use case: one wants to memoize a function <code>f : 'a -&gt; 'b</code>. Code sample:</p><pre><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-tags"><li class="since"><span class="at-tag">since</span> 0.6</li></ul><div class="odoc-spec"><div class="spec type" id="type-t" class="anchored"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> <span>('a, 'b) t</span></span></code></div></div><div class="odoc-spec"><div class="spec value" id="val-clear" class="anchored"><a href="#val-clear" class="anchor"></a><code><span><span class="keyword">val</span> clear : <span><span><span>( <span class="type-var">_</span>, <span class="type-var">_</span> )</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Clear the content of the cache.</p></div></div><div class="odoc-spec"><div class="spec type" id="type-callback" class="anchored"><a href="#type-callback" class="anchor"></a><code><span><span class="keyword">type</span> <span>('a, 'b) callback</span></span><span> = <span>in_cache:bool <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Type of the callback that is called once a cached value is found or not. Should never raise.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">in_cache</span> <p>is <code>true</code> if the value was in cache, <code>false</code> if the value was just produced.</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.3</li></ul></div></div><div class="odoc-spec"><div class="spec value" id="val-with_cache" class="anchored"><a href="#val-with_cache" class="anchor"></a><code><span><span class="keyword">val</span> with_cache :
<span>?cb:<span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-callback">callback</a></span> <span class="arrow">&#45;&gt;</span></span>
<span><span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span>
<span><span>( <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span> )</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'b</span></span></code></div><div class="spec-doc"><p><code>with_cache c f</code> behaves like <code>f</code>, but caches calls to <code>f</code> in the cache <code>c</code>. It always returns the same value as <code>f x</code>, if <code>f x</code> returns, or raise the same exception. However, <code>f</code> may not be called if <code>x</code> is in the cache.</p><ul class="at-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">cb</span> <p>called after the value is generated or retrieved.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value" id="val-with_cache_rec" class="anchored"><a href="#val-with_cache_rec" class="anchor"></a><code><span><span class="keyword">val</span> with_cache_rec :
<span>?cb:<span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-callback">callback</a></span> <span class="arrow">&#45;&gt;</span></span>
<span><span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span>
<span><span>( <span><span>( <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span> )</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'b</span> )</span> <span class="arrow">&#45;&gt;</span></span>
<span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span>
<span class="type-var">'b</span></span></code></div><div class="spec-doc"><p><code>with_cache_rec c f</code> is a function that first, applies <code>f</code> to some <code>f' = fix f</code>, such that recursive calls to <code>f'</code> are cached in <code>c</code>. It is similar to <a href="#val-with_cache"><code>with_cache</code></a> but with a function that takes as first argument its own recursive version. Example (memoized Fibonacci function):</p><pre><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-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">cb</span> <p>called after the value is generated or retrieved.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value" id="val-size" class="anchored"><a href="#val-size" class="anchor"></a><code><span><span class="keyword">val</span> size : <span><span><span>( <span class="type-var">_</span>, <span class="type-var">_</span> )</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>Size of the cache (number of entries). At most linear in the number of entries.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-iter" class="anchored"><a href="#val-iter" class="anchor"></a><code><span><span class="keyword">val</span> iter : <span><span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span>( <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> unit )</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Iterate on cached values. Should yield <code>size cache</code> pairs.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-add" class="anchored"><a href="#val-add" class="anchor"></a><code><span><span class="keyword">val</span> add : <span><span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'b</span> <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p>Manually add a cached value. Return <code>true</code> if the value has successfully been added, and <code>false</code> if the value was already bound.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.5</li></ul></div></div><div class="odoc-spec"><div class="spec value" id="val-dummy" class="anchored"><a href="#val-dummy" class="anchor"></a><code><span><span class="keyword">val</span> dummy : <span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p>Dummy cache, never stores any value.</p></div></div><div class="odoc-spec"><div class="spec value" id="val-linear" class="anchored"><a href="#val-linear" class="anchor"></a><code><span><span class="keyword">val</span> linear : <span>eq:<span><span class="type-var">'a</span> <a href="#type-equal">equal</a></span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-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-tags"><li class="parameter"><span class="at-tag">parameter</span> <span class="value">eq</span> <p>optional equality predicate for keys.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value" id="val-replacing" class="anchored"><a href="#val-replacing" class="anchor"></a><code><span><span class="keyword">val</span> replacing : <span>eq:<span><span class="type-var">'a</span> <a href="#type-equal">equal</a></span> <span class="arrow">&#45;&gt;</span></span> <span>?hash:<span><span class="type-var">'a</span> <a href="#type-hash">hash</a></span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-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="odoc-spec"><div class="spec value" id="val-lru" class="anchored"><a href="#val-lru" class="anchor"></a><code><span><span class="keyword">val</span> lru : <span>eq:<span><span class="type-var">'a</span> <a href="#type-equal">equal</a></span> <span class="arrow">&#45;&gt;</span></span> <span>?hash:<span><span class="type-var">'a</span> <a href="#type-hash">hash</a></span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-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="odoc-spec"><div class="spec value" id="val-unbounded" class="anchored"><a href="#val-unbounded" class="anchor"></a><code><span><span class="keyword">val</span> unbounded : <span>eq:<span><span class="type-var">'a</span> <a href="#type-equal">equal</a></span> <span class="arrow">&#45;&gt;</span></span> <span>?hash:<span><span class="type-var">'a</span> <a href="#type-hash">hash</a></span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span><span>( <span class="type-var">'a</span>, <span class="type-var">'b</span> )</span> <a href="#type-t">t</a></span></span></code></div><div class="spec-doc"><p>Unbounded cache, backed by a Hash table. Will grow forever unless <a href="#val-clear"><code>clear</code></a> is called manually.</p></div></div></div></body></html>