mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
187 lines
No EOL
12 KiB
HTML
187 lines
No EOL
12 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<link rel="stylesheet" href="style.css" type="text/css">
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
<link rel="Start" href="index.html">
|
|
<link rel="previous" href="CCFlatHashtbl.html">
|
|
<link rel="next" href="CCPersistentHashtbl.html">
|
|
<link rel="Up" href="index.html">
|
|
<link title="Index of types" rel=Appendix href="index_types.html">
|
|
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
|
|
<link title="Index of values" rel=Appendix href="index_values.html">
|
|
<link title="Index of class methods" rel=Appendix href="index_methods.html">
|
|
<link title="Index of class types" rel=Appendix href="index_class_types.html">
|
|
<link title="Index of modules" rel=Appendix href="index_modules.html">
|
|
<link title="Index of module types" rel=Appendix href="index_module_types.html">
|
|
<link title="CCVector" rel="Chapter" href="CCVector.html">
|
|
<link title="CCHeap" rel="Chapter" href="CCHeap.html">
|
|
<link title="CCList" rel="Chapter" href="CCList.html">
|
|
<link title="CCOpt" rel="Chapter" href="CCOpt.html">
|
|
<link title="CCPair" rel="Chapter" href="CCPair.html">
|
|
<link title="CCFun" rel="Chapter" href="CCFun.html">
|
|
<link title="CCHash" rel="Chapter" href="CCHash.html">
|
|
<link title="CCInt" rel="Chapter" href="CCInt.html">
|
|
<link title="CCBool" rel="Chapter" href="CCBool.html">
|
|
<link title="CCFloat" rel="Chapter" href="CCFloat.html">
|
|
<link title="CCArray" rel="Chapter" href="CCArray.html">
|
|
<link title="CCRef" rel="Chapter" href="CCRef.html">
|
|
<link title="CCSet" rel="Chapter" href="CCSet.html">
|
|
<link title="CCOrd" rel="Chapter" href="CCOrd.html">
|
|
<link title="CCRandom" rel="Chapter" href="CCRandom.html">
|
|
<link title="CCString" rel="Chapter" href="CCString.html">
|
|
<link title="CCHashtbl" rel="Chapter" href="CCHashtbl.html">
|
|
<link title="CCMap" rel="Chapter" href="CCMap.html">
|
|
<link title="CCFormat" rel="Chapter" href="CCFormat.html">
|
|
<link title="CCIO" rel="Chapter" href="CCIO.html">
|
|
<link title="CCInt64" rel="Chapter" href="CCInt64.html">
|
|
<link title="CCChar" rel="Chapter" href="CCChar.html">
|
|
<link title="CCResult" rel="Chapter" href="CCResult.html">
|
|
<link title="CCParse" rel="Chapter" href="CCParse.html">
|
|
<link title="CCArray_slice" rel="Chapter" href="CCArray_slice.html">
|
|
<link title="CCListLabels" rel="Chapter" href="CCListLabels.html">
|
|
<link title="CCArrayLabels" rel="Chapter" href="CCArrayLabels.html">
|
|
<link title="Containers" rel="Chapter" href="Containers.html">
|
|
<link title="CCKTree" rel="Chapter" href="CCKTree.html">
|
|
<link title="CCKList" rel="Chapter" href="CCKList.html">
|
|
<link title="CCLazy_list" rel="Chapter" href="CCLazy_list.html">
|
|
<link title="CCMultiMap" rel="Chapter" href="CCMultiMap.html">
|
|
<link title="CCMultiSet" rel="Chapter" href="CCMultiSet.html">
|
|
<link title="CCTrie" rel="Chapter" href="CCTrie.html">
|
|
<link title="CCFlatHashtbl" rel="Chapter" href="CCFlatHashtbl.html">
|
|
<link title="CCCache" rel="Chapter" href="CCCache.html">
|
|
<link title="CCPersistentHashtbl" rel="Chapter" href="CCPersistentHashtbl.html">
|
|
<link title="CCDeque" rel="Chapter" href="CCDeque.html">
|
|
<link title="CCFQueue" rel="Chapter" href="CCFQueue.html">
|
|
<link title="CCBV" rel="Chapter" href="CCBV.html">
|
|
<link title="CCMixtbl" rel="Chapter" href="CCMixtbl.html">
|
|
<link title="CCMixmap" rel="Chapter" href="CCMixmap.html">
|
|
<link title="CCRingBuffer" rel="Chapter" href="CCRingBuffer.html">
|
|
<link title="CCIntMap" rel="Chapter" href="CCIntMap.html">
|
|
<link title="CCPersistentArray" rel="Chapter" href="CCPersistentArray.html">
|
|
<link title="CCMixset" rel="Chapter" href="CCMixset.html">
|
|
<link title="CCGraph" rel="Chapter" href="CCGraph.html">
|
|
<link title="CCHashSet" rel="Chapter" href="CCHashSet.html">
|
|
<link title="CCBitField" rel="Chapter" href="CCBitField.html">
|
|
<link title="CCHashTrie" rel="Chapter" href="CCHashTrie.html">
|
|
<link title="CCWBTree" rel="Chapter" href="CCWBTree.html">
|
|
<link title="CCRAL" rel="Chapter" href="CCRAL.html">
|
|
<link title="CCImmutArray" rel="Chapter" href="CCImmutArray.html">
|
|
<link title="CCHet" rel="Chapter" href="CCHet.html">
|
|
<link title="CCZipper" rel="Chapter" href="CCZipper.html">
|
|
<link title="CCPool" rel="Chapter" href="CCPool.html">
|
|
<link title="CCLock" rel="Chapter" href="CCLock.html">
|
|
<link title="CCSemaphore" rel="Chapter" href="CCSemaphore.html">
|
|
<link title="CCThread" rel="Chapter" href="CCThread.html">
|
|
<link title="CCBlockingQueue" rel="Chapter" href="CCBlockingQueue.html">
|
|
<link title="CCTimer" rel="Chapter" href="CCTimer.html">
|
|
<link title="CCUnix" rel="Chapter" href="CCUnix.html">
|
|
<link title="CCSexp" rel="Chapter" href="CCSexp.html">
|
|
<link title="CCSexp_lex" rel="Chapter" href="CCSexp_lex.html"><link title="Value interface" rel="Section" href="#2_Valueinterface">
|
|
<title>Containers doc : CCCache</title>
|
|
</head>
|
|
<body>
|
|
<div class="navbar"><a class="pre" href="CCFlatHashtbl.html" title="CCFlatHashtbl">Previous</a>
|
|
<a class="up" href="index.html" title="Index">Up</a>
|
|
<a class="post" href="CCPersistentHashtbl.html" title="CCPersistentHashtbl">Next</a>
|
|
</div>
|
|
<h1>Module <a href="type_CCCache.html">CCCache</a></h1>
|
|
|
|
<pre><span class="keyword">module</span> CCCache: <code class="code"><span class="keyword">sig</span></code> <a href="CCCache.html">..</a> <code class="code"><span class="keyword">end</span></code></pre><div class="info module top">
|
|
<h1 id="1_Caches">Caches</h1>
|
|
<p>
|
|
|
|
Particularly useful for memoization. See <a href="CCCache.html#VALwith_cache"><code class="code"><span class="constructor">CCCache</span>.with_cache</code></a> and <a href="CCCache.html#VALwith_cache_rec"><code class="code"><span class="constructor">CCCache</span>.with_cache_rec</code></a>
|
|
for more details.<br>
|
|
<b>Since</b> 0.6<br>
|
|
</div>
|
|
<hr width="100%">
|
|
|
|
<pre><span id="TYPEequal"><span class="keyword">type</span> <code class="type">'a</code> equal</span> = <code class="type">'a -> 'a -> bool</code> </pre>
|
|
|
|
|
|
<pre><span id="TYPEhash"><span class="keyword">type</span> <code class="type">'a</code> hash</span> = <code class="type">'a -> int</code> </pre>
|
|
|
|
<br>
|
|
<h2 id="2_Valueinterface">Value interface</h2>
|
|
<p>
|
|
|
|
Typical use case: one wants to memoize a function <code class="code">f : <span class="keywordsign">'</span>a <span class="keywordsign">-></span> <span class="keywordsign">'</span>b</code>. Code sample:
|
|
<pre class="codepre"><code class="code"> <span class="keyword">let</span> f x =
|
|
print_endline <span class="string">"call f"</span>;
|
|
x + 1;;
|
|
|
|
<span class="keyword">let</span> f' = with_cache (lru 256) f;;
|
|
f' 0;; <span class="comment">(* prints *)</span>
|
|
f' 1;; <span class="comment">(* prints *)</span>
|
|
f' 0;; <span class="comment">(* doesn't print, returns cached value *)</span>
|
|
</code></pre><br>
|
|
|
|
<pre><span id="TYPEt"><span class="keyword">type</span> <code class="type">('a, 'b)</code> t</span> </pre>
|
|
|
|
|
|
<pre><span id="VALclear"><span class="keyword">val</span> clear</span> : <code class="type">('a, 'b) <a href="CCCache.html#TYPEt">t</a> -> unit</code></pre><div class="info ">
|
|
Clear the content of the cache<br>
|
|
</div>
|
|
|
|
<pre><span id="VALwith_cache"><span class="keyword">val</span> with_cache</span> : <code class="type">('a, 'b) <a href="CCCache.html#TYPEt">t</a> -> ('a -> 'b) -> 'a -> 'b</code></pre><div class="info ">
|
|
<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.<br>
|
|
</div>
|
|
|
|
<pre><span id="VALwith_cache_rec"><span class="keyword">val</span> with_cache_rec</span> : <code class="type">('a, 'b) <a href="CCCache.html#TYPEt">t</a> -> (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b</code></pre><div class="info ">
|
|
<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="CCCache.html#VALwith_cache"><code class="code"><span class="constructor">CCCache</span>.with_cache</code></a> but with a function that takes as
|
|
first argument its own recursive version.
|
|
Example (memoized Fibonacci function):
|
|
<pre class="codepre"><code class="code"> <span class="keyword">let</span> fib = with_cache_rec (lru 256)
|
|
(<span class="keyword">fun</span> fib' n <span class="keywordsign">-></span> <span class="keyword">match</span> n <span class="keyword">with</span>
|
|
<span class="keywordsign">|</span> 1 <span class="keywordsign">|</span> 2 <span class="keywordsign">-></span> 1
|
|
<span class="keywordsign">|</span> _ <span class="keywordsign">-></span> fib' (n-1) + fib' (n-2)
|
|
);;
|
|
|
|
fib 70;;
|
|
</code></pre><br>
|
|
</div>
|
|
|
|
<pre><span id="VALsize"><span class="keyword">val</span> size</span> : <code class="type">('a, 'b) <a href="CCCache.html#TYPEt">t</a> -> int</code></pre><div class="info ">
|
|
Size of the cache (number of entries). At most linear in the number
|
|
of entries.<br>
|
|
</div>
|
|
|
|
<pre><span id="VALiter"><span class="keyword">val</span> iter</span> : <code class="type">('a, 'b) <a href="CCCache.html#TYPEt">t</a> -> ('a -> 'b -> unit) -> unit</code></pre><div class="info ">
|
|
Iterate on cached values. Should yield <code class="code">size cache</code> pairs.<br>
|
|
</div>
|
|
|
|
<pre><span id="VALdummy"><span class="keyword">val</span> dummy</span> : <code class="type">('a, 'b) <a href="CCCache.html#TYPEt">t</a></code></pre><div class="info ">
|
|
Dummy cache, never stores any value<br>
|
|
</div>
|
|
|
|
<pre><span id="VALlinear"><span class="keyword">val</span> linear</span> : <code class="type">?eq:'a <a href="CCCache.html#TYPEequal">equal</a> -> int -> ('a, 'b) <a href="CCCache.html#TYPEt">t</a></code></pre><div class="info ">
|
|
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.<br>
|
|
</div>
|
|
<div class="param_info"><code class="code">eq</code> : optional equality predicate for keys</div>
|
|
|
|
<pre><span id="VALreplacing"><span class="keyword">val</span> replacing</span> : <code class="type">?eq:'a <a href="CCCache.html#TYPEequal">equal</a> -> ?hash:'a <a href="CCCache.html#TYPEhash">hash</a> -> int -> ('a, 'b) <a href="CCCache.html#TYPEt">t</a></code></pre><div class="info ">
|
|
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.<br>
|
|
</div>
|
|
|
|
<pre><span id="VALlru"><span class="keyword">val</span> lru</span> : <code class="type">?eq:'a <a href="CCCache.html#TYPEequal">equal</a> -> ?hash:'a <a href="CCCache.html#TYPEhash">hash</a> -> int -> ('a, 'b) <a href="CCCache.html#TYPEt">t</a></code></pre><div class="info ">
|
|
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.<br>
|
|
</div>
|
|
|
|
<pre><span id="VALunbounded"><span class="keyword">val</span> unbounded</span> : <code class="type">?eq:'a <a href="CCCache.html#TYPEequal">equal</a> -> ?hash:'a <a href="CCCache.html#TYPEhash">hash</a> -> int -> ('a, 'b) <a href="CCCache.html#TYPEt">t</a></code></pre><div class="info ">
|
|
Unbounded cache, backed by a Hash table. Will grow forever
|
|
unless <a href="CCCache.html#VALclear"><code class="code"><span class="constructor">CCCache</span>.clear</code></a> is called manually.<br>
|
|
</div>
|
|
</body></html> |