<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>stlab</title>
    <description>stlab hosts modern, modular C++ algorithms and data structures.
</description>
    <link>http://www.stlab.cc/</link>
    <atom:link href="http://www.stlab.cc/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Wed, 04 Nov 2026 01:09:16 +0000</pubDate>
    <lastBuildDate>Wed, 04 Nov 2026 01:09:16 +0000</lastBuildDate>
    <generator>Jekyll v4.4.1</generator>
    
      <item>
        <title>Forest: An Introduction</title>
        <description>&lt;style&gt;
    svg {
        display: block;
        margin-left: auto;
        margin-right: auto;
       }

    .svg-img {
        display: block;
        margin-left: auto;
        margin-right: auto;
       }
&lt;/style&gt;

&lt;h1 id=&quot;why-hierarchies-are-hard&quot;&gt;Why Hierarchies are Hard&lt;/h1&gt;

&lt;p&gt;From &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vector&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unordered_map&lt;/code&gt;, container types serve to separate the details of the container from the data they maintain. C++ developers are no stranger to this valuable tenet. However, when it comes to hierarchical containers, we find ourselves without a generic go-to solution. At best, we’ll roll our own custom type for the specific use-case. At worst, that implementation embeds the hierarchy into the data itself. Who hasn’t seen something like the below, only to get nerd sniped in all the wrong ways as they have to maintain it?&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;my_type&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;my_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;my_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_first_child&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;my_type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_next_sibling&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;nullptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_my_actual_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;What does it look like to enforce that your hierarchy is actually a hierarchy? What’s to stop a user of your container type from setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_first_child&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_parent&lt;/code&gt;? How many squirrelly traversals do you need to do to assert that the user hasn’t embedded a cycle somewhere, somehow? The policing of such a structure becomes too expensive, and users are left to themselves to make sure they get things right. In the end, they are required to maintain the container’s consistency, which can be more expensive than maintaining the data itself. &lt;strong&gt;If a container type cannot maintain its own internal consistency, it fails a basic requirement of these types.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;easy-hierarchies-beads-on-a-string&quot;&gt;Easy Hierarchies: Beads on a String&lt;/h2&gt;

&lt;p&gt;Let’s go back to childhood for a minute. Imagine if you will a bead with a hole through it and a bit of string that is tied to itself to form a loop:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; height=&quot;200px&quot; src=&quot;/images/forest-introduction/bead_and_string.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s not hard to feed the string through the bead, giving you two loops of string that come out either side of the bead:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; height=&quot;200px&quot; src=&quot;/images/forest-introduction/one_bead.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Feeding the string through another bead is easy enough, giving us the beginnings of some kind of necklace:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; height=&quot;250px&quot; src=&quot;/images/forest-introduction/two_beads.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now let’s add a third, but this time we feed it &lt;em&gt;between&lt;/em&gt; the two beads we have. Since we have only been feeding the string through a bead once, we’ll keep following that rule:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; height=&quot;250px&quot; src=&quot;/images/forest-introduction/three_beads.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It turns out, if you follow the rule that the string can only be fed once through a bead, you cannot help but make a hierarchy. No matter where you add a new bead, you still have a hierarchy:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; height=&quot;350px&quot; src=&quot;/images/forest-introduction/five_beads.png&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;enter-the-forest&quot;&gt;Enter the Forest&lt;/h1&gt;

&lt;p&gt;It is not a big step to transform our beads into nodes, and the loop of string into the edges between them. Sean Parent made this discovery many years back, spoke at length about it in a &lt;a href=&quot;https://youtu.be/sWgDk-o-6ZE?t=2668&quot;&gt;CppCon talk from 2015&lt;/a&gt;, and codified it into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;&lt;/code&gt;. The remainder of this article will cover the high-level concepts of a forest, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;&lt;/code&gt; implementation details, and a few examples. The next article will go more in-depth on an example that makes good use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-root-node&quot;&gt;The Root Node&lt;/h2&gt;

&lt;p&gt;Every forest has a root node, which is not a node used to store values in the forest. Rather, its primary purpose is as the anchor to which all top-level nodes in the forest are attached. (Going back to our childhood illustration, this is the case where all you have is the string.) Here, we will draw the root node as a rectangle:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/root.svg&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;nodes&quot;&gt;Nodes&lt;/h2&gt;

&lt;p&gt;The forest’s value type is the node. They are heap-allocated as necessary for storing user data. Each node has exactly one parent, zero or more children, and zero or more siblings. Here, we will draw nodes as circles:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/node.svg&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;edges&quot;&gt;Edges&lt;/h2&gt;

&lt;p&gt;Forest nodes are connected by edges. For every node in the forest, there are always two edges that lead away from the node, and two that lead towards it. Here, edges are drawn as arrows pointed in the direction of forward travel:&lt;/p&gt;

&lt;svg width=&quot;125&quot; height=&quot;125&quot; viewBox=&quot;0 0 250 250&quot;&gt;
    &lt;defs&gt;
        &lt;marker id=&quot;arrowhead&quot; markerWidth=&quot;6&quot; markerHeight=&quot;5&quot; refX=&quot;5&quot; refY=&quot;2.5&quot; orient=&quot;auto&quot;&gt;
            &lt;polygon points=&quot;0 0, 6 2.5, 0 5&quot; /&gt;
        &lt;/marker&gt;
        &lt;g id=&quot;root&quot;&gt;
            &lt;rect width=&quot;100&quot; height=&quot;100&quot; x=&quot;-50&quot; y=&quot;-50&quot; fill=&quot;white&quot; stroke=&quot;darkred&quot; stroke-width=&quot;5&quot; /&gt;
            &lt;text font-size=&quot;36&quot; text-anchor=&quot;middle&quot; dominant-baseline=&quot;central&quot;&gt;R&lt;/text&gt;
        &lt;/g&gt;
        &lt;g id=&quot;node&quot;&gt;
            &lt;circle r=&quot;50&quot; fill=&quot;white&quot; stroke=&quot;blue&quot; stroke-width=&quot;5&quot; /&gt;
            &lt;text font-size=&quot;36&quot; text-anchor=&quot;middle&quot; dominant-baseline=&quot;central&quot;&gt;N&lt;tspan dy=&quot;15&quot; font-size=&quot;.7em&quot;&gt;&lt;/tspan&gt;&lt;/text&gt;
        &lt;/g&gt;
        &lt;g id=&quot;parent&quot;&gt;
            &lt;circle r=&quot;50&quot; fill=&quot;white&quot; stroke=&quot;grey&quot; stroke-width=&quot;5&quot; /&gt;
            &lt;text font-size=&quot;36&quot; text-anchor=&quot;middle&quot; fill=&quot;grey&quot; dominant-baseline=&quot;central&quot;&gt;P&lt;tspan dy=&quot;15&quot; font-size=&quot;.7em&quot;&gt;&lt;/tspan&gt;&lt;/text&gt;
        &lt;/g&gt;
        &lt;g id=&quot;sib_prior&quot;&gt;
            &lt;circle r=&quot;50&quot; fill=&quot;white&quot; stroke=&quot;grey&quot; stroke-width=&quot;5&quot; /&gt;
            &lt;text font-size=&quot;36&quot; text-anchor=&quot;middle&quot; fill=&quot;grey&quot; dominant-baseline=&quot;central&quot;&gt;S&lt;tspan dy=&quot;15&quot; font-size=&quot;.7em&quot;&gt;prior&lt;/tspan&gt;&lt;/text&gt;
        &lt;/g&gt;
        &lt;g id=&quot;sib_next&quot;&gt;
            &lt;circle r=&quot;50&quot; fill=&quot;white&quot; stroke=&quot;grey&quot; stroke-width=&quot;5&quot; /&gt;
            &lt;text font-size=&quot;36&quot; text-anchor=&quot;middle&quot; fill=&quot;grey&quot; dominant-baseline=&quot;central&quot;&gt;S&lt;tspan dy=&quot;15&quot; font-size=&quot;.7em&quot;&gt;next&lt;/tspan&gt;&lt;/text&gt;
        &lt;/g&gt;
        &lt;g id=&quot;child_first&quot;&gt;
            &lt;circle r=&quot;50&quot; fill=&quot;white&quot; stroke=&quot;grey&quot; stroke-width=&quot;5&quot; /&gt;
            &lt;text font-size=&quot;36&quot; text-anchor=&quot;middle&quot; fill=&quot;grey&quot; dominant-baseline=&quot;central&quot;&gt;C&lt;tspan dy=&quot;15&quot; font-size=&quot;.7em&quot;&gt;first&lt;/tspan&gt;&lt;/text&gt;
        &lt;/g&gt;
        &lt;g id=&quot;child_last&quot;&gt;
            &lt;circle r=&quot;50&quot; fill=&quot;white&quot; stroke=&quot;grey&quot; stroke-width=&quot;5&quot; /&gt;
            &lt;text font-size=&quot;36&quot; text-anchor=&quot;middle&quot; fill=&quot;grey&quot; dominant-baseline=&quot;central&quot;&gt;C&lt;tspan dy=&quot;15&quot; font-size=&quot;.7em&quot;&gt;last&lt;/tspan&gt;&lt;/text&gt;
        &lt;/g&gt;
        &lt;g id=&quot;edge_lo_ti&quot;&gt;
            &lt;path d=&quot;M -25 50 Q -75 125 0 125 Q 75 125 25 50&quot; fill=&quot;none&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_li&quot;&gt;
            &lt;line x1=&quot;-100&quot; y1=&quot;-100&quot; x2=&quot;-40&quot; y2=&quot;-40&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_lo&quot;&gt;
            &lt;line x1=&quot;-40&quot; y1=&quot;40&quot; x2=&quot;-100&quot; y2=&quot;100&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_ti&quot;&gt;
            &lt;line x1=&quot;100&quot; y1=&quot;100&quot; x2=&quot;40&quot; y2=&quot;40&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_to&quot;&gt;
            &lt;line x1=&quot;40&quot; y1=&quot;-40&quot; x2=&quot;100&quot; y2=&quot;-100&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_lo_li&quot;&gt;
            &lt;path d=&quot;M 0 50 Q -50 100 0 150&quot; fill=&quot;none&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_lo_li_2&quot;&gt;
            &lt;path d=&quot;M 100 50 Q -50 100 0 150&quot; fill=&quot;none&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_to_li&quot;&gt;
            &lt;path d=&quot;M 50 0 Q 100 -50 150 0&quot; fill=&quot;none&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_to_ti&quot;&gt;
            &lt;path d=&quot;M 50 150 Q 100 100 50 50&quot; fill=&quot;none&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
        &lt;g id=&quot;edge_to_ti_2&quot;&gt;
            &lt;path d=&quot;M 50 150 Q 100 100 -50 50&quot; fill=&quot;none&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; marker-end=&quot;url(#arrowhead)&quot; /&gt;?
        &lt;/g&gt;
    &lt;/defs&gt;
    &lt;use xlink:href=&quot;#edge_li&quot; x=&quot;125&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_lo&quot; x=&quot;125&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_ti&quot; x=&quot;125&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_to&quot; x=&quot;125&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#node&quot; x=&quot;125&quot; y=&quot;125&quot; /&gt;
&lt;/svg&gt;

&lt;p&gt;The two left edges are known as &lt;strong&gt;leading edges&lt;/strong&gt;, and the two right edges are known as &lt;strong&gt;trailing edges&lt;/strong&gt;. (The terms come from the edges’ order of visitation during a full order traversal of the forest.)&lt;/p&gt;

&lt;svg width=&quot;175&quot; height=&quot;125&quot; viewBox=&quot;0 0 350 250&quot;&gt;
    &lt;use xlink:href=&quot;#edge_li&quot; x=&quot;175&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_lo&quot; x=&quot;175&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_ti&quot; x=&quot;175&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_to&quot; x=&quot;175&quot; y=&quot;125&quot; /&gt;
    &lt;use xlink:href=&quot;#node&quot; x=&quot;175&quot; y=&quot;125&quot; /&gt;
    &lt;line x1=&quot;175&quot; y1=&quot;25&quot; x2=&quot;175&quot; y2=&quot;225&quot; stroke=&quot;black&quot; stroke-width=&quot;5&quot; stroke-dasharray=&quot;10&quot; /&gt;?
    &lt;text font-size=&quot;24&quot; x=&quot;25&quot; y=&quot;125&quot; dominant-baseline=&quot;central&quot;&gt;leading&lt;/text&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;245&quot; y=&quot;125&quot; dominant-baseline=&quot;central&quot;&gt;trailing&lt;/text&gt;
&lt;/svg&gt;

&lt;p&gt;The two edges that point to $N$ are known as &lt;strong&gt;in-edges&lt;/strong&gt; and the edges that point away from $N$ are &lt;strong&gt;out-edges&lt;/strong&gt;:&lt;/p&gt;

&lt;svg width=&quot;150&quot; height=&quot;150&quot; viewBox=&quot;0 0 300 300&quot;&gt;
    &lt;use xlink:href=&quot;#edge_li&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_lo&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_ti&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_to&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#node&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;25&quot; y=&quot;30&quot; dominant-baseline=&quot;central&quot;&gt;in&lt;/text&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;10&quot; y=&quot;260&quot; dominant-baseline=&quot;central&quot;&gt;out&lt;/text&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;250&quot; y=&quot;260&quot; dominant-baseline=&quot;central&quot;&gt;in&lt;/text&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;250&quot; y=&quot;30&quot; dominant-baseline=&quot;central&quot;&gt;out&lt;/text&gt;
&lt;/svg&gt;

&lt;p&gt;It is worth noting that the terms “in” and “out” are relative to $N$. In other words, an in-edge for $N$ will also be an out-edge for some other node.&lt;/p&gt;

&lt;h2 id=&quot;iterators&quot;&gt;Iterators&lt;/h2&gt;

&lt;p&gt;Forest iterators are bidirectional: although the diagrams are simplified by drawing one-way edges, in reality they are doubly-linked. Forest iterators come in forward, reverse, full order, preorder, and postorder flavors, and all are comprised of two pieces of data:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The node ($N$) to which they point&lt;/li&gt;
  &lt;li&gt;Whether they are on the leading ($L$) or trailing ($T$) edge of the node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, iterators will be described by this pair as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{node, edge}&lt;/code&gt;. Since iterators always point to a node, we never speak of an iterator being on the out-edge of a node it is leaving. Rather, we always consider it on the in-edge of the node that it points to. Visually, iterators will only ever be on the northwest or southeast edge of the node to which they point.&lt;/p&gt;

&lt;p&gt;An iterator pointing to the root node of a forest can be compared to other iterators in the same forest, but cannot be dereferenced.&lt;/p&gt;

&lt;h3 id=&quot;examples&quot;&gt;Examples&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{P, L}&lt;/code&gt; is an iterator that points to node $P$ on the leading edge:&lt;/li&gt;
&lt;/ul&gt;

&lt;svg width=&quot;112.5&quot; height=&quot;112.5&quot; viewBox=&quot;0 0 225 225&quot;&gt;
    &lt;use xlink:href=&quot;#edge_li&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#parent&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;25&quot; y=&quot;30&quot; dominant-baseline=&quot;central&quot;&gt;{P, L}&lt;/text&gt;
&lt;/svg&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, T}&lt;/code&gt; is an iterator that points to node $N$ on the trailing edge:&lt;/li&gt;
&lt;/ul&gt;

&lt;svg width=&quot;112.5&quot; height=&quot;112.5&quot; viewBox=&quot;0 0 225 225&quot;&gt;
    &lt;use xlink:href=&quot;#node&quot; x=&quot;75&quot; y=&quot;75&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_ti&quot; x=&quot;75&quot; y=&quot;75&quot; /&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;150&quot; y=&quot;200&quot; dominant-baseline=&quot;central&quot;&gt;{N, T}&lt;/text&gt;
&lt;/svg&gt;

&lt;h3 id=&quot;iterator-advancement&quot;&gt;Iterator Advancement&lt;/h3&gt;

&lt;p&gt;While pointing at a node, there are always two ways an iterator could advance: the leading out-edge, or the trailing out-edge. The rule of thumb for iterator advancement is that iterators prefer the edge they are on. Thus, if an iterator is on the leading edge of a node, it will advance through the leading out-edge (and so for a trailing edge iterator.)&lt;/p&gt;

&lt;p&gt;There are two specific cases where this rule does not hold. The first is on a leaf node, where the leading out-edge of the node arrives at the trailing in-edge of that same node. In this case, the iterator is flipped from leading to trailing. The second is when an iterator traverses from the trailing out-edge of a node to the leading in-edge of its next sibling. In this case, the iterator is flipped from the trailing to leading.&lt;/p&gt;

&lt;h3 id=&quot;edge-flipping&quot;&gt;Edge Flipping&lt;/h3&gt;

&lt;p&gt;Utility functions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::leading_of(I)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::trailing_of(I)&lt;/code&gt; change the edge of an iterator to leading or trailing, respectively. It does not matter what edge the iterator was originally on, and the iterator will still point to the same node. These are free functions, not members of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;&lt;/code&gt;. In this document we will represent these routines in pseudocode as such:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leading_of({P, X})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{P, L}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trailing_of({N, X})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, T}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In C++, these calls are straightforward:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;leading_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;A&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leading_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest_leading_edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leading_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;A&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trailing_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leading_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest_trailing_edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;A&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;a-note-on-begin-and-end&quot;&gt;A note on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;begin()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end()&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;begin()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end()&lt;/code&gt; in a forest behave just like their equivalent member functions found in a typical standard library container.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::end()&lt;/code&gt; will always return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Root, T}&lt;/code&gt;. This is true regardless of whether or not the forest is empty.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::begin()&lt;/code&gt; will always return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++{Root, L}&lt;/code&gt;. This follows the rules of the leading out-edge explained above, meaning that it will either point to the first node in the forest (if it is not empty), or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end()&lt;/code&gt; (if it is).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;forest-edge-constraints&quot;&gt;Forest Edge Constraints&lt;/h2&gt;

&lt;p&gt;Now that we know about nodes, edges, and iterators, there are additional constraints the forest imposes upon its edges that maintain its structure. (Going back to our original illustration, these constraints make sure the string is only fed once through a bead.)&lt;/p&gt;

&lt;h3 id=&quot;leading-in-edge&quot;&gt;Leading In-Edge&lt;/h3&gt;

&lt;p&gt;Visually, the northwest edge is the &lt;strong&gt;leading in-edge&lt;/strong&gt;. It originates (is an out-edge) from one of two possible nodes. First, if $N$ is the first child of its parent node $P$, this edge is the leading out-edge of $P$:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/li_child.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Otherwise, this edge is the trailing out-edge of $N$’s prior sibling $S_{prior}$:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/li_sibling.svg&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;leading-out-edge&quot;&gt;Leading Out-Edge&lt;/h3&gt;

&lt;p&gt;The southwest edge is the &lt;strong&gt;leading out-edge&lt;/strong&gt;. It terminates (is an in-edge) at one of two possible nodes. First, if $N$ is a parent, this edge is the leading out-edge of the first child of $N$, $C_{first}$:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/lo_parent.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Otherwise, this edge is the trailing out-edge of $N$ itself:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/lo_self.svg&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;trailing-in-edge&quot;&gt;Trailing In-Edge&lt;/h3&gt;

&lt;p&gt;The southeast edge is the &lt;strong&gt;trailing in-edge&lt;/strong&gt;. It originates (is an out-edge) from one of two possible nodes. First, if $N$ is a parent, it comes from the trailing out-edge of $N$’s last child, $C_{last}$:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/ti_parent.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Otherwise, it comes from the leading out-edge of $N$ itself:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/ti_self.svg&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;trailing-out-edge&quot;&gt;Trailing Out-Edge&lt;/h3&gt;

&lt;p&gt;The northeast edge is the &lt;strong&gt;trailing out-edge&lt;/strong&gt;. It terminates (is an in-edge) at one of two related nodes. First, if $N$ is the last child of $P$, this edge points to the trailing out-edge of $P$:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/to_child.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Otherwise, this edge points to the leading out-edge of $N$’s next sibling:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/to_sibling.svg&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;example-forests&quot;&gt;Example Forests&lt;/h2&gt;

&lt;p&gt;With the fundamental building blocks in place, here is how they combine to form some of the basic relationships in a forest. Note that every node in the forest maintains four relationships with the nodes around it.&lt;/p&gt;

&lt;p&gt;The examples also include C++ that would construct the forest in question. If the code does not make sense right now, don’t worry: we’ll cover what’s missing in the following sections. If you’d like, you can skip over it now and revisit it later.&lt;/p&gt;

&lt;h3 id=&quot;empty&quot;&gt;Empty&lt;/h3&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/empty.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Given a forest that contains only the root node, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::empty()&lt;/code&gt; will return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;. Otherwise, it will return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;the-root-top-loop&quot;&gt;The Root Top Loop&lt;/h4&gt;

&lt;p&gt;The root has a trailing out-edge which always points to its leading in-edge:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/root_topped.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This “root top loop” is a unique property of the forest, and is used as a termination case in some traversals. For example, since preorder traversal iterators always point to a leading edge, the end iterator is the root top loop. (Because it is such a specialized implementation detail, the root top loop is often omitted when diagramming.)&lt;/p&gt;

&lt;h3 id=&quot;one-node&quot;&gt;One Node&lt;/h3&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/one_node.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To construct the above forest, the code might look like this:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;A&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f.begin()&lt;/code&gt; would work equally well as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f.end()&lt;/code&gt; in the above case. Like the standard containers, they are equal when a forest is empty.&lt;/p&gt;

&lt;h3 id=&quot;two-sibling-nodes&quot;&gt;Two Sibling Nodes&lt;/h3&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/two_nodes.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To construct the above forest, the code might look like this:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;A&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;B&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;many-nodes-and-levels&quot;&gt;Many Nodes and Levels&lt;/h3&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/sample.svg&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;A&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;B&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;C&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_iter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;D&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;E&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;F&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;G&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;H&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;I&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;J&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_iter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;K&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The frequent use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::trailing_of&lt;/code&gt; is necessary to insert subsequent nodes as children of the newly inserted nodes. Without flipping the iterator to the trailing edge, new nodes inserted with those iterators would be added as prior siblings, not children.&lt;/p&gt;

&lt;h2 id=&quot;node-insertion&quot;&gt;Node Insertion&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::insert&lt;/code&gt; requires an iterator (where to insert) and a value (what to insert). The iterator is never invalidated during an insertion. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; returns a leading edge iterator to the new node.&lt;/p&gt;

&lt;h3 id=&quot;leading-edge-insertion&quot;&gt;Leading Edge Insertion&lt;/h3&gt;

&lt;p&gt;When the location for insertion is on the leading edge of $N$ (that is, $I$ is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, L}&lt;/code&gt;) the new node will be created as $N$’s new prior sibling. So:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/l_insert_before.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;becomes the following. Note that the iterator is unchanged, still pointing to $N$ on the leading edge even after the insertion:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/l_insert_after.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Sprior, L}&lt;/code&gt;. Leading edge insertion can be used to repeatedly “push back” prior siblings of $N$. To repeatedly “push front” prior siblings of $N$, use the resulting iterator of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; as the next insertion position.&lt;/p&gt;

&lt;h3 id=&quot;trailing-edge-insertion&quot;&gt;Trailing Edge Insertion&lt;/h3&gt;

&lt;p&gt;When the location for insertion is on the trailing edge of $N$ (that is, $I$ is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, T}&lt;/code&gt;) the new node will be created as $N$’s new last child. So:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/t_insert_before.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;becomes the following. Again, note that original iterator $I$ is unchanged after the call:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/t_insert_after.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Clast, L}&lt;/code&gt;. Trailing edge insertion can be used to repeatedly “push back” children of $N$. To repeatedly “push front” children of $N$, use the resulting iterator of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; as the next insertion position.&lt;/p&gt;

&lt;h2 id=&quot;forest-traversal&quot;&gt;Forest Traversal&lt;/h2&gt;

&lt;h3 id=&quot;full-order&quot;&gt;Full order&lt;/h3&gt;

&lt;p&gt;The traversal behavior of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;::iterator&lt;/code&gt; is always full order. This means every node is visited twice: first, right before any of its children are visited (on the leading edge), and then again after the last child is visited (on the trailing edge). This behavior is recursive, and results in a depth-first traversal of the forest:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/fullorder_one.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In C++, one might output the above labels with the following code. The exception of course is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7&lt;/code&gt;, which cannot be output because it is the forest’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end()&lt;/code&gt; iterator.&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;\n&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here is the same diagram with the leading and trailing edges colorized green and red, respectively:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/fullorder_two.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To output the “color” of the edges, one might write the following:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest_leading_edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; green&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; red&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;full-order-traversal-of-ns-subtree&quot;&gt;Full order Traversal of $N$’s Subtree&lt;/h3&gt;

&lt;p&gt;To traverse a node $N$ and all of its descendants, the iterator range is:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leading_of({N, x})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++trailing_of({N, x})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;svg width=&quot;175&quot; height=&quot;150&quot; viewBox=&quot;0 0 350 300&quot;&gt;
    &lt;use xlink:href=&quot;#edge_li&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_lo&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_ti&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#edge_to&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;use xlink:href=&quot;#node&quot; x=&quot;150&quot; y=&quot;150&quot; /&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;0&quot; y=&quot;30&quot; dominant-baseline=&quot;central&quot;&gt;{N, L} (first)&lt;/text&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;250&quot; y=&quot;260&quot; dominant-baseline=&quot;central&quot;&gt;{N, T}&lt;/text&gt;
    &lt;text font-size=&quot;24&quot; x=&quot;200&quot; y=&quot;30&quot; dominant-baseline=&quot;central&quot;&gt;++{N, T} (last)&lt;/text&gt;
&lt;/svg&gt;

&lt;p&gt;Note this technique does not work if $N==R$. In that case, you’d be traversing the entire forest, so you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::begin()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::end()&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;preorder-traversal&quot;&gt;Preorder Traversal&lt;/h3&gt;

&lt;p&gt;A preorder traversal of the forest will visit every node once. During preorder traversal, a parent will be visited before its children. We achieve preorder iteration by incrementing full order repeatedly, visiting a node only when the iterator is on a leading edge. Note the use of the root top loop as the terminating iterator for a full-forest preorder traversal:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/preorder.svg&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;implementation&quot;&gt;Implementation&lt;/h4&gt;

&lt;p&gt;A C++ implementation of preorder traversal may look like this:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;preorder_traversal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;preorder_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{[](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest_trailing_edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;preorder_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;preorder_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())};&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;preorder_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A better solution would be to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;preorder_range&lt;/code&gt; utility already provided by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;preorder_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// each node in the forest will be visited once, in preorder order.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;postorder-traversal&quot;&gt;Postorder Traversal&lt;/h3&gt;

&lt;p&gt;A postorder traversal of the forest will visit every node once. During postorder traversal, a parent will be visited after its children. We achieve postorder iteration by incrementing full order repeatedly, visiting a node only when the iterator is on a trailing edge:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/postorder.svg&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;implementation-1&quot;&gt;Implementation&lt;/h4&gt;

&lt;p&gt;A C++ implementation of postorder traversal may look like this:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;postorder_traversal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postorder_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{[](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest_leading_edge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;postorder_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()};&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postorder_next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A better solution would be to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postorder_range&lt;/code&gt; utility already provided by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postorder_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// each node in the forest will be visited once, in postorder order.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;child-traversal&quot;&gt;Child Traversal&lt;/h3&gt;

&lt;p&gt;A child traversal of a node P traverses only its immediate children; P itself is not visited. Child traversal is achieved by setting the edge of the iterator to trailing, then incrementing it:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++trailing_of({Cn, L})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Cn+1, L}&lt;/code&gt; (next child) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{P, T}&lt;/code&gt; (the end of the range)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/child_iteration.svg&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;implementation-2&quot;&gt;Implementation&lt;/h4&gt;

&lt;p&gt;A C++ implementation of child traversal may look like this. Note that the iterator passed points to the parent whose children we are going to traverse:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ForestIterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;child_traversal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ForestIterator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leading_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)};&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stlab&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;trailing_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A better solution would be to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;child_range&lt;/code&gt; utility already provided by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;child_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_forest_iterator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// each child of the node at my_forest_iterator will be visited once&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To iterate just the top-level nodes in the forest, use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;::root()&lt;/code&gt; to get an iterator to the root node:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;child_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_forest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// each top-level node will be visited once&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1 id=&quot;erasing-nodes&quot;&gt;Erasing Nodes&lt;/h1&gt;

&lt;p&gt;Like most STL containers, erasing nodes in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&lt;/code&gt; starts by passing two iterators denoting the range to erase. The rule for deleting forest nodes is this: a node will be erased if and only if the deletion traversal passes through that node twice. The nodes to be erased from the forest are always processed bottom-up. This means that a parent’s child nodes will always be deleted before the parent. It also means that all nodes will be leaf nodes at the time they are erased.&lt;/p&gt;

&lt;h2 id=&quot;erasing-example&quot;&gt;Erasing Example&lt;/h2&gt;

&lt;p&gt;Consider the following forest and the defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last&lt;/code&gt; iterators into that forest for erasing:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/erase_begin.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Initially, the iterators are at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{B, L}&lt;/code&gt; and at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{R, T}&lt;/code&gt;. The nodes $B$ and $C$ will be deleted because the traversal from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last&lt;/code&gt; will go through each of those nodes twice. Even though the deletion iterator will pass through $A$, it is not deleted because it only passes through on the trailing edge, not on the leading edge:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/erase_middle.svg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After the above erase has completed, the resulting forest will be:&lt;/p&gt;

&lt;p&gt;&lt;img class=&quot;svg-img&quot; src=&quot;/images/forest-introduction/svg/erase_end.svg&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;Containers should make a developer’s life easier, not harder. When a container type requires you to think about maintaining its validity, it fails to achieve that intent. Forests present one solution to the hierarchy container type that is internally consistent. By maintaining itself, a forest lets you get back to handling the data, instead of the container.&lt;/p&gt;

&lt;h1 id=&quot;the-sources&quot;&gt;The Sources&lt;/h1&gt;

&lt;p&gt;“Enough already,” you cry, “show me the sources!” &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stlab::forest&amp;lt;T&amp;gt;&lt;/code&gt; is maintained as part of the Stlab Library, and is available &lt;a href=&quot;https://github.com/stlab/stlab/blob/develop/stlab/forest.hpp&quot;&gt;on GitHub&lt;/a&gt;. We also have &lt;a href=&quot;http://stlab.cc/libraries/forest.hpp/&quot;&gt;documentation&lt;/a&gt; for the class, as well as utility data structures and forest algorithms.&lt;/p&gt;
</description>
        <pubDate>Tue, 01 Dec 2020 00:00:00 +0000</pubDate>
        <link>http://www.stlab.cc/2020/12/01/forest-introduction.html</link>
        <guid isPermaLink="true">http://www.stlab.cc/2020/12/01/forest-introduction.html</guid>
        
        
      </item>
    
      <item>
        <title>Small Object Optimization for Polymorphic Types</title>
        <description>&lt;p&gt;At &lt;a href=&quot;http://meetingcpp.com/2017/&quot;&gt;Meeting C++ 2017&lt;/a&gt; I presented a lightning talk &lt;a href=&quot;http://sean-parent.stlab.cc/papers-and-presentations#polymorphic-task-template-in-ten&quot;&gt;Polymorphic Task Template in Ten&lt;/a&gt; which showed an easy way to implement a polymorphic task template, similar to &lt;a href=&quot;http://en.cppreference.com/w/cpp/utility/functional/function&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::function&lt;/code&gt;&lt;/a&gt;, with a small object optimization in 10 minutes.&lt;/p&gt;

&lt;p&gt;Unfortunately, the small object optimization, as described, leads to &lt;a href=&quot;http://en.cppreference.com/w/cpp/language/ub&quot;&gt;&lt;em&gt;undefined behavior&lt;/em&gt;&lt;/a&gt;. As a general rule, you cannot store an object of one type in memory and read it back as another, even if the other type is the objects base class (there are exceptions to this rule, but they do not apply to this situation).&lt;/p&gt;

&lt;p&gt;This tip looks at the details of the issue and describes a solution which turns out to be more efficient although it does require slightly more, and more complex, code to implement. A small object optimization for polymorphic types is a very useful construct so it important to understand how it can be done correctly.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;the-problem&quot;&gt;The Problem&lt;/h2&gt;

&lt;p&gt;If you haven’t seen &lt;a href=&quot;https://www.youtube.com/watch?v=2KGkcGtGVM4&quot;&gt;the talk&lt;/a&gt;, I recommend watching it before reading further.&lt;/p&gt;

&lt;p&gt;Maikel Nadolski pointed out in the #meetingcpp Slack channel that this solution relies on undefined behavior because it reads back an object as a different type than what was stored.&lt;/p&gt;

&lt;p&gt;From the example in the talk, here is the issue:&lt;/p&gt;

&lt;p&gt;The constructor for the task is using placement new on a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt; type in the aligned storage member, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_data&lt;/code&gt; .&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decay_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;small_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decay_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The code to access the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt; does so by casting the address of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_data&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept&lt;/code&gt;, a base class of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept&lt;/code&gt; was not stored at this location, this cast is only allowed if the pointer types &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model*&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept*&lt;/code&gt; are &lt;a href=&quot;http://en.cppreference.com/w/cpp/language/static_cast&quot;&gt;&lt;em&gt;pointer-interconvertible&lt;/em&gt;&lt;/a&gt;. Pointer-interconvertible means that a void* which points to an object of one type, also points to an object of the other type. That would only be true if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt; is a &lt;a href=&quot;http://en.cppreference.com/w/cpp/named_req/StandardLayoutType&quot;&gt;&lt;em&gt;standard-layout class&lt;/em&gt;&lt;/a&gt;. Unfortunately, that is not the case for two reasons.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt; (and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept&lt;/code&gt;) contain virtual functions.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt; may have a member which is not a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StandardLayoutType&lt;/code&gt; because it holds an arbitrary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Callable&lt;/code&gt; object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of this, the cast to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept&lt;/code&gt; is undefined behavior.&lt;/p&gt;

&lt;h2 id=&quot;the-fix&quot;&gt;The Fix&lt;/h2&gt;

&lt;p&gt;One possible fix is to store the pointer returned by placement new, cast to the base &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept*&lt;/code&gt;. I believe this is the approach taken by most standard library implementations. This costs an extra word of storage, and an extra indirection on every use, and it will &lt;em&gt;always&lt;/em&gt; (as far as I can determine) be pointing to the aligned storage (a fixed offset), but it is not guaranteed to point directly to the aligned storage.&lt;/p&gt;

&lt;p&gt;I could lobby the standard committee to loosen the requirements on pointer-interconvertible. As one example; the class &lt;a href=&quot;https://github.com/stlab/adobe_source_libraries/blob/master/adobe/any_regular.hpp&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adobe::any_regular_t&lt;/code&gt;&lt;/a&gt;, which has been in fairly wide use for over a decade, suffers the same problem when the captured value is not a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;StandardLayoutType&lt;/code&gt;. I believe the standard should ratify existing practice, but this is a very complex area so I will leave it to those who have a deeper understanding to debate.&lt;/p&gt;

&lt;p&gt;There is a trick used in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any_regular_t&lt;/code&gt; to remove the need for virtual member functions which can be used to fix the task implementation (and to fix &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any_regular_t&lt;/code&gt;). &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any_regular_t&lt;/code&gt; still uses inheritance, but it avoids language virtual functions by building &lt;a href=&quot;https://en.wikipedia.org/wiki/Virtual_method_table&quot;&gt;vtables&lt;/a&gt; with templates. This was done to guarantee object layout for ABI compatibility. To have a complete solution, we also need to remove inheritance. To do that, we split the vtable pointer for our pure virtual base class from the remainder of the object. Seeing how this is done may be very instructive if you are not familiar with how vtables work.&lt;/p&gt;

&lt;p&gt;Using the terminology from my talk, I recast the idea of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept&lt;/code&gt; class into a struct holding function pointers for the virtual operations. The definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;task::concept&lt;/code&gt; becomes:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_dtor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first argument to each function pointer will be the &lt;em&gt;this&lt;/em&gt; pointer. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_dtor&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_move&lt;/code&gt; functions are used to implement the destructor and both the move constructor and move assignment operators. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_invoke&lt;/code&gt; function is used to invoke the type erased &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Callable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The task stores a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const concept*&lt;/code&gt;, simply called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_concept&lt;/code&gt; (this is our vtable pointer), as well as aligned storage for the model. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_concept&lt;/code&gt; member is initialized to point to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept&lt;/code&gt; with a do nothing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_dtor&lt;/code&gt; function. This is the equivalent of declaring a pure base class as:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;concept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But since we can’t use inheritance or virtual functions, we write this as:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{[](&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}};&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// vtable&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// vtable pointer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To implement a model declare a set of static functions, which will act as virtual functions, and use them to populate the vtable. The static functions can cast the first argument to the model type safely (the &lt;em&gt;this&lt;/em&gt; pointer) and then access any members in the model. Our small model looks like this:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_dtor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)...);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vtable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_dtor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And the large model (where the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Callable&lt;/code&gt; is held by a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unique_ptr&lt;/code&gt;) looks like this:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;G&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;make_unique&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_dtor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)...);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vtable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_dtor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;unique_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is straightforward to fill in the basic operations on the task in terms of model operations:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;concept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Small&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;small_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{[](&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}};&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;aligned_storage_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;small_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decay_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;small_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decay_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decay_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_small&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vtable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_dtor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noexcept&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_dtor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_concept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_invoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)...);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;task&lt;/code&gt; never casts to either a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concept&lt;/code&gt; or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt; pointer, instead a pointer to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_model&lt;/code&gt; storage is passed to a function in the vtable which casts the pointer to the correct model type stored in that memory. Undefined behavior is avoided because the pointer is only ever cast to the stored type.&lt;/p&gt;

&lt;p&gt;Using this technique generates less code, largely because without virtual functions there are no tables for RTTI generated. But even looking beyond that the code appears to be slightly more optimal (if anyone wants to construct a good benchmark, post a link in the comments). Here is the &lt;a href=&quot;https://godbolt.org/g/ki2TSJ&quot;&gt;original code&lt;/a&gt;, with undefined behavior, presented at my talk and here is the &lt;a href=&quot;https://godbolt.org/g/oxBz9T&quot;&gt;code without inheritance or language virtual functions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I’m in the process of updating the &lt;a href=&quot;https://github.com/stlab/stlab/pull/118&quot;&gt;stlab library implementation of tasks&lt;/a&gt;. It hasn’t been decided if we roll this into a 1.1.2 bug fix release or hold it for a 1.2 release. I’m also looking at fixing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adobe::any_regular_t&lt;/code&gt; in ASL.&lt;/p&gt;

&lt;p&gt;I’ll update my lighting talk if I get asked to present it again. This solution is a little more complicated but it still demonstrates the power of using concept based polymorphism to shift the allocation strategy to the use of the objects.&lt;/p&gt;

&lt;p&gt;As part of preparing this errata, Eric Niebler informed me that his &lt;a href=&quot;https://github.com/facebook/folly/blob/master/folly/docs/Poly.md&quot;&gt;folly/Poly library&lt;/a&gt; uses a similar technique to implement the small object optimization.&lt;/p&gt;

&lt;p&gt;I frequently tell developers that writing papers and presenting talks is a really good way to improve your code. It can be a bit embarrassing when you present a great way to use undefined behavior at a major conference, but this is how we learn and grow as professionals. Thanks Maikel for the catch!&lt;/p&gt;
</description>
        <pubDate>Sat, 23 Dec 2017 00:00:00 +0000</pubDate>
        <link>http://www.stlab.cc/tip/2017/12/23/small-object-optimizations.html</link>
        <guid isPermaLink="true">http://www.stlab.cc/tip/2017/12/23/small-object-optimizations.html</guid>
        
        <category>tips</category>
        
        
        <category>tip</category>
        
      </item>
    
      <item>
        <title>New Concurrency Library</title>
        <description>&lt;p&gt;A new concurrency library was added as a first one to this new stlab instance.&lt;/p&gt;

&lt;p&gt;This library provides an alternative to the C++11/14 and upcoming C++17 TS futures. As well it provides an implementation of channels that allow the creation of process graphs.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h3 id=&quot;futures&quot;&gt;Futures&lt;/h3&gt;
&lt;p&gt;The future implementation differs in several aspects compared to the C++11/14/17 standard futures: It provides continuations and joins, which were just added in a C++17 TS. But more important this futures propagate values through the graph and not futures. This allows an easy way of creating splits. That means a single future can have multiple continuations into different directions. An other important difference is that the futures support cancellation. So if one is not anymore interested in the result of a future, then one can destroy the future without the need to wait that the future is fulfilled, as it is the case with std::future (and boost::future). An already started future will run until its end, but will not trigger any continuation. So in all these cases, all chained continuations will never be triggered. Additionally the future interface is designed in a way, that one can use build in or custom build executors.&lt;/p&gt;

&lt;h3 id=&quot;channels&quot;&gt;Channels&lt;/h3&gt;
&lt;p&gt;Since one can create with futures only graphs for single use, this library provides as well channels. With these channels one can build graphs, that can be used for multiple invocations.&lt;/p&gt;

&lt;h3 id=&quot;documentation&quot;&gt;Documentation&lt;/h3&gt;
&lt;p&gt;The full documentation can be found by following the libraries link on the top.&lt;/p&gt;
</description>
        <pubDate>Thu, 11 May 2017 00:00:00 +0000</pubDate>
        <link>http://www.stlab.cc/concurrency/2017/05/11/concurrency.html</link>
        <guid isPermaLink="true">http://www.stlab.cc/concurrency/2017/05/11/concurrency.html</guid>
        
        <category>concurrency</category>
        
        <category>library</category>
        
        
        <category>concurrency</category>
        
      </item>
    
      <item>
        <title>Forest Documentation &amp; Tutorials</title>
        <description>&lt;p&gt;A &lt;em&gt;forest&lt;/em&gt; is a hierarchical, node-based data structure. This document serves to cover the high-level concepts of a forest, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adobe::forest&amp;lt;T&amp;gt;&lt;/code&gt; implementation details, as well as examples of frequent patterns.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h1 id=&quot;concepts&quot;&gt;Concepts&lt;/h1&gt;

&lt;h2 id=&quot;terminology&quot;&gt;Terminology&lt;/h2&gt;

&lt;p&gt;$N$: An exemplar node&lt;/p&gt;

&lt;p&gt;$P$: The parent node of $N$&lt;/p&gt;

&lt;p&gt;$C$: A child node of $N$. The first and last children may also be termed $C_f$ and $C_l$, respectively.&lt;/p&gt;

&lt;p&gt;$S$: A sibling node of $N$. The previous and next sibling may also be termed $S_p$ and $S_n$, respectively.&lt;/p&gt;

&lt;p&gt;$R$: A root node&lt;/p&gt;

&lt;p&gt;$I$: A forest (fullorder) iterator&lt;/p&gt;

&lt;h2 id=&quot;nodes&quot;&gt;Nodes&lt;/h2&gt;

&lt;p&gt;Forest’s fundamental element type is the node. They are heap-allocated by the forest as necessary for storing a value. Each node has exactly one parent and zero or more children. In this document we will draw nodes as circles:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/n.png&quot; alt=&quot;A single node&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;the-root-node&quot;&gt;The Root Node&lt;/h2&gt;

&lt;p&gt;Every forest has a root node, which is not a node used to store values in the forest. Rather, its primary purpose is as the anchor to which all top-level nodes in the forest are attached. In this document we will draw the root node as a rectangle:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/r.png&quot; alt=&quot;The root node&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;edges&quot;&gt;Edges&lt;/h2&gt;

&lt;p&gt;Forest nodes are connected to one another with edges. For every node in the forest, there are exactly two edges that lead away from the node, and exactly two that lead towards the node. In this document edges will be drawn as arrows pointed in the direction of forward travel:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/e.png&quot; alt=&quot;A single node with its four edges&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The two left edges are known as leading edges, and the two right edges are known as trailing edges:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/elt.png&quot; alt=&quot;Leading and trailing edges&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The two edges that point to $N$ are known as in edges and the edges that point away from $N$ are out edges:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/eio.png&quot; alt=&quot;In and out edges&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It is worth noting that the terms “in” and “out” are relative to $N$. In other words an in edge for $N$ will also be an out edge for some other node.&lt;/p&gt;

&lt;h3 id=&quot;leading-in-edge&quot;&gt;Leading In Edge&lt;/h3&gt;

&lt;p&gt;The northwest edge is the leading in edge. It originates from one of two related nodes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;If $N$ is the first child of $P$, this edge comes from the leading edge of $P$:&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;/images/forest/p_to_n.png&quot; alt=&quot;Parent to first child&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Otherwise, this edge comes from the trailing edge of $N$’s prior sibling:&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;/images/forest/sp_to_n.png&quot; alt=&quot;Sibling to next sibling&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;leading-out-edge&quot;&gt;Leading Out Edge&lt;/h3&gt;

&lt;p&gt;The southwest edge is the leading out edge. Its points to one of two related nodes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;If $N$ is a parent, it points to the leading edge of the first child of $N$.&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;/images/forest/n_to_cf.png&quot; alt=&quot;Parent to first child&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Otherwise, it points to the trailing edge of $N$ itself:&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;/images/forest/n_to_n.png&quot; alt=&quot;Node to self&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;trailing-in-edge&quot;&gt;Trailing In Edge&lt;/h3&gt;

&lt;p&gt;The southeast edge is the trailing in edge. It originates from one of two related nodes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;If $N$ is a parent, it comes from the trailing edge of $N$’s last child:&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;/images/forest/cl_to_n.png&quot; alt=&quot;Last child to parent&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Otherwise, it comes from the leading edge of $N$ itself:&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;/images/forest/n_to_n.png&quot; alt=&quot;Node to self&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;trailing-out-edge&quot;&gt;Trailing Out Edge&lt;/h3&gt;

&lt;p&gt;The northeast edge is the trailing out edge. It points to one of two related nodes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If $N$ is the last child of $P$, this edge points to the trailing edge of $P$:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/n_to_p.png&quot; alt=&quot;Last child to parent&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Otherwise, this edge points to the leading edge of $N$’s next sibling:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/n_to_sn.png&quot; alt=&quot;Parent to first child&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;example-forests&quot;&gt;Example Forests&lt;/h2&gt;

&lt;p&gt;With the fundamental building blocks in place, here is how they combine to form some of the basic relationships in a forest. Note that (with the exception of the root node) every node in the forest maintains four relationships with the nodes around it.&lt;/p&gt;

&lt;h3 id=&quot;empty&quot;&gt;Empty&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/empty.png&quot; alt=&quot;An empty forest&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Given a forest that contains only the root node, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::empty&lt;/code&gt; will return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;. Otherwise, it will return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;one-node&quot;&gt;One Node&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/one_node.png&quot; alt=&quot;A forest with one node&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;two-sibling-nodes&quot;&gt;Two Sibling Nodes&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/two_siblings.png&quot; alt=&quot;A forest with one node&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;iterators&quot;&gt;Iterators&lt;/h2&gt;

&lt;p&gt;Iterators are comprised of two pieces of data:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the node to which they point&lt;/li&gt;
  &lt;li&gt;the edge they are on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this documentation Iterators will be described by this pair as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{node, edge}&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;examples&quot;&gt;Examples&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{P, L}&lt;/code&gt; is an iterator that points to node $P$ on the leading edge:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/iterator_pl.png&quot; alt=&quot;{P, L}&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, T}&lt;/code&gt; is an iterator that points to node $N$ on the trailing edge:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/iterator_nt.png&quot; alt=&quot;{N, T}&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Since iterators always point to a node, we never speak of an iterator being on the “out” edge of a node it is from. Rather, we consider it on the “in” edge of the node that it points to (even though it’s the same edge).&lt;/p&gt;

&lt;h3 id=&quot;edge-flipping&quot;&gt;Edge Flipping&lt;/h3&gt;

&lt;p&gt;You can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adobe::leading_of(I)&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adobe::trailing_of(I)&lt;/code&gt; to change the edge of an iterator to leading or trailing, respectively. Note that these are free functions, not member functions of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;&lt;/code&gt;. In this document we will represent these routines in pseudocode as such:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leading_of({P, T})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{P, L}&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trailing_of({N, L})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, T}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;iterator-concept&quot;&gt;Iterator Concept&lt;/h3&gt;

&lt;p&gt;Forest iterators are bidirectional. In this document we denote incrementing and decrementing an iterator with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++{node, edge}&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--{node, edge}&lt;/code&gt;, respectively.&lt;/p&gt;

&lt;h3 id=&quot;iterator-adaptors&quot;&gt;Iterator Adaptors&lt;/h3&gt;

&lt;p&gt;Although there is one iterator type for forest, there are several iterator adaptors that facilitate various methods of traversal. More will be said about these later.&lt;/p&gt;

&lt;h3 id=&quot;a-note-on-begin-and-end&quot;&gt;A note on begin() and end()&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;begin()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end()&lt;/code&gt; in a forest behave just like their equivalent member functions found in a typical standard library container.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::end()&lt;/code&gt; will always return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{R, T}&lt;/code&gt;. This is true regardless of whether or not the forest is empty.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::begin()&lt;/code&gt; will always return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++{R, L}&lt;/code&gt;. This follows the rules of the leading out edge explained above, meaning that it will either point to the first node in the forest (if it is not empty), or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end()&lt;/code&gt; (if it is).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;forest-traversal&quot;&gt;Forest Traversal&lt;/h2&gt;

&lt;p&gt;The root node is not considered during forest traversal. As such it is omitted from traversal diagrams.&lt;/p&gt;

&lt;h3 id=&quot;fullorder&quot;&gt;Fullorder&lt;/h3&gt;

&lt;p&gt;The default traversal behavior for a forest is fullorder. This means every node is visited twice: first, right before any of its children are visited (on the leading edge), and then again right after (on the trailing edge). This behavior is recursive, and results in a depth-first traversal of the forest:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/fullorder_traversal.png&quot; alt=&quot;Fullorder traversal&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here is the same diagram with the leading and trailing edges colorized:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/fullorder_traversal_color.png&quot; alt=&quot;Colorized fullorder traversal&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note that iteration 7 above has no color: it could be either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Sn, L}&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{P, T}&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;fullorder-traversal-of-ns-subtree&quot;&gt;Fullorder traversal of $N$’s subtree&lt;/h3&gt;

&lt;p&gt;To traverse a node $N$ and all of its descendants, the iterator range is:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leading_of({N, x})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++trailing_of({N, x})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;last&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/fullorder_traversal_of_n.png&quot; alt=&quot;Fullorder traversal of N&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note this technique does not work if $N == R$.&lt;/p&gt;

&lt;h3 id=&quot;preorder&quot;&gt;Preorder&lt;/h3&gt;

&lt;p&gt;A preorder traversal of the forest will visit every node once. During preorder traversal a parent will be visited before its children. Preorder iteration achieved by incrementing fullorder continually and visiting a node only when the iterator is on a leading edge:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do ++{node, edge} while edge == trailing&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/preorder_traversal.png&quot; alt=&quot;Preorder traversal&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;postorder&quot;&gt;Postorder&lt;/h3&gt;

&lt;p&gt;A postorder traversal of the forest will visit every node once. During postorder traversal a parent will be visited after its children. Postorder iteration achieved by incrementing fullorder continually and visiting a node only when the iterator is on a trailing edge:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do ++{node, edge} while edge == leading&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/postorder_traversal.png&quot; alt=&quot;Postorder traversal&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;child&quot;&gt;Child&lt;/h3&gt;

&lt;p&gt;A child traversal of a node $P$ traverses only its immediate children; $P$ itself is not visited. Child traversal is achieved by setting the edge of the iterator to trailing, then incrementing it:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++trailing_of({Cn, L})&lt;/code&gt; → &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Cn+1, L}&lt;/code&gt; (next child) or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{P, T}&lt;/code&gt; (the end of the range)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/child_traversal.png&quot; alt=&quot;Child traversal&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;node-insertion&quot;&gt;Node Insertion&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forest&amp;lt;T&amp;gt;::insert&lt;/code&gt; requires an iterator (where to insert) and a value (what to insert). The iterator is never invalidated during an insertion. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; returns a leading edge iterator to the new node.&lt;/p&gt;

&lt;h3 id=&quot;leading-edge-insertion&quot;&gt;Leading edge insertion&lt;/h3&gt;

&lt;p&gt;When the location for insertion is on the leading edge of $N$ (that is, $I$ is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, L}&lt;/code&gt;) the new node will be created as $N$’s new previous sibling:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/insert_leading.png&quot; alt=&quot;Leading insertion&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Sp, L}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N,T}&lt;/code&gt; can be used repeatedly to push back children of $N$.&lt;/p&gt;

&lt;p&gt;Leading edge insertion can be used to repeatedly “push back” prior siblings of $N$. To repeatedly “push front” prior siblings of $N$, use the resulting iterator of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; as the next insertion position.&lt;/p&gt;

&lt;h3 id=&quot;trailing-edge-insertion&quot;&gt;Trailing edge insertion&lt;/h3&gt;

&lt;p&gt;When the location for insertion is on the trailing edge of $N$ (that is, $I$ is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{N, T}&lt;/code&gt;) the new node will be created as $N$’s new last child:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/insert_trailing.png&quot; alt=&quot;Trailing insertion&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Cl, L}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Trailing edge insertion can be used to repeatedly “push back” children of $N$. To repeatedly “push front” children of $N$, use the resulting iterator of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert&lt;/code&gt; as the next insertion position.&lt;/p&gt;

&lt;h1 id=&quot;algorithms--examples&quot;&gt;Algorithms &amp;amp; Examples&lt;/h1&gt;

&lt;h2 id=&quot;detecting-if-i_node-is-the-first-child-of-its-parent&quot;&gt;Detecting if $I_{node}$ is the first child of its parent&lt;/h2&gt;

&lt;p&gt;Given:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An iterator $I$ pointing to $I_{node}$&lt;/li&gt;
  &lt;li&gt;Its predecessor $H = –I$&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If $I_{edge} == leading$ and $H_{edge} == leading$, then $I_{node}$ is the first child of $H_{node}$.&lt;/p&gt;

&lt;h3 id=&quot;pseudocode&quot;&gt;Pseudocode&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bool is_first_child = (--leading_of({N, x})).edge() == leading
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;visualization&quot;&gt;Visualization&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/first_child_check.png&quot; alt=&quot;First child check&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;detecting-if-i_node-is-the-last-child-of-its-parent&quot;&gt;Detecting if $I_{node}$ is the last child of its parent&lt;/h2&gt;

&lt;p&gt;Given:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An iterator $I$ pointing to $I_{node}$&lt;/li&gt;
  &lt;li&gt;Its successor $J = ++I$ pointing to $I_{node}$&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If $I_{edge} == trailing$ and $J_{edge} == trailing$, then $I_{node}$ is the last child of $J_{node}$.&lt;/p&gt;

&lt;h3 id=&quot;pseudocode-1&quot;&gt;Pseudocode&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bool is_last_child = (++trailing_of({N, x})).edge() == trailing
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;visualization-1&quot;&gt;Visualization&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/last_child_check.png&quot; alt=&quot;Last child check&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;detecting-if-i_node-has-children&quot;&gt;Detecting if $I_{node}$ has children&lt;/h2&gt;

&lt;p&gt;Given:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An iterator $I$ pointing to $I_{node}$&lt;/li&gt;
  &lt;li&gt;Its successor $J = ++I$ pointing to $J_{node}$&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If $I_{edge} == leading$ and $J_{edge} == leading$, then $I_{node}$ has children (the first of which is $J_{node}$). Otherwise $I_{node}$ is a leaf node, and $I_{node} == J_{node}$.&lt;/p&gt;

&lt;h3 id=&quot;pseudocode-2&quot;&gt;Pseudocode&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bool has_children = (++leading_of({N, x})).edge() == leading
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;visualization-2&quot;&gt;Visualization&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/has_children_check.png&quot; alt=&quot;Has children check&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;push-back-children-of-i_node&quot;&gt;“Push back” children of $I_{node}$&lt;/h2&gt;

&lt;p&gt;Given:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An iterator $I = {node, trailing}$&lt;/li&gt;
  &lt;li&gt;A sequence of values $S$ to insert&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We repeatedly insert subsequent values found in $S$ at position $I$.&lt;/p&gt;

&lt;h3 id=&quot;pseudocode-3&quot;&gt;Pseudocode&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for (const auto&amp;amp; s : S) {
    forest.insert(I, s);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;visualization-3&quot;&gt;Visualization&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/children_push_back.png&quot; alt=&quot;Children push back&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;push-front-children-of-i_node&quot;&gt;“Push front” children of $I_{node}$&lt;/h2&gt;

&lt;p&gt;Given:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;An iterator $I = {node, trailing}$&lt;/li&gt;
  &lt;li&gt;A sequence of values $S$ to insert&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We repeatedly insert subsequent values found in $S$ at position $I$, each time reassigning $I$ to be the result of the insertion.&lt;/p&gt;

&lt;h3 id=&quot;pseudocode-4&quot;&gt;Pseudocode&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for (const auto&amp;amp; s : S) {
    I = forest.insert(I, s);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;visualization-4&quot;&gt;Visualization&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/forest/children_push_front.png&quot; alt=&quot;Children push front&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 28 May 2016 00:00:00 +0000</pubDate>
        <link>http://www.stlab.cc/forest/2016/05/28/forest.html</link>
        <guid isPermaLink="true">http://www.stlab.cc/forest/2016/05/28/forest.html</guid>
        
        <category>forest</category>
        
        <category>tutorial</category>
        
        <category>documentation</category>
        
        
        <category>forest</category>
        
      </item>
    
  </channel>
</rss>
