<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://cdacamar.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://cdacamar.github.io/" rel="alternate" type="text/html" /><updated>2023-12-11T00:42:51+00:00</updated><id>https://cdacamar.github.io/feed.xml</id><title type="html">invoke::thought()</title><subtitle>A collection of random (mostly C++) things.</subtitle><author><name>Cameron DaCamara</name></author><entry><title type="html">Text Editor Data Structures: Rethinking Undo</title><link href="https://cdacamar.github.io/data%20structures/algorithms/benchmarking/text%20editors/c++/rethinking-undo/" rel="alternate" type="text/html" title="Text Editor Data Structures: Rethinking Undo" /><published>2023-12-09T00:00:00+00:00</published><updated>2023-12-09T00:00:00+00:00</updated><id>https://cdacamar.github.io/data%20structures/algorithms/benchmarking/text%20editors/c++/rethinking-undo</id><content type="html" xml:base="https://cdacamar.github.io/data%20structures/algorithms/benchmarking/text%20editors/c++/rethinking-undo/">&lt;script src=&quot;/assets/js/post-scripts/undo.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Undo and redo have been a staple operation of text editors probably since the first typo was ever made, yet there has not been a lot of innovation
around refining the idea of what undo and redo &lt;em&gt;could&lt;/em&gt; be.  Let&amp;#8217;s explore what I mean&amp;#8230;&lt;/p&gt;

&lt;h2 id=&quot;on-the-subject-of-undo&quot;&gt;On The Subject of Undo&lt;/h2&gt;

&lt;h3 id=&quot;undo&quot;&gt;Undo&lt;/h3&gt;

&lt;p&gt;In order to understand how to change undo we first need to understand what the fundamental operation of &amp;#8216;undo&amp;#8217; is supposed to do.
I have always learned best through example so let&amp;#8217;s assume you start with the string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello&quot;&lt;/code&gt; and you start typing the sequence &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; World!&quot;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Hello&lt;span id=&quot;undo-basic&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In the scenario above, when the undo operation is invoked (typically CTRL+z) what is the typical expectation?  There are a myriad of possibilities:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Undo would delete the entire string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; World!&quot;&lt;/code&gt; since it was all part of the same insert operation.&lt;/li&gt;
  &lt;li&gt;Undo would delete only the last character added, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'!'&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Undo would delete the sequence of characters within some time threshold of each keystroke.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are many approaches to what the undo operation should do and each one has its own merits.  The approach I use is close to #1 where the undo
operation will undo blocks of the same style of edit.  I find this approach to be useful for my workflow.  If you are interested in reading a bit
more about how popular text editors more concretely implement undo/redo I encourage you to check out &lt;a href=&quot;https://www.mattduck.com/undo-redo-text-editors&quot;&gt;Undo/redo implementations in text editors&lt;/a&gt;
by Matt Duck who very carefully breaks down the approaches of several editors.&lt;/p&gt;

&lt;p&gt;There are a few properties of undo that are very nice to have:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The undo operation should create a corresponding redo operation, more on redo later.&lt;/li&gt;
  &lt;li&gt;If undo is applied to the beginning of edit history, the system should report that an undo is not possible and do nothing as a result.&lt;/li&gt;
  &lt;li&gt;If you undo (or redo) to the point of the last save of the document the system should recognize that there is nothing to save.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Having #3 is very important to me because it helps me understand where my last &amp;#8216;committed&amp;#8217; work was during an edit session.  It seems like a small
&amp;#8220;nice to have feature&amp;#8221; but the system recognizing various points in history is essential for the feature we&amp;#8217;re about to implement.&lt;/p&gt;

&lt;h3 id=&quot;redo&quot;&gt;Redo&lt;/h3&gt;

&lt;p&gt;Redo is really quite similar to undo in many ways.  Let&amp;#8217;s take our example of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello World!&quot;&lt;/code&gt;, and apply an undo operation (using the preferred implementation above):&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Hello&lt;span id=&quot;redo-basic&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Redo is the complement to undo and it should perform the same operation that the undo operation just removed.  Since there&amp;#8217;s a dependency on undo in order to redo it implies that
the redo operation cannot be applied on a document which has had no undo operations performed.  Furthermore, a redo operation should inherit all the same properties that undo has, e.g.
the system should be aware of redo operations as a moment in time rather than a regular operation.&lt;/p&gt;

&lt;h3 id=&quot;perils-of-undo-and-redo&quot;&gt;Perils of Undo and Redo&lt;/h3&gt;

&lt;p&gt;Undo and redo are fantastic operations to have in our editor toolbox, in fact in the way I edit documents today I would argue that undo and redo are a fundamental operation.
There are some drawbacks in the way that undo and redo tend to work in most text editors though.  To help illustrate what I mean, let&amp;#8217;s go back to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello World!&quot;&lt;/code&gt; example one more time:&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Hello&lt;span id=&quot;undo-history-break&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If we apply undo to remove &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; World!&quot;&lt;/code&gt; and start typing again, most editors will remove the redo state.  There are some interesting approaches to help combat this problem, one that comes
to mind is the &lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/emacs/Undo.html&quot;&gt;Emacs undo system&lt;/a&gt; which keeps undo operations in a separate buffer after you edit again so you can undo that undo and get
back to the point where you can redo after an initial undo.&lt;/p&gt;

&lt;p&gt;To get a better idea of exactly what happens in most editors, let&amp;#8217;s see a small graph of the situation above:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/undo-redo-graph-initial.png&quot; class=&quot;img-gallery&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;In our initial state, all we&amp;#8217;ve done is inserted the text &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; World!&quot;&lt;/code&gt;.  When we apply the undo operation we get:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/undo-redo-graph-undo.png&quot; class=&quot;img-gallery&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;However, after we insert the new string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; Cameron!&quot;&lt;/code&gt; we end up orphaning the original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; World!&quot;&lt;/code&gt; node because the history in most editors is intended to be linear:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/undo-redo-graph-insert.png&quot; class=&quot;img-gallery&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;Perhaps with &lt;a href=&quot;https://github.com/cdacamar/fredbuf&quot;&gt;fredbuf&lt;/a&gt; we could do better&amp;#8230;&lt;/p&gt;

&lt;h2 id=&quot;rethinking-undo&quot;&gt;Rethinking Undo&lt;/h2&gt;

&lt;h3 id=&quot;basic-idea&quot;&gt;Basic Idea&lt;/h3&gt;

&lt;p&gt;Since fredbuf is a purely functional data structure, we have the ability to very cheaply store any reference to an editor buffer state.  So what if we created a graph out of it?  What if instead of having a
linear list of undos and redos that fredbuf currently has as a builtin, perhaps we could manage a graph of editor states ourself so we could then use some kind of system of UI to navigate it and &amp;#8216;snap&amp;#8217; back
to any of these states.&lt;/p&gt;

&lt;p&gt;Imagine we now store the undo/redo data in the following type of structure:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UndoRedoNode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UndoRedoNodePtr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unique_ptr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UndoRedoNode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UndoRedoChildren&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UndoRedoNodePtr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UndoRedoNode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;UndoRedoChildren&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;children&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;PieceTree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RedBlackTree&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Using the definition above, we can create a graph of undo/redo nodes.  There might be a question of, why do we need &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;children&lt;/code&gt; to be an array?  This comes back to our last example where we showed the string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; Cameron!&quot;&lt;/code&gt;
inserted.  If the undo operation were applied yet again and a new string entered, we would create yet another branch at the root node, so we need the capability of the structure above to refer to all possible states.&lt;/p&gt;

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

&lt;p&gt;Now that we know how we need to model the data, how do we view it?  Graph visualization is a massive topic and there are many intuitive ways to view various types of graphs.  The graph we are building with our undo data
is closer to a tree (but we can talk about how to turn this into something like a &lt;a href=&quot;https://en.wikipedia.org/wiki/Directed_acyclic_graph&quot;&gt;DAG&lt;/a&gt; in the future).  While editing I have always found it useful to associate timestamps
with historical data, as they help me get a better idea for roughly what that change might have been, so visualizing the time the change was made is sensible to me.  Additionally, since the tree is a sequence made over time
it made sense to me to lay out the tree horizontally with branches moving towards the right of the screen, somewhat like a &lt;a href=&quot;https://git-school.github.io/visualizing-git/&quot;&gt;git branch structure visualization&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When looking at an undo graph in particular it is generally not sufficient for me to simply see the nodes, I also need to see &lt;em&gt;what&lt;/em&gt; those nodes contain so I can make an informed decision about whether or not I actually want to
apply that undo/redo operation.  Enter, the diffing method.  The most natural way for me to see deltas in edits is good ol&amp;#8217; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diff&lt;/code&gt;.  Let&amp;#8217;s look at a diff of adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot; World!&quot;&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello&quot;&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-diff highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gd&quot;&gt;-Hello
&lt;/span&gt;&lt;span class=&quot;gi&quot;&gt;+Hello World!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Since I work with git extensively, this is exactly the kind of thing I want to see in the UI as I navigate from the current edit to some other edit in the past.  It allows me to reason about the changes the jump will cause.
This means there is only one last problem to solve: implementing the diff algorithm in the editor for various types of snap points.  Luckily for us, there is a fantastic series of posts by James Coglan
(&lt;a href=&quot;https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/&quot;&gt;&amp;#8220;The Myers diff algorithm&amp;#8221;&lt;/a&gt;) which goes over all the gory details of how to implement the popular Myers diff algorithm&amp;mdash;I can&amp;#8217;t recommend this series
enough for anyone interested in implementing their own diff algorithm.&lt;/p&gt;

&lt;h3 id=&quot;putting-it-all-together&quot;&gt;Putting It All Together&lt;/h3&gt;

&lt;p&gt;Now that we have the data structure and the visualization strategy down, let&amp;#8217;s see how it looks all together.  Let&amp;#8217;s go back to that example where we branched the UI states but had no way to recover the redo of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Hello&quot;&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Hello&lt;span id=&quot;undo-history-break2&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;And here&amp;#8217;s that same series of edits made in fred:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/fred-diff-demo.gif&quot; class=&quot;img-gallery&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;When you enter the undo/redo graph mode the editor allows you to navigate to any edit made at any point in time.  Additionally, the UI will display the diff from the current edit to the selected node in the upper left-hand corner.  It sort of turns your editor into a small source control system!&lt;/p&gt;

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

&lt;p&gt;What did I learn from this?&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Immutable data structures remain a fascinating point of interest for me.  They enable so many interesting avenues to solve problems.&lt;/li&gt;
  &lt;li&gt;The Myers diff algorithm is incredibly easy to implement, but takes a lot of time to understand what is happening so that you can render it properly.&lt;/li&gt;
  &lt;li&gt;It is going to be hard for me to go back to linear undo/redo of most editors :sweat_smile:.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you&amp;#8217;re also interested in text editor design, the C++ language, or compilers have a chat with me over on Twitter.  I&amp;#8217;m always interested in learning and sharing any knowledge that could prove to be useful to others.&lt;/p&gt;

&lt;p&gt;Until next time! &lt;i class=&quot;fa fa-thumbs-up&quot;&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>Cameron DaCamara</name></author><category term="Data Structures" /><category term="Algorithms" /><category term="Benchmarking" /><category term="Text Editors" /><category term="C++" /><category term="STL" /><category term="pretty code" /><category term="data structures" /><category term="c++" /><summary type="html"></summary></entry><entry><title type="html">Text Editor Data Structures</title><link href="https://cdacamar.github.io/data%20structures/algorithms/benchmarking/text%20editors/c++/editor-data-structures/" rel="alternate" type="text/html" title="Text Editor Data Structures" /><published>2023-06-08T00:00:00+00:00</published><updated>2023-06-08T00:00:00+00:00</updated><id>https://cdacamar.github.io/data%20structures/algorithms/benchmarking/text%20editors/c++/editor-data-structures</id><content type="html" xml:base="https://cdacamar.github.io/data%20structures/algorithms/benchmarking/text%20editors/c++/editor-data-structures/">&lt;p&gt;Text editors can be an interesting challenge to program.  The types of problems that text editors need to solve can range from trivial to mind-bogglingly difficult.  Recently, I have been on something of a spiritual journey to rework some internal data structures in an editor I have been building, specifically the most fundamental data structure to any text editor: the text.&lt;/p&gt;

&lt;h2 id=&quot;table-of-contents&quot;&gt;Table of Contents&lt;/h2&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#resources&quot;&gt;Resources&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#why&quot;&gt;Why?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#in-the-beginning&quot;&gt;In The Beginning&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#investigation&quot;&gt;Investigation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#my-own-piece-tree&quot;&gt;My Own Piece Tree&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fredbuf&quot;&gt;fredbuf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources&lt;/h2&gt;

&lt;p&gt;Before we dive into what I have done here it is important to call out some very useful resources I found for building your own text editor:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://viewsourcecode.org/snaptoken/kilo/&quot;&gt;Build Your Own Text Editor&lt;/a&gt; - Probably the most fundamental text editor from scratch blog post I&amp;#8217;ve seen.  This is an excellent tutorial if you are looking to get started on creating your own text editor.  It&amp;#8217;s worthwhile to note that the editor in this tutorial uses, essentially, a vector of lines as its internal data structure for text.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.averylaird.com/programming/the%20text%20editor/2017/09/30/the-piece-table&quot;&gt;Text Editor: Data Structures&lt;/a&gt; - A great overview of several data structures that one could use when implementing a text editor.  (Spoiler: at least one of these will be covered in this post)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=2UY_Am-Q-oI&amp;amp;list=PLpM-Dvs8t0VZVshbPeHPculzFFBdQWIFu&quot;&gt;Ded (Text Editor) YouTube playlist&lt;/a&gt; - This is a fantastic series in which &lt;a href=&quot;https://twitter.com/tsoding&quot;&gt;@tscoding&lt;/a&gt; goes through the process of building a text editor from scratch and served as the inspiration for me going down this rabbit hole in the first place.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;why&quot;&gt;Why?&lt;/h2&gt;

&lt;p&gt;If there are so many good resources out there for building your own text editor (let&amp;#8217;s also ignore the fact that there already exists phenomenal text editors today), why am I writing this?  There are a few reasons:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;I wanted to push myself into a project kind that I&amp;#8217;ve never tried before.&lt;/li&gt;
  &lt;li&gt;I wanted to create a tool that I might actually use.&lt;/li&gt;
  &lt;li&gt;Building my own data structures have always been something I wanted to tackle more of.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I went through the process of implementing my own data structure specifically because I observed a problem in my program where using something off-the-shelf would not exactly address the concerns I had or provide the flexibility I need.  Furthermore, the entire project of writing my own text editor is very much inspired from &lt;a href=&quot;https://handmade.network/&quot;&gt;Handmade Community&lt;/a&gt; so everything is built from scratch.  If I used an existing solution here I feel it would have been a disservice to the spirit on which the editor is built.&lt;/p&gt;

&lt;h2 id=&quot;in-the-beginning&quot;&gt;In The Beginning&amp;#8230;&lt;/h2&gt;

&lt;p&gt;I am a strong believer in &amp;#8220;experiment and get things working as fast as possible&amp;#8221;&amp;mdash;essentially, a fail fast mentality.  This is not to say that your first pass should ignore optimization, and I refuse to pessimize my code.  That said, I started from the simplest possible representation of a text file to start: a giant string.&lt;/p&gt;

&lt;p&gt;There are some pretty great properties of having a single string as your text buffer:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It is the most compact possible representation.&lt;/li&gt;
  &lt;li&gt;The algorithms for insertion and removal are simple.&lt;/li&gt;
  &lt;li&gt;It is very friendly to the rendering process because you can slice up the string into views which can be independently rendered without additional allocation.&lt;/li&gt;
  &lt;li&gt;Did I mention it is simple?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here&amp;#8217;s a short example of insertion and deletion:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;insert_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Editor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_view&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;old_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;insert_at&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cursor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end_range&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;old_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// First splat the buffer on the end, then rotate it to where&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// it needs to be.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;copy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rotate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;need_save&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;remove_range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Editor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;remove_start&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;remove_last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;removed_begin&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rotate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;remove_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;remove_last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;erase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;removed_begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;need_save&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note: Yes, the algorithms above could be simplified by just calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buf.insert&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buf.erase&lt;/code&gt; but the code above just expands the the work that would otherwise be done in those STL functions and do not change the overall complexity.&lt;/p&gt;

&lt;p&gt;But, oh boy, do the drawbacks hit hard.  If you want to work with any file beyond 1MB, good luck.  Not only do you really start to feel those linear time operations but if the underlying buffer needs to reallocate after some edits the entire process slows to a crawl as you need to allocate a larger buffer and copy each byte into the new buffer and destroy the old one.  It can turn an &lt;em&gt;O(n)&lt;/em&gt; operation into &lt;em&gt;O(n^2)&lt;/em&gt; randomly.&lt;/p&gt;

&lt;p&gt;There is another, more subtle, drawback to using a giant text buffer: how do you implement undo/redo?  The most obvious approach would be to have a separate data structure that tracks all of the individual insertions and deletes.  Insertions are much easier to track as you can store them as a range of offsets and simply discard them from the original buffer if the undo operation is invoked.  Deletions are a bit more tricky, as with the big string approach you really do need to store a small buffer containing the characters which were removed and the offset from which they were extracted.  Something like this would work:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;cstddef&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;string&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;InsertionData&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DeletionData&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted_buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UndoRedoData&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Kind&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Insertion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Deletion&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;Kind&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;union&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;InsertionData&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;DeletionData&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;UndoRedoData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InsertionData&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insertion&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ins&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;UndoRedoData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DeletionData&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Deletion&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UndoRedoData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insertion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InsertionData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Deletion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;del&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DeletionData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;but is overly complicated and requires potentially large allocation overheads when deleting large portions of text.  There must be a better way&amp;#8230;&lt;/p&gt;

&lt;h2 id=&quot;investigation&quot;&gt;Investigation&lt;/h2&gt;

&lt;p&gt;I knew I wanted to solve all the problems that having a single giant text buffer had, so my concrete goals are as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Efficient insertion/deletion.&lt;/li&gt;
  &lt;li&gt;Efficient undo/redo.&lt;/li&gt;
  &lt;li&gt;Must be flexible enough to enable UTF-8 encoding.&lt;/li&gt;
  &lt;li&gt;Efficient multi-cursor editing.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;gap-buffer&quot;&gt;Gap buffer&lt;/h3&gt;

&lt;p&gt;I initially started going down the path of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Gap_buffer&quot;&gt;gap buffer&lt;/a&gt;.  The gap buffer is very simple to implement and &lt;a href=&quot;https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer-Gap.html&quot;&gt;Emacs famously uses a gap buffer&lt;/a&gt; as its data structure.  A gap buffer would address the first point quite well for isolated edits.  It&amp;#8217;s not very common that I am editing all over the file all at once, is it?  Well&amp;#8230; Not quite.  One of my favorite (perhaps gimmicky) features of modern text editors is multi-cursor editing (sometimes called block-style edits, though block-style editing is slightly different) and after reading &lt;a href=&quot;https://nullprogram.com/blog/2017/09/07/&quot;&gt;&amp;#8220;Gap Buffers Are Not Optimized for Multiple Cursors&amp;#8221;&lt;/a&gt; (which, btw, has one of the best animations I&amp;#8217;ve ever seen to describe a gap buffer in action) Chris Wellons thoroughly convinced me that gap buffers are the wrong way to go because multi-cursor edits are important to &lt;em&gt;me&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Gap buffers are also plagued with similar problems as the giant string representation where efficient undo/redo is difficult without lots of extra storage/data structures.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient insertion/deletion.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-close&quot;&gt;&lt;/i&gt; Efficient undo/redo.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Must be flexible enough to enable UTF-8 encoding.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-close&quot;&gt;&lt;/i&gt; Efficient multi-cursor editing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Gap buffer is out.&lt;/p&gt;

&lt;h3 id=&quot;rope&quot;&gt;Rope&lt;/h3&gt;

&lt;p&gt;A &lt;a href=&quot;https://en.wikipedia.org/wiki/Rope_(data_structure)&quot;&gt;rope&lt;/a&gt; can be a very attractive data structure for text editors.  A rope has a lot of nice properties for editing in particular because it splits the file up into several smaller allocations which allow for very fast amortized insertions or deletions at any point in the file, &lt;em&gt;O(lg n)&lt;/em&gt;.  At first glance, it would seem that a rope addresses all of my initial concerns because operations like undo/redo can more easily be implemented in terms of a tree snapshot (or retaining the removed or changed nodes) and multi-cursor editing becomes a much more lightweight operation.&lt;/p&gt;

&lt;p&gt;So where&amp;#8217;s the catch?  Let&amp;#8217;s revisit undo/redo for a moment.&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/rope-1.png&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;Which represents the string &amp;#8220;HelloWorld&amp;#8221;.  If I want to introduce a space between &amp;#8220;Hello&amp;#8221; and &amp;#8220;World&amp;#8221; then the new tree would look like:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/rope-2.png&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;Where the node &amp;#8220;Hello&amp;#8221; was appended with a space at the end.  The most attractive version of a rope data structure is to make the entire thing immutable, which implies that when we go to extend the node containing &amp;#8220;Hello&amp;#8221; we actually end up creating a new node with a fresh buffer to contain the new string or we create a new node just to hold the &amp;#8220; &amp;#8220;, which seems equally as wasteful (though the rope should define some kind of constant to limit the size of each node).  Copying the node on write implies that, while your undo stack will contain a lightweight version of the original tree, your newly constructed tree can expand your total memory consumption much more than you might initially think.&lt;/p&gt;

&lt;p&gt;For the purposes of my editor, I want something a bit more lightweight.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient insertion/deletion.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-close&quot;&gt;&lt;/i&gt; Efficient undo/redo.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Must be flexible enough to enable UTF-8 encoding.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient multi-cursor editing.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;piece-table&quot;&gt;Piece Table&lt;/h3&gt;

&lt;p&gt;Now things are getting interesting.  The &lt;a href=&quot;https://en.wikipedia.org/wiki/Piece_table&quot;&gt;piece table&lt;/a&gt; is a data structure that has a long history with text editors.  &lt;a href=&quot;https://web.archive.org/web/20160308183811/http://1017.songtrellisopml.com/whatsbeenwroughtusingpiecetables&quot;&gt;Microsoft Word used a piece table once upon a time&lt;/a&gt; to implement features like fast undo/redo as well as efficient file saving.  The piece table appears to check many of the boxes for me.&lt;/p&gt;

&lt;p&gt;There is one, perhaps, slight drawback.  Due to the fact that the traditional piece table is implemented as a contiguous array of historical edits, it implies that long edit sessions could end up in a place where adding lots of small edits to the same file will cause some of the artifacts we saw with the giant text buffer start to appear, e.g. randomly your editor may slow down because the piece table is reallocated to a larger array.  Not only this, undo/redo stacks need to store this potentially large array of entries.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient insertion/deletion (minus very long edit sessions).&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient undo/redo (minus very long edit sessions).&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Must be flexible enough to enable UTF-8 encoding.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient multi-cursor editing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then, the VSCode team went and solved the problem.  Enter &lt;a href=&quot;https://code.visualstudio.com/blogs/2018/03/23/text-buffer-reimplementation&quot;&gt;piece tree&lt;/a&gt;&amp;#8230;&lt;/p&gt;

&lt;h3 id=&quot;piece-tree&quot;&gt;Piece Tree&lt;/h3&gt;

&lt;p&gt;I do not want to rehash everything that was covered in the original VSCode blog about their text buffer reimplementation, but I do want to cover why this particular version of a piece table is so interesting and what I needed to change to fill the gaps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit 6/13/2023&lt;/strong&gt;: It was pointed out that the VSCode team were not the ones to invent the piece tree.  Utilizing a tree to represent the individual pieces was suggested as far back as 1998 in &lt;a href=&quot;https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.48.1265&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;Data Structures for Text Sequences&lt;/a&gt;.  I merely found the VSCode implementation of the idea proposed in this paper to be of high quality and well-tested.&lt;/p&gt;

&lt;p&gt;The piece table that VSCode implemented is like if the traditional piece table and a rope data structure had a baby and that baby went on to outclass its parents in every conceivable way.  You get all of the beauty of rope-like insertion amortization cost with the memory compression of a piece table.  The piece tree achieves the fast and compressed insertion times through the use of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Red%E2%80%93black_tree&quot;&gt;red-black tree (RB tree)&lt;/a&gt; to store the individual pieces.  This data structure is useful because its guarantees about being a balanced search tree allow it to maintain a &lt;em&gt;O(lg n)&lt;/em&gt; search time no matter how many pieces are added over time.&lt;/p&gt;

&lt;p&gt;There was just one tiny piece missing&amp;#8230; the VSCode implementation does not use immutable data structures, which means that I will need to copy the entire piece tree (not the underlying data of course) in order to capture undo/redo stacks.  So it does have &lt;em&gt;some&lt;/em&gt; of the drawback of the piece table, so let&amp;#8217;s just fix it!  How hard could it be?  (spoilers: it was hard).&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient insertion/deletion.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient undo/redo (almost, a tree copy is involved).&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Must be flexible enough to enable UTF-8 encoding.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient multi-cursor editing.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;my-own-piece-tree&quot;&gt;My Own Piece Tree&lt;/h2&gt;

&lt;p&gt;The primary goal for implementing my own piece tree was to make it a purely functional data structure, which means the underlying view of the text should be entirely immutable and changing it would require a new partial tree.&lt;/p&gt;

&lt;p&gt;The piece tree that VSCode implemented uses a traditional RB tree.  After digging out my copy of &lt;a href=&quot;https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844&quot;&gt;Introduction to Algorithms&lt;/a&gt; book again I quickly realized that trying to reimplement the RB tree described in the book as a purely functional variant was not going to be easy for two reasons:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The book makes heavy use of a sentinel node which represents the NIL node but it can be mutated like a regular node, except there&amp;#8217;s only one.&lt;/li&gt;
  &lt;li&gt;The algorithms in the book make heavy use of the parent pointer in each node.  Parent pointers in purely functional data structures are a no go because a change anywhere in the tree essentially invalidates the entire tree.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Somebody had to have implemented an immutable RB tree, right?  Turns out&amp;#8230; yeah?  Kind of?  In my search I found &lt;a href=&quot;https://bartoszmilewski.com/2013/11/25/functional-data-structures-in-c-trees/&quot;&gt;Functional Data Structures in C++: Trees&lt;/a&gt; by Bartosz Milewski in which he covers a simple way to implement a purely functional RB tree, but only for insertion.  Insertion is essential to have but I also need deletion, I have to have deletion otherwise the piece tree simply will not work.  In the comments it was discussed that deletion is hard, very hard, and that often you need a new node concept to even handle it.  I really liked the approach Milewski took for insertion, so I ended up starting my RB tree with this data structure and implemented the rest of the piece tree around it, minus delete but we&amp;#8217;ll get to that later.&lt;/p&gt;

&lt;h3 id=&quot;where-i-diverge&quot;&gt;Where I Diverge&lt;/h3&gt;

&lt;p&gt;Now that I&amp;#8217;m implementing the text buffer as a tree it is extremely important that I design powerful debugging tools to accommodate my journey and evaluate what parts of the implementation need to change in order to solve my specific problems.  The original VSCode textbuffer code can be seen here: &lt;a href=&quot;https://github.com/microsoft/vscode-textbuffer&quot;&gt;https://github.com/microsoft/vscode-textbuffer&lt;/a&gt; as a stand-alone repo.  The real textbuffer implementation (which isn&amp;#8217;t really much different from the stand-alone repo) can be seen here: &lt;a href=&quot;https://github.com/microsoft/vscode/tree/main/src/vs/editor/common/model/pieceTreeTextBuffer&quot;&gt;https://github.com/microsoft/vscode/tree/main/src/vs/editor/common/model/pieceTreeTextBuffer&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id=&quot;crlf&quot;&gt;CRLF&lt;/h4&gt;

&lt;p&gt;My new implementation of this data structure does not stray too far from what VSCode&amp;#8217;s version does.  Where I start to differ is in the decision &lt;em&gt;not&lt;/em&gt; to codify CRLF line endings as a formal concept in the tree.  I don&amp;#8217;t like the workarounds they have in the implementation to account for CRLF to be contained within a single piece and I think it massively complicates the data structure for a feature which only needs to be really accounted for in a handful of scenarios.  In my existing text editor (without the new text buffer implementation) I account for CRLF in a separate data structure on the side which records line ranges.  If the editor is in &amp;#8220;CRLF mode&amp;#8221; it will rebuild the line ranges in a way that chops the carriage return when it is followed by a newline character.  With my new text buffer I don&amp;#8217;t record where CRLFs are at all, I simply account for them if a buffer modification happens which needs to delete both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\r&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt; or when moving the cursor to retract its position to before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\r&lt;/code&gt; if it precedes a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s an example of what the user sees when &amp;#8220;CRLF mode&amp;#8221; is active and the cursor navigates before/after a CRLF line:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/crlf-user.gif&quot; class=&quot;img-gallery&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s what the system does underneath to either skip the carriage return or retract to the character before it (the &amp;#xB6; character represents the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\r&lt;/code&gt;):&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
 &lt;img src=&quot;/images/crlf-system.gif&quot; class=&quot;img-gallery&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;The data structure does not need to record the CRLF due to the limited number of times the editor even needs to consider the carriage return character.&lt;/p&gt;

&lt;h4 id=&quot;debugging&quot;&gt;Debugging&lt;/h4&gt;

&lt;p&gt;Being able to debug a complicated data structure is essential if you intend to make any progress.  If you observe the stand-alone repo for the VSCode implementation you will note that the primary way of validating the buffer contents is through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getLineContent&lt;/code&gt;, which is a fundamental and important API.  For the purposes of really confirming the entire buffer I needed a different approach so I designed a tree walker with an iterator-like interface so I can use a for-each loop:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TreeWalker&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seek&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exhausted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Length&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;remaining&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// For Iterator-like behavior.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;TreeWalker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;WalkSentinel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TreeWalker&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TreeWalker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WalkSentinel&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WalkSentinel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TreeWalker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;walker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WalkSentinel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;walker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exhausted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This walker proved to be invaluable for validating edits and deletes and opened up a lot of extra room to ensure that UTF-8 support was correct since I needed the ability to walk specific codepoints at a given offset.  Naturally, the other debugging tools that follow are a way to print the tree nodes themselves as well as the associated pieces:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_piece&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Piece&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;piece&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;level&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tree&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;undoredo&quot;&gt;Undo/Redo&lt;/h4&gt;

&lt;p&gt;The VSCode implementation did not implement undo/redo directly as a text buffer operation, they did it through a system in which edits applied to the text buffer would record the series of &amp;#8216;reverse&amp;#8217; operations needed to undo the prior edit and pass that to the caller which was then responsible for storing this information and responsible for applying reverse edits in sequence.  This system is certainly one approach and avoids having to copy the entire tree again, but it relies on allocating buffers of data similarly to how we postulated undo/redo could be done with the single string implementation.  Here&amp;#8217;s my undo/redo routines:&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;UndoRedoResult&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;try_undo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;success&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;redo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_front&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undo_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;front&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;undo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop_front&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;compute_buffer_meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;success&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undo_offset&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;UndoRedoResult&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Tree&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;try_redo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;redo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;success&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CharOffset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;undo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;push_front&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;redo_offset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;redo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;front&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;redo_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pop_front&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;compute_buffer_meta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;success&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op_offset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;redo_offset&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Yep, that is all you need.  Two linked lists and a call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compute_buffer_meta()&lt;/code&gt; (which is a &lt;em&gt;O(lg n)&lt;/em&gt; operation).  When the entire data structure is immutable you can snap back to any root you have previously saved a reference to and the editor state will be completely consistent&amp;mdash;which, I will mention, brings up the possibility of creating a UI for branching editor states (like a mini-source control system) where the user could navigate an editor state graph to some state they had previously, no more messy &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit -am'testing'&lt;/code&gt; commits to only be reverted later!&lt;/p&gt;

&lt;h3 id=&quot;revisiting-deletion&quot;&gt;Revisiting Deletion&lt;/h3&gt;

&lt;p&gt;I said I would revisit delete operations because they&amp;#8217;re just as fundamental as an insertion operation when talking about text editors.  When going down the path of implementing immutable RB tree deletion, I&amp;#8217;ll be honest, I nearly gave up several times.  The algorithm mentioned in my algorithms book was simply not workable because it needed to be converted into a recursive version where we also somehow eliminate the sentinel nodes (which VSCode&amp;#8217;s RB tree implementation uses, but that is not necessarily a bad thing).  There is an excellent RB tree algorithm visualizer provided by the &lt;a href=&quot;https://www.cs.usfca.edu/~galles/visualization/RedBlack.html&quot;&gt;University of San Francisco CS department&lt;/a&gt; that I strongly urge you to check out if you&amp;#8217;re interested in exploring the algorithms in action.&lt;/p&gt;

&lt;p&gt;To get some idea of the complications behind why RB tree deletion is difficult we need to peek into the cases that need to be covered by the &lt;a href=&quot;https://en.wikipedia.org/wiki/Red%E2%80%93black_tree#Removal&quot;&gt;traditional RB tree deletion&lt;/a&gt;.  In particular the cases where you introduce a double-black violation by deleting a red node leaves a lot of room for error.  Luckily, someone else thought that implementing an immutable RB tree was useful and I found &lt;a href=&quot;https://github.com/dotnwat/persistent-rbtree&quot;&gt;persistent-rbtree&lt;/a&gt; which was actually adapted from the &lt;a href=&quot;https://github.com/orium/rpds&quot;&gt;Rust Persistent Data Structures&lt;/a&gt;.  This code had some reliable node deletion logic that I could adapt to my existing RB tree and it ended up serving as the basis for my deletion routine.  You can see how it is done in the final repo below.&lt;/p&gt;

&lt;p&gt;Finally, after implementing deletion I had my &amp;#8216;perfect&amp;#8217; data structure (for me):&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient insertion/deletion.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient undo/redo.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Must be flexible enough to enable UTF-8 encoding.&lt;/li&gt;
  &lt;li&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; Efficient multi-cursor editing.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;fredbuf&quot;&gt;fredbuf&lt;/h2&gt;

&lt;p&gt;It would be crazy of me to get this far and have no code to show for it.  &amp;#8216;fred&amp;#8217; is the name of my text editor and since I have sufficiently modified the VSCode textbuffer and adapted it specifically for &amp;#8216;fred&amp;#8217; I have dubbed it &amp;#8216;fredbuf&amp;#8217;.  You can view all of the code and tests at the &lt;a href=&quot;https://github.com/cdacamar/fredbuf&quot;&gt;repo for fredbuf&lt;/a&gt;.  It has no 3rd party library requirements so the build is as simple as you can get.  It is MIT licensed, so go wild!  Improve it beyond what I ever thought possible!  The repro just requires a C++ compiler that supports C++20.  Perhaps I&amp;#8217;ll release &amp;#8216;fred&amp;#8217; one day too.&lt;/p&gt;

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

&lt;p&gt;What did I learn from this?&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Doing research and identifying the constraints up-front is absolutely essential.  You don&amp;#8217;t want to waste time finding out that a complicated data structure won&amp;#8217;t solve your problems after you go through the pain of implementing it.&lt;/li&gt;
  &lt;li&gt;Create debugging utilities for data structures &lt;em&gt;very&lt;/em&gt; early on.  I doubt I would have completed this project if I had not created powerful debugging utilities from the start.&lt;/li&gt;
  &lt;li&gt;Immutable RB tree deletion is hard :sweat_smile:.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you&amp;#8217;re also interested in text editor design, the C++ language, or compilers have a chat with me over on Twitter.  I&amp;#8217;m always interested in learning and sharing any knowledge that could prove to be useful to others.&lt;/p&gt;

&lt;p&gt;Until next time! &lt;i class=&quot;fa fa-thumbs-up&quot;&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>Cameron DaCamara</name></author><category term="Data Structures" /><category term="Algorithms" /><category term="Benchmarking" /><category term="Text Editors" /><category term="C++" /><category term="STL" /><category term="pretty code" /><category term="data structures" /><category term="c++" /><summary type="html">Text editors can be an interesting challenge to program. The types of problems that text editors need to solve can range from trivial to mind-bogglingly difficult. Recently, I have been on something of a spiritual journey to rework some internal data structures in an editor I have been building, specifically the most fundamental data structure to any text editor: the text. Table of Contents Resources Why? In The Beginning Investigation My Own Piece Tree fredbuf Conclusion Resources Before we dive into what I have done here it is important to call out some very useful resources I found for building your own text editor: Build Your Own Text Editor - Probably the most fundamental text editor from scratch blog post I&amp;#8217;ve seen. This is an excellent tutorial if you are looking to get started on creating your own text editor. It&amp;#8217;s worthwhile to note that the editor in this tutorial uses, essentially, a vector of lines as its internal data structure for text. Text Editor: Data Structures - A great overview of several data structures that one could use when implementing a text editor. (Spoiler: at least one of these will be covered in this post) Ded (Text Editor) YouTube playlist - This is a fantastic series in which @tscoding goes through the process of building a text editor from scratch and served as the inspiration for me going down this rabbit hole in the first place. Why? If there are so many good resources out there for building your own text editor (let&amp;#8217;s also ignore the fact that there already exists phenomenal text editors today), why am I writing this? There are a few reasons: I wanted to push myself into a project kind that I&amp;#8217;ve never tried before. I wanted to create a tool that I might actually use. Building my own data structures have always been something I wanted to tackle more of. I went through the process of implementing my own data structure specifically because I observed a problem in my program where using something off-the-shelf would not exactly address the concerns I had or provide the flexibility I need. Furthermore, the entire project of writing my own text editor is very much inspired from Handmade Community so everything is built from scratch. If I used an existing solution here I feel it would have been a disservice to the spirit on which the editor is built. In The Beginning&amp;#8230; I am a strong believer in &amp;#8220;experiment and get things working as fast as possible&amp;#8221;&amp;mdash;essentially, a fail fast mentality. This is not to say that your first pass should ignore optimization, and I refuse to pessimize my code. That said, I started from the simplest possible representation of a text file to start: a giant string. There are some pretty great properties of having a single string as your text buffer: It is the most compact possible representation. The algorithms for insertion and removal are simple. It is very friendly to the rendering process because you can slice up the string into views which can be independently rendered without additional allocation. Did I mention it is simple? Here&amp;#8217;s a short example of insertion and deletion: void insert_buffer(Editor::Data* data, std::string_view buf) { auto old_size = data-&amp;gt;buf.size(); data-&amp;gt;buf.resize(data-&amp;gt;buf.size() + buf.size()); auto insert_at = next(begin(data-&amp;gt;buf), rep(data-&amp;gt;cursor)); auto end_range = next(begin(data-&amp;gt;buf), old_size); // First splat the buffer on the end, then rotate it to where // it needs to be. std::copy(begin(buf), end(buf), end_range); std::rotate(insert_at, end_range, end(data-&amp;gt;buf)); data-&amp;gt;need_save = true; } void remove_range(Editor::Data* data, CharOffset first, CharOffset last) { auto remove_start = next(begin(data-&amp;gt;buf), rep(first)); auto remove_last = next(begin(data-&amp;gt;buf), rep(last)); auto removed_begin = std::rotate(remove_start, remove_last, end(data-&amp;gt;buf)); data-&amp;gt;buf.erase(removed_begin, end(data-&amp;gt;buf)); data-&amp;gt;need_save = true; } Note: Yes, the algorithms above could be simplified by just calling buf.insert or buf.erase but the code above just expands the the work that would otherwise be done in those STL functions and do not change the overall complexity. But, oh boy, do the drawbacks hit hard. If you want to work with any file beyond 1MB, good luck. Not only do you really start to feel those linear time operations but if the underlying buffer needs to reallocate after some edits the entire process slows to a crawl as you need to allocate a larger buffer and copy each byte into the new buffer and destroy the old one. It can turn an O(n) operation into O(n^2) randomly. There is another, more subtle, drawback to using a giant text buffer: how do you implement undo/redo? The most obvious approach would be to have a separate data structure that tracks all of the individual insertions and deletes. Insertions are much easier to track as you can store them as a range of offsets and simply discard them from the original buffer if the undo operation is invoked. Deletions are a bit more tricky, as with the big string approach you really do need to store a small buffer containing the characters which were removed and the offset from which they were extracted. Something like this would work: #include &amp;lt;cstddef&amp;gt; #include &amp;lt;string&amp;gt; enum class CharOffset : std::size_t { }; struct InsertionData { CharOffset first; CharOffset last; }; struct DeletionData { std::string deleted_buf; CharOffset from; }; struct UndoRedoData { enum class Kind { Insertion, Deletion }; Kind kind; union { InsertionData ins; DeletionData del; }; UndoRedoData(InsertionData ins): kind{ Kind::Insertion }, ins{ ins } { } UndoRedoData(DeletionData del): kind{ Kind::Deletion }, del{ del } { } ~UndoRedoData() { switch (kind) { case Kind::Insertion: ins.~InsertionData(); break; case Kind::Deletion: del.~DeletionData(); break; } } }; but is overly complicated and requires potentially large allocation overheads when deleting large portions of text. There must be a better way&amp;#8230; Investigation I knew I wanted to solve all the problems that having a single giant text buffer had, so my concrete goals are as follows: Efficient insertion/deletion. Efficient undo/redo. Must be flexible enough to enable UTF-8 encoding. Efficient multi-cursor editing. Gap buffer I initially started going down the path of a gap buffer. The gap buffer is very simple to implement and Emacs famously uses a gap buffer as its data structure. A gap buffer would address the first point quite well for isolated edits. It&amp;#8217;s not very common that I am editing all over the file all at once, is it? Well&amp;#8230; Not quite. One of my favorite (perhaps gimmicky) features of modern text editors is multi-cursor editing (sometimes called block-style edits, though block-style editing is slightly different) and after reading &amp;#8220;Gap Buffers Are Not Optimized for Multiple Cursors&amp;#8221; (which, btw, has one of the best animations I&amp;#8217;ve ever seen to describe a gap buffer in action) Chris Wellons thoroughly convinced me that gap buffers are the wrong way to go because multi-cursor edits are important to me. Gap buffers are also plagued with similar problems as the giant string representation where efficient undo/redo is difficult without lots of extra storage/data structures. Efficient insertion/deletion. Efficient undo/redo. Must be flexible enough to enable UTF-8 encoding. Efficient multi-cursor editing. Gap buffer is out. Rope A rope can be a very attractive data structure for text editors. A rope has a lot of nice properties for editing in particular because it splits the file up into several smaller allocations which allow for very fast amortized insertions or deletions at any point in the file, O(lg n). At first glance, it would seem that a rope addresses all of my initial concerns because operations like undo/redo can more easily be implemented in terms of a tree snapshot (or retaining the removed or changed nodes) and multi-cursor editing becomes a much more lightweight operation. So where&amp;#8217;s the catch? Let&amp;#8217;s revisit undo/redo for a moment. Which represents the string &amp;#8220;HelloWorld&amp;#8221;. If I want to introduce a space between &amp;#8220;Hello&amp;#8221; and &amp;#8220;World&amp;#8221; then the new tree would look like: Where the node &amp;#8220;Hello&amp;#8221; was appended with a space at the end. The most attractive version of a rope data structure is to make the entire thing immutable, which implies that when we go to extend the node containing &amp;#8220;Hello&amp;#8221; we actually end up creating a new node with a fresh buffer to contain the new string or we create a new node just to hold the &amp;#8220; &amp;#8220;, which seems equally as wasteful (though the rope should define some kind of constant to limit the size of each node). Copying the node on write implies that, while your undo stack will contain a lightweight version of the original tree, your newly constructed tree can expand your total memory consumption much more than you might initially think. For the purposes of my editor, I want something a bit more lightweight. Efficient insertion/deletion. Efficient undo/redo. Must be flexible enough to enable UTF-8 encoding. Efficient multi-cursor editing. Piece Table Now things are getting interesting. The piece table is a data structure that has a long history with text editors. Microsoft Word used a piece table once upon a time to implement features like fast undo/redo as well as efficient file saving. The piece table appears to check many of the boxes for me. There is one, perhaps, slight drawback. Due to the fact that the traditional piece table is implemented as a contiguous array of historical edits, it implies that long edit sessions could end up in a place where adding lots of small edits to the same file will cause some of the artifacts we saw with the giant text buffer start to appear, e.g. randomly your editor may slow down because the piece table is reallocated to a larger array. Not only this, undo/redo stacks need to store this potentially large array of entries. Efficient insertion/deletion (minus very long edit sessions). Efficient undo/redo (minus very long edit sessions). Must be flexible enough to enable UTF-8 encoding. Efficient multi-cursor editing. Then, the VSCode team went and solved the problem. Enter piece tree&amp;#8230; Piece Tree I do not want to rehash everything that was covered in the original VSCode blog about their text buffer reimplementation, but I do want to cover why this particular version of a piece table is so interesting and what I needed to change to fill the gaps. Edit 6/13/2023: It was pointed out that the VSCode team were not the ones to invent the piece tree. Utilizing a tree to represent the individual pieces was suggested as far back as 1998 in Data Structures for Text Sequences. I merely found the VSCode implementation of the idea proposed in this paper to be of high quality and well-tested. The piece table that VSCode implemented is like if the traditional piece table and a rope data structure had a baby and that baby went on to outclass its parents in every conceivable way. You get all of the beauty of rope-like insertion amortization cost with the memory compression of a piece table. The piece tree achieves the fast and compressed insertion times through the use of a red-black tree (RB tree) to store the individual pieces. This data structure is useful because its guarantees about being a balanced search tree allow it to maintain a O(lg n) search time no matter how many pieces are added over time. There was just one tiny piece missing&amp;#8230; the VSCode implementation does not use immutable data structures, which means that I will need to copy the entire piece tree (not the underlying data of course) in order to capture undo/redo stacks. So it does have some of the drawback of the piece table, so let&amp;#8217;s just fix it! How hard could it be? (spoilers: it was hard). Efficient insertion/deletion. Efficient undo/redo (almost, a tree copy is involved). Must be flexible enough to enable UTF-8 encoding. Efficient multi-cursor editing. My Own Piece Tree The primary goal for implementing my own piece tree was to make it a purely functional data structure, which means the underlying view of the text should be entirely immutable and changing it would require a new partial tree. The piece tree that VSCode implemented uses a traditional RB tree. After digging out my copy of Introduction to Algorithms book again I quickly realized that trying to reimplement the RB tree described in the book as a purely functional variant was not going to be easy for two reasons: The book makes heavy use of a sentinel node which represents the NIL node but it can be mutated like a regular node, except there&amp;#8217;s only one. The algorithms in the book make heavy use of the parent pointer in each node. Parent pointers in purely functional data structures are a no go because a change anywhere in the tree essentially invalidates the entire tree. Somebody had to have implemented an immutable RB tree, right? Turns out&amp;#8230; yeah? Kind of? In my search I found Functional Data Structures in C++: Trees by Bartosz Milewski in which he covers a simple way to implement a purely functional RB tree, but only for insertion. Insertion is essential to have but I also need deletion, I have to have deletion otherwise the piece tree simply will not work. In the comments it was discussed that deletion is hard, very hard, and that often you need a new node concept to even handle it. I really liked the approach Milewski took for insertion, so I ended up starting my RB tree with this data structure and implemented the rest of the piece tree around it, minus delete but we&amp;#8217;ll get to that later. Where I Diverge Now that I&amp;#8217;m implementing the text buffer as a tree it is extremely important that I design powerful debugging tools to accommodate my journey and evaluate what parts of the implementation need to change in order to solve my specific problems. The original VSCode textbuffer code can be seen here: https://github.com/microsoft/vscode-textbuffer as a stand-alone repo. The real textbuffer implementation (which isn&amp;#8217;t really much different from the stand-alone repo) can be seen here: https://github.com/microsoft/vscode/tree/main/src/vs/editor/common/model/pieceTreeTextBuffer. CRLF My new implementation of this data structure does not stray too far from what VSCode&amp;#8217;s version does. Where I start to differ is in the decision not to codify CRLF line endings as a formal concept in the tree. I don&amp;#8217;t like the workarounds they have in the implementation to account for CRLF to be contained within a single piece and I think it massively complicates the data structure for a feature which only needs to be really accounted for in a handful of scenarios. In my existing text editor (without the new text buffer implementation) I account for CRLF in a separate data structure on the side which records line ranges. If the editor is in &amp;#8220;CRLF mode&amp;#8221; it will rebuild the line ranges in a way that chops the carriage return when it is followed by a newline character. With my new text buffer I don&amp;#8217;t record where CRLFs are at all, I simply account for them if a buffer modification happens which needs to delete both \r and \n or when moving the cursor to retract its position to before the \r if it precedes a \n. Here&amp;#8217;s an example of what the user sees when &amp;#8220;CRLF mode&amp;#8221; is active and the cursor navigates before/after a CRLF line: Here&amp;#8217;s what the system does underneath to either skip the carriage return or retract to the character before it (the &amp;#xB6; character represents the \r): The data structure does not need to record the CRLF due to the limited number of times the editor even needs to consider the carriage return character. Debugging Being able to debug a complicated data structure is essential if you intend to make any progress. If you observe the stand-alone repo for the VSCode implementation you will note that the primary way of validating the buffer contents is through getLineContent, which is a fundamental and important API. For the purposes of really confirming the entire buffer I needed a different approach so I designed a tree walker with an iterator-like interface so I can use a for-each loop: class TreeWalker { char current(); char next(); void seek(CharOffset offset); bool exhausted() const; Length remaining() const; CharOffset offset() const; // For Iterator-like behavior. TreeWalker&amp;amp; operator++(); char operator*(); ... }; struct WalkSentinel { }; inline TreeWalker begin(const Tree&amp;amp; tree) { return TreeWalker{ &amp;amp;tree }; } constexpr WalkSentinel end(const Tree&amp;amp;) { return WalkSentinel{ }; } inline bool operator==(const TreeWalker&amp;amp; walker, WalkSentinel) { return walker.exhausted(); } This walker proved to be invaluable for validating edits and deletes and opened up a lot of extra room to ensure that UTF-8 support was correct since I needed the ability to walk specific codepoints at a given offset. Naturally, the other debugging tools that follow are a way to print the tree nodes themselves as well as the associated pieces: void print_piece(const Piece&amp;amp; piece, const Tree* tree, int level); void print_tree(const Tree&amp;amp; tree); Undo/Redo The VSCode implementation did not implement undo/redo directly as a text buffer operation, they did it through a system in which edits applied to the text buffer would record the series of &amp;#8216;reverse&amp;#8217; operations needed to undo the prior edit and pass that to the caller which was then responsible for storing this information and responsible for applying reverse edits in sequence. This system is certainly one approach and avoids having to copy the entire tree again, but it relies on allocating buffers of data similarly to how we postulated undo/redo could be done with the single string implementation. Here&amp;#8217;s my undo/redo routines: UndoRedoResult Tree::try_undo(CharOffset op_offset) { if (undo_stack.empty()) return { .success = false, .op_offset = CharOffset{ } }; redo_stack.push_front({ .root = root, .op_offset = op_offset }); auto [node, undo_offset] = undo_stack.front(); root = node; undo_stack.pop_front(); compute_buffer_meta(); return { .success = true, .op_offset = undo_offset }; } UndoRedoResult Tree::try_redo(CharOffset op_offset) { if (redo_stack.empty()) return { .success = false, .op_offset = CharOffset{ } }; undo_stack.push_front({ .root = root, .op_offset = op_offset }); auto [node, redo_offset] = redo_stack.front(); root = node; redo_stack.pop_front(); compute_buffer_meta(); return { .success = true, .op_offset = redo_offset }; } Yep, that is all you need. Two linked lists and a call to compute_buffer_meta() (which is a O(lg n) operation). When the entire data structure is immutable you can snap back to any root you have previously saved a reference to and the editor state will be completely consistent&amp;mdash;which, I will mention, brings up the possibility of creating a UI for branching editor states (like a mini-source control system) where the user could navigate an editor state graph to some state they had previously, no more messy git commit -am'testing' commits to only be reverted later! Revisiting Deletion I said I would revisit delete operations because they&amp;#8217;re just as fundamental as an insertion operation when talking about text editors. When going down the path of implementing immutable RB tree deletion, I&amp;#8217;ll be honest, I nearly gave up several times. The algorithm mentioned in my algorithms book was simply not workable because it needed to be converted into a recursive version where we also somehow eliminate the sentinel nodes (which VSCode&amp;#8217;s RB tree implementation uses, but that is not necessarily a bad thing). There is an excellent RB tree algorithm visualizer provided by the University of San Francisco CS department that I strongly urge you to check out if you&amp;#8217;re interested in exploring the algorithms in action. To get some idea of the complications behind why RB tree deletion is difficult we need to peek into the cases that need to be covered by the traditional RB tree deletion. In particular the cases where you introduce a double-black violation by deleting a red node leaves a lot of room for error. Luckily, someone else thought that implementing an immutable RB tree was useful and I found persistent-rbtree which was actually adapted from the Rust Persistent Data Structures. This code had some reliable node deletion logic that I could adapt to my existing RB tree and it ended up serving as the basis for my deletion routine. You can see how it is done in the final repo below. Finally, after implementing deletion I had my &amp;#8216;perfect&amp;#8217; data structure (for me): Efficient insertion/deletion. Efficient undo/redo. Must be flexible enough to enable UTF-8 encoding. Efficient multi-cursor editing. fredbuf It would be crazy of me to get this far and have no code to show for it. &amp;#8216;fred&amp;#8217; is the name of my text editor and since I have sufficiently modified the VSCode textbuffer and adapted it specifically for &amp;#8216;fred&amp;#8217; I have dubbed it &amp;#8216;fredbuf&amp;#8217;. You can view all of the code and tests at the repo for fredbuf. It has no 3rd party library requirements so the build is as simple as you can get. It is MIT licensed, so go wild! Improve it beyond what I ever thought possible! The repro just requires a C++ compiler that supports C++20. Perhaps I&amp;#8217;ll release &amp;#8216;fred&amp;#8217; one day too. Conclusion What did I learn from this? Doing research and identifying the constraints up-front is absolutely essential. You don&amp;#8217;t want to waste time finding out that a complicated data structure won&amp;#8217;t solve your problems after you go through the pain of implementing it. Create debugging utilities for data structures very early on. I doubt I would have completed this project if I had not created powerful debugging utilities from the start. Immutable RB tree deletion is hard :sweat_smile:. If you&amp;#8217;re also interested in text editor design, the C++ language, or compilers have a chat with me over on Twitter. I&amp;#8217;m always interested in learning and sharing any knowledge that could prove to be useful to others. Until next time!</summary></entry><entry><title type="html">Mapping Strings in C++</title><link href="https://cdacamar.github.io/data%20structures/algorithms/benchmarking/mapping-strings/" rel="alternate" type="text/html" title="Mapping Strings in C++" /><published>2017-04-09T00:00:00+00:00</published><updated>2017-04-09T00:00:00+00:00</updated><id>https://cdacamar.github.io/data%20structures/algorithms/benchmarking/mapping-strings</id><content type="html" xml:base="https://cdacamar.github.io/data%20structures/algorithms/benchmarking/mapping-strings/">&lt;p&gt;Mapping strings to things makes sense.  You take a meaningful string and you assign it a value.  As with most things we do in programming
there are many pitfalls to approaching a problem.  So, lets explore some of them!&lt;/p&gt;

&lt;h2 id=&quot;what-exactly-is-your-problem&quot;&gt;What &lt;em&gt;Exactly&lt;/em&gt; Is Your Problem?&lt;/h2&gt;

&lt;p&gt;Here’s a situation we’ve all run into at some point.&lt;/p&gt;

&lt;p&gt;Say you’re reading records from a database and it has a column called “Type”.  Lets say for some reason the designer wanted things to be
a little bit human readable so this “Type” column is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;varchar(30)&lt;/code&gt;.  When you read this string in you need to parse it into some internal
type within your application.&lt;/p&gt;

&lt;p&gt;What method do you use?&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&amp;lt;std::string&amp;gt;&lt;/code&gt; and use the index as your enum type?&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&amp;lt;std::string, enum_t&amp;gt;&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&amp;lt;std::string, enum_t&amp;gt;&lt;/code&gt;?&lt;/p&gt;

&lt;h2 id=&quot;hey-stdvector-is-fast&quot;&gt;Hey, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&lt;/code&gt; is fast!&lt;/h2&gt;

&lt;p&gt;And indeed you would be right!  Of all the STL containers &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&lt;/code&gt; is by far the most used, tested, and fully understood container
to ever grace C++.  What it &lt;em&gt;isn’t&lt;/em&gt; is a Swiss Army Knife.  But lets use it like one anyway!&lt;/p&gt;

&lt;p&gt;Consider this:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;types_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;triangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;num_types&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// initialize size == num_types&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_types&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;circle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;square&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;triangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;triangle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this you can now lookup your internal type like so:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// returns type_t::num_types if failed&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;distance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I understand this is an extremely naïve implementation but the concept is there.  We use strings in order to obtain a handle to some
internal type.&lt;/p&gt;

&lt;p&gt;Don’t worry we’ll get to benchmarks and how we can make this particular implementation faster.&lt;/p&gt;

&lt;h2 id=&quot;but-why-not-use-stdmap&quot;&gt;But Why Not Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt;?&lt;/h2&gt;

&lt;p&gt;Good question.  Let’s use one:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// using same 'types_t'&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;circle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;circle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;square&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;triangle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;types_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;triangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Well that was easier.  What does the request to get the type look like?&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_types&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;OK so that looks a little easier to read and if we have a ton of types we won’t exactly do a linear search due to the way &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt;
stores its entries (using RB trees).&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt; follows the same rules as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt; the only difference is that when you want to request all of the types from it
the resulting list is unsorted.  This behavior does have some performance implications and tells us a little bit about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt;
stores its elements (probably using buckets and separate-chaining to handle collisions).&lt;/p&gt;

&lt;h2 id=&quot;lets-see-some-numbers&quot;&gt;Lets See Some Numbers!&lt;/h2&gt;

&lt;p&gt;Right, so you’re probably tired of hearing me ramble about silly implementations of different things string storing techniques.  Lets compare
some.&lt;/p&gt;

&lt;p&gt;For this test I generated random strings all between the lengths of 10 and 100 so we can observe strings outside of SSO (Small String Optimization)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/vec-map-unmap-chart.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So… That’s less than helpful.  At around 1,000,000 elements our unsorted vector takes around 32 minutes to lookup a string.&lt;/p&gt;

&lt;p&gt;Let’s fix this.  An easy way to get our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&lt;/code&gt; implementation in-line with the rest of the containers is we can sort it and utilize an algorithm,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::lower_bound&lt;/code&gt;, in order to speed up our lookup times.&lt;/p&gt;

&lt;p&gt;Lets see how that change affects the benchmark:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/svec-map-unmap-chart.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;These are much better numbers.  We can actually see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::vector&lt;/code&gt; basically even with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt; while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt; starts beating out both.&lt;/p&gt;

&lt;h2 id=&quot;there-might-be-a-better-way&quot;&gt;There &lt;em&gt;Might&lt;/em&gt; Be A Better Way…&lt;/h2&gt;

&lt;p&gt;If you think about the problem we’re actually solving here you could actually relate it to another common problem of pattern matching in search engines.&lt;/p&gt;

&lt;p&gt;In our problem we have a finite set of things that a string could match to.  With that in mind we can short cut a lot of the matching process if we
manage to find a string with a certain prefix.&lt;/p&gt;

&lt;p&gt;For example, in the list of strings &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[&quot;cat&quot;, &quot;cake&quot;, &quot;bat&quot;]&lt;/code&gt; if we have the prefix &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ca&quot;&lt;/code&gt; then we have two potential matches, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;cat&quot;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;cake&quot;&lt;/code&gt;,
however if we have a prefix of just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;b&quot;&lt;/code&gt; then we don’t even have to compare the rest of the string to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;bat&quot;&lt;/code&gt; to have a full match, we can just take &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;bat&quot;&lt;/code&gt;.
This, of course, is all under the assumption you can short circuit like that in your string match.  It’s possible you have malformed type strings.  Keep this
in mind when considering the following solution!&lt;/p&gt;

&lt;p&gt;Luckily there is a data structure that will do just this type of prefix matching. A &lt;em&gt;Trie&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-trie&quot;&gt;The Trie&lt;/h2&gt;

&lt;p&gt;Many may know what a Trie is but for those who don’t it is a tree based structure that is optimized for matching string prefixes to words inserted into
the tree.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/trie.png&quot; alt=&quot;&quot; class=&quot;align-center&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is the resulting structure after we insert the words &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ask&quot;, &quot;as&quot;, &quot;bake&quot;, &quot;bat&quot;, &quot;cat&quot;&lt;/code&gt; and, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;to&quot;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;An extremely simple implementation of this structure uses a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt; to place a char leading to another node in the tree.  Nodes can then be annotated
with whether or not they’re a word (since individual branches can also be words in the case of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;as&quot;&lt;/code&gt; above).&lt;/p&gt;

&lt;p&gt;Lets see how this implementation might compare to our existing benchmarks:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/impl1-lookup-chart.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;OK, so with the naïve implementation we don’t even beat &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt;.  This is unsurprising because it uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::map&lt;/code&gt; under the covers to maintain the
Trie invariant of being sorted.  We want to maintain that the Trie is sorted so we can return sorted lists
of words, so we won’t bother using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt; to implement the node behavior.&lt;/p&gt;

&lt;h2 id=&quot;improving-trie&quot;&gt;Improving Trie&lt;/h2&gt;

&lt;p&gt;All this said, there is a lot of room for improvement.  Mainly in the way we store words that are leaves.  If you’ll notice, whenever we created the
subtree for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;bake&quot;&lt;/code&gt; we added an extra branch node between where the leaf and the end of the word.  This problem is exacerbated when we have very long
words with no common prefixes with other words in the tree.&lt;/p&gt;

&lt;p&gt;In essence the tree has two different types of nodes: leaf nodes and branch nodes.  Leaf nodes are those that only contain the full words and branch
nodes have child nodes that are either leaves or more branches.  Here, I chose inheritance to do the trick for me:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;node_concept_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;branch_node_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_concept_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;leaf_node_t&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_concept_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To determine if I was at a leaf or branch I used the visitor pattern to tell me the information I needed.&lt;/p&gt;

&lt;p&gt;Lets see how this implementation stacks up:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/impl2-lookup-chart.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is looking promising!  This implementation even beats &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt; in terms of lookups.  The reason we start to edge out &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt; is
because this structure is allowed to shortcut longer string comparisons by binary searching and has a decent memory layout when inspecting leaf data.&lt;/p&gt;

&lt;h2 id=&quot;so-i-kinda-lied&quot;&gt;So I Kinda Lied…&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impl2&lt;/code&gt; doesn’t quite have what we need just yet.  It can’t actually store values.  It only looks up strings.  So the final implementation, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impl3&lt;/code&gt;, will store
values and has yet another neat feature.&lt;/p&gt;

&lt;p&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impl2&lt;/code&gt; branch nodes were annotated via a boolean variable to indicate whether or not it was word.  Just like in the naïve implementation, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impl1&lt;/code&gt;.  In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impl3&lt;/code&gt;,
however, we take a different approach.  We distinguish branches that carry values with a type of their own:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;node_concept_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;branch_node_t&lt;/span&gt;       &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_concept_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tmplate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;branch_value_node_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_concept_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;leaf_node_t&lt;/span&gt;         &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_concept_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice we also templated each type in order to store the values in leaf and value branches.  How does &lt;em&gt;this&lt;/em&gt; implementation fare?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/impl3-lookup-chart.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Good!  We’re still beating out the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt; implementation even while storing values!&lt;/p&gt;

&lt;h2 id=&quot;bringing-it-all-together&quot;&gt;Bringing It All Together&lt;/h2&gt;

&lt;p&gt;Now that we have all the information, lets see how it &lt;em&gt;all&lt;/em&gt; looks:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/all-lookup-chart.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s not enough to just benchmark lookup times, insert times are a concern too.  Here’s a benchmark of inserting various numbers of elements using the same parameters
(words between 10 and 100 characters in length):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/insert-times-chart.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here it’s pretty expected that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::unordered_map&lt;/code&gt; beats &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impl{1,2,3}&lt;/code&gt; since its insert time only occasionally rehashes the whole data set.  Generally the hash is linear
in complexity, making the total insert time &lt;em&gt;O(n + k)&lt;/em&gt; where &lt;em&gt;n&lt;/em&gt; is the string length and &lt;em&gt;k&lt;/em&gt; is (potentially your chain size).  In our Trie we need to do a prefix match
and potentially breakup a leaf node into multiple branches and up to two other leaf nodes.  This makes our insert time on the order of &lt;em&gt;O(n lg n)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I encourage you to check out the &lt;a href=&quot;https://github.com/cdacamar/trie&quot;&gt;code&lt;/a&gt; where the benchmark code can be found along with the Trie implementations.&lt;/p&gt;

&lt;p&gt;A quick shout out to &lt;a href=&quot;https://github.com/zieckey/gochart&quot;&gt;gochart&lt;/a&gt; for providing the awesome chart making utility and &lt;a href=&quot;http://www.draw.io&quot;&gt;draw.io&lt;/a&gt; for the Trie visualization.&lt;/p&gt;

&lt;p&gt;Until next time! &lt;i class=&quot;fa fa-thumbs-up&quot;&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>Cameron DaCamara</name></author><category term="Data Structures" /><category term="Algorithms" /><category term="Benchmarking" /><category term="STL" /><category term="pretty code" /><category term="trie" /><category term="data structures" /><summary type="html"></summary></entry><entry><title type="html">Localizing Code With std::variant</title><link href="https://cdacamar.github.io/organization/localizing-code-with-variant/" rel="alternate" type="text/html" title="Localizing Code With std::variant" /><published>2016-09-08T00:00:00+00:00</published><updated>2016-09-08T00:00:00+00:00</updated><id>https://cdacamar.github.io/organization/localizing-code-with-variant</id><content type="html" xml:base="https://cdacamar.github.io/organization/localizing-code-with-variant/">&lt;p&gt;As we all know, C++17 is on the horizon.  With it&amp;#8217;s release will comes a new member to the data container family, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;looking-at-stdvariant&quot;&gt;Looking at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Let&amp;#8217;s take a look at it&amp;#8217;s definition:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Types&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Variant types are useful for all types of applications, particularly when you want to flatten hierarchies into simpler, more independent types.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s see a simple use case for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;variant&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_literals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// use std::string side&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// use int side&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Meaning of life: &quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As contrived as this example may be it shows how flexable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt; can be.  It allows us to bind several types together that can have semantic meaning within our application without the types poisoning each other with interface requirements and let&amp;#8217;s us treat the base class, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt; in this case, as a value type.&lt;/p&gt;

&lt;h2 id=&quot;working-with-variants&quot;&gt;Working with variants&lt;/h2&gt;

&lt;p&gt;Along with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt; comes a very welcomed utility meant for generically handling types within a variant, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::visit&lt;/code&gt; defined as follows:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Visitor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Variants&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;constexpr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;common_type&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Visitor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vis&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Variants&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vars&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are a couple of things to notice about &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::visit&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We accept a single visitor for multiple variant types and,&lt;/li&gt;
  &lt;li&gt;variants don&amp;#8217;t need to contain homogeneous types across them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because of these properties our visitor provides a good solution to the &lt;a href=&quot;https://en.wikipedia.org/wiki/Multiple_dispatch&quot;&gt;multi-method problem&lt;/a&gt;, pre-canned in the STL!&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s see a simple use case for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::visit&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;variant&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_variant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;print_visitor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print_visitor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_literals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// use std::string side&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print_variant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// use int side&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print_variant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As great as this is, do you see a problem?  The problem that I see is that, while a visitor object &lt;em&gt;can&lt;/em&gt; be locally declared, if we want to handle generic cases we have two options.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Either collapse all of the visit cases into a single generic lambda:&lt;/p&gt;

    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Declare your visitor outside of your function with a templated member function to handle the generic cases (due to &amp;sect;&lt;em&gt;14.5.2.2&lt;/em&gt; in the standard).&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Luckily, there is a very easy fix for this, lambda composition.&lt;/p&gt;

&lt;h2 id=&quot;localizing-with-stdvariant&quot;&gt;Localizing with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;What are the goals of variants?  One could argue some goals behind variants are to:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Flatten hierarchies&lt;/li&gt;
  &lt;li&gt;Mix value semantics with the complexity of interface based types (ie. have a generic interface that multiple types &lt;em&gt;may&lt;/em&gt; use)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Localize behaviours / algorithms&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This last aspect is easy to solve with a small library that is meant to compose lambda objects into a single, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator()&lt;/code&gt; overloaded object.  Let&amp;#8217;s see what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print_variant&lt;/code&gt; looks like after this transformation:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;
#include &amp;lt;variant&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;lambda_util&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_variant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lambda_util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;'\n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;TODO&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string_literals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// use std::string side&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print_variant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// use int side&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;print_variant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this transformation the reader has all of the necessary code for the behaviour in the function itself.  Maintainers will truly appreciate a compact function like this.&lt;/p&gt;

&lt;p&gt;Just to show this composer doesn&amp;#8217;t do any extra work outside of template metaprogramming, let&amp;#8217;s see one more example:&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;variant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;10.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;lambda_util&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;[](&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}),&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The compiler I was using was &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GCC 7 (snapshot)&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-O2 -std=c++1z&lt;/code&gt;.  Here is its output:&lt;/p&gt;

&lt;div class=&quot;language-nasm highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nl&quot;&gt;main:&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;xorl&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This anecdote speaks to 3 things, the amout of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::variant&lt;/code&gt; that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constexpr&lt;/code&gt; compatible, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lambda_util::compose&lt;/code&gt; lightweight functionality, and the fine folks on the GNU GCC team and the excellent work they do with their optimizer &lt;i class=&quot;fa fa-smile-o&quot;&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;For those who want to take a good look at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lambda_util::compose&lt;/code&gt;&amp;#8217;s implementation, see &lt;a href=&quot;https://gist.github.com/cdacamar/584c6d43a9cca1ccffec3b36ad5dfe3f&quot;&gt;here!&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Until next time! &lt;i class=&quot;fa fa-thumbs-up&quot;&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>Cameron DaCamara</name></author><category term="Organization" /><category term="STL" /><category term="pretty code" /><category term="variant" /><summary type="html"></summary></entry><entry><title type="html">Breathing Life: What to Expect Here</title><link href="https://cdacamar.github.io/general/breathing-life/" rel="alternate" type="text/html" title="Breathing Life: What to Expect Here" /><published>2016-07-14T00:00:00+00:00</published><updated>2016-07-14T00:00:00+00:00</updated><id>https://cdacamar.github.io/general/breathing-life</id><content type="html" xml:base="https://cdacamar.github.io/general/breathing-life/">&lt;p&gt;So what can you expect here? In a word, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;code&lt;/code&gt;. Lots of code! Why? A couple of reasons:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;I am a very visual person, I learned (and continue to learn) through example. When I see results I want to
know how we got there. Understanding results is a small part of a long and convoluted battle in converting
and handling data. My personal interest lies in that conversion.&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Most of us may know of an interesting quote,&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;“Show me your flowchart and conceal your tables, and I shall continue to be mystified. Show me your tables,
and I won’t usually need your flowchart; it’ll be obvious.”
– &lt;strong&gt;Fred Brooks, The Mythical Man-Month&lt;/strong&gt;&lt;/p&gt;
    &lt;/blockquote&gt;

    &lt;p&gt;A very similar quote was derived from this,&lt;/p&gt;

    &lt;blockquote&gt;
      &lt;p&gt;“Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data
structures, and I won’t usually need your code; it’ll be obvious.”&lt;/p&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;strong&gt;Eric S. Raymond, The Cathedral and The Bazaar&lt;/strong&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/blockquote&gt;

    &lt;p&gt;While I don’t necessarily disagree with latter quote, I don’t identify with it. Code, to me, is important.
 What’s more, good code is essential for the longevity of any project/product.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There’s one more aspect of the type of code I, personally, enjoy. I like using code that has a good and intuitive interface.
If a library, for example, has a good interface you almost don’t find yourself going to the documentation to discover
it’s secrets. The C++ standard library is a good example of code that is very self documenting. Most Windows API
calls are an example a library that isn’t very self documenting. Just for kicks, let’s quickly take a look:&lt;/p&gt;

&lt;p&gt;Example of the Windows Thread API:
&lt;script src=&quot;https://gist.github.com/cdacamar/6c4eb9fdbdb6ecf3d62d56ec6ccccc8e.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And here’s how to use it:
&lt;script src=&quot;https://gist.github.com/cdacamar/e0afb184ea68404c8b3437406464e033.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;So… this is… not pretty. In my personal opinion. Perhaps it’s not fair for me to pick on Win API because, after all,
it’s old. Really old. In fact &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CreateThread&lt;/code&gt; is still supported as far back as Windows XP. That makes this API at least &lt;strong&gt;15&lt;/strong&gt;
years old. Be that as it may, libraries like this exist, and are still being developed. C++ &lt;em&gt;can&lt;/em&gt; do better!&lt;/p&gt;

&lt;p&gt;So let’s do better. Here’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::thread&lt;/code&gt;:
&lt;script src=&quot;https://gist.github.com/cdacamar/2ef0285ae8aaa6a0b6be7185d87ffeb1.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;And here’s how to use it:
&lt;script src=&quot;https://gist.github.com/cdacamar/95ee0b0f300e2eb906817554f619922a.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s obvious to see why it might be preferable to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std::thread&lt;/code&gt; over the direct &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CreateThread&lt;/code&gt;. Abstracting complexity from the
developer allows them to focus their efforts on a projects functionality over ramping up to the level of understanding required to
use direct Win API calls.&lt;/p&gt;

&lt;p&gt;This is what I like to do. Abstract. Efficiently. And sometimes make some fun things along the way. &lt;i class=&quot;fa fa-thumbs-up&quot;&gt;&lt;/i&gt;&lt;/p&gt;</content><author><name>Cameron DaCamara</name></author><category term="General" /><category term="winapi" /><category term="STL" /><category term="pretty code" /><summary type="html"></summary></entry></feed>