ocaml-containers/3.12/containers-data/CCBV/index.html
2023-06-01 16:44:06 -04:00

3 lines
No EOL
21 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>CCBV (containers-data.CCBV)</title><link rel="stylesheet" href="../../odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.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; CCBV</nav><header class="odoc-preamble"><h1>Module <code><span>CCBV</span></code></h1><p>Imperative Bitvectors.</p><p>A bitvector is stored in some form of internal array (on the heap). Is it a bit similar to a more storage-efficient version of <code>bool
CCVector.vector</code>, with additional operations.</p><p><b>BREAKING CHANGES</b> since 1.2: size is now stored along with the bitvector. Some functions have a new signature.</p><p>The size of the bitvector used to be rounded up to the multiple of 30 or 62. In other words some functions such as <a href="#val-iter"><code>iter</code></a> would iterate on more bits than what was originally asked for. This is not the case anymore.</p></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span></code></div><div class="spec-doc"><p>A resizable bitvector</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-empty"><a href="#val-empty" class="anchor"></a><code><span><span class="keyword">val</span> empty : <span>unit <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Empty bitvector. Length is 0.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-create"><a href="#val-create" class="anchor"></a><code><span><span class="keyword">val</span> create : <span>size:int <span class="arrow">&#45;&gt;</span></span> <span>bool <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Create a bitvector of given size, with given default value. Length of result is <code>size</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-init"><a href="#val-init" class="anchor"></a><code><span><span class="keyword">val</span> init : <span>int <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>int <span class="arrow">&#45;&gt;</span></span> bool)</span> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>init len f</code> initializes a bitvector of length <code>len</code>, where bit <code>i</code> is true iff <code>f i</code> is.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.9</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-copy"><a href="#val-copy" class="anchor"></a><code><span><span class="keyword">val</span> copy : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Copy of bitvector.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-cardinal"><a href="#val-cardinal" class="anchor"></a><code><span><span class="keyword">val</span> cardinal : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>Number of bits set to one, seen as a set of bits.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-length"><a href="#val-length" class="anchor"></a><code><span><span class="keyword">val</span> length : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>Size of underlying bitvector. This is not related to the underlying implementation. Changed at 1.2</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-capacity"><a href="#val-capacity" class="anchor"></a><code><span><span class="keyword">val</span> capacity : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>The number of bits this bitvector can store without resizing.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-resize"><a href="#val-resize" class="anchor"></a><code><span><span class="keyword">val</span> resize : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Resize the BV so that it has the specified length. This can grow the underlying array, but it will not shrink it, to minimize memory traffic.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>on negative sizes.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-resize_minimize_memory"><a href="#val-resize_minimize_memory" class="anchor"></a><code><span><span class="keyword">val</span> resize_minimize_memory : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Same as <a href="#val-resize"><code>resize</code></a>, but this can also shrink the underlying array if this reduces the size.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>on negative sizes.</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.9</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-is_empty"><a href="#val-is_empty" class="anchor"></a><code><span><span class="keyword">val</span> is_empty : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p>Are there any true bits?</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set"><a href="#val-set" class="anchor"></a><code><span><span class="keyword">val</span> set : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Set i-th bit, extending the bitvector if needed.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-get"><a href="#val-get" class="anchor"></a><code><span><span class="keyword">val</span> get : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p>Is the i-th bit true? Return false if the index is too high.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-reset"><a href="#val-reset" class="anchor"></a><code><span><span class="keyword">val</span> reset : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Set i-th bit to 0, extending the bitvector if needed.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_bool"><a href="#val-set_bool" class="anchor"></a><code><span><span class="keyword">val</span> set_bool : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>bool <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Set or reset <code>i</code>-th bit.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.9</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-flip"><a href="#val-flip" class="anchor"></a><code><span><span class="keyword">val</span> flip : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Flip i-th bit, extending the bitvector if needed.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-clear"><a href="#val-clear" class="anchor"></a><code><span><span class="keyword">val</span> clear : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Set every bit to 0. Does not change the length.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-clear_and_shrink"><a href="#val-clear_and_shrink" class="anchor"></a><code><span><span class="keyword">val</span> clear_and_shrink : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Set every bit to 0, and set length to 0.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.9</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-iter"><a href="#val-iter" class="anchor"></a><code><span><span class="keyword">val</span> iter : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>int <span class="arrow">&#45;&gt;</span></span> <span>bool <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 all bits.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-iter_true"><a href="#val-iter_true" class="anchor"></a><code><span><span class="keyword">val</span> iter_true : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>int <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 bits set to 1.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_list"><a href="#val-to_list" class="anchor"></a><code><span><span class="keyword">val</span> to_list : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int list</span></span></code></div><div class="spec-doc"><p>List of indexes that are true.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_sorted_list"><a href="#val-to_sorted_list" class="anchor"></a><code><span><span class="keyword">val</span> to_sorted_list : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int list</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-to_list"><code>to_list</code></a>, but also guarantees the list is sorted in increasing order.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_list"><a href="#val-of_list" class="anchor"></a><code><span><span class="keyword">val</span> of_list : <span><span>int list</span> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>From a list of true bits.</p><p>The bits are interpreted as indices into the returned bitvector, so the final bitvector <code>bv</code> will have <code>length bv</code> equal to 1 more than max of list indices.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-first"><a href="#val-first" class="anchor"></a><code><span><span class="keyword">val</span> first : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int option</span></span></code></div><div class="spec-doc"><p>First set bit, or return <code>None</code>. Changed type at 1.2</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-first_exn"><a href="#val-first_exn" class="anchor"></a><code><span><span class="keyword">val</span> first_exn : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>First set bit, or</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Not_found</span> <p>if all bits are 0.</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-filter"><a href="#val-filter" class="anchor"></a><code><span><span class="keyword">val</span> filter : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span>int <span class="arrow">&#45;&gt;</span></span> bool)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>filter bv p</code> only keeps the true bits of <code>bv</code> whose <code>index</code> satisfies <code>p index</code>. Length is unchanged.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-negate_self"><a href="#val-negate_self" class="anchor"></a><code><span><span class="keyword">val</span> negate_self : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>negate_self t</code> flips all of the bits in <code>t</code>. Length is unchanged.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-negate"><a href="#val-negate" class="anchor"></a><code><span><span class="keyword">val</span> negate : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>negate t</code> returns a copy of <code>t</code> with all of the bits flipped. Length is unchanged.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-union_into"><a href="#val-union_into" class="anchor"></a><code><span><span class="keyword">val</span> union_into : <span>into:<a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>union_into ~into bv</code> sets <code>into</code> to the union of itself and <code>bv</code>. Also updates the length of <code>into</code> to be at least <code>length bv</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-inter_into"><a href="#val-inter_into" class="anchor"></a><code><span><span class="keyword">val</span> inter_into : <span>into:<a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>inter_into ~into bv</code> sets <code>into</code> to the intersection of itself and <code>bv</code>. Also updates the length of <code>into</code> to be at most <code>length bv</code>.</p><p>After executing:</p><ul><li><code>length ~into' = min (length into) (length bv)</code>.</li><li><code>for all i: get into' ==&gt; get into i /\ get bv i</code></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-union"><a href="#val-union" class="anchor"></a><code><span><span class="keyword">val</span> union : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>union bv1 bv2</code> returns the union of the two sets. The length of the result is the max of the inputs' lengths.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-inter"><a href="#val-inter" class="anchor"></a><code><span><span class="keyword">val</span> inter : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>inter bv1 bv2</code> returns the intersection of the two sets. The length of the result is the min of the inputs' lengths.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-diff_into"><a href="#val-diff_into" class="anchor"></a><code><span><span class="keyword">val</span> diff_into : <span>into:<a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>diff_into ~into t</code> modifies <code>into</code> with only the bits set but not in <code>t</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-diff"><a href="#val-diff" class="anchor"></a><code><span><span class="keyword">val</span> diff : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>diff t1 t2</code> returns those bits found in <code>t1</code> but not in <code>t2</code>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 1.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-select"><a href="#val-select" class="anchor"></a><code><span><span class="keyword">val</span> select : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span><span class="type-var">'a</span> array</span> <span class="arrow">&#45;&gt;</span></span> <span><span class="type-var">'a</span> list</span></span></code></div><div class="spec-doc"><p><code>select arr bv</code> selects the elements of <code>arr</code> whose index corresponds to a true bit in <code>bv</code>. If <code>bv</code> is too short, elements of <code>arr</code> with too high an index cannot be selected and are therefore not selected.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-selecti"><a href="#val-selecti" class="anchor"></a><code><span><span class="keyword">val</span> selecti : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><span><span class="type-var">'a</span> array</span> <span class="arrow">&#45;&gt;</span></span> <span><span>(<span class="type-var">'a</span> * int)</span> list</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-select"><code>select</code></a>, but selected elements are paired with their indexes.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-equal"><a href="#val-equal" class="anchor"></a><code><span><span class="keyword">val</span> equal : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p>Bitwise comparison, including the size (<code>equal a b</code> implies <code>length a=length b</code>).</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 3.5</li></ul></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-iter"><a href="#type-iter" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a iter</span></span><span> = <span><span>(<span><span class="type-var">'a</span> <span class="arrow">&#45;&gt;</span></span> unit)</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_iter"><a href="#val-to_iter" class="anchor"></a><code><span><span class="keyword">val</span> to_iter : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <a href="#type-iter">iter</a></span></span></code></div><div class="spec-doc"><p>Iterate over the true bits.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_iter"><a href="#val-of_iter" class="anchor"></a><code><span><span class="keyword">val</span> of_iter : <span><span>int <a href="#type-iter">iter</a></span> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Build from true bits.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pp"><a href="#val-pp" class="anchor"></a><code><span><span class="keyword">val</span> pp : <span><span class="xref-unresolved">Stdlib</span>.Format.formatter <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Print the bitvector as a string of bits.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 0.13</li></ul></div></div></div></body></html>