Files
Nim/sharedtables.html
github-actions[bot] de413ab6bb Deploy to GitHub pages
2025-12-25 20:14:24 +00:00

435 lines
42 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- This file is generated by Nim. -->
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>std/sharedtables</title>
<!-- Google fonts -->
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
<!-- Favicon -->
<link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
<link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
<!-- JS -->
<script type="text/javascript" src="dochack.js?v=2.3.1"></script>
</head>
<body>
<div class="document" id="documentId">
<div class="container">
<h1 class="title">std/sharedtables</h1>
<div class="row">
<div class="three columns">
<div class="theme-select-wrapper">
<label for="theme-select">Theme:&nbsp;</label>
<select id="theme-select" onchange="setTheme(this.value)">
<option value="auto">🌗 Match OS</option>
<option value="dark">🌑 Dark</option>
<option value="light">🌕 Light</option>
</select>
</div>
<div id="global-links">
<ul class="simple-boot">
<li><a href="manual.html">Manual</a></li>
<li><a href="lib.html">Standard library</a></li>
<li> <a id="indexLink" href="theindex.html">Index</a></li>
<li><a href="compiler/theindex.html">Compiler docs</a></li>
<li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
<li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
</ul>
</div>
<div id="searchInputDiv">
Search: <input type="search" id="searchInput"
oninput="search()" />
</div>
<ul class="simple simple-toc" id="toc-list">
<li>
<a class="reference reference-toplevel" href="#6" id="56">Imports</a>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#7" id="57">Types</a></summary>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#SharedTable" title="SharedTable[A; B] = object">SharedTable</a></li>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#10" id="60">Consts</a></summary>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#defaultInitialSize" title="defaultInitialSize = 32">defaultInitialSize</a></li>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#12" id="62">Procs</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">[]=
<li><a class="reference" href="#%5B%5D%3D%2CSharedTable%5BA%2CB%5D%2CA%2CB" title="`[]=`[A, B](t: var SharedTable[A, B]; key: A; val: B)">`[]=`[A, B](t: var SharedTable[A, B]; key: A; val: B)</a></li>
</ul>
<ul class="simple nested-toc-section">add
<li><a class="reference" href="#add%2CSharedTable%5BA%2CB%5D%2CA%2CB" title="add[A, B](t: var SharedTable[A, B]; key: A; val: B)">add[A, B](t: var SharedTable[A, B]; key: A; val: B)</a></li>
</ul>
<ul class="simple nested-toc-section">deinitSharedTable
<li><a class="reference" href="#deinitSharedTable%2CSharedTable%5BA%2CB%5D" title="deinitSharedTable[A, B](t: var SharedTable[A, B])">deinitSharedTable[A, B](t: var SharedTable[A, B])</a></li>
</ul>
<ul class="simple nested-toc-section">del
<li><a class="reference" href="#del%2CSharedTable%5BA%2CB%5D%2CA" title="del[A, B](t: var SharedTable[A, B]; key: A)">del[A, B](t: var SharedTable[A, B]; key: A)</a></li>
</ul>
<ul class="simple nested-toc-section">hasKeyOrPut
<li><a class="reference" href="#hasKeyOrPut%2CSharedTable%5BA%2CB%5D%2CA%2CB" title="hasKeyOrPut[A, B](t: var SharedTable[A, B]; key: A; val: B): bool">hasKeyOrPut[A, B](t: var SharedTable[A, B]; key: A; val: B): bool</a></li>
</ul>
<ul class="simple nested-toc-section">init
<li><a class="reference" href="#init%2CSharedTable%5BA%2CB%5D%2Cint" title="init[A, B](t: var SharedTable[A, B]; initialSize = 32)">init[A, B](t: var SharedTable[A, B]; initialSize = 32)</a></li>
</ul>
<ul class="simple nested-toc-section">len
<li><a class="reference" href="#len%2CSharedTable%5BA%2CB%5D" title="len[A, B](t: var SharedTable[A, B]): int">len[A, B](t: var SharedTable[A, B]): int</a></li>
</ul>
<ul class="simple nested-toc-section">mget
<li><a class="reference" href="#mget%2CSharedTable%5BA%2CB%5D%2CA" title="mget[A, B](t: var SharedTable[A, B]; key: A): var B">mget[A, B](t: var SharedTable[A, B]; key: A): var B</a></li>
</ul>
<ul class="simple nested-toc-section">mgetOrPut
<li><a class="reference" href="#mgetOrPut%2CSharedTable%5BA%2CB%5D%2CA%2CB" title="mgetOrPut[A, B](t: var SharedTable[A, B]; key: A; val: B): var B">mgetOrPut[A, B](t: var SharedTable[A, B]; key: A; val: B): var B</a></li>
</ul>
<ul class="simple nested-toc-section">withKey
<li><a class="reference" href="#withKey%2CSharedTable%5BA%2CB%5D%2CA%2Cproc%28A%2CB%2Cbool%29" title="withKey[A, B](t: var SharedTable[A, B]; key: A;
mapper: proc (key: A; val: var B; pairExists: var bool))">withKey[A, B](t: var SharedTable[A, B]; key: A;
mapper: proc (key: A; val: var B; pairExists: var bool))</a></li>
</ul>
</ul>
</details>
</li>
<li>
<details open>
<summary><a class="reference reference-toplevel" href="#18" id="68">Templates</a></summary>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">withValue
<li><a class="reference" href="#withValue.t%2CSharedTable%5BA%2CB%5D%2CA%2Cuntyped%2Cuntyped%2Cuntyped" title="withValue[A, B](t: var SharedTable[A, B]; key: A; value, body1, body2: untyped)">withValue[A, B](t: var SharedTable[A, B]; key: A; value, body1, body2: untyped)</a></li>
<li><a class="reference" href="#withValue.t%2CSharedTable%5BA%2CB%5D%2CA%2Cuntyped%2Cuntyped" title="withValue[A, B](t: var SharedTable[A, B]; key: A; value, body: untyped)">withValue[A, B](t: var SharedTable[A, B]; key: A; value, body: untyped)</a></li>
</ul>
</ul>
</details>
</li>
</ul>
</div>
<div class="nine columns" id="content">
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L1" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L1" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
<div id="tocRoot"></div>
<div class="deprecation-message">
<b>Deprecated</b>
</div>
<p class="module-desc"><p>Shared table support for Nim. Use plain old non GC'ed keys and values or you'll be in trouble. Uses a single lock to protect the table, lockfree implementations welcome but if lock contention is so high that you need a lockfree hash table, you're doing it wrong.</p>
<p>Unstable API.</p>
</p>
<div class="section" id="6">
<h1><a class="toc-backref" href="#6">Imports</a></h1>
<dl class="item">
<a class="reference external" href="hashes.html">hashes</a>, <a class="reference external" href="math.html">math</a>, <a class="reference external" href="locks.html">locks</a>, <a class="reference external" href="assertions.html">assertions</a>, <a class="reference external" href="outparams.html">outparams</a>
</dl>
</div>
<div class="section" id="7">
<h1><a class="toc-backref" href="#7">Types</a></h1>
<dl class="item">
<div id="SharedTable">
<dt><pre><a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">B</span><span class="Other">]</span> <span class="Other">=</span> <span class="Keyword">object</span></pre></dt>
<dd>
generic hash SharedTable
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L25" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L25" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</dl>
</div>
<div class="section" id="10">
<h1><a class="toc-backref" href="#10">Consts</a></h1>
<dl class="item">
<div id="defaultInitialSize">
<dt><pre><a href="sharedtables.html#defaultInitialSize"><span class="Identifier">defaultInitialSize</span></a> <span class="Other">=</span> <span class="DecNumber">32</span></pre></dt>
<dd>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/tableimpl.nim#L15" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/tableimpl.nim#L15" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</dl>
</div>
<div class="section" id="12">
<h1><a class="toc-backref" href="#12">Procs</a></h1>
<dl class="item">
<div id="[]=-procs-all">
<div id="[]=,SharedTable[A,B],A,B">
<dt><pre><span class="Keyword">proc</span> <a href="#%5B%5D%3D%2CSharedTable%5BA%2CB%5D%2CA%2CB"><span class="Identifier">`[]=`</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">val</span><span class="Other">:</span> <span class="Identifier">B</span><span class="Other">)</span></pre></dt>
<dd>
Puts a (key, value)-pair into <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L221" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L221" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="add-procs-all">
<div id="add,SharedTable[A,B],A,B">
<dt><pre><span class="Keyword">proc</span> <a href="#add%2CSharedTable%5BA%2CB%5D%2CA%2CB"><span class="Identifier">add</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">val</span><span class="Other">:</span> <span class="Identifier">B</span><span class="Other">)</span></pre></dt>
<dd>
Puts a new (key, value)-pair into <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span></span></tt> even if <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span><span class="Punctuation">[</span><span class="Identifier">key</span><span class="Punctuation">]</span></span></tt> already exists. This can introduce duplicate keys into the table!
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L226" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L226" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="deinitSharedTable-procs-all">
<div id="deinitSharedTable,SharedTable[A,B]">
<dt><pre><span class="Keyword">proc</span> <a href="#deinitSharedTable%2CSharedTable%5BA%2CB%5D"><span class="Identifier">deinitSharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">)</span></pre></dt>
<dd>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L253" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L253" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="del-procs-all">
<div id="del,SharedTable[A,B],A">
<dt><pre><span class="Keyword">proc</span> <a href="#del%2CSharedTable%5BA%2CB%5D%2CA"><span class="Identifier">del</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">)</span></pre></dt>
<dd>
Deletes <tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt> from hash table <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L232" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L232" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="hasKeyOrPut-procs-all">
<div id="hasKeyOrPut,SharedTable[A,B],A,B">
<dt><pre><span class="Keyword">proc</span> <a href="#hasKeyOrPut%2CSharedTable%5BA%2CB%5D%2CA%2CB"><span class="Identifier">hasKeyOrPut</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">val</span><span class="Other">:</span> <span class="Identifier">B</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#bool"><span class="Identifier">bool</span></a></pre></dt>
<dd>
Returns true if <tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt> is in the table, otherwise inserts <tt class="docutils literal"><span class="pre"><span class="Identifier">value</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L164" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L164" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="init-procs-all">
<div id="init,SharedTable[A,B],int">
<dt><pre><span class="Keyword">proc</span> <a href="#init%2CSharedTable%5BA%2CB%5D%2Cint"><span class="Identifier">init</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">initialSize</span> <span class="Other">=</span> <span class="DecNumber">32</span><span class="Other">)</span></pre></dt>
<dd>
<p>Creates a new hash table that is empty.</p>
<p>This proc must be called before any other usage of <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span></span></tt>.</p>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L242" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L242" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="len-procs-all">
<div id="len,SharedTable[A,B]">
<dt><pre><span class="Keyword">proc</span> <a href="#len%2CSharedTable%5BA%2CB%5D"><span class="Identifier">len</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <a href="system.html#int"><span class="Identifier">int</span></a></pre></dt>
<dd>
Number of elements in <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L237" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L237" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="mget-procs-all">
<div id="mget,SharedTable[A,B],A">
<dt><pre><span class="Keyword">proc</span> <a href="#mget%2CSharedTable%5BA%2CB%5D%2CA"><span class="Identifier">mget</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">)</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">B</span></pre></dt>
<dd>
Retrieves the value at <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span><span class="Punctuation">[</span><span class="Identifier">key</span><span class="Punctuation">]</span></span></tt>. The value can be modified. If <tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt> is not in <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span></span></tt>, the <tt class="docutils literal"><span class="pre"><span class="Identifier">KeyError</span></span></tt> exception is raised.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L139" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L139" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="mgetOrPut-procs-all">
<div id="mgetOrPut,SharedTable[A,B],A,B">
<dt><pre><span class="Keyword">proc</span> <a href="#mgetOrPut%2CSharedTable%5BA%2CB%5D%2CA%2CB"><span class="Identifier">mgetOrPut</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">val</span><span class="Other">:</span> <span class="Identifier">B</span><span class="Other">)</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">B</span></pre></dt>
<dd>
Retrieves value at <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span><span class="Punctuation">[</span><span class="Identifier">key</span><span class="Punctuation">]</span></span></tt> or puts <tt class="docutils literal"><span class="pre"><span class="Identifier">val</span></span></tt> if not present, either way returning a value which can be modified. <strong>Note</strong>: This is inherently unsafe in the context of multi-threading since it returns a pointer to <tt class="docutils literal"><span class="pre"><span class="Identifier">B</span></span></tt>.
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L156" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L156" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
<div id="withKey-procs-all">
<div id="withKey,SharedTable[A,B],A,proc(A,B,bool)">
<dt><pre><span class="Keyword">proc</span> <a href="#withKey%2CSharedTable%5BA%2CB%5D%2CA%2Cproc%28A%2CB%2Cbool%29"><span class="Identifier">withKey</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span>
<span class="Identifier">mapper</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">val</span><span class="Other">:</span> <span class="Keyword">var</span> <span class="Identifier">B</span><span class="Other">;</span> <span class="Identifier">pairExists</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="system.html#bool"><span class="Identifier">bool</span></a><span class="Other">)</span><span class="Other">)</span></pre></dt>
<dd>
<p>Computes a new mapping for the <tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt> with the specified <tt class="docutils literal"><span class="pre"><span class="Identifier">mapper</span></span></tt> procedure.</p>
<p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">mapper</span></span></tt> takes 3 arguments:</p>
<ol class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Identifier">key</span></span></tt> - the current key, if it exists, or the key passed to <tt class="docutils literal"><span class="pre"><span class="Identifier">withKey</span></span></tt> otherwise;</li>
<li><tt class="docutils literal"><span class="pre"><span class="Identifier">val</span></span></tt> - the current value, if the key exists, or default value of the type otherwise;</li>
<li><tt class="docutils literal"><span class="pre"><span class="Identifier">pairExists</span></span></tt> - <tt class="docutils literal"><span class="pre"><span class="Identifier">true</span></span></tt> if the key exists, <tt class="docutils literal"><span class="pre"><span class="Identifier">false</span></span></tt> otherwise.</li>
</ol>
<p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">mapper</span></span></tt> can can modify <tt class="docutils literal"><span class="pre"><span class="Identifier">val</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">pairExists</span></span></tt> values to change the mapping of the key or delete it from the table. When adding a value, make sure to set <tt class="docutils literal"><span class="pre"><span class="Identifier">pairExists</span></span></tt> to <tt class="docutils literal"><span class="pre"><span class="Identifier">true</span></span></tt> along with modifying the <tt class="docutils literal"><span class="pre"><span class="Identifier">val</span></span></tt>.</p>
<p>The operation is performed atomically and other operations on the table will be blocked while the <tt class="docutils literal"><span class="pre"><span class="Identifier">mapper</span></span></tt> is invoked, so it should be short and simple.</p>
<p>Example usage:</p>
<p><pre class="listing"><span class="Comment"># If value exists, decrement it.</span>
<span class="Comment"># If it becomes zero or less, delete the key</span>
<span class="Identifier">t</span><span class="Operator">.</span><span class="Identifier">withKey</span><span class="Punctuation">(</span><span class="DecNumber">1'i64</span><span class="Punctuation">)</span> <span class="Keyword">do</span> <span class="Punctuation">(</span><span class="Identifier">k</span><span class="Punctuation">:</span> <span class="Identifier">int64</span><span class="Punctuation">,</span> <span class="Identifier">v</span><span class="Punctuation">:</span> <span class="Keyword">var</span> <span class="Identifier">int</span><span class="Punctuation">,</span> <span class="Identifier">pairExists</span><span class="Punctuation">:</span> <span class="Keyword">var</span> <span class="Identifier">bool</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">if</span> <span class="Identifier">pairExists</span><span class="Punctuation">:</span>
<span class="Identifier">dec</span> <span class="Identifier">v</span>
<span class="Keyword">if</span> <span class="Identifier">v</span> <span class="Operator">&lt;=</span> <span class="DecNumber">0</span><span class="Punctuation">:</span>
<span class="Identifier">pairExists</span> <span class="Operator">=</span> <span class="Identifier">false</span></pre></p>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L173" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L173" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
<div class="section" id="18">
<h1><a class="toc-backref" href="#18">Templates</a></h1>
<dl class="item">
<div id="withValue-templates-all">
<div id="withValue.t,SharedTable[A,B],A,untyped,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#withValue.t%2CSharedTable%5BA%2CB%5D%2CA%2Cuntyped%2Cuntyped%2Cuntyped"><span class="Identifier">withValue</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span>
<span class="Identifier">value</span><span class="Other">,</span> <span class="Identifier">body1</span><span class="Other">,</span> <span class="Identifier">body2</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span></pre></dt>
<dd>
Retrieves the value at <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span><span class="Punctuation">[</span><span class="Identifier">key</span><span class="Punctuation">]</span></span></tt>. <tt class="docutils literal"><span class="pre"><span class="Identifier">value</span></span></tt> can be modified in the scope of the <tt class="docutils literal"><span class="pre"><span class="Identifier">withValue</span></span></tt> call.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">table</span><span class="Punctuation">:</span> <span class="Identifier">SharedTable</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">string</span><span class="Punctuation">]</span>
<span class="Identifier">init</span><span class="Punctuation">(</span><span class="Identifier">table</span><span class="Punctuation">)</span>
<span class="Identifier">table</span><span class="Punctuation">[</span><span class="StringLit">&quot;a&quot;</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;x&quot;</span>
<span class="Identifier">table</span><span class="Punctuation">[</span><span class="StringLit">&quot;b&quot;</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;y&quot;</span>
<span class="Identifier">table</span><span class="Punctuation">[</span><span class="StringLit">&quot;c&quot;</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;z&quot;</span>
<span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">withValue</span><span class="Punctuation">(</span><span class="StringLit">&quot;a&quot;</span><span class="Punctuation">,</span> <span class="Identifier">value</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Identifier">value</span><span class="Punctuation">[</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;m&quot;</span>
<span class="Keyword">var</span> <span class="Identifier">flag</span> <span class="Operator">=</span> <span class="Identifier">false</span>
<span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">withValue</span><span class="Punctuation">(</span><span class="StringLit">&quot;d&quot;</span><span class="Punctuation">,</span> <span class="Identifier">value</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Keyword">discard</span> <span class="Identifier">value</span>
<span class="Identifier">doAssert</span> <span class="Identifier">false</span>
<span class="Keyword">do</span><span class="Punctuation">:</span> <span class="Comment"># if &quot;d&quot; notin table</span>
<span class="Identifier">flag</span> <span class="Operator">=</span> <span class="Identifier">true</span>
<span class="Keyword">if</span> <span class="Identifier">flag</span><span class="Punctuation">:</span>
<span class="Identifier">table</span><span class="Punctuation">[</span><span class="StringLit">&quot;d&quot;</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;n&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">mget</span><span class="Punctuation">(</span><span class="StringLit">&quot;a&quot;</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;m&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">mget</span><span class="Punctuation">(</span><span class="StringLit">&quot;d&quot;</span><span class="Punctuation">)</span> <span class="Operator">==</span> <span class="StringLit">&quot;n&quot;</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L97" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L97" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
<div id="withValue.t,SharedTable[A,B],A,untyped,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#withValue.t%2CSharedTable%5BA%2CB%5D%2CA%2Cuntyped%2Cuntyped"><span class="Identifier">withValue</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="sharedtables.html#SharedTable"><span class="Identifier">SharedTable</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">key</span><span class="Other">:</span> <span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">value</span><span class="Other">,</span> <span class="Identifier">body</span><span class="Other">:</span> <a href="system.html#untyped"><span class="Identifier">untyped</span></a><span class="Other">)</span></pre></dt>
<dd>
Retrieves the value at <tt class="docutils literal"><span class="pre"><span class="Identifier">t</span><span class="Punctuation">[</span><span class="Identifier">key</span><span class="Punctuation">]</span></span></tt>. <tt class="docutils literal"><span class="pre"><span class="Identifier">value</span></span></tt> can be modified in the scope of the <tt class="docutils literal"><span class="pre"><span class="Identifier">withValue</span></span></tt> call.
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">var</span> <span class="Identifier">table</span><span class="Punctuation">:</span> <span class="Identifier">SharedTable</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">string</span><span class="Punctuation">]</span>
<span class="Identifier">init</span><span class="Punctuation">(</span><span class="Identifier">table</span><span class="Punctuation">)</span>
<span class="Identifier">table</span><span class="Punctuation">[</span><span class="StringLit">&quot;a&quot;</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;x&quot;</span>
<span class="Identifier">table</span><span class="Punctuation">[</span><span class="StringLit">&quot;b&quot;</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;y&quot;</span>
<span class="Identifier">table</span><span class="Punctuation">[</span><span class="StringLit">&quot;c&quot;</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;z&quot;</span>
<span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">withValue</span><span class="Punctuation">(</span><span class="StringLit">&quot;a&quot;</span><span class="Punctuation">,</span> <span class="Identifier">value</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Identifier">assert</span> <span class="Identifier">value</span><span class="Punctuation">[</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="StringLit">&quot;x&quot;</span>
<span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">withValue</span><span class="Punctuation">(</span><span class="StringLit">&quot;b&quot;</span><span class="Punctuation">,</span> <span class="Identifier">value</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Identifier">value</span><span class="Punctuation">[</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="StringLit">&quot;modified&quot;</span>
<span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">withValue</span><span class="Punctuation">(</span><span class="StringLit">&quot;b&quot;</span><span class="Punctuation">,</span> <span class="Identifier">value</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Identifier">assert</span> <span class="Identifier">value</span><span class="Punctuation">[</span><span class="Punctuation">]</span> <span class="Operator">==</span> <span class="StringLit">&quot;modified&quot;</span>
<span class="Identifier">table</span><span class="Operator">.</span><span class="Identifier">withValue</span><span class="Punctuation">(</span><span class="StringLit">&quot;nonexistent&quot;</span><span class="Punctuation">,</span> <span class="Identifier">value</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
<span class="Identifier">assert</span> <span class="Identifier">false</span> <span class="Comment"># not called</span></pre>
<a href="https://github.com/nim-lang/Nim/tree/devel/lib/pure/collections/sharedtables.nim#L63" class="link-seesrc" target="_blank">Source</a>&nbsp;&nbsp;
<a href="https://github.com/nim-lang/Nim/edit/devel/lib/pure/collections/sharedtables.nim#L63" class="link-seesrc" target="_blank" >Edit</a>&nbsp;&nbsp;
</dd>
</div>
</div>
</dl>
</div>
</div>
</div>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br>
<small style="color: var(--hint);">Made with Nim. Generated: 2025-12-25 20:13:46 UTC</small>
</div>
</div>
</div>
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
</body>
</html>