|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>Accumulators and Counters · DataStructures.jl</title><meta name="title" content="Accumulators and Counters · DataStructures.jl"/><meta property="og:title" content="Accumulators and Counters · DataStructures.jl"/><meta property="twitter:title" content="Accumulators and Counters · DataStructures.jl"/><meta name="description" content="Documentation for DataStructures.jl."/><meta property="og:description" content="Documentation for DataStructures.jl."/><meta property="twitter:description" content="Documentation for DataStructures.jl."/><script data-outdated-warner src="../assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.050/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL=".."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="../assets/documenter.js"></script><script src="../search_index.js"></script><script src="../siteinfo.js"></script><script src="../../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-mocha.css" data-theme-name="catppuccin-mocha"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-macchiato.css" data-theme-name="catppuccin-macchiato"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-frappe.css" data-theme-name="catppuccin-frappe"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/catppuccin-latte.css" data-theme-name="catppuccin-latte"/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="../assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="../assets/themeswap.js"></script></head><body><div id="documenter"><nav class="docs-sidebar"><div class="docs-package-name"><span class="docs-autofit"><a href="../">DataStructures.jl</a></span></div><button class="docs-search-query input is-rounded is-small is-clickable my-2 mx-auto py-1 px-2" id="documenter-search-query">Search docs (Ctrl + /)</button><ul class="docs-menu"><li><a class="tocitem" href="../">DataStructures.jl</a></li><li><a class="tocitem" href="../deque/">Deque</a></li><li><a class="tocitem" href="../circ_buffer/">CircularBuffer</a></li><li><a class="tocitem" href="../circ_deque/">CircularDeque</a></li><li><a class="tocitem" href="../stack/">Stack</a></li><li><a class="tocitem" href="../queue/">Queue</a></li><li><a class="tocitem" href="../priority-queue/">Priority Queue</a></li><li><a class="tocitem" href="../fenwick/">Fenwick Tree</a></li><li class="is-active"><a class="tocitem" href>Accumulators and Counters</a><ul class="internal"><li><a class="tocitem" href="#Constructors"><span>Constructors</span></a></li><li><a class="tocitem" href="#Usage"><span>Usage</span></a></li><li><a class="tocitem" href="#Use-as-a-multiset"><span>Use as a multiset</span></a></li></ul></li><li><a class="tocitem" href="../disjoint_sets/">Disjoint-Sets</a></li><li><a class="tocitem" href="../heaps/">Heaps</a></li><li><a class="tocitem" href="../ordered_containers/">OrderedDicts and OrderedSets</a></li><li><a class="tocitem" href="../default_dict/">DefaultDict and DefaultOrderedDict</a></li><li><a class="tocitem" href="../robin_dict/">RobinDict</a></li><li><a class="tocitem" href="../swiss_dict/">SwissDict</a></li><li><a class="tocitem" href="../trie/">Trie</a></li><li><a class="tocitem" href="../linked_list/">Linked List</a></li><li><a class="tocitem" href="../mutable_linked_list/">Mutable Linked List</a></li><li><a class="tocitem" href="../intset/">DataStructures.IntSet</a></li><li><a class="tocitem" href="../sorted_containers/">Sorted Containers</a></li><li><a class="tocitem" href="../dibit_vector/">DiBitVector</a></li><li><a class="tocitem" href="../red_black_tree/">Red Black Tree</a></li><li><a class="tocitem" href="../avl_tree/">AVL Tree</a></li><li><a class="tocitem" href="../splay_tree/">Splay Tree</a></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><a class="docs-sidebar-button docs-navbar-link fa-solid fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href>Accumulators and Counters</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href>Accumulators and Counters</a></li></ul></nav><div class="docs-right"><a class="docs-navbar-link" href="https://github.com/JuliaCollections/DataStructures.jl" title="View the repository on GitHub"><span class="docs-icon fa-brands"></span><span class="docs-label is-hidden-touch">GitHub</span></a><a class="docs-navbar-link" href="https://github.com/JuliaCollections/DataStructures.jl/blob/master/docs/src/accumulators.md" title="Edit source on GitHub"><span class="docs-icon fa-solid"></span></a><a class="docs-settings-button docs-navbar-link fa-solid fa-gear" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-article-toggle-button fa-solid fa-chevron-up" id="documenter-article-toggle-button" href="javascript:;" title="Collapse all docstrings"></a></div></header><article class="content" id="documenter-page"><h1 id="Accumulators-and-Counters"><a class="docs-heading-anchor" href="#Accumulators-and-Counters">Accumulators and Counters</a><a id="Accumulators-and-Counters-1"></a><a class="docs-heading-anchor-permalink" href="#Accumulators-and-Counters" title="Permalink"></a></h1><p>A accumulator, as defined below, is a data structure that maintains an accumulated number for each key. This is a counter when the accumulated values reflect the counts:</p><pre><code class="language-julia hljs">struct Accumulator{K, V<:Number} |
| 3 | + map::Dict{K, V} |
| 4 | +end</code></pre><h2 id="Constructors"><a class="docs-heading-anchor" href="#Constructors">Constructors</a><a id="Constructors-1"></a><a class="docs-heading-anchor-permalink" href="#Constructors" title="Permalink"></a></h2><p>There are different ways to construct an accumulator/counter:</p><pre><code class="language-julia hljs">a = Accumulator{K, V}() # construct an accumulator with key-type K and |
| 5 | + # accumulated value type V |
| 6 | + |
| 7 | +a = Accumulator(dict) # construct an accumulator from a dictionary |
| 8 | + |
| 9 | +a = counter(K) # construct a counter, i.e. an accumulator with |
| 10 | + # key type K and value type Int |
| 11 | + |
| 12 | +a = counter(dict) # construct a counter from a dictionary |
| 13 | + |
| 14 | +a = counter(seq) # construct a counter by counting keys in a sequence |
| 15 | + |
| 16 | +a = counter(gen) # construct a counter by counting keys in a generator</code></pre><h2 id="Usage"><a class="docs-heading-anchor" href="#Usage">Usage</a><a id="Usage-1"></a><a class="docs-heading-anchor-permalink" href="#Usage" title="Permalink"></a></h2><p>Usage of an accumulator/counter:</p><pre><code class="language-julia hljs"># let a and a2 be accumulators/counters |
| 17 | + |
| 18 | +a[x] # get the current value/count for x, |
| 19 | + # if x was not added to a, it returns zero. |
| 20 | + |
| 21 | +a[x] = v # sets the current value/count for `x` to `v` |
| 22 | + |
| 23 | +inc!(a, x) # increment the value/count for x by 1 |
| 24 | +inc!(a, x, v) # increment the value/count for x by v |
| 25 | + |
| 26 | +dec!(a, x) # decrement the value/count for x by 1 |
| 27 | +dec!(a, x, v) # decrement the value/count for x by v |
| 28 | + |
| 29 | +reset!(a, x) # remove a key x from a, and return its current value |
| 30 | + |
| 31 | +merge!(a, a2) # add all counts from a2 to a1 |
| 32 | +merge(a, a2) # return a new accumulator/counter that combines the |
| 33 | + # values/counts in both a and a2 |
| 34 | + # `a[v] + a2[v]` over all `v` in the universe</code></pre><p><code>merge</code> is the multiset sum operation (sometimes written ⊎).</p><h2 id="Use-as-a-multiset"><a class="docs-heading-anchor" href="#Use-as-a-multiset">Use as a multiset</a><a id="Use-as-a-multiset-1"></a><a class="docs-heading-anchor-permalink" href="#Use-as-a-multiset" title="Permalink"></a></h2><p>An <code>Accumulator{T, <:Integer} where T</code> such as is returned by <code>counter</code>, is a <a href="https://en.wikipedia.org/wiki/Multiset">multiset</a> or Bag, of objects of type <code>T</code>. If the count type is not an integer but a more general real number, then this is a form of fuzzy multiset. We support a number of operations supporting the use of <code>Accumulator</code>s as multisets.</p><p>Note that these operations will throw an error if the accumulator has negative or zero counts for any items.</p><pre><code class="language-julia hljs"> |
| 35 | +setdiff(a1, a2) # The opposite of `merge` (i.e. multiset sum), |
| 36 | + # returns a new multiset with the count of items in `a2` removed from `a1`, down to a minimum of zero |
| 37 | + # `max(a1[v] - a2[v], 0)` over all `v` in the universe |
| 38 | + |
| 39 | + |
| 40 | +union(a1, a2) # multiset union (sometimes called maximum, or lowest common multiple) |
| 41 | + # returns a new multiset with the counts being the higher of those in `a1` or `a2`. |
| 42 | + # `max(a1[v], a2[v])` over all `v` in the universe |
| 43 | + |
| 44 | +intersect(a1, a2) # multiset intersection (sometimes called infimum or greatest common divisor) |
| 45 | + # returns a new multiset with the counts being the lowest of those in `a1` or `a2`. |
| 46 | + # Note that this means things not occurring in both with be removed (count zero). |
| 47 | + # `min(a1[v], a2[v])` over all `v` in the universe</code></pre></article><nav class="docs-footer"><a class="docs-footer-prevpage" href="../fenwick/">« Fenwick Tree</a><a class="docs-footer-nextpage" href="../disjoint_sets/">Disjoint-Sets »</a><div class="flexbox-break"></div><p class="footer-message">Powered by <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> and the <a href="https://julialang.org/">Julia Programming Language</a>.</p></nav></div><div class="modal" id="documenter-settings"><div class="modal-background"></div><div class="modal-card"><header class="modal-card-head"><p class="modal-card-title">Settings</p><button class="delete"></button></header><section class="modal-card-body"><p><label class="label">Theme</label><div class="select"><select id="documenter-themepicker"><option value="auto">Automatic (OS)</option><option value="documenter-light">documenter-light</option><option value="documenter-dark">documenter-dark</option><option value="catppuccin-latte">catppuccin-latte</option><option value="catppuccin-frappe">catppuccin-frappe</option><option value="catppuccin-macchiato">catppuccin-macchiato</option><option value="catppuccin-mocha">catppuccin-mocha</option></select></div></p><hr/><p>This document was generated with <a href="https://github.com/JuliaDocs/Documenter.jl">Documenter.jl</a> version 1.15.0 on <span class="colophon-date" title="Tuesday 4 November 2025 02:08">Tuesday 4 November 2025</span>. Using Julia version 1.12.1.</p></section><footer class="modal-card-foot"></footer></div></div></div></body></html> |
0 commit comments