
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Assume False</title>
 <link href="/atom.xml" rel="self"/>
 <link href="https://assumefalse.com/"/>
 <updated>2026-04-11T23:36:56.449Z</updated>
 <id>https://assumefalse.com</id>
 <author>
   <name>Nick Waddoups</name>
   <email>npw1010@gmail.com</email>
 </author>

 
 <entry>
   <title>Recreating Recipes Using SMT</title>
   <link href="https://assumefalse.com/posts/z3_recipe_generation.html"/>
   <updated>2025-02-27T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/z3_recipe_generation.html</id>
   <content type="html">&lt;p&gt;{: .prompt-warning }&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This post is a draft, so it will likely change in the future. Also, I make
no guarantees that the post is currently coherent, legible, or enjoyable to
read. Read at your own risk :)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I &lt;em&gt;love&lt;/em&gt; snacks, in fact I love them so much that I have attained the holy
status of &amp;quot;snack monster&amp;quot; in my household. If my wife and I go on roadtrips,
we usually end up buying our own bags of snacks and labelling them so that I
don&apos;t accidentally eat all the snacks before she gets some. Unfortunately,
all this snacking comes at a cost. Literally. It costs money to buy snacks.
This post is about how I used Z3, an SMT solver, to derive recipes for one
of my favorite snacks.&lt;/p&gt;
&lt;h2&gt;The Target Snack&lt;/h2&gt;
&lt;p&gt;I will not out the exact snack here that I like, but there is a local business
where I live that makes a &lt;em&gt;protein bite&lt;/em&gt; style snack. If you google protein
bite you will be greeted with many such snacks, but the basic idea of the
snack is that you make a small uncooked ball of chocolate chips, peanut
butter, honey, and protien powder and then you let it sit overnight to harden
and shazam, you have yourself a snack.&lt;/p&gt;
&lt;p&gt;These sound easy to make by yourself and typically they are, but I&apos;ve tried
making variations on my own that replicate the macros of the specific version
available in my town, and they always end up gross. Likely I just have the wrong
combination of ingredients and perhaps an incorrect mixing style. To correct
the first problem of incorrect ingredient balance, I decided that I could write
a program using the Z3 SMT solver and python to determine exactly what ratio of
ingredients I could use!&lt;/p&gt;
&lt;h2&gt;Setting up an Example Problem&lt;/h2&gt;
&lt;p&gt;I haven&apos;t really used an SMT solver for anything before, but I&apos;m aware of the
basic concept. You specify a &lt;em&gt;formula&lt;/em&gt;, and Z3 determines if it can assign a
specific value to each variable in the formula and have the formula result in
&lt;code&gt;true&lt;/code&gt;. If it can assign each variable a value, then the equation is said to be
&lt;em&gt;satisfied&lt;/em&gt;, or sat. If you can&apos;t assign a variable a value, the equation is
&lt;em&gt;unsatisfied&lt;/em&gt;, or unsat.&lt;/p&gt;
&lt;p&gt;For example, let&apos;s say we have two floating point variables, $$x$$ and $$y$$. One
possible formula we could make with these variables would be the following:&lt;/p&gt;
&lt;p&gt;$$
x \le y \wedge x \ge 5 \wedge y \le 6
$$&lt;/p&gt;
&lt;p&gt;An SMT solver asks itself if there is a possible assignment to $$x$$ and $$y$$
that would satisfy this equation. In this case, there is at least one possible
assignment:&lt;/p&gt;
&lt;p&gt;$$
x = 5, y = 5
$$&lt;/p&gt;
&lt;p&gt;So Z3 returns &lt;em&gt;sat&lt;/em&gt; and gives us these values for the assignment. If we change
the formula slightly, we can see that there will not be a possible solution
as $$x$$ must be greater than 5 and $$y$$ must be less than 4. Let&apos;s see what
happens when we pass this formula to the computer though.&lt;/p&gt;
&lt;p&gt;$$
x \le y \wedge x &amp;gt; 5 \wedge y &amp;lt; 4
$$&lt;/p&gt;
&lt;p&gt;Z3 returns &amp;quot;&lt;code&gt;no solution&lt;/code&gt;&amp;quot; indicating that there is not a possible assignment of
real number to $$x$$ or $$y$$ that can satify this formula. For reference, here&apos;s
how I encoded this problem into Z3 using python.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;from&lt;/span&gt; z3 &lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;

x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Real&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;X&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Real&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&apos;Y&apos;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

solve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Output: [Y = 5, X = 5]&lt;/span&gt;

solve&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;And&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; y&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Output: no solution&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Now Onto... Recipes?&lt;/h2&gt;
&lt;p&gt;I know what you&apos;re thinking, &amp;quot;that&apos;s really cool, but how in the world are you
going to steal recipes with it?&amp;quot; Well, let&apos;s try to talk through it together.&lt;/p&gt;
&lt;p&gt;First, we need to define what a &amp;quot;recipe&amp;quot; is going to be to the SMT solver. This
is going to be quite different from what we as humans view as a recipe, but it
will maintain the same purpose.&lt;/p&gt;
&lt;p&gt;Macro Triple
: A triple $$\langle F, C, P \rangle$$ where $$F$$ is fat in grams, $$C$$ is carbs
in grams, and $$P$$ is protein in grams.&lt;/p&gt;
&lt;p&gt;Recipe
: A combination of ingredients that when combined together result in a equivalently
proportioned macro triple to the target recipe. We can represent a target recipe
by the following equation.&lt;/p&gt;
&lt;p&gt;$$
\sum_{i=0}^{n} k_i I_i = R
$$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$$n$$ is the number of ingredients,&lt;/li&gt;
&lt;li&gt;$$I_i$$ is the macro triple for each ingredient in the recipe&lt;/li&gt;
&lt;li&gt;$$k_i$$ is the coeffiecient that represents how many multiples of the
ingredient we will use in the recipe, and&lt;/li&gt;
&lt;li&gt;$$R$$ is the macro triple for the target recipe.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To put this into the SMT solver we&apos;re first going to need some constraints. First off,
we need to make sure that the ingredient coeffiecients can&apos;t be zero. If we had only two
ingredients, and one of them had the same macros as the target recipe, then the SMT
solver might just say that for our &amp;quot;recipe&amp;quot; we only need the one ingredient.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Ingredient&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Macros&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Bacon&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;$$b = \langle 10, 0, 2 \rangle$$&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Eggs&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;$$e = \langle 5, 0, 3 \rangle$$&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;$$
R = \langle 20, 0, 4 \rangle
$$&lt;/p&gt;
&lt;p&gt;Given the above ingredients and the target recipe, then the SMT solver may just decide
that assignments for $$k_b$$ and $$k_e$$ are 2 and 0.&lt;/p&gt;
&lt;p&gt;$$
\begin{align*}
R = \langle 20, 0, 4 \rangle &amp;amp;= \sum_{i=0}^{n} k_i I_i\
&amp;amp;= k_b b + k_e e \
&amp;amp;= 2 \langle 10, 0, 2 \rangle + 0 \langle 5, 0, 3 \rangle \
&amp;amp;= \langle 20, 0, 4 \rangle \
\end{align*}
$$&lt;/p&gt;
&lt;p&gt;So our first constraint will be all $$k_i$$ must be greater than 0.&lt;/p&gt;
&lt;p&gt;$$
\forall k, k &amp;gt; 0
$$&lt;/p&gt;
&lt;p&gt;At the moment I can&apos;t think of any other constraints, so we&apos;ll keep going from here.&lt;/p&gt;
&lt;h2&gt;Recipes As a Formula&lt;/h2&gt;
&lt;p&gt;Now that we have an abstract mathematical way of representing our recipes, we need
to be able to encode these recipes as formulas that we can pass to Z3. Z3 has no
concept of a &lt;em&gt;macro triple&lt;/em&gt;, so we&apos;ll need to figure out how we can tell the Z3
solver what that is. The good news is that it&apos;s actually pretty easy. Basically,
we just need to make the transformation from this:&lt;/p&gt;
&lt;p&gt;$$
\sum_{i=0}^{n} k_i I_i = R
$$&lt;/p&gt;
&lt;p&gt;To this:&lt;/p&gt;
&lt;p&gt;$$
\sum_{i=0}^{n} k_i F_i = R_F\
\sum_{i=0}^{n} k_i C_i = R_C\
\sum_{i=0}^{n} k_i P_i = R_P\
$$&lt;/p&gt;
&lt;p&gt;All we are doing here is splitting out the ingredient triple into each of its separate
components, and sharing the coeffiecients for each portion of the triple. We&apos;re also
going to make the transistion into coding land here.&lt;/p&gt;
&lt;h2&gt;Representing the Recipe in Python&lt;/h2&gt;
&lt;p&gt;We&apos;ll start by creating a small python class to represent a macro triple. This will
be the core data structure we&apos;ll use to store the ingredients and the target recipe.
Our class will have the three key data values we need, fat, carbs, and protien, and
we&apos;ll also add a name to each triple to differentiate triples from one another.
We&apos;ll use this later to generate names for our coeffiecients.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;MacroTriple&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;self&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; fat&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; carbs&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; protien&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; gramsPerServing&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; name
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fat &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; fat &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; gramsPerServing
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;carbs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; carbs &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; gramsPerServing
        self&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;protien &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; protien &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; gramsPerServing&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we&apos;ll need a way to generate our recipe formula. To do this we&apos;ll need a list
of &lt;code&gt;MacroTriple&lt;/code&gt;s for the ingredients and a &lt;code&gt;MacroTriple&lt;/code&gt; for the target recipe.
Based off our equations from above, we&apos;ll need to generate a constraint for each
ingredient ($$k_i &amp;gt; 0$$) and create the sum of coeffiecients that are equal to
the target recipe.&lt;/p&gt;
&lt;p&gt;To create all this we&apos;ll introduce the concept of a Z3 &lt;em&gt;solver&lt;/em&gt;. A solver is an
object that we can add formulas to in different places and then we can call the
&lt;code&gt;check()&lt;/code&gt; method on the object to find satisfying assignments (or show that the
formula is unsat).&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; z3 

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;recipeFormula&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;ingredients&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    s &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Solver&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# initial constraint&lt;/span&gt;
    k0 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Real&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;k_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;k0 &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;# initial equations &lt;/span&gt;
    fatEquation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k0 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fat
    carbsEquation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k0 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;carbs
    protienEquation &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; k0 &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;protien

    &lt;span class=&quot;token comment&quot;&gt;# the rest of the equation and constraints &lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; ingredient &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; ingredients&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        kn &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; z3&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Real&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;k_&lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;ingredient&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;kn &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

        fatEquation &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; kn &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ingredient&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fat
        carbsEquation &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; kn &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ingredient&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;carbs
        protienEquation &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; kn &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; ingredient&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;protien

    &lt;span class=&quot;token comment&quot;&gt;# adding the final equations to the solver &lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;fatEquation &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;fat&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;carbsEquation &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;carbs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    s&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;add&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;proteinEquation &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; target&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;protien&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; s&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now that we have this formula we can start generating recipes! First, we&apos;ll need to set&lt;br&gt;
up the list of ingredients.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/z3_recipes/ingredients.png&quot; alt=&quot;Protien Bites Ingredients&quot;&gt;{: width=&amp;quot;80%&amp;quot; }&lt;/p&gt;
&lt;p&gt;Using the ingredients from the bag as a starting point, I grabbed all the macros of the ingredients
from Cronometer, a macro tracking app, and I entered them into my python file.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;Ingredients &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
        MacroTriple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Pb&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;46.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;25.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;21.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        MacroTriple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Honey&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;82.4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        MacroTriple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Oats&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5.9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;68.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;13.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        MacroTriple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Protien&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;34&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        MacroTriple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;ChocChips&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;29.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;66.7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we need to set up the target recipe, which also is conveniently on the bag.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;Target &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; MacroTriple&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Bites&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we create the recipe formula, call &lt;code&gt;check()&lt;/code&gt; on it to find a satisfying assignment
and then print it to get our recipe!&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;formula &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; recipeFormula&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Ingredients&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Target&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
formula&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;check&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;formula&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;model&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we run this we get:&lt;/p&gt;
&lt;pre class=&quot;language-sh&quot;&gt;&lt;code class=&quot;language-sh&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;K_Pb &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8.915&lt;/span&gt;?,
 K_Oats &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;11.399&lt;/span&gt;?,
 K_Honey &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;,
 K_Protien &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.826&lt;/span&gt;?,
 K_ChocChips &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can read this recipe as 8.9 grams of peanut butter, 11.4 grams of oats, and so on. Right off
the bat this recipe looks a little strange, and definitely gives me the sense that the peanut
butter and oats will overpower everything else. To fix this I added two parameters to the
&lt;code&gt;recipeFormula&lt;/code&gt; function, &lt;code&gt;min_bound&lt;/code&gt; and &lt;code&gt;max_bound&lt;/code&gt;. These build off the same idea as the
constraint we added early that all $$k$$ must be greater than 0. Now we can use these bound
variables to additionally specifiy that $$\texttt{min_bound} &amp;lt; k &amp;lt; \texttt{max_bound}$$.&lt;/p&gt;
&lt;h2&gt;Final Bites&lt;/h2&gt;
&lt;p&gt;This brings me to the end of this blog post, but not to the end of my recipe generating. After
I attempt to construct some of these recipes I&apos;ll update this post with the results.&lt;/p&gt;
&lt;p&gt;If you&apos;d like to explore the code yourself, check it our on my &lt;a href=&quot;https://github.com/nwad123/z3_recipes&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Additional Optimizations to the Recipe Generator&lt;/h2&gt;
&lt;p&gt;In the US, &lt;a href=&quot;https://www.fda.gov/food/food-additives-and-gras-ingredients-information-consumers/types-food-ingredients&quot;&gt;ingredients on labels are listed in descending weight order&lt;/a&gt;, so we could
add an additional constraint that the weight of all ingredients must be ordered to get more
precise recipes.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Impacts of Compiler Optimization on Cache Latency Profiling</title>
   <link href="https://assumefalse.com/posts/optimization_impact_on_cache_measurement.html"/>
   <updated>2025-02-26T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/optimization_impact_on_cache_measurement.html</id>
   <content type="html">&lt;p&gt;{: .prompt-warning }&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;This post is a draft, so it will likely change in the future. Also, I make
no guarantees that the post is currently coherent, legible, or enjoyable to
read. Read at your own risk :)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As a TA for the computer hardware security course taught at Utah State University
I helped create and run a lab where the students used a cache side channel to
transmit information between processes using only memory access time latencies
to communicate bytes. Likely I&apos;ll one day write a blog post about the whole lab,
as it was one of my favorite things I did in university, but alas today is just
about a part of it, timing memory accesses.&lt;/p&gt;
&lt;p&gt;We ran the lab on machine that had Intel chips, which have hierarchical
set-associative memory caches (L1, L2, and L3) inbetween the logical cores and
DRAM in order to improve memory access times for data that was accessed regularly.
There are many posts about what CPU caches are, so I won&apos;t really offer an explanation
beyond this here.&lt;/p&gt;
&lt;p&gt;As a general rule of thumb, access to each successive level of cache takes longer
than accessing the previous level of cache. The L2 cache is also shared between two
cores. Given this information we can take advantage of it to create a &lt;em&gt;covert side
channel&lt;/em&gt;, where we can pass information between processes using the shared L2 cache.
The lab we ran used a basic &amp;quot;prime and probe&amp;quot; attack, where one process would &amp;quot;prime&amp;quot;
the L2 cache, effectively filling it for a time, then it would sleep. During that time,
the victim process would access some data inside the L2 cache, evicting the data stored
by the attacker process. The attacker process would then probe the cache, and based on
which data was evicted by the victim the attacker could receive a message.&lt;/p&gt;
&lt;p&gt;That was far to short an explanation likely, but hopefully enough for this post to
make some sense. A key part in a cache side channel is &lt;em&gt;timing&lt;/em&gt;. If you can&apos;t accurately
measure how long it takes to load a particular address from memory, you&apos;ll never be
able to probe effectively enough to receive a message across the side channel.&lt;/p&gt;
&lt;h2&gt;What is &amp;quot;Now&amp;quot;?&lt;/h2&gt;
&lt;p&gt;In modern processors, unfortunately the concept of &amp;quot;now&amp;quot; is a bit amorphous. Modern
processors are out-of-order pipelined beasts that speculatively execute your code whenever
they feel like it. At any time, you could have 10&apos;s (or more) instructions being executed
at once in your processor. Because of this, it&apos;s really hard to tell exactly what
instant an instruction is being executed. As you&apos;ll see, we&apos;ll pick and prod at the
processor in order to get it to do what we want, but we&apos;ll still see noise and spurious
data in our timing analysis.&lt;/p&gt;
&lt;p&gt;With poor code, compiler optimizations can get in the way of this timing as well. If
the compiler decides to reorder some statements (which it is allowed to do in many
circumstances), unroll our loops, or optimize away spurious memory accesses we&apos;ll
get no useful timing information at all. Here&apos;s how we managed to get reasonable
timing results with both out of order processors and compiler optimizations turned on.&lt;/p&gt;
&lt;h2&gt;The Psuedocode&lt;/h2&gt;
&lt;p&gt;Basically, to measure the access time of a specific cache we want to execute some code
that looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Target Cache = L2

1. Access a single piece of memory, known as the 
    target line, pulling it into the L1 cache 

2. Access a chunk of memory the size of the cache 
    immediately lower than our target cache, 
    in this case L1

3. Time the access of the target line. Due to step 
    2 the target line should not reside in our 
    target cache, so the time it takes to access 
    should be the time it takes to access our 
    target cache.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In practice, we can basically transform this algorithm into C-code, and then start generating
results. A key part of this algorithm (step 3) is timing the access to the target line though,
and how do we even do that?&lt;/p&gt;
&lt;h2&gt;Enter &lt;code&gt;rdtsc&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;rdtsc&lt;/code&gt; is the x86 instruction to read the &lt;em&gt;timestamp counter&lt;/em&gt;, which nominally is a counter
that tracks which clock cycle the processor is on. As discussed before &amp;quot;now&amp;quot; is a bit of a
unreachable state with out-of-order speculative CPUs, but reading the timestamp will give us
accurate enough measurements to perform a side channel attack. Using &lt;code&gt;rdtsc&lt;/code&gt; (and it&apos;s sibling,
&lt;code&gt;rdtscp&lt;/code&gt;) is apparently not as easy as just writing out the assembly instruction, and to
get the best use out of it I had to read a classic book, the &lt;em&gt;Intel Software Developer Manual&lt;/em&gt;.
Several volumes thick, this bad boy is a giant pdf that talks about what Intel processors do
and don&apos;t do. In the comments of my code I highlight the specific sections I used to generate
my functions.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Reads the time stamp counter as a 64-bit integer value. &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// (Code is derived from https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;//&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// This function uses the `rdtscp` instruction to retrieve the &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// timestamp. According to the Intel Software Developer Manual &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// Vol. 2B, pg. 4-560 `rdtscp` is &quot;not a serializing instruction&quot; &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// but it does wait for all previous _loads_ to become visible &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// and all previous instructions to execute. It also states that &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// for this instruction to complete prior to any following &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// instructions running that an `lfence` instruction can be inserted &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// immediately following the instruction.&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// The `rdtsc` instruction returns the lower 32-bits of the timestamp &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// into the EAX register, and the upper 32-bits of the timestamp into &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// the EDX register. The IA32_TSC_AUX is also stored in the RCX &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// register. We don&apos;t use this value but we indicate in the inline &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// assembly that it&apos;s used so that the compiler knows that the value in &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// RCX will be overwritten.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;always_inline&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;read_timestamp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;asm&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;volatile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;rdtscp\n\t&quot;&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;// Returns the time in EDX:EAX.&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;shl $32, %%rdx\n\t&quot;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;// Shift the upper bits left.&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;or %%rdx, %0&quot;&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;// &apos;Or&apos; in the lower bits.&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;=a&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;time&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;// Put result from RAX into `time` variable&lt;/span&gt;
        &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; 
        &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;rdx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;rcx&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;// Indicate that RDX and RCX will be written to&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; time&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you&apos;ve never written inline assembly before in C, this may look like a mess. That&apos;s
because it kind of is. Also, technically, this inline assembly is only valid using GCC&apos;s
extended assembly syntax, so I don&apos;t really know if you can even compile this code with
Clang or MSVC. Luckily, portability is not a goal for this code.&lt;/p&gt;
&lt;p&gt;So, despite it being a mess, we now have a function that we can get the timestamp value
of the processor at any time! So, timing an access should be as simple as this:&lt;/p&gt;
&lt;pre class=&quot;language-c&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; start &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;read_timestamp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;ptr&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// access to time &lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; elapsed &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;read_timestamp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; start&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Right? Please?&lt;/p&gt;
&lt;p&gt;Unfortunately, our new bible the Intel Software Developer&apos;s Manual says &amp;quot;maybe not&amp;quot;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Insert SDM quotes here.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Go over fence instructions.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Load fence (Intel Software Developer Manual Vol. 1 - 11.4.4.3)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// This function guarantees ordering between two loads and prevents &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// speculative loads until all load prior to this instruction have &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// been carried out.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;always_inline&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;lfence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;asm&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;volatile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;lfence&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Memory fence (Intel Software Developer Manual Vol. 1 - 11.4.4.3) &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// This functions ensures that no loads or stores after this instruction &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// will be globally visible until after all loads or stored previous to &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// this instruction are globally visible.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;always_inline&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;mfence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;asm&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;volatile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;mfence&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we can complete what we came here to do an write a function that
reliabily times accesses to a specific memory address.&lt;/p&gt;
&lt;pre class=&quot;language-c&quot;&gt;&lt;code class=&quot;language-c&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Uses `read_timestamp()` and fencing instruction to time a read access &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// to *addr.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;always_inline&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;time_one_line_read_access&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; byte &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;addr&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;volatile&lt;/span&gt; byte b&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token function&quot;&gt;mfence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// ensure all previous memory access is complete&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; time_init &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;read_timestamp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;lfence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// inserted due to the advice in Intel SDM Vol. 2B 4-560&lt;/span&gt;
    b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;addr&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// read address&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;uint64_t&lt;/span&gt; elapsed_time &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;read_timestamp&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; time_init&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; elapsed_time&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Timing Results and Optimizations&lt;/h2&gt;
&lt;p&gt;As usual, I&apos;m realizing that writing is hard, and I&apos;m doing a lot more it than I originally bargained
for. We haven&apos;t even covered the title of this post yet! Here&apos;s the thing, the code we wrote before
may be slightly transformed by optimizations.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Insert assembly generated by the timing functions
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To see the impact that optimization had on timing, I ran the timing analysis on optimization levels
O0-O3, and plotted the results. All of the results were generated on an Intel I7-4790 and were run
pinned to CPU core 4.&lt;/p&gt;
&lt;p&gt;The results show a bar graph of the the timings generated by each run, and the L2 upper bound
and L3 lower bound are calculated by taking the mean of each group of measurements and adding
one standard deviation to it.&lt;/p&gt;
&lt;p&gt;In general, adding optimizations seems to shift the access time of all measurements down by 10
clock cycles or so. The relative separation between the L2 and L3 bounds however remains fairly
constant. This means that as long as we profile well and are careful, we should be able to enable
optimizations in our cache side channel attack code without a decrease in transmission performance.&lt;/p&gt;
&lt;h3&gt;-O0&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/cache_latencies_hardware_security/latencies_O0_CPU4.png&quot; alt=&quot;Optimization Level 0 Timing Results&quot;&gt;{: width=&amp;quot;100%&amp;quot; }&lt;/p&gt;
&lt;h3&gt;-O1&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/cache_latencies_hardware_security/latencies_O1_CPU4.png&quot; alt=&quot;Optimization Level 1 Timing Results&quot;&gt;{: width=&amp;quot;100%&amp;quot; }&lt;/p&gt;
&lt;h3&gt;-O2&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/cache_latencies_hardware_security/latencies_O2_CPU4.png&quot; alt=&quot;Optimization Level 2 Timing Results&quot;&gt;{: width=&amp;quot;100%&amp;quot; }&lt;/p&gt;
&lt;h3&gt;-O3&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/cache_latencies_hardware_security/latencies_O3_CPU4.png&quot; alt=&quot;Optimization Level 3 Timing Results&quot;&gt;{: width=&amp;quot;100%&amp;quot; }&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>SPAC 2025</title>
   <link href="https://assumefalse.com/posts/links_spac_25.html"/>
   <updated>2025-02-22T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/links_spac_25.html</id>
   <content type="html">&lt;h2&gt;Basic Examples&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/Krdq7Kvca&quot;&gt;Map example&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/1qj4exqdP&quot;&gt;Filter example&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/dE4erYr7o&quot;&gt;Reduce example&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Longer Examples&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/rxq7EedP4&quot;&gt;Cpp&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://godbolt.org/z/qMaoT9W9j&quot;&gt;Rust&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>[](){}();</title>
   <link href="https://assumefalse.com/posts/immediate-lambda.html"/>
   <updated>2025-02-11T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/immediate-lambda.html</id>
   <content type="html">&lt;p&gt;Recently I had the opportunity to write a multi-threaded histogram generator for my
high performance computing (HPC) class at university. The assignment was the first
time I&apos;d completed a &amp;quot;larger&amp;quot; project since finding my way around the &lt;a href=&quot;(https://github.com/tcbrindle/flux)&quot;&gt;Flux&lt;/a&gt;
project. When attempting to contribute to Flux, something I found interesting
was the use of &amp;quot;immediately applied lambdas&amp;quot;. This is the idiom where a variable is
assigned to the output of a lambda function that is immediately invoked, thus
resulting in the variable being assigned a value as opposed to a closure type.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// immediately call the lambda to return a value.&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;assert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;x &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above is possibly the simplest example of an immediately applied lambda, with &lt;code&gt;x&lt;/code&gt;
simply being assigned to 0 through it. For such a trivial use it&apos;s definitely not
good practice to use an immediately applied lambda, but we&apos;ll build out some more
concrete examples in the future. For an example in &lt;a href=&quot;(https://github.com/tcbrindle/flux)&quot;&gt;Flux&lt;/a&gt;, take a look at
line 113 of the &lt;a href=&quot;(https://github.com/tcbrindle/flux/blob/504a107204a2fc3cd67eada992b987b21dbbbbd2/include/flux/adaptor/flatten.hpp#L113)&quot;&gt;flatten adaptor&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Why?&lt;/h2&gt;
&lt;p&gt;Based on the example above, using this &amp;quot;trick&amp;quot; seems to be a very verbose and confusing
method to assign variables. However, I think that using immediately applied lambdas
leads to code that easier to reason about, understand, and refactor. While I don&apos;t yet
have any experience working on industrial scale codebases, I still feel the pain of
going back to an old project that I didn&apos;t document and trying to parse the code that
I previously wrote. I think that using this idiom makes that process less painful, and
through the power of constant expressions and optimization there is little if any
performance penalty.&lt;/p&gt;
&lt;h2&gt;The Syntax&lt;/h2&gt;
&lt;p&gt;The syntax for creating an immediately applied lambda is simple. Simply call the lambda
using the &lt;code&gt;()&lt;/code&gt; operator immediately after the lambda&apos;s closing bracket. The example below
shows the difference between creating a closure object and creating a value by immediately
calling the lambda.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; closure &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/*...*/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; value   &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/*...*/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;An Example&lt;/h2&gt;
&lt;p&gt;When working on my histogram generator I needed a way to break down a input of randomly
generated floating point numbers (simulating real data) into different slices for each
thread to operate on. To do this I used &lt;code&gt;std::span&lt;/code&gt; to wrap the input data, and then used
&lt;code&gt;.subspan()&lt;/code&gt; to get the slice for each thread.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/immediate_lambda/thread_slices.png&quot; alt=&quot;Slicing up the original dataset across threads&quot;&gt;&lt;/p&gt;
&lt;p&gt;It&apos;s important that every element in the original dataset is&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;eventually assigned to a thread, and&lt;/li&gt;
&lt;li&gt;no element is assigned to two threads.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If either of those properties are violated, then we&apos;ll have either the case where the
histogram did not accurately count all the elements in the input dataset, or that an
element was counted twice. Both lead to incorrect results.&lt;/p&gt;
&lt;p&gt;Since the dataset size and number of threads have to be determined at runtime for this
assignment (according to the assignment description), we&apos;ll have to calculate the slices
for each thread at runtime. My first attempt at doing so worked successfully like this:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;thread_func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; thread_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; number_threads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; slice_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; number_threads&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; slice_starting_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; thread_id &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; slice_size&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; dataset_slice
        &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subspan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slice_starting_index&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; slice_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;thread_id&lt;/code&gt; corresponds to which thread is currently operating on the data, and is
assigned by me when the threads are created and ranges from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;n-1&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is
the desired number of threads. Looking over this code it should not be hard to convince
yourself that in general, &lt;code&gt;dataset_slice&lt;/code&gt; will be a subspan over the elements that the
current thread is to operate on.&lt;/p&gt;
&lt;p&gt;However, this function has a flaw. What if the datasize is &lt;em&gt;not&lt;/em&gt; easily divisible by
the number of threads. For example, what if you have three threads, and 11 elements?&lt;/p&gt;
&lt;p&gt;Using our function, we&apos;ll generate three dataset slices, one for each thread. The table
below tells us what the corresponding starting index and size of each slice will be:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Thread ID&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Slice Size&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Slice Starting Index&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Slice Range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;0&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;0&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;[0:2]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;1&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;[3:5]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;2&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;6&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;[6:8]&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Immediately, the issue becomes clear. Elements 9 and 10 will not be accessed by any
thread, as the last subspan assigned (to thread 2) is the elements from 6 to 8. Clearly
this algorithm for splitting up work is not correct, because we are not calculating the
histogram from all of the input data.&lt;/p&gt;
&lt;p&gt;An easy way to fix this algorithm would be to just have the last slice cover all of the
remaining data. That way, the last thread may do more work but all of the data will be
covered.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Thread ID&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Slice Size&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Slice Starting Index&lt;/th&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Slice Range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;0&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;0&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;[0:2]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;1&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;3&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;[3:5]&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;strong&gt;3&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;strong&gt;6&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align:center&quot;&gt;&lt;strong&gt;[6:10]&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;For this assignment it was reasonable to assume that the datasize would be much larger
than the number of threads (e.g. 10&apos;000&apos;000&apos;000 elements vs. 16 threads), so it wouldn&apos;t
be an issue if one thread processed at most &lt;code&gt;n-1&lt;/code&gt; more elements than the other threads.
The design decision I made was to have the last thread process all of the remaining
elements.&lt;/p&gt;
&lt;p&gt;Now thread two is processing elements 6 through 10, and no elements are missed.&lt;/p&gt;
&lt;p&gt;Now we run into the issue of changing our code. A simple refactoring results in something
like this:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;thread_func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; thread_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; number_threads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; slice_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; number_threads&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; slice_starting_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; thread_id &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; slice_size&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; dataset_slice&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;thread_id &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; number_threads &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; 
        dataset_slice
            &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subspan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slice_starting_index&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; slice_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        dataset_slice
            &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subspan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slice_starting_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;Calling &lt;code&gt;.subspan()&lt;/code&gt; without a size just returns a span from the given index to the
end of the input data.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To me, this looks messy. First, we lose the &lt;code&gt;const&lt;/code&gt; specifier on
our dataset slice. While the code still compiles (at least on GCC 14 on compiler
explorer) it&apos;s quite misleading as we definitely do &lt;em&gt;not&lt;/em&gt; want the underlying data
changed. We annotated that by marking the input dataset span as &lt;code&gt;const&lt;/code&gt;, so it feels
bad to get rid of it now.&lt;/p&gt;
&lt;p&gt;Also, we lose the ability to use &lt;code&gt;auto&lt;/code&gt;. While not the end of the world, there are
many reasons to use auto over explicitly declaring the type. Far smarter people than
me have given reasons, so to learn more about prefering &lt;code&gt;auto&lt;/code&gt; I would check out
the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=xnqTKD8uD64&quot;&gt;Herb Sutter on &amp;quot;Always Use Auto&amp;quot;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=PJ-byW33-Hs&quot;&gt;Jason Turner on &lt;code&gt;auto&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ZCGyvPDM0YY&quot;&gt;Andy Bond on &lt;code&gt;auto&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Finally, this code introduces a new if-statement to be analyzed when trying to determine
what the code is doing. It&apos;s not immediately clear that this if-statement relates to
&lt;code&gt;dataset_slice&lt;/code&gt;, and opens up the opportunity for more convoluted code to be created in
the future as other operations could be stuffed into the statement, further muddying the
waters.&lt;/p&gt;
&lt;p&gt;By using an immediately applied lambda, we can solve these problems and more! Here&apos;s the
updated code:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;thread_func&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; thread_id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; number_threads&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;span&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;float&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; slice_size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; number_threads&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; slice_starting_index &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; thread_id &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; slice_size&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; dataset_slice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;thread_id &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; number_threads &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; 
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subspan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slice_starting_index&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; slice_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subspan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slice_starting_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In basically the same number of lines of code we have brought back the ability to use
&lt;code&gt;auto&lt;/code&gt;, the &lt;code&gt;const&lt;/code&gt; specifier, and we now have a clearly defined purpose for our
if-statement defined by the way we wrote the code. When reading this, (if you know about
immediately applied lambdas) it&apos;s easy to see that, &amp;quot;Oh, this line of code gets the
slice of the dataset that applies to this thread&amp;quot;. All of the logic is in one location,
and it&apos;s easy to reason about.&lt;/p&gt;
&lt;p&gt;I&apos;d argue as well that using this approach coupled with optimizations actually makes it
possible for code to become even more readable as well. Consider the following small
change:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; dataset_slice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; max_thread_id &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; number_threads &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;thread_id &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; max_thread_id&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; 
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subspan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slice_starting_index&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; slice_size&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; dataset&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;subspan&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;slice_starting_index&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This introduces a variable local to the lambda (inaccessible outside of it) that gives
meaning to the statement &lt;code&gt;number_threads - 1&lt;/code&gt;. With optimizations this code will function
just the same as the code before, but now it&apos;s more maintainable and clear as to what it&apos;s
purpose is.&lt;/p&gt;
&lt;p&gt;While there are other, likely more optimal ways to implement this functionality, we&apos;ll end
this example here as it has shown the general point.&lt;/p&gt;
&lt;h2&gt;Other Tips and Tricks&lt;/h2&gt;
&lt;p&gt;You can use this to initialize class members:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/*...*/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/*...*/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can use this in statements:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/*...*/&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/*...*/&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can use this in constant expressions:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; x &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; y &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;x&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;static_assert&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;y &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also use this in template parameters:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;size_t Size &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; Type&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;The End&lt;/h2&gt;
&lt;p&gt;Overall, I think this is a great idiom to have in your toolbox, and it may come in handy more often
than you think!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Flux Permutations Adaptor (1/4)</title>
   <link href="https://assumefalse.com/posts/flux-permutations-basic.html"/>
   <updated>2025-01-02T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/flux-permutations-basic.html</id>
   <content type="html">&lt;p&gt;I&apos;ve been using the &lt;a href=&quot;(https://github.com/tcbrindle/flux)&quot;&gt;Flux&lt;/a&gt; C++ library for a while
now, ever since hearing Tristan Brindle talk about it on the &lt;a href=&quot;https://adspthepodcast.com/&quot;&gt;ADSP&lt;/a&gt;
podcast. As a brief introdution for the unintroduced, Flux is a library for &amp;quot;sequence-oriented
programming, similar in spirit to C++20 ranges, Python itertools, and Rust iterators.&amp;quot;
Flux provides an easy API to apply algorithms to sequences. For example, this is how
you might sum all even integers from 0 to 10 using Flux.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; sum &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ints&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [0, 1, 2, 3, ...]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;take&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;// [0, 1, 2, 3, ..., 10]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;is_even&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;// [0, 2, 4, 6, 8, 10]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;             &lt;span class=&quot;token comment&quot;&gt;// 30&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Flux is also an open source project, and I thought it would a great library to make a
contribution to. I saw on GitHub that there was a request for a &lt;code&gt;permutations&lt;/code&gt; adaptor
I thought this would be something that I could handle, so I commented on the GitHub issue that
I would try to implement it. After a busy Christmas season I was able to get a working
implementation. These four articles detail how I implemented the adaptor.&lt;/p&gt;
&lt;h1&gt;The Idea&lt;/h1&gt;
&lt;p&gt;A permutations adapator (or iterator, or function, etc.) is a common tool in other iteration
libraries. The adaptor takes in an input sequence, and returns a sequence made up of sequences
that are a permutation of the previous sequence. A basic example would be as follows (in
Python).&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; itertools &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; it 

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; permutation &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; it&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;permutations&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;permutation&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot; Output:
(1, 2)
(2, 1)
&quot;&quot;&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The goal would be to add an adaptor to Flux such that operations like the previous example
would be possible.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ints&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;// [1, 2, 3, ...]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;take&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;// [1, 2]&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;permutations&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// [[1, 2], [2, 1]]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Defining a Permutation&lt;/h2&gt;
&lt;p&gt;Before doing some implementation, let&apos;s spend a few minutes trying to understand what a permutation
actually is, and how the word is used in mathematics and programming. The Wikipedia page for
permutation is displayed from a mathematics perspective, and states as follows.&lt;/p&gt;
&lt;p&gt;Permutation (Wikipedia)
: In mathematics, a permutation of a set can mean one of two different things: an
arrangement of its members in a sequence or linear order, or the act or process of
changing the linear order of an ordered set.
(&lt;a href=&quot;https://en.wikipedia.org/wiki/Permutation&quot;&gt;Wikipedia/Permutation&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Our adaptor will fulfil both definitions. We want the adaptor to (1) produce every permutation of
the input sequence and (2) perform the process of generating new permutations from the sequence.
Based on this we&apos;ll make some definitions for our adaptor.&lt;/p&gt;
&lt;p&gt;Input Sequence
: The original sequence to perform the permutations on. This must be a &lt;code&gt;flux::bounded_sequence&lt;/code&gt;,
because there have to be a finite number of elements to permute.&lt;/p&gt;
&lt;p&gt;Output Sequence
: A sequence of sub-sequences where the sub-sequences are all of the permutations of the input
sequence. The order of the sub-sequence is in lexicographical order.&lt;/p&gt;
&lt;p&gt;Sub-Sequence
: A sequence of length &lt;code&gt;r&lt;/code&gt; that contains a permutation of the input sequence.&lt;/p&gt;
&lt;h1&gt;The Simplest Permutations Adaptor&lt;/h1&gt;
&lt;p&gt;While the &lt;a href=&quot;https://docs.python.org/3/library/itertools.html#itertools.permutations&quot;&gt;Python &lt;code&gt;itertools&lt;/code&gt;&lt;/a&gt;
and &lt;a href=&quot;https://docs.rs/itertools/0.13.0/itertools/structs/struct.Permutations.html&quot;&gt;Rust &lt;code&gt;itertools&lt;/code&gt;&lt;/a&gt;
iterators allow the user to specify the length of each sub-sequence, the simplest permutations
adaptor will always output sub-sequences the length of the original sequence.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;(https://github.com/tcbrindle/flux)&quot;&gt;Flux&lt;/a&gt; allows for C++ coroutines to be used to specify simple adaptors, which is great for
quickly developing a new adaptor. For a introduction to coroutines I would recommend
Joel Schumacher&apos;s
&lt;a href=&quot;https://theshoemaker.de/posts/yet-another-cpp-coroutine-tutorial&quot;&gt;Yet Another C++ Coroutine Tutorial&lt;/a&gt;
or David Mazieres&apos;
&lt;a href=&quot;https://www.scs.stanford.edu/~dm/blog/c++-coroutines.html&quot;&gt;My tutorial and take on C++20 Coroutines&lt;/a&gt;.
The basics for using a coroutine to create a Flux adaptor are as follows:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Construction&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; adaptor_name &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;sequence Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Seq seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;generator&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;element_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// Implementation&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Usage&lt;/span&gt;
flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ints&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;take&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;adaptor_name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;//...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;flux::generator&lt;/code&gt; type implements the &lt;code&gt;flux::sequence&lt;/code&gt; functionality we need, and the &lt;code&gt;_&lt;/code&gt; function
applies the adaptor to the input sequence.&lt;/p&gt;
&lt;p&gt;Now to create the permutations adaptor we&apos;ll consider what steps we need to take. First, we&apos;ll need to
cache the input sequence. We do this because each output-subsequence will be composed of the input values
and the input sequence may be a single pass input so that if we don&apos;t cache it we&apos;ll only be able to
produce the first output.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; permutations_adaptor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;sequence Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Seq seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;generator&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token comment&quot;&gt;/* subsequence type */&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;elem &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elem&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we need to consider what the subsequence type should be. Since we want our output to be a sequence
of subsequences, and we already have our input values cached in a &lt;code&gt;std::vector&lt;/code&gt; we might as well just
have the subsequence type be a &lt;code&gt;std::vector&lt;/code&gt; as well. There are other types we could use in order to
limit extraneous memory allocation, but we&apos;ll talk about that another day.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; permutations_adaptor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;sequence Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Seq seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;generator&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// ...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Producing the First Sub-Sequence&lt;/h2&gt;
&lt;p&gt;The first permutation in the sequence is the easiest, as it&apos;s just the exact same as the input sequence.
To return the first sequence, we&apos;ll use &lt;code&gt;co_yield&lt;/code&gt; to return the first element.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; permutations_adaptor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;sequence Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Seq seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;generator&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;elem &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elem&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;co_yield&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// first subsequence&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Producing Following Sub-Sequences&lt;/h2&gt;
&lt;p&gt;To get later sequences we can use the &lt;code&gt;std::next_permutation&lt;/code&gt; to produce the permutations. According to
the &lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm/next_permutation&quot;&gt;documentation&lt;/a&gt; the function
permutes the input sequence to the next permutation, and returns true if there is another permutation
possible, or false if all permutations have been exhausted. We can use the return value of
&lt;code&gt;std::next_permutation&lt;/code&gt; to determine if we should continue producing elements &lt;em&gt;and&lt;/em&gt; permute the elements
in one step.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; permutations_adaptor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;/* function signature */&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;/* cache input */&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;co_yield&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// first subsequence&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next_permutation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;co_yield&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// following subsequences&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we do a simple test with this adaptor and compare it with the Rust and Python implementations, we
see that for simple input sequences we get the correct result! (See the examples on
&lt;a href=&quot;https://godbolt.org/z/1rn7dev7E&quot;&gt;compiler explorer&lt;/a&gt;)&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Language&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;C++&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0],&lt;/code&gt;&lt;br&gt;&lt;code&gt; [2, 0, 1], [2, 1, 0]]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Python&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[(0, 1, 2), (0, 2, 1), (1, 0, 2), (1, 2, 0),&lt;/code&gt;&lt;br&gt;&lt;code&gt; (2, 0, 1), (2, 1, 0)]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Rust&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0],&lt;/code&gt;&lt;br&gt;&lt;code&gt; [2, 0, 1], [2, 1, 0]]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The number of possible permutations can be caculated using the following formula:&lt;/p&gt;
&lt;p&gt;$$ P(n,r) = \frac{n!}{(n - r)!} $$&lt;/p&gt;
&lt;p&gt;If we calculate that for an input of size three (e.g. size of &lt;code&gt;[0, 1, 2]&lt;/code&gt;) and a sub-sequence size
of three as well, we calculate that the number of permutations should be six.&lt;/p&gt;
&lt;p&gt;$$ P(3,3) = \frac{3!}{0!} = 6 $$&lt;/p&gt;
&lt;p&gt;This just verifies the output is the correct length.&lt;/p&gt;
&lt;p&gt;Let&apos;s add another test though really quick, we&apos;ll do the input sequence &lt;code&gt;[2,1,0]&lt;/code&gt;.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Language&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;C++&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[[2, 1, 0]]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Python&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[(2, 1, 0), (2, 0, 1), (1, 2, 0), (1, 0, 2)&lt;/code&gt;&lt;br&gt;&lt;code&gt; (0, 2, 1), (0, 1, 2)]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Rust&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[[2, 1, 0], [2, 0, 1], [1, 2, 0], [1, 0, 2]&lt;/code&gt;&lt;br&gt;&lt;code&gt; [0, 2, 1], [0, 1, 2]]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Well, that&apos;s clearly not correct. If we step through the code using &lt;code&gt;lldb&lt;/code&gt; (or any other debugger) the
issue becomes apparent: &lt;code&gt;std::next_permutation&lt;/code&gt; returns false on the first call to it, so the adaptor
only returns the very first sub-sequence in the result, which is not what we expected. Let&apos;s return
back to the documentation.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Permutes the range [first, last) into the next permutation. Returns true if such a “next permutation”
exists; otherwise transforms the range into the lexicographically first permutation (as if by
std::sort) and returns false.
&lt;br&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm/next_permutation&quot;&gt;cppreference/next_permutation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This function does the permutation in-place on the input sequence, using the ordering of the values
of the sequence itself. Since the input sequence is sorted in reverse order (&lt;code&gt;[2, 1, 0]&lt;/code&gt;) then the
next lexicographical permutation of the sequence should be &lt;code&gt;[0, 1, 2]&lt;/code&gt; (the sequence in sorted order).
Clearly, we can see that the Python and Rust sequences don&apos;t return this as the next value. The second
value of both of other implementations is &lt;code&gt;[2, 0, 1]&lt;/code&gt;, which isn&apos;t the next lexicographical permutation
of the input sequence. So what is going on here?&lt;/p&gt;
&lt;p&gt;I pulled up the source for both the Python and the Rust implementations, and I found that both
implementations don&apos;t actually permute the input based off the input values, but rather they permute
the input based off the indices of the input. Here&apos;s an example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Input:
  [2, 1, 0]

Permutations based off value:
  [[2, 0, 1], [0, 1, 2], [0, 2, 1], [1, 0, 2], ...]

Permutations based off index:
  Values:  [[2, 1, 0], [2, 0, 1], [1, 2, 0], ...]
  Indices: [[0, 1, 2], [0, 2, 1], [1, 0, 2], ...]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on this information, we can change our adaptor in a few ways. First, we need to keep track of
all of the permuted indices of the input sequence. Secondly, we need a way to reindex the cached
input sequence by the permuted indices. Let&apos;s write the reindex function first.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;using&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reindex_sequence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;size_t&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;indices
                      &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;index &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Great! Now that we have a function that can take in the vectors of indices and inputs and return a
vector with the input values ordered by the indices vector. Let&apos;s add indices into our adaptor now.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; permutations_adaptor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;sequence Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Seq seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;generator&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;elem &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elem&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;size_t&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;indices&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;iota&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;co_yield&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// first subsequence&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next_permutation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// following subsequences&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;co_yield&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reindex_sequence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Great! Our output now matches our reference implementations in Python and Rust.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align:center&quot;&gt;Language&lt;/th&gt;
&lt;th style=&quot;text-align:left&quot;&gt;Output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;C++&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[[2, 1, 0], [2, 0, 1], [1, 2, 0], [1, 0, 2]&lt;/code&gt;&lt;br&gt;&lt;code&gt; [0, 2, 1], [0, 1, 2]]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Python&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[(2, 1, 0), (2, 0, 1), (1, 2, 0), (1, 0, 2)&lt;/code&gt;&lt;br&gt;&lt;code&gt; (0, 2, 1), (0, 1, 2)]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align:center&quot;&gt;Rust&lt;/td&gt;
&lt;td style=&quot;text-align:left&quot;&gt;&lt;code&gt;[[2, 1, 0], [2, 0, 1], [1, 2, 0], [1, 0, 2]&lt;/code&gt;&lt;br&gt;&lt;code&gt; [0, 2, 1], [0, 1, 2]]&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;With that complete, our simple permutations adaptor is now complete! To follow the process
of adding this adaptor into the official Flux library read the follow up articles to this
one.&lt;/p&gt;
&lt;p&gt;Thanks for reading!&lt;/p&gt;
&lt;hr&gt;
&lt;h1&gt;Complete Adaptor&lt;/h1&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Made with Flux 0.4.0 and C++20&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;algorithm&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;iostream&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;numeric&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token macro property&quot;&gt;&lt;span class=&quot;token directive-hash&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;token directive keyword&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&amp;lt;flux.hpp&gt;&lt;/span&gt;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Return a vector containing a permutation of the &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// input values in the order of the indices given in &lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;// the indices vector&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;reindex_sequence&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;size_t&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;indices
                      &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&lt;/span&gt;index &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push_back&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;input&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;index&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; output&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Generates all permutations of a given input sequence&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; permutations_adaptor &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;sequence Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;Seq seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;generator&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// cache the input&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;elem &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; seq&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;emplace_back&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;elem&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// generate the indices vector from [0, n) where &lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// n is the size of the input sequence &lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;size_t&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;indices&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;iota&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;co_yield&lt;/span&gt; cache&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// first subsequence&lt;/span&gt;

    &lt;span class=&quot;token comment&quot;&gt;// permute the indices, and return the input reindexed by &lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;// the indices&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;next_permutation&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;// following subsequences&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;co_yield&lt;/span&gt; &lt;span class=&quot;token generic-function&quot;&gt;&lt;span class=&quot;token function&quot;&gt;reindex_sequence&lt;/span&gt;&lt;span class=&quot;token generic class-name&quot;&gt;&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;flux&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;value_t&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;Seq&lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;cache&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; indices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 <entry>
   <title>How to Write a Flux Adaptor Using Coroutines</title>
   <link href="https://assumefalse.com/posts/flux-adaptor-coyield.html"/>
   <updated>2024-12-30T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/flux-adaptor-coyield.html</id>
   <content type="html">&lt;p&gt;Todo:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;[ ] Fix date when finished.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;$$
T_{over} = p \sum{x}
$$&lt;/p&gt;
&lt;h2&gt;Target Audience&lt;/h2&gt;
&lt;p&gt;Professional C++ programmers and students/researchers who are familiar with C++.&lt;/p&gt;
&lt;h2&gt;Goal&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Provide a resource that details how to write a quick adaptor using coroutines in
Flux.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Provide working examples version locked on compiler explorer.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Outline&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;Quick intro to flux
&lt;ul&gt;
&lt;li&gt;Example of sequence oriented programming&lt;/li&gt;
&lt;li&gt;Links to some talks and source&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Basics of coyield
&lt;ul&gt;
&lt;li&gt;Explain how cpp implements coroutines, and what it actually
means to use a coroutine&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Very basics of writing an adaptor
&lt;ul&gt;
&lt;li&gt;We need a resumable function that yields elements in the sequence
and returns when complete.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Writing a simple adaptor using coyield
&lt;ul&gt;
&lt;li&gt;Replicate the adaptor made by tcbrindle in his tutorial file&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Writing a more complex adaptor using coyield
&lt;ul&gt;
&lt;li&gt;Write a permutations adaptor using coyield&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Optional. Performance and usage tradeoffs&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>WaddoupsNet - A TCP/IP Stack on Linux</title>
   <link href="https://assumefalse.com/posts/ece_5600_tcp_ip_stack.html"/>
   <updated>2024-10-23T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/ece_5600_tcp_ip_stack.html</id>
   <content type="html">&lt;p&gt;In ECE 5600 I created a TCP/IP stack for a lab that we worked on throughout the
semester. This is a document of what I did and how I did it. It&apos;s also a good
change to learn jekyll better. One other note, I started these labs before I
started making notes here, so it&apos;s definitely going to feel disjoint until I have
time to clean this post up after the semester.&lt;/p&gt;
&lt;h2&gt;Lab 3 - Adding IP v4 and ICMP&lt;/h2&gt;
&lt;p&gt;Having added the ability to unwrap ether frames and handle ARP requests / replies,
we are now ready to sprinkle some IP v4 all over our project with a dash of ICMP
on top.&lt;/p&gt;
&lt;p&gt;Here&apos;s how our stack is going to look after we are done.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/5600_tcpip/icmp_ip_stack.png&quot; alt=&quot;Planned Stack After Adding IP v4 and ICMP&quot;&gt;&lt;/p&gt;
&lt;p&gt;Code wise in our stack application we hopefully will be able to end up with something
similar to this for our IP and ICMP handler:&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ipv4_handler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    channel&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;ether_frame&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; ether_frames&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    channel&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;ipv4_packet&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; ipv4_packets&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;icmp_handler&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;channel&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;ipv4_packet&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; ipv4_packets&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Adding the IP v4 Packet &lt;code&gt;Struct&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;In line with what we have done previously in creating a struct to represent ether frames
we&apos;ll do the same for IP v4 packets. For reference, the current description of an IP v4
packet is given in RFC 791, but I just used the &lt;a href=&quot;https://en.wikipedia.org/wiki/IPv4&quot;&gt;wikipedia page&lt;/a&gt;
as my reference. It gets tiresome to continually refer to the packet as an &amp;quot;IP v4&amp;quot; packet
so from now on we&apos;ll assume that anytime called IP is just referring to IP v4, unless
otherwise stated.&lt;/p&gt;
&lt;pre class=&quot;language-cpp&quot;&gt;&lt;code class=&quot;language-cpp&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ipv4_packet&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; octet version&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0x4U&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    octet ihl&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    octet dscp&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    octet ecn&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; total_length&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; identification&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    octet flags&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; fragment_offset&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    octet time_to_live&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    octet protocol&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;uint16_t&lt;/span&gt; checksum&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    ip_v4_address source&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    ip_v4_address dest&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;octet&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; options&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;octet&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; data&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ipv4_packet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;explicit&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ipv4_packet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; raw_packet rpacket&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;from_raw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;rpacket&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;from_raw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; raw_packet rpacket&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;nodiscard&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;to_raw&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; std&lt;span class=&quot;token double-colon punctuation&quot;&gt;::&lt;/span&gt;vector&lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;octet&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One of the challenges with going from and ether frame into an IP packet is that a single
packet may be spread across several frames. We can see that the &lt;code&gt;total_length&lt;/code&gt; of a packet
is determined by a 16-bit unsigned integer, which means that an IP packet can be
about &lt;em&gt;65000&lt;/em&gt; bytes long! Our ether frames are only limited to about 1500 bytes, so we will
have to handle the case where a single IP packet is scattered across many frames when
we build up a packet.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Finding all n-sized subgraphs in a given graph</title>
   <link href="https://assumefalse.com/posts/all-subgraphs-in-a-graph.html"/>
   <updated>2024-08-13T00:00:00.000Z</updated>
   <id>https://assumefalse.com/posts/all-subgraphs-in-a-graph.html</id>
   <content type="html">&lt;p&gt;A few weeks ago a co-worker approached with me with an interesting problem he was dealing with. How could one
find all &lt;em&gt;n&lt;/em&gt;-sized subtrees in any given undirected graph?&lt;/p&gt;
&lt;p&gt;We were driving at the time, and so we talked and hand-waved our way through how we thought we could
implement an algorithm that finds
all the subtrees of size &lt;em&gt;n&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Originally, this problem was solved in MATLAB, but since MATLAB is not free and not commonly used outside of
engineering companies, I reimplemented a solution in Python and then a solution in C++ using the Boost Graph
Library (coming soon... eventually...).&lt;/p&gt;
&lt;h1&gt;A Motivating Demonstration&lt;/h1&gt;
&lt;p&gt;&lt;img src=&quot;/assets/image/all_subgraphs/blank_graph.png&quot; alt=&quot;Example Graph with 8 Vertices&quot;&gt;{: width=&amp;quot;50%&amp;quot; }&lt;/p&gt;
&lt;p&gt;Say we want to find all the subtrees with only two vertices in the graph shown here.
How would we go about doing it by hand?
I would start by choosing a random vertex, and then I would make a list of all the
neighboring vertices. When combined with the original vertex as a pair, I now have all subgraphs
of size two that include our initial vertex.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Initial vertex = 4
Neighbors of V[4] = 7, 0

Subgraphs of size 2 = [
    [4, 7],
    [4, 0],
]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After I make a list of pairs for the chosen vetex, I would move on to another vertex and make
a list of the pairs following the same steps, then I would concatenate that list onto some global
list of all of the subgraphs of size 2. After doing this process for each vertex, there will be
many duplicate subgraphs in the global list, so I&apos;ll need to filter out duplicates, and then I
will finally have the list of all subgraphs of size 2 of the given graph.&lt;/p&gt;
&lt;p&gt;This process is not too hard to do by hand, but it is time consuming, even for this
To do this process repeatedly on larger graphs we need to automate it in some way.&lt;/p&gt;
&lt;h1&gt;Automating the Process&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;Please note that code in this section does not actually run due to the way python lists work, but
is used to show my thought process in discovering the initial algorithm&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When I don&apos;t know exactly how an algorithm or a process is going to look before I write it out, I
like to start with what I do know. For this problem, I know that I&apos;ll probably need to start by
iterating over each of the vertices in the original graph, as that is essentially how I started when
doing this problem by hand. Each vertex in the
original graph &lt;em&gt;may&lt;/em&gt; be part of a &lt;em&gt;n&lt;/em&gt;-sized subgraph, so we need to consider each one. That means that from the
get-go, we are looking at a minimum of &lt;em&gt;O(V)&lt;/em&gt; complexity, where &lt;em&gt;V&lt;/em&gt; is the number of vertices in the starting
graph.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; vertex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next if we consider our hand algorithm, we can see that from each vertex we need to get a list of all the neighbors
of the vertex. We do this to consider what the &amp;quot;next vertex&amp;quot; in our subgraph may be.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; vertex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    next_vertices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For our previous example of finding all subgraphs of size two, this is essentially all we need to do! We just need
to find the neighbors of each nodes in the graph, enumerate all the unique pairs, and call it a day. A python
implementation of this simple case might look something like this.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

subgraphs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; vertex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    next_vertices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    pairs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; v2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; v2 &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; next_vertices&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    subgraphs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;pairs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

unique_subgraphs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;subgraphs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach works great for when &lt;em&gt;n = 2&lt;/em&gt;, but has no way to track the size of the current subgraph
we are looking at which is needed for &lt;em&gt;n&lt;/em&gt; to be genericized.
That way we can tell when we have created a subgraph of size &lt;em&gt;n&lt;/em&gt;, and we can
return the subgraph once we have reached the desired size. We actually do this in our previous implementation,
but it is not clear unless we rewrite the solution to include comments that track &lt;em&gt;n&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;g &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; Graph&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

subgraphs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; vertex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;
    next_vertices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    current_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# n = 1&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; v2 &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; next_vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        current_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; current_subgraph &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;v2&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# n = 2&lt;/span&gt;
       
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            subgraphs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;updated_current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now it&apos;s clear that we actually are building up subgraphs until we reach the desired size (in this
case 2) and then stopping and moving on to the next subgraph. If we wanted to extend this to find
subgraphs of size 3, it&apos;s as simple as adding one more step to get to &lt;em&gt;n = 3&lt;/em&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; vertex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    current_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# n = 1&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; v2 &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        current_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; current_subgraph &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;v2&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# n = 2&lt;/span&gt;
       
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; v3 &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            current_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; current_subgraph &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;v3&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
            n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# n = 3&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
                subgraphs&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;append&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;updated_current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To extend this further we just need to another &lt;code&gt;for&lt;/code&gt; loop, and another &lt;code&gt;for&lt;/code&gt; loop, etc. etc. If we
take a step back, we can see that we are actually taking recursive steps here. The structure of
the &lt;code&gt;for&lt;/code&gt; loop for &lt;code&gt;v2&lt;/code&gt; and for &lt;code&gt;v3&lt;/code&gt; is the same, so we can actually just write a recursive function
to do this work. Here are the operations for the &lt;em&gt;k_th&lt;/em&gt; step in the sequence.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Iterate over all the neighbors of the last node in the current subgraph&lt;/li&gt;
&lt;li&gt;Append each neighbor to the subgraph&lt;/li&gt;
&lt;li&gt;If &lt;em&gt;k == n&lt;/em&gt;, append the subgraph to the global list of subgraphs and end&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&apos;s what this might look like in python.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; v_k &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    current_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; current_subgraph &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;v_k&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# n = k&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Algorithm Developement&lt;/h2&gt;
&lt;p&gt;Given the four generic steps we determined above, we can now refactor our code into a recursive
function to calculate all the subgraphs. We&apos;ll call this function &lt;code&gt;all_n_subgraphs()&lt;/code&gt;. We&apos;ll also
take this opportunity to plan out what types to use, and how we want our calling specification
laid out.&lt;/p&gt;
&lt;h3&gt;Types&lt;/h3&gt;
&lt;p&gt;Python doesn&apos;t have a built-in graph type, and hand rolling a graph type is out of scope, so we&apos;ll
use a prebuilt graph package to represent our graphs. I did some some research, and I decided that the
&lt;a href=&quot;https://python.igraph.org/en/stable/index.html&quot;&gt;&lt;code&gt;igraph&lt;/code&gt;&lt;/a&gt; package for python would suit
our needs just fine. &lt;a href=&quot;https://python.igraph.org/en/stable/index.html&quot;&gt;&lt;code&gt;igraph&lt;/code&gt;&lt;/a&gt; has a &lt;em&gt;lot&lt;/em&gt; of
functionality, but for this algorithm to run, we really only need two functions from
the &lt;a href=&quot;https://python.igraph.org/en/stable/index.html&quot;&gt;&lt;code&gt;igraph&lt;/code&gt;&lt;/a&gt; library:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Graph.neighbors(v: vertex)&lt;/code&gt; -- returns the neighbors to a vertex in a graph.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Graph.vs&lt;/code&gt; -- returns the list of all vertices in the graph.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Inputs and Outputs&lt;/h3&gt;
&lt;p&gt;Let&apos;s consider what we want &lt;code&gt;all_n_subgraphs()&lt;/code&gt; to do, and build a specification for this
function. The graph &lt;em&gt;G&lt;/em&gt;, and the desired size of the subtrees, &lt;em&gt;n&lt;/em&gt;, will be inputs to
&lt;code&gt;all_n_subgraphs()&lt;/code&gt;. Our output will be a list of all the subgraphs, represented by lists
of size &lt;em&gt;n&lt;/em&gt; with the vertex indices (integers) of the subgraph.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; igraph &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; ig

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;all_n_subgraphs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;G&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ig&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Another important part of creating a specification for a function is defining the input and
output conditions. Considering our inputs, we can make a few assertions that should be fulfilled
for the function to work properly. First, we know that &lt;em&gt;n&lt;/em&gt; should be greater than zero. While it&apos;s
certainly possible to represent a subgraph of size zero as an empty list, &lt;code&gt;[]&lt;/code&gt;, it&apos;s not that
useful. Secondly, we should state that &lt;em&gt;n&lt;/em&gt; cannot be greater than the number of vertices &lt;em&gt;V&lt;/em&gt; in
the graph &lt;em&gt;G&lt;/em&gt;. This is because a subgraph of &lt;em&gt;G&lt;/em&gt; cannot have more vertices than &lt;em&gt;G&lt;/em&gt;. These
two assertions can be formalized in &lt;code&gt;assert&lt;/code&gt; statements at the top of our function.&lt;/p&gt;
&lt;p&gt;Beyond declaring the type of our output, there isn&apos;t really anything we can state about the output
of &lt;code&gt;all_n_subgraphs()&lt;/code&gt;, as the list could theoretically be any size greater than or equal to zero.&lt;/p&gt;
&lt;h3&gt;Specification&lt;/h3&gt;
&lt;p&gt;When we bring all the parts together, we get a nice specification for our function.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; igraph &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; ig

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;all_n_subgraphs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;G&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ig&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Error in &apos;all_n_subgraphs&apos;: n must be greater than 0&quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;G&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Error in &apos;all_n_subgraphs&apos;: n is greater than len(Graph.vs)&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, is doing input validation in this way very pythonic? Probably not, but it&apos;s good
enough for me in this example! Now we need to add in the rest of the logic to complete
this function.&lt;/p&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; igraph &lt;span class=&quot;token keyword&quot;&gt;as&lt;/span&gt; ig

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;all_n_subgraphs&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;G&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ig&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Error in &apos;all_n_subgraphs&apos;: n must be greater than 0&quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;G&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vs&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;=&lt;/span&gt; n&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Error in &apos;all_n_subgraphs&apos;: n is greater than len(Graph.vs)&quot;&lt;/span&gt;&lt;/span&gt;

    subgraphs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; vertex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        next_vertices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        current_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        subgraphs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subgraphs &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; subtrees&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;G&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; next_vertices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;difference&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;b&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;subtrees&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;G&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; ig&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Graph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
             n&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
             current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; 
             next_vertices&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;current_subgraph&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    subgraphs&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; vertex &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; next_vertices&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token comment&quot;&gt;# concat instead of appending because we need a copy&lt;/span&gt;
        new_subgraph &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; current_subgraph &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;

        new_next_vertices &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; next_vertices &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; g&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;neighbors&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;vertex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        unique_next_steps &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; difference&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;new_next_vertices&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; new_subgraph&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        subgraphs &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; subgraphs &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; next_subtree&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;g&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            new_subgraph&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; unique_next_steps&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; original_vertex&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 

</feed>
