mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
15 lines
No EOL
10 KiB
HTML
15 lines
No EOL
10 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>CCCache (containers.CCCache)</title><link rel="stylesheet" href="../../odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc %%VERSION%%"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div class="content"><header><nav><a href="../index.html">Up</a> – <a href="../index.html">containers</a> » CCCache</nav><h1>Module <code>CCCache</code></h1><h2 id="caches"><a href="#caches" class="anchor"></a>Caches</h2><p>Particularly useful for memoization. See <a href="index.html#val-with_cache"><code>with_cache</code></a> and <a href="index.html#val-with_cache_rec"><code>with_cache_rec</code></a> for more details.</p><dl><dt>since</dt><dd>0.6</dd></dl><nav class="toc"><ul><li><a href="#value-interface">Value interface</a></li></ul></nav></header><dl><dt class="spec type" id="type-equal"><a href="#type-equal" class="anchor"></a><code><span class="keyword">type</span> <span>'a equal</span></code><code> = <span class="type-var">'a</span> <span>-></span> <span class="type-var">'a</span> <span>-></span> bool</code></dt><dt class="spec type" id="type-hash"><a href="#type-hash" class="anchor"></a><code><span class="keyword">type</span> <span>'a hash</span></code><code> = <span class="type-var">'a</span> <span>-></span> int</code></dt></dl><section><header><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 -> 'b</code>. Code sample:</p><pre><code class="ml">let f x =
|
||
print_endline "call f";
|
||
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><dl><dt>since</dt><dd>0.6</dd></dl></header><dl><dt class="spec type" id="type-t"><a href="#type-t" class="anchor"></a><code><span class="keyword">type</span> <span>('a, 'b) t</span></code></dt></dl><dl><dt class="spec value" id="val-clear"><a href="#val-clear" class="anchor"></a><code><span class="keyword">val</span> clear : <span><span>(<span class="type-var">_</span>, <span class="type-var">_</span>)</span> <a href="index.html#type-t">t</a></span> <span>-></span> unit</code></dt><dd><p>Clear the content of the cache.</p></dd></dl><dl><dt class="spec type" id="type-callback"><a href="#type-callback" class="anchor"></a><code><span class="keyword">type</span> <span>('a, 'b) callback</span></code><code> = <span>in_cache:bool</span> <span>-></span> <span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span> <span>-></span> unit</code></dt><dd><p>Type of the callback that is called once a cached value is found or not. Should never raise.</p><dl><dt>parameter in_cache</dt><dd><p>is <code>true</code> if the value was in cache, <code>false</code> if the value was just produced.</p></dd></dl><dl><dt>since</dt><dd>1.3</dd></dl></dd></dl><dl><dt class="spec value" id="val-with_cache"><a href="#val-with_cache" class="anchor"></a><code><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="index.html#type-callback">callback</a></span></span> <span>-></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span> <span>-></span> <span>(<span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span>)</span> <span>-></span> <span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span></code></dt><dd><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><dl><dt>parameter cb</dt><dd><p>called after the value is generated or retrieved.</p></dd></dl></dd></dl><dl><dt class="spec value" id="val-with_cache_rec"><a href="#val-with_cache_rec" class="anchor"></a><code><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="index.html#type-callback">callback</a></span></span> <span>-></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span> <span>-></span> <span>(<span>(<span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span>)</span> <span>-></span> <span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span>)</span> <span>-></span> <span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span></code></dt><dd><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="index.html#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 class="ml">let fib = with_cache_rec (lru 256)
|
||
(fun fib' n -> match n with
|
||
| 1 | 2 -> 1
|
||
| _ -> fib' (n-1) + fib' (n-2)
|
||
);;
|
||
|
||
fib 70;;</code></pre><dl><dt>parameter cb</dt><dd><p>called after the value is generated or retrieved.</p></dd></dl></dd></dl><dl><dt class="spec value" id="val-size"><a href="#val-size" class="anchor"></a><code><span class="keyword">val</span> size : <span><span>(<span class="type-var">_</span>, <span class="type-var">_</span>)</span> <a href="index.html#type-t">t</a></span> <span>-></span> int</code></dt><dd><p>Size of the cache (number of entries). At most linear in the number of entries.</p></dd></dl><dl><dt class="spec value" id="val-iter"><a href="#val-iter" class="anchor"></a><code><span class="keyword">val</span> iter : <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span> <span>-></span> <span>(<span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span> <span>-></span> unit)</span> <span>-></span> unit</code></dt><dd><p>Iterate on cached values. Should yield <code>size cache</code> pairs.</p></dd></dl><dl><dt class="spec value" id="val-add"><a href="#val-add" class="anchor"></a><code><span class="keyword">val</span> add : <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span> <span>-></span> <span class="type-var">'a</span> <span>-></span> <span class="type-var">'b</span> <span>-></span> bool</code></dt><dd><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><dl><dt>since</dt><dd>1.5</dd></dl></dd></dl><dl><dt class="spec value" id="val-dummy"><a href="#val-dummy" class="anchor"></a><code><span class="keyword">val</span> dummy : <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span></code></dt><dd><p>Dummy cache, never stores any value.</p></dd></dl><dl><dt class="spec value" id="val-linear"><a href="#val-linear" class="anchor"></a><code><span class="keyword">val</span> linear : <span>eq:<span><span class="type-var">'a</span> <a href="index.html#type-equal">equal</a></span></span> <span>-></span> int <span>-></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span></code></dt><dd><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><dl><dt>parameter eq</dt><dd><p>optional equality predicate for keys.</p></dd></dl></dd></dl><dl><dt class="spec value" id="val-replacing"><a href="#val-replacing" class="anchor"></a><code><span class="keyword">val</span> replacing : <span>eq:<span><span class="type-var">'a</span> <a href="index.html#type-equal">equal</a></span></span> <span>-></span> <span>?⁠hash:<span><span class="type-var">'a</span> <a href="index.html#type-hash">hash</a></span></span> <span>-></span> int <span>-></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span></code></dt><dd><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></dd></dl><dl><dt class="spec value" id="val-lru"><a href="#val-lru" class="anchor"></a><code><span class="keyword">val</span> lru : <span>eq:<span><span class="type-var">'a</span> <a href="index.html#type-equal">equal</a></span></span> <span>-></span> <span>?⁠hash:<span><span class="type-var">'a</span> <a href="index.html#type-hash">hash</a></span></span> <span>-></span> int <span>-></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span></code></dt><dd><p>LRU cache of the given size ("Least Recently Used": keys that have not been used recently are deleted first). Never grows wider than the given size.</p></dd></dl><dl><dt class="spec value" id="val-unbounded"><a href="#val-unbounded" class="anchor"></a><code><span class="keyword">val</span> unbounded : <span>eq:<span><span class="type-var">'a</span> <a href="index.html#type-equal">equal</a></span></span> <span>-></span> <span>?⁠hash:<span><span class="type-var">'a</span> <a href="index.html#type-hash">hash</a></span></span> <span>-></span> int <span>-></span> <span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="index.html#type-t">t</a></span></code></dt><dd><p>Unbounded cache, backed by a Hash table. Will grow forever unless <a href="index.html#val-clear"><code>clear</code></a> is called manually.</p></dd></dl></section></div></body></html> |