<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mark Nelson &#187; Programming</title>
	<atom:link href="http://marknelson.us/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://marknelson.us</link>
	<description>Programming, mostly.</description>
	<lastBuildDate>Fri, 13 Apr 2012 19:25:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Visual Studio 11 and Modern C++</title>
		<link>http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/</link>
		<comments>http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 13:13:08 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Standards]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1467</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/' addthis:title='Visual Studio 11 and Modern C++' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Despite some harsh words about Visual Studio 11, I&#8217;m finding that it makes my heart go pitter-pat every time I use it. Why? Because this early release is finally incorporating a decent set of long-awaited C++11 features. In this article I&#8217;ll show you how a little thing like a lambda can make a big difference [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/' addthis:title='Visual Studio 11 and Modern C++' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>Despite some <a href="http://drdobbs.com/windows/232602205" class="newpage">harsh words</a> about Visual Studio 11, I&#8217;m finding that it makes my heart go pitter-pat every time I use it. Why?  Because this early release is finally incorporating a decent set of long-awaited C++11 features. In this article I&#8217;ll show you how a little thing like a lambda can make a big difference in your coding style.<br />
<span id="more-1467"></span></p>
<h4>Microsoft and C++ &#8211; We Have History</h4>
<p>Microsoft has a cyclic relationship with C++. In the early MFC days, the love was there big time &#8211; you had access to most of the system API using C++. However, around the turn of the millennium, Microsoft came under the Rasputin-like influence of Anders Hejlsberg and his beloved offspring, C#. Now it appears that maybe the pendulum is swinging back a bit, and C++ is no longer viewed as an afterthought. Great news.</p>
<p>Although Visual Studio 11 is a developer&#8217;s preview, Microsoft is <a href="http://herbsutter.com/2012/02/29/vc11-beta-on-feb-29/" class="newpage">saying</a> that it is production ready &#8211; you can use this to create programs that are ready for release. In addition to touting a complete implementation of the C++11 standard library, an <a href="http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx" class="newpage">impressive list</a> of language features have been turned on as well. (N.B. the path ahead is still long and arduous.)</p>
<h4>Modern C++</h4>
<p>Before even using Visual Studio C++ 11 to test a single line of code, I really appreciated reading <a href="http://msdn.microsoft.com/en-us/library/hh279654(v=vs.110).aspx" class="newpage">Welcome Back to C++ (Modern C++)</a>, a manifesto that includes the following bullet points:</p>
<blockquote><p>
Modern C++ emphasizes:</p>
<ul>
<li>Stack-based scope instead of heap or static global scope.</li>
<li>Auto type inference instead of explicit type names.</li>
<li>Smart pointers instead of raw pointers.</li>
<li>std::string and std::wstring types instead of raw char[] arrays.</li>
<li>Standard template library (STL) containers—for example, vector, list, and map—instead of raw arrays or custom containers.</li>
<li>STL algorithms instead of manually coded ones.</li>
<li>Exceptions, to report and handle error conditions.</li>
<li>Inline lambda functions instead of small functions implemented separately.</li>
</blockquote>
<p>I feel that all of these changes result in safer code that is easier to read and maintain, without giving up the type-safety and efficiency that we love so much. Fully implementing these features either leans heavily on C++11 or requires it outright.</p>
<h4>A Simple Example Using Naive C++98</h4>
<p>It&#8217;s interesting to watch the evolution of code from C++ 98 to C++11 and see how it affects your code. You&#8217;ll see that the transformation can make it look like you are literally using a new programming language.</p>
<p>In this simple program, I&#8217;m taking a Scrabble rack of tiles and whipping through the Scrabble dictionary to find matches. Since it is a one-time call, I&#8217;m not storing the words, just doing a quick online comparison. In C++ 98, my code might have looked like this:</p>
<pre>
void find_matches( std::string rack, const std::string &amp;filename )
{
    std::sort( rack.begin(), rack.end() );
    std::ifstream sowpods( filename.c_str() );
    std::string word;
    while ( sowpods &gt;&gt; word ) {
        std::string sorted = word;
        std::sort( sorted.begin(), sorted.end() );
        if ( sorted == rack )
            std::cout &lt;&lt; word &lt;&lt; &quot; &quot;;
    }
}

int main(int argc, char* argv[])
{
    find_matches( &quot;etaionsr&quot;, &quot;sowpods.txt&quot; );
    return 0;
}
</pre>
<p>This works properly and I get what looks like correct output:</p>
<pre>
anoestri arsonite notaries notarise rosinate senorita
</pre>
<h4>Classes Good, Templates Better</h4>
<p>As people started to get more comfortable with templates and iterators, algorithms like this were commonly rewritten to take an range of iterators as input &#8211; much as the standard library algorithm functions do. This meant changing the function to a template function, but it did make it a lot more flexible. I could now call the function to operate on data from a file, just as before, but I can also now use any other container, or even an array as input:</p>
<pre>
template&lt;typename ITERATOR&gt;
void find_matches( std::string rack, ITERATOR ii, ITERATOR jj )
{
    std::sort( rack.begin(), rack.end() );
    for ( ; ii != jj ; ii++ ) {
        std::string sorted = *ii;
        std::sort( sorted.begin(), sorted.end() );
        if ( sorted == rack )
            std::cout &lt;&lt; *ii &lt;&lt; &quot; &quot;;
    }
}

int main(int argc, char* argv[])
{
    std::ifstream sowpods( &quot;sowpods.txt&quot; );
    find_matches( &quot;etaionsr&quot;,
                  std::istream_iterator&lt;std::string&gt;( sowpods ),
                  std::istream_iterator&lt;std::string&gt;() );
    return 0;
}
</pre>
<p>More or less the same number of lines of code, but it is now generic.</p>
<p>Of course, just like with OOP, you need to take some care with template programming. Generic programming can&#8217;t be beat when it makes sense, but programmers have a particularly strong susceptibility to <a href="http://en.wikipedia.org/wiki/Pro-innovation_bias" class="newpage">pro-innovation bias</a>. </p>
<h4>Using the Algorithms Library</h4>
<p>Again, prodded by changing styles in the C++ world, my next step is to use a standard library algorithm to do the work. We&#8217;re told over and over that turning to the algorithms library allows you to use code that has been optimized to the n-th degree by the clever library teams. </p>
<p>In order to make this work, I have to call an algorithm with a predicate functor, seen below as class <code>sorted_not_equal</code>. Note also that I can&#8217;t use the logical function for this, which would be <code>copy_if()</code>. Why not? The committee forgot to put it in back in 1998, 2003, and 2005, a mistake that was fortunately remedied in C++11. So I have to use the inverse function, <code>remove_copy_if()</code>, and invert the logical sense of my functor:</p>
<pre>
class sorted_not_equal {
    std::string str;
public :
    sorted_not_equal( const std::string &amp; test )
    {
        str = test;
        sort( str.begin(), str.end() );
    }
    bool operator()( std::string test )
    {
        sort( test.begin(), test.end() );
        return ( str != test );
    }
};

template&lt;typename INPUT_ITERATOR, typename OUTPUT_ITERATOR&gt;
void find_matches( std::string rack,
                   INPUT_ITERATOR ii,
                   INPUT_ITERATOR jj,
                   OUTPUT_ITERATOR kk )
{
    std::sort( rack.begin(), rack.end() );
    std::remove_copy_if( ii, jj, kk, sorted_not_equal( rack ) );
}

int main(int argc, char* argv[])
{
    std::ifstream sowpods( &quot;sowpods.txt&quot; );
    find_matches( &quot;etaionsr&quot;,
                  std::istream_iterator&lt;std::string&gt;( sowpods ),
                  std::istream_iterator&lt;std::string&gt;(),
                  std::ostream_iterator&lt;std::string&gt;(std::cout,  &quot;\n&quot; ) );
    return 0;
}
</pre>
<h4>Functors Not So Hot</h4>
<p>So this new approach is supposed to soup up my code by taking advantage of the algorithms that come with the standard library. But if you look around at the code people have been writing for the past 10 years, you&#8217;ll find that this style is pretty common in textbooks and magazine articles, but no so much in the real world.</p>
<p>Why not? Well, it&#8217;s pretty obvious. The generic algorithms in the library need lots of predicate glue to make them useful, and the work to create these predicates is just a pain. My code is almost twice as long, and the functionality that took two lines of code earlier is now bloated into a complete class definition. It pollutes my namespace, takes up a lot of space, and has to be defined somewhere distant from where it is actually used. Not a win.</p>
<p>This is obviously a problem when you look at the history of Linux, C, and C++. An entire family of technologies and infrastructure was developed with the implicit goal of reducing the number of keystrokes programmers had to enter. (I&#8217;m kidding, but only somewhat.) Functors are a step in the wrong direction.</p>
<h4>Lambda to the Rescue</h4>
<p>So it is with much relief that C++11 delivers lambdas, which allow us to write short sweet predicates exactly where we need them, as shown in this C++11 version of the example:</p>
<pre>
template&lt;typename INPUT_ITERATOR, typename OUTPUT_ITERATOR&gt;
void find_matches( std::string rack,
                   INPUT_ITERATOR ii,
                   INPUT_ITERATOR jj,
                   OUTPUT_ITERATOR kk )
{
    std::sort( rack.begin(), rack.end() );
    std::copy_if( ii, jj, kk,
                  [&amp;rack](std::string str) -&gt;bool
                  {
                      std::sort( str.begin(), str.end() );
                      return rack == str;
                  }
                );
}

int main(int argc, char* argv[])
{
    std::ifstream sowpods( &quot;sowpods.txt&quot; );
    find_matches( &quot;etaionsr&quot;,
                  std::istream_iterator&lt;std::string&gt;( sowpods ),
                  std::istream_iterator&lt;std::string&gt;(),
                  std::ostream_iterator&lt;std::string&gt;(std::cout,  &quot;\n&quot; ) );
	return 0;
}
</pre>
<p>Yes, I now have to get used to a new syntax for writing lambda functions &#8211; I think that was unavoidable. But my lambda function is short, it is quite easy to see exactly what it is doing, and it replaces a gangly and awkward functor class. </p>
<p>Best of all, I use the lambda exactly where I need it &#8211; as the predicate parameter to an algorithm used in the standard library. Locality rules.</p>
<h4>Using Lambdas</h4>
<p>Visual C++ 11 provides a great framework for experimenting with lambdas, as they are supporting the 1.1 definition that was ratified as part of the standard. If you want the gory details, I believe the <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2927.pdf" class="newpage">working group&#8217;s</a> proposal has essentially the same wording that went into the standard. For a detailed tutorial, <a href="http://herbsutter.com/2011/05/20/my-lambdas-talk-nwcpp-is-now-online/" class="newpage">Herb Sutter&#8217;s talk</a> can&#8217;t be beat.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2012/03/13/visual-studio-11-and-modern-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Streams or Iterators?</title>
		<link>http://marknelson.us/2011/12/24/streams-or-iterators/</link>
		<comments>http://marknelson.us/2011/12/24/streams-or-iterators/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 18:21:11 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Data Compression]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1393</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/12/24/streams-or-iterators/' addthis:title='Streams or Iterators?' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>When I updated my LZW reference code to use the latest C++ features, I abstracted my input and output functions using templates. Data was read and written using the iostreams paradigm, which requires simple classes that implement just a few functions. Would I have been better off using the iterator paradigm instead? The C++ algorithms [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/12/24/streams-or-iterators/' addthis:title='Streams or Iterators?' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>When I updated my <a href="http://marknelson.us/2011/11/08/lzw-revisited/" class="newpage">LZW</a> reference code to use the latest C++ features, I abstracted my input and output functions using templates. Data was read and written using the iostreams paradigm, which requires simple classes that implement just a few functions. Would I have been better off using the iterator paradigm instead? The C++ algorithms library favors that method of processing data, and it can be both elegant and powerful. Which of the two paradigms is the right one for data compression?<br />
<span id="more-1393"></span></p>
<h4>The Conflict</h4>
<p>General purpose data compression routines tend to be used on binary streams of data, either from files or in-memory objects. So what is the best general paradigm for input and output when compressing data? </p>
<p>You might analyze this problem by imagining that you need to write a binary copy routine. </p>
<pre>
template&lt;class INPUT_ITERATOR, class OUTPUT_ITERATOR&gt;
void bcopy( INPUT_ITERATOR input, INPUT_ITERATOR eof, OUTPUT_ITERATOR output )
{
    while ( input != eof )
        *output++ = *input++;
}
</pre>
<p>This routine is particularly nice when you are performing a simple copy using pointers to memory &#8211; the generated code should be really efficient.</p>
<p>However, the iterator paradigm doesn&#8217;t work quite as well when you want to perform a binary copy of data in a file. I can make use of iterators that almost do the job:</p>
<pre>
 std::ifstream in( &quot;input.txt&quot;, std::ios_base::binary );
 std::ofstream out(&quot;output.txt&quot;, std::ios_base::binary );
 bcopy( std::istream_iterator(in),
        std::istream_iterator(),
	std::ostream_iterator(out) );
</pre>
<p>But the bad news is that both <code>istream_iterator</code> and <code>ostream_iterator</code> use the insertion and extraction operators, which are really meant for whitespace-delimited textual data, not binary data. The copy routine shown here will not make a binary byte-for-byte copy of the input file.</p>
<p>So when using files, the stream approach seems to be the way to go:</p>
<pre>
template&lt;class INPUT_STREAM, class OUTPUT_STREAM&gt;
void bcopy( INPUT_STREAM in, OUTPUT_STREAM out )
{
    char c;
    while ( in.get(c) )
        out.put(c);
}
</pre>
<p>If my files have been opened using the <code>iostream</code> classes, you can use this binary copy function without having to write any glue code &#8211; they already support the <code>get</code> and <code>put</code> methods, so this works right out of the box.</p>
<h4>My Choice</h4>
<p>If I&#8217;ve made up my mind that my data compression routine is going to use one of these two paradigms, it means I am going to have to write some glue code. If I choose the iterator-based approach, I need the equivalent of <code>istream_iterator</code> and <code>ostream_iterator</code> for binary files &#8211; and these aren&#8217;t in the standard library. If I choose the stream-based approach, I need efficient <code>put()</code> and <code>get()</code> members for blocks of memory. In some cases <code>basic_stringstream</code> might do the job, but not in all cases.</p>
<p>After dithering around with various solutions, I tentatively opted for the stream paradigm. I found the implementation for various sources of data to be fairly simple, and the interface is easy to understand. I don&#8217;t know if it&#8217;s the perfect choice, and I&#8217;ll keep experimenting, but for now it works for me. My abstraction of the LZW code still needs a lot of work, so it&#8217;s always possible I could rethink this at a later date.</p>
<p>I&#8217;d like to hear your thoughts &#8211; is there an obvious right answer to this question?</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/12/24/streams-or-iterators/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>VC++ 10 Hash Table Performance Problems</title>
		<link>http://marknelson.us/2011/11/28/vc-10-hash-table-performance-problems/</link>
		<comments>http://marknelson.us/2011/11/28/vc-10-hash-table-performance-problems/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 14:05:45 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Complaining]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=1347</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/11/28/vc-10-hash-table-performance-problems/' addthis:title='VC++ 10 Hash Table Performance Problems' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Microsoft's implementation of <code>unordered_map</code> in Visual Studio 10 has performance issues so severe it may be unusable in your projects.]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/11/28/vc-10-hash-table-performance-problems/' addthis:title='VC++ 10 Hash Table Performance Problems' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>Microsoft has never been a slacker in the C++ department &#8211; they&#8217;ve always worked hard to provide a top-notch, compliant product. Visual Studio 10 supports their current incarnation, and for the most part it is up to their usual standards. It&#8217;s a great development environment, and I am a dedicated user, but I have to give Microsoft a demerit in one area: their C++11 hash containers have some serious performance problems &#8211; so much that the Debug versions of the containers may well be unusable in your application.<br />
<span id="more-1347"></span></p>
<h4>Background</h4>
<p>I first noticed the problem with <code>unordered_map</code> when I was working on the the code for my updated <a href="http://marknelson.us/2011/11/08/lzw-revisited/" class="newpage">LZW article</a>. I found that when running in the debugger, my program would hang after exiting the compression routine. A little debugging showed that the destructor for my hash table was taking a long time to run. And by a long time, I mean it was approaching an <i>hour</i>!.</p>
<p>This seemed pretty crazy. Destroying a hash table wouldn&#8217;t seem to be a complicated task. I decided to see if I could come up with a reasonable benchmark. I wrote a test program that does a simple word frequency count. As a starter data set, I used the first one million white space delimited words in the 2010 CIA factbook, as published by <a href="http://www.gutenberg.org/ebooks/35830.txt.utf8" class="newpage">Project Gutenberg</a>. This data set yields 74,208 unique tokens.</p>
<p>I wrote a simple test rig that I used to test the word count program using four different containers:</p>
<ul>
<li/><code>unordered_map</code> indexed by <code>std::string</code>
<li/><code>unordered_map</code> indexed by <code>std::string *</code>
<li/><code>map</code> indexed by <code>std::string</code>
<li/><code>map</code> indexed by <code>std::string *</code>
</ul>
<p>The reason for testing with <code>std::string *</code> was to reduce the cost of copying strings into the hash table as it was filled, and then to reduce the cost of destroying those strings when the table was destroyed.</p>
<p>I ran tests against <code>map</code> expecting to see a pretty big difference in performance. Because <code>map</code> is normally implemented using a balanced binary tree structure, it has O(log(N)) performance on insertions. A sparsely populated hash table can have O(1) performance. By using fairly large data sets, I expected to see a big difference between the two.</p>
<p>I tried to eliminate a few obvious sources for error in my test function &#8211; and I used a template function so that I could use the same code on all the different container types:</p>
<pre>
template&lt;class CONTAINER, class DATA&gt;
void test( const DATA &amp;data, const char *test_name )
{
  std::cout &lt;&lt; &quot;Testing container: &quot; &lt;&lt; test_name &lt;&lt; std::endl;

#ifdef _DEBUG
  const int passes = 2;
#else
  const int passes = 10;
#endif
  double fill_times = 0;
  double delete_times = 0;
  size_t entries;
  for ( int i = 0 ; i &lt; passes ; i++ ) {
    CONTAINER *container = new CONTAINER();
    std::cout &lt;&lt; &quot;Filling... &quot; &lt;&lt; std::flush;
    clock_t t0 = clock();
    for ( auto ii = data.begin() ; ii != data.end() ; ii++ )
      (*container)[*ii]++;
    double span = double(clock() - t0)/CLOCKS_PER_SEC;
    fill_times += span;
    entries = container-&gt;size();
    std::cout &lt;&lt; &quot; &quot; &lt;&lt; span &lt;&lt; &quot; Deleting... &quot; &lt;&lt; std::flush;
    t0 = clock();
    delete container;
    span = double(clock() - t0)/CLOCKS_PER_SEC;
    delete_times += span;
    std::cout &lt;&lt; span &lt;&lt; &quot; &quot; &lt;&lt; std::endl;
  }
  std::cout &lt;&lt; &quot;Entries: &quot; &lt;&lt; entries
            &lt;&lt; &quot;, Fill time: &quot; &lt;&lt; (fill_times/passes)
            &lt;&lt; &quot;, Delete time: &quot; &lt;&lt; (delete_times/passes)
            &lt;&lt; std::endl;
}
</pre>
<p>I didn&#8217;t go overboard when it came to instrumenting this problem, I just used the timing functions built into the C++ library. On my Windows and Linux test systems, the values of CLOCKS_PER_SEC are both high enough that I&#8217;m not worried about granularity issues.</p>
<h4>The First Results</h4>
<p>I ran my test program in Visual C++ Release mode, using all the standard settings for a console application. For purposes of comparison, I ran the same program using g++ 4.6.1 on the same computer, booted up under Linux. For the set of 1,000,000 tokens, the results are shown below:</p>
<table border="1" cellpadding="5">
<thead>
<tr>
<th>Task</th>
<th>VC++ 10 Release</th>
<th>g++ 4.6.1 -O3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fill <code>unordered_map&lt;string&gt;</code></td>
<td>0.41s</td>
<td>.11s</td>
</tr>
<tr>
<td>Fill <code>unordered_map&lt;string const *&gt;</code></td>
<td>0.39s</td>
<td>0.14s</td>
</tr>
<tr>
<td>Destroy <code>unordered_map&lt;string&gt;</code></td>
<td>3.17s</td>
<td>0.01s</td>
</tr>
<tr>
<td>Destroy <code>unordered_map&lt;string const *&gt;</code></td>
<td>3.24s</td>
<td>0.004s</td>
</tr>
<tr>
<td>Fill <code>map&lt;string&gt;</code></td>
<td>0.83s</td>
<td>.53s</td>
</tr>
<tr>
<td>Fill <code>map&lt;string const *&gt;</code></td>
<td>0.88s</td>
<td>0.66s</td>
</tr>
<tr>
<td>Destroy <code>map&lt;string&gt;</code></td>
<td>.14s</td>
<td>0.01s</td>
</tr>
<tr>
<td>Destroy <code>map&lt;string const *&gt;</code></td>
<td>.07s</td>
<td>0.002s</td>
</tr>
</tbody>
</table>
<p>There are a few interesting points to take away from these tests:</p>
<ul>
<li/>Microsoft&#8217;s compiler is taking an exceptionally long time to destroy hashed containers &#8211; one order of magnitude greater than it took to create it, and two orders of magnitude greater than it takes g++ to do the same task.
<li/>It doesn&#8217;t look like constructing and destroying the strings is a big factor. Both compilers have roughly the same performance with both <code>std::string</code> and <code>std::string *</code>. Microsoft&#8217;s behavior is counterintuitive, as it takes longer to construct and destroy containers using the pointer.
<li/>The GNU compiler appears to be able to run through this exercise notably faster.
</ul>
<p>The time it takes to destroy the table is a concern &#8211; having a C++ program hang for over 3 seconds to destroy a modestly large data structure is a serious concern &#8211; particularly when the same task completes in a few milliseconds with g++.</p>
<h4>The Pathological Results</h4>
<p>These concerns are nothing compared to what I see when running in debug mode. Setting my Visual Studio project to Debug mode, then running the same test, yields the results shown here:</p>
<table border="1" cellpadding="5">
<thead>
<tr>
<th>Task</th>
<th>VC++ 10 Debug</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fill <code>unordered_map&lt;string&gt;</code></td>
<td>17.41s</td>
</tr>
<tr>
<td>Fill <code>unordered_map&lt;string const *&gt;</code></td>
<td>17.08s</td>
</tr>
<tr>
<td>Destroy <code>unordered_map&lt;string&gt;</code></td>
<td>505.36s</td>
</tr>
<tr>
<td>Destroy <code>unordered_map&lt;string const *&gt;</code></td>
<td>505.99s</td>
</tr>
<tr>
<td>Fill <code>map&lt;string&gt;</code></td>
<td>13.29s</td>
</tr>
<tr>
<td>Fill <code>map&lt;string const *&gt;</code></td>
<td>13.15s</td>
</tr>
<tr>
<td>Destroy <code>map&lt;string&gt;</code></td>
<td>0.94s</td>
</tr>
<tr>
<td>Destroy <code>map&lt;string const *&gt;</code></td>
<td>0.18s</td>
</tr>
</tbody>
</table>
<p>Those numbers are hard to believe. Destroying a hash table takes one millisecond when using g++. In VC++ 10, it takes almost ten minutes!</p>
<p>Worse, we suddenly see that hashed containers are <i>slower</i> than the containers built on red-black trees. Again, this just doesn&#8217;t make sense.</p>
<p>The big problem with these numbers is that it means the debug mode of the compiler is effectively unusable for a lot of tasks. Regardless of how much testing it does, when it is this slow, it is just not useful.</p>
<h4>A Workaround</h4>
<p>I didn&#8217;t invest the time to try debugging Microsoft&#8217;s library, so I don&#8217;t really know where the time is being spent. I did try a few things to speed things up, and I found one technique that helps a lot. Before including any Microsoft header files, try entering this single line in your source:</p>
<pre>
#define ITERATOR_DEBUG_LEVEL 0
</pre>
<p>With this definition in place, the delete times return to ball park of the times seen when running in release mode. Of course, you give up some debugging. I believe that an explanation of what this macro does might be found <a href="http://blogs.msdn.com/b/vcblog/archive/2011/04/05/10150198.aspx" class="newpage">here</a>.</p>
<p>In the final analysis, I think Microsoft has some serious work to to do here. The performance of their hashed containers, and to some lesser extent, the pre-C++11 associative containers, needs some serious examination. If the library is going to run this much slower than the competition, I need a good explanation why.</p>
<h4>Source</h4>
<p><a href="/attachments/2011/msvc_hash/HashTest.cpp">HashTest.cpp</a></p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/11/28/vc-10-hash-table-performance-problems/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Stranger In a Strange Land</title>
		<link>http://marknelson.us/2011/06/26/stranger-in-a-strange-land/</link>
		<comments>http://marknelson.us/2011/06/26/stranger-in-a-strange-land/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 18:56:38 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Complaining]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=470</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/06/26/stranger-in-a-strange-land/' addthis:title='Stranger In a Strange Land' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>For most of the thirty-some years that I&#8217;ve been paid to program, I&#8217;ve worked with Assembly language, C, or C++. There have been some diversions to Java, which is pretty painless for a C++ programmer, and always the odd short job or two in anything from Perl to Haskell. But for the last three months [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/06/26/stranger-in-a-strange-land/' addthis:title='Stranger In a Strange Land' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>For most of the thirty-some years that I&#8217;ve been paid to program, I&#8217;ve worked with Assembly language, C, or C++. There have been some diversions to Java, which is pretty painless for a C++ programmer, and always the odd short job or two in anything from Perl to Haskell.</p>
<p>But for the last three months or so I&#8217;ve been working full time on a short-timeline project that is using PHP as a general-purpose scripting language. So I&#8217;ve been working in this unfamiliar language at a fairly frantic pace, and I have to say that PHP, like Perl, is a strange place for a C programmer.<br />
<span id="more-470"></span></p>
<h4>The Normal Problems</h4>
<p>Any tutorial in a scripting language like PHP or Perl always makes a big deal out of <a href="http://php.net/manual/en/language.types.type-juggling.php" class="newpage">Type Juggling</a>, meaning that the type of a variable is inferred from the context in which it is used. In a simple example, a string literal can behave like an integer without any fuss, and vice versa:</p>
<pre>
{appliance:~} php -r '
> $a = 25;
> printf( strlen($a) . " " );
> printf( 15 + $a );
> printf( "\n" );'
2 40
</pre>
<p>Maybe it&#8217;s just me, but this aspect of PHP doesn&#8217;t seem to cause too much trouble. It may be because C and C++ already do implicit data conversions in a lot of situations; PHP just turns the dial up a notch or two.</p>
<p>What does give me a lot of trouble (and this is not exactly news) is that PHP doesn&#8217;t require declaration of variables, being perfectly content to create them on first use.</p>
<p>There&#8217;s of course nothing wrong with this, but as a C programmer, I have not developed any defensive skills in this area. A PHP programmer is much less likely to enter the following code, because he or she will be a lot more careful about entering variable names:</p>
<pre>
{appliance:~} php -r '
>$first = "Mark";
>$last = "Nelson";
>$name = $fist . " " . $last;
>print( "$name\n" );'
 Nelson
</pre>
<p>The good news on variable declaration is that I can jack up PHP&#8217;s error reporting so that these mistakes are flagged at runtime. Increased error reporting will also catch another error that plagues C++ programmers:</p>
<pre>
{appliance:~}php -r '
>$version = "1.2.3";
>print( version . "\n" );'
version
</pre>
<p>Yes, string literals are just as good without quotes as they are with.</p>
<h4>This Week&#8217;s Puzzle</h4>
<p>After a few months I start feeling a bit of a false sense of confidence in my understanding of the language, and naturally, I got some serious comeuppance this week, demonstrated by the following code:</p>
<pre>
{cslinux1:~} php -r '
>class base {
>  private $secret="42";
>  public function print_secret() {
>    printf( "$this->secret\n" );
>  }
>};
>class derived extends base {};
>$foo = new derived();
>$foo->secret="43";
>printf( $foo->secret . " " );
>$foo->print_secret();'
43 42
</pre>
<p>As a C++ programmer, I fully expect the attempt to modify <code>$foo->secret</code> to generate a runtime error. <code>$secre</code>t is a private member, and as such I shouldn&#8217;t have access to it via the derived class. But the runtime happily makes the assignment.</p>
<p>When I actually print the value, I see that the base class and derived class print different values for $secret. What has happened here is that the assignment actually created a new data member in the derived class with the name <code>$secret</code>.</p>
<p>So what exactly is happening here? The reason for the difference in behavior is that PHP uses the private and protected modifiers to define not just <em>accessiblity</em>, but <em>visibility </em>as well. The <code>$secret</code> member in the base class is not only inaccessible outside of the base class, it is invisible as well.</p>
<p>That invisibility is the key to the behavior. When I try to assign a value to <code>$this->secret</code>, the runtime looks to see if the object of class <code>derived </code>already has a member called <code>$secret</code>. Since the member in the base class is invisible, the conclusion is that there is no member with that name, and as a result, a new variable is created on the fly: <code>derived::$secret</code>.</p>
<p>This can&#8217;t be fixed by simply amping up the error level, unfortunately, and it definitely is a problem for C++ or Java programmers. It doesn&#8217;t just cause me a lot of confusion, it just seems <I>wrong</i>. </p>
<p>Of course, there is plenty to like about developing in PHP, and it is definitely the right tool for this project. The speed bumps just make the process a lot more interesting. The concept of visibility set me back a few hours, but I&#8217;m ready to move on &#8211; until I hit the next bit conceptual roadblock.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/06/26/stranger-in-a-strange-land/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sapir-Whorf to Dijkstra to Torvalds &#8211; Language Bigotry In Our Time</title>
		<link>http://marknelson.us/2011/06/14/sapir-whorf-to-dijkstra-to-torvalds-language-bigotry-in-our-time/</link>
		<comments>http://marknelson.us/2011/06/14/sapir-whorf-to-dijkstra-to-torvalds-language-bigotry-in-our-time/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 20:29:09 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[People]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=439</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/06/14/sapir-whorf-to-dijkstra-to-torvalds-language-bigotry-in-our-time/' addthis:title='Sapir-Whorf to Dijkstra to Torvalds &#8211; Language Bigotry In Our Time' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Back in the day the Sapir-Whorf hypothesis was all the rage in the study of linguistics. With apologies to those who actually work in the field, I&#8217;ll crudely summarize it as the idea that the language you speak both constrains and influences how you think. The idea says that if your language only has one [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/06/14/sapir-whorf-to-dijkstra-to-torvalds-language-bigotry-in-our-time/' addthis:title='Sapir-Whorf to Dijkstra to Torvalds &#8211; Language Bigotry In Our Time' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>Back in the day the Sapir-Whorf hypothesis was all the rage in the study of linguistics. With apologies to those who actually work in the field, I&#8217;ll crudely summarize it as the idea that the language you speak both constrains and influences how you think. The idea says that if your language only has one word for <i>snow</i>, for example, you will actually have a hard time seeing any difference between light powder and crunchy ice pack.</p>
<p><a href="http://en.wikipedia.org/wiki/Sapir-whorf" class="newpage">Sapir-Whorf</a> was seen as completely discredited back when I learned about it, and while Linguistic Relativity has enjoyed a slight comeback with a weakly restated set of hypotheses, it seems fairly certain that human thought is by no means confined to a cage built out of vocabulary and grammar.</p>
<p>Our field has long had its own Sapir in E.W. Dijkstra, whoe <a href="http://www.cs.utexas.edu/~EWD/transcriptions/EWD04xx/EWD498.html" class="newpage">spelled it</a> out with money quotes like these:<br />
<span id="more-439"></span></p>
<blockquote><p>It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.<br />
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence.
</p></blockquote>
<p>Closer to today, we have the famous <a href="http://lwn.net/Articles/249460/" class="newpage">rant against C++</a> from Linus Torvalds, who feels that a programmer who uses C++ is going to wreck any project he or she touches:</p>
<blockquote><p>I&#8217;ve come to the conclusion that any programmer that would prefer the project to be in C++ over C is likely a programmer that I really *would* prefer to piss off, so that he doesn&#8217;t come and screw up any project I&#8217;m involved with.</p>
<p>C++ leads to really really bad design choices.</p></blockquote>
<h4>Dogmatism à la Torvalds</h4>
<p>At the large corporate entity that pays my bills, we have a slogan on the back of our badges: <i>No Technology Religion</i>. It might not be easy to live up to this, but yes, I try. </p>
<p>To me, this admonishment means two things:</p>
<ol>
<li/>Try to objectively choose the best tool for the job
<li/>Don&#8217;t let your preferences in tools dictate the way the job should be done
</ol>
<p>Linus is clearly saying in his rant that anyone who programs in C++ is guilty of breaking both of these rules.</p>
<p>I disagree. I think there are times when C++ is clearly the right tool for the job, and that you can arrive at this conclusion fairly objectively. I think Linus is clearly fogged in by his particular Technology Religion.</p>
<h4>A Simple Example</h4>
<p>As the final assignment for my C/C++ programming class last semester, I asked my students to implement a simple token counting program in C. The goal was to reproduce the behavior given by this C++ fragment:</p>
<pre>
map&lt;string,int&gt; counts;
string s;
while ( cin &gt;&gt; s )
    counts[s]++;
for ( auto ii = counts.begin() ; ii != counts.end() ; ii++ )
    cout &lt;&lt; ii-&gt;second &lt;&lt; " : " &lt;&lt; ii-&gt;first &lt;&lt; endl;
</pre>
<p>This particular program highlights a number of features of C++ that are not present in C:</p>
<ul>
<li/>The versatile replacement for C arrays, vector&lt;T&gt;.
<li/>The string class.
<li/>Safe input using iostreams.
<li/>Associative arrays as part of the standard library.
</ul>
<p>This program is quite easy to write in C++, and is basically complete. One could flesh it out a bit with of error handling on the input stream, but that&#8217;s really not even necessary.</p>
<h4>Do it in C</h4>
<p>Rewriting this in C is a straightforward task with one big speed bump: the lack of any sort of associative array in the library. There are a number of ways to deal with this &#8211; I chose the following strategy:</p>
<ul>
<li/>Read all the tokens into an array.
<li/>Upon completion, sort the array
<li/>Once the array is sorted, walk through it to get the count for each token.
</ul>
<p>While this algorithm uses more space than the C++ program, it probably takes up the same amount of time, assuming you don&#8217;t bump into one of the pathologically bad cases of qsort().</p>
<p>Since I asserted that this program is easier to write in C++ than C, it behooves me to give a list of reasons why. </p>
<ul>
<li/>C I/O deficiencies. Reading strings in C is considerably more difficult due to the fact that the C I/O library doesn&#8217;t have a standard way to read strings of unbounded length. (Compiler-specific extensions can be used, but that raises other problems.) Your input code has to do a lot of checking for error cases, or you have to build your own string input functions.
<li/>Memory management of C arrays is a very manual task. I have to allocate the original space for my array, take care that I reallocate if I exceed its length, and free the space when I am done.
<li/>Memory management of C strings has exactly the same probelms.
<li/>Sorting the array of strings is just a tiny bit more inconvenient with qsort(), and qsort() doesn&#8217;t give me the performance guarantees of the sort() function in the C++ library.
</ul>
<p>The C version of my function has more lines of code, and more bookkeeping tasks that need to be done manually. There are more opportunities to make mistakes.</p>
<p>A final reason I like the C++ version of the program better is that it lends itself well to working with other types. Any type that has insertion and extraction operators, and a comparison operator, can use that same code with just one declaration change. Turning it into a function template accomplishes the same thing with no code changes needed at all.</p>
<h4>Some of My Best Friends are C Programmers</h4>
<p>So am I a language bigot for preferring the C++ version of this code?</p>
<p>I hope not. For one thing, I can see that the C version of the program has some nice advantages:</p>
<ul>
<li/>You can write this program using POSIX system calls for almost everything except memory allocation and sorting, resulting in an extremely small footprint.
<li/>The C version of the program will be faster due to the use of low-level I/O. C++ iostreams get better all the time, but their layered approach will always be at a disadvantage when it comes to efficiency.
</ul>
<p>So for a program like this, the choice of language really comes down to context. If you believe in the 80/20 rule, you might think this code should be written in C++ if it is outside of the expensive core part of your program. With fewer lines of code you have fewer chances for error, and efficiency is probably not a big consideration.</p>
<p>If this is in a critical section of code that is executed frequently, you might decide C is your best choice. Make sure to put a little extra time into code review to ensure that the code is free of memory leaks and pointer errors, and you are in business.</p>
<h4>Sic Temper Linus</h4>
<p>So how does Linus&#8217; rant hold up when looking at the C++ code shown at the top of the post? I would venture a guess that given the assignment, any decent C++ programmer would produce code similar to this. Linus says:</p>
<blockquote><p>You invariably start using the &#8220;nice&#8221; library features of the language like STL and Boost and other total and utter crap, that may &#8220;help&#8221; you program, but causes infinite amounts of pain when they don&#8217;t work (and anybody who tells me that STL and especially Boost are stable and portable is just so full of BS that it&#8217;s not even funny</p></blockquote>
<p>In this program I make good use of the standard library components that were once part of the STL. Having been part of the standard for over a decade, they work really well and have no portability or correctness issues in any compiler I am aware of. Saying that components like map and vector are problematic is just wrong.</p>
<blockquote><p>inefficient abstracted programming models where two years down the road you notice that some abstraction wasn&#8217;t very efficient, but now all your code depends on all the nice object models around it, and you cannot fix it without rewriting your app.</p></blockquote>
<p>Well, despite the fact that C++ has made it impossible for me to do so, I managed to write the program without using any abstractions &#8211; no new classes, no interfaces. Basically just straight procedural C code that happens to employ a few useful classes.</p>
<p>And at least with the people I work with, I think this is the rule rather than the exception.</p>
<blockquote><p>
In other words, the only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C.
</p></blockquote>
<p>When C has container classes, a string class, typesafe I/O, and the programmer&#8217;s gift from the gods, <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" class="newpage">RAII</a>, then this statement will be true. For now, it is bollocks.</p>
<p>Before modern C++ was available, I probably would have stuck with a simple pipeline to accomplish this task:</p>
<pre>
tr [:blank:] '\n'  | grep -v "^$" | sort | uniq -c
</pre>
<p>The fact that I can do the same thing just as easily in a compiled language gives me some flexiblity. I think I can appreciate that fact without being a bigot.</p>
<p>Can you?</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/06/14/sapir-whorf-to-dijkstra-to-torvalds-language-bigotry-in-our-time/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>20 Heads In a Row &#8211; What Are the Odds?</title>
		<link>http://marknelson.us/2011/01/17/20-heads-in-a-row-what-are-the-odds/</link>
		<comments>http://marknelson.us/2011/01/17/20-heads-in-a-row-what-are-the-odds/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 02:44:35 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://marknelson.us/?p=187</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/01/17/20-heads-in-a-row-what-are-the-odds/' addthis:title='20 Heads In a Row &#8211; What Are the Odds?' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>An exercise in probability demonstrates the exact chances of flipping a fair coin k times in a row given n tosses.]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2011/01/17/20-heads-in-a-row-what-are-the-odds/' addthis:title='20 Heads In a Row &#8211; What Are the Odds?' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>A couple of weeks back I <a href="http://marknelson.us/2010/09/12/innumeracy-revisited/" class="newpage">took</a> <a href="http://www.nytimes.com/2010/09/26/magazine/26letters-t-ENGINEERINGT_LETTERS.html" class="newpage">issue</a> with a <a href="http://www.nytimes.com/2010/09/12/magazine/12FOB-IdeaLab-t.html" class="newpage">statement</a> in the New York Times Magazine:</p>
<blockquote><p>
If you have a million coin flips, it’s almost certain that somewhere in those coin flips there will be 20 heads in a row.
</p></blockquote>
<p>My blog post took the Times to task for printing what seemed to be an obviously bogus statement. Since a run of 20 heads is roughly a one-in-a-million occurence, a basic feel for probability should tell you that trying to do this a million times is not going to be a certainty &#8211; fairly far from it.</p>
<p>Naturally, I thought to back up my argument with some hard facts, and I came up with a calculation that showed that the chances of this happening were actually around 60%. Unfortunately, I made a pretty basic mistake in calculating the probability, as was demonstrated to me by correspondent Andy Langowitz. My intuition about the likelihood of success was on the money, but my estimate was a bit high. </p>
<p>The story of how I corrected my error in order to get the correct number, around 37.9%, is a good lesson in careful examination of probability rules, with a small detour into the land of the bignum.<br />
<span id="more-187"></span></p>
<h4>Independent Probabilities</h4>
<p>When trying to develop a formula like this one, it is often easier to work from the inverse. Instead of calculating how likely it is for 20 heads to occur at any point in the sequence of a million tosses, I thought to instead calculate the probability that it wouldn&#8217;t happen. We can then take that number and subtract it from one to get the desired result.</p>
<p>The chance of <i>n</i> heads in a row occurring is 1/2<sup>n</sup>, so the inverse probability is (2<sup>n</sup>-1)/2<sup>n</sup>. If we multiply that probability once for all 999,981 possible occurences of a streak of 20 heads, it seemed to me that I would be in business. Doing this is a simple enough calculation, and the result was the 60% figure. That figure felt like it was in the ballpark to me, and I left it that.</p>
<p>Mr. Langowitz, however, was smart enough to actually test the theory on a smaller set of numbers. Let&#8217;s apply this theory to find out how likely we are to throw two heads in a row in four tries.</p>
<p>The chance of two heads in a row is 1/4, so my formula would give a result of 1 &#8211; (3/4)<sup>3</sup>, or a result of 37/64 &#8211; a little better than 50% chance of it occuring. But probability is nothing but the art of enumerating and counting, and we can do just that to check the result. The 16 equally possible outcome of four tosses are:<br />
<code><br />
HHHH HHHT HHTH HHTT<br />
HTHH HTHT HTTH HTTT<br />
THHH THHT THTH THTT<br />
TTHH TTHT TTTH TTTT<br />
</code><br />
Look through those outcomes and see how many have two consecutive heads. It turns out to be exactly 8, meaning that my calculation of 37/64 is just flat wrong. </p>
<h4>Locating the Mistake</h4>
<p>My mistake is a pretty common one among probability neophytes. I assumed that probability at each step in the sequence was identical, because the sequences were completely independent. It&#8217;s easy enough to think that if you don&#8217;t examine problem carefully. </p>
<p>What actually happens is that when we examine the possibility of an unsuccessful run of heads at toss <i>i</i>, we slightly bias the outcome at toss <i>i+1</i>. A demonstration will show exactly how this happens.</p>
<p>For the sample problem of a sequence of two heads out of four tosses, we can first examine the chance of a negative outcome starting at toss 1. There are just four possible outcomes that have two tosses starting at position 1:<br />
<code><br />
HH HT TH TT<br />
</code><br />
And only one of these tosses yielded two heads in a row, so the probability of not seeing two heads after two tosses is 3/4.</p>
<p>But now when we look at the sequence of tosses starting at position two, we have to throw out the outcomes where we had two heads at toss one &#8211; we&#8217;ve already seen two heads, so we can&#8217;t continue flipping coins in those outcomes. So our universe of possible outcomes is now a bit different:<br />
<code><br />
HTH HTT<br />
THH THT<br />
TTH TTT<br />
</code><br />
Instead of eight outcomes, we have six. And if we look at the first toss seen in position two, instead of having an even distribution of heads and tails, you can see that sample is biased: only two have a head in position two, while four have tails. So the chances of not seeing two heads starting at position two increases to 5/6. Note that this change in probability occurs because we have selected only those outcomes without a streak of two heads at position one.</p>
<p>Likewise, when we look at the possible outcomes for streaks starting at position three, we get a different probability again. Because we have to throw out one sequence in the previous test, the universe of possible outcomes is now limited to:<br />
<code><br />
HTHH HTHT<br />
HTTH HTTT<br />
THTH THTT<br />
TTHH TTHT<br />
TTTH TTTT<br />
</code><br />
So now we have just ten possible outcomes, and  two of those will produce the desired outcome, meaning the probability has changed to 4/5.</p>
<p>So what is the probability of all three possible positions not containing a streak? That would be (3/4)*(5/6)*(4/5) which reduces nicely to 1/2, the correct answer.</p>
<h4>Finding the General Solution</h4>
<p>So let&#8217;s generalize the question at hand: what is the probability of seeing <i>k</i> consecutive heads when a fair coin is tossed <i>n</i> times? The previous section showed that we can work it out by hand for small numbers of tosses, but it should be clear that if are going to toss a coin a million times, the universe of possible outcomes is going to get unwieldy. We need a general description of the problem in order to solve it for any values of <i>k</i> and <i>n</i>.</p>
<p>For many probability problems, finding a solution is simply a way of figuring out how to count things, and coin tosses indeed appear to be just such a problem. Let&#8217;s try to see if we can count the number of times a sequence of <i>k</i> heads will appear at a given toss.</p>
<p>To start to work out the solution to the problem, I will set <i>k</i> to a value of three &#8211; in other words, we will be trying to see what is the probability of seeing three consecutive heads is at toss <i>i</i>,  given that there have not been three heads at an earlier toss.  To calculate the probability, we need to know two things. First, we need to know all the possible outcomes in our universe of samples at toss <i>i</i>.  In the previous section, with a value of <i>k</i>=2, we saw the the number of outcomes for tosses 1, 2, 3, and 4, was 2, 4, 6, and 10.</p>
<p>After determining the number of outcomes, we then need to determine how many of those outcomes were successes. If we defined success as being the number of outcomes in which a <i>k</i> heads in a row appear at position <i>i</i>, the values from the previous section would have been 0, 1, 1, 2.</p>
<h4>Counting the Successes</h4>
<p>I&#8217;ll start with the more difficult problem: counting the number of times <i>k</i> heads appear at toss <i>i</i>.  To start with, we have the degenerate cases where <i>i</i> is less than <i>k</i>. In all of those tosses, we know that the number of successful outcomes is going to be zero, because there have not been enough tosses to achieve success yet.</p>
<p>If we work our way forwards with the example of <i>k</i>=3, our first four tosses end up giving us three sets of outcomes:<br />
<code><br />
H T</p>
<p>HH HT<br />
TH TT</p>
<p>HHH HHT<br />
HTH HTT<br />
THH THT<br />
TTH TTT</p>
<p>HHTH HHTT<br />
HTHH HTHT<br />
HTTH HTTT<br />
THHH THHT<br />
THTH THTT<br />
TTHH TTHT<br />
TTTH TTTT<br />
</code><br />
Note that when we get to toss 3, there is just one successful outcome.  Likewise, in toss 4, there is just one successful outcome.</p>
<p>It may not be immediately obvious, but we can in fact always tell how many successes we will achieve at toss <i>i+k</i> after we have enumerated all the possible outcomes at toss <i>i</i>. The number is defined as the number of outcomes at toss <i>i</i> that end in a tail.</p>
<p>The logic behind this is straightforward: in order to have a success at position <i>i+k</i>, we need to generate a sequence of <i>k</i> heads, starting at toss <i>i+1</i>. If we have an outcome that currently ends in a tail, it will generate 2^<i>k</i> outcomes in the next <i>k</i> tosses, and precisely one and only one of these will have <i>k</i> consecutive heads. None of these outcomes will result in an sequence of <i>k</i> heads before toss <i>i+k</i>, because they currently terminate in a tail, so all of the outcomes generated from that outcome at position <i>i</i> will be included in the outcomes seen at toss <i>i+k</i>.</p>
<p>Likewise, none of the outcomes at position <i>i</i> that currently end in a head are going to be able to contribute to a success at toss <i>i+k</i>. Any streak of <i>k</i> heads that follows a terminating head at toss <i>i</i> will result in a run of <i>k</i> heads <i>before</i> we reach toss <i>i+k</i>.</p>
<p>Looking at our outcomes for <i>k</i>=3, we can see that at toss 1 we have one outcome ending in a tail, so at toss 4 we will have one success. At toss 2 we have two outcomes ending in a tail, so at toss five we will have two successes. And we have the special case of toss 0 &#8211; we have one sequence starting at toss 0 that generates a sequence of <i>k</i> heads at toss <i>k</i>. Although there were no tails tossed at position 0, any sequence that starts there doesn&#8217;t have any preceding heads tosses either, so it is as if there was a single outcome at toss 0 with a value of tails.</p>
<p>So each toss that ends in a tail acts as the root of a successful outcome at a future position. This is good information, but in order to turn this in to a formula we need to be able to compute the number of outcomes ending in tails at toss <i>i</i> &#8211; we don&#8217;t want to have to enumerate all the outcomes in order to get there. I&#8217;ll refer to these special outcomes as <i>anchors</i>, as they form the anchor of a future outcome.</p>
<h4>Counting the Anchors</h4>
<p>The number of anchor outcomes at each position starts out as a nice number while <i>i</i> is less than or equal to <i>k</i>: 2^<i>i</i>. But after toss <i>k</i>, successful outcomes start being removed from the sample set and the formula no longer holds. For <i>k</i>=3, the anchor count starting at toss1 is: 1, 2, 4, 7, 13, 24.</p>
<p>It turns out that the anchor at position <i>i</i> does more than just generate a success at toss <i>i+k</i>. It is also responsible for generating new anchor outcomes at tosses <i>i+1</i>, <i>i+2</i>, &#8230;, <i>i+k-1</i>. </p>
<p>Looking at an example for <i>k=3</i> should clarify this. Our lone anchor outcome at toss 1 is the sequence <code>T</code>. We know that this anchor will create a new successful outcome at toss 4: <code>THHH</code>. But it also creates new anchors at all intermediate tosses: <code>TT</code>, <code>THT</code>, and <code>THHT</code>. </p>
<p>This observation holds true for the generation of all new anchors, and with a little work we can turn this into a usable recurrence. If each anchor at toss <i>i</i> is going to create a new anchor at tosses <i>i+1</i> through <i>i+k-1</i>, we can calculate the number of anchors at toss <i>i</i> using this formula:<br />
<center><br />
<img src="/attachments/2011/tosses/Figure1.png"/><br />
</center><br />
You might recognize this formula if we write it out for <i>k</i>=2:<br/></p>
<p> <code>anchors(i)=anchors(i-1) + anchors(i-2)</code>.</p>
<p>Yes, that is the Fibonacci sequence. For values of k higher than 2, the formula is the <a href="http://mathworld.wolfram.com/Fibonaccin-StepNumber.html" class="newpage">n-step Fibonacci sequence</a>. The well-known Fibonacci sequence recursively adds the previous two values to get the current value. The <b>n-step Fibonacci</b> sequence adds the previous <i>n</i> values in order to get the current value. (For the rest of this article, the n-step Fibonacci number will be referred to as fib<sub>n</sub>(i).)</p>
<p>In the standard Fibonacci recurrence definition, we define a base value of fib(1) = 1, and fib(x) = 0 for all x less than 1. Our anchor count is skewed by one, since our base value at toss 0 is 1. As a result, the the anchor count at toss <i>i</i> is equal to fib<sub>k</sub>(<em>i+1</em>). And from our observation of the link between anchors and successful outcomes, we can then observe that the number of successful outcomes at toss <i>i</i> is equal to fib<sub>k</sub>(<i>i+1-k</i>). </p>
<h4>Counting the Outcomes</h4>
<p>Knowing the number of successful outcomes at toss <i>i</i> only gets us halfway to knowing the actual probability of seeing a sequence of <i>k</i> heads at that point. To get the full probability, we need to know the number of outcomes as well.</p>
<p>Fortunately, this calculation is nearly trivial. In the previous section we saw that the number of anchors at toss <i>i</i> is equal to fib<sub>k</sub>(<em>i+1</em>). Each anchor at toss 1 and greater is simply a sequence of tosses that ends in a tail. And for every sequence ending in a tail, there is a corresponding sequence that is identical except for one key change: it ends in a head instead of a tail. So the number of outcomes at toss <i>i</i> is twice the number of anchors, or 2*fib<sub>k</sub>(<em>i+1</em>).<br />
<center><br />
<img src="/attachments/2011/tosses/Figure2.png"/><br />
</center><br />
Now that we have those numbers, we can finally crank out the probabilities of a streak of <i>k</i> heads appearing at all possible tosses without having to painstakingly enumerate all those sequences of heads and tails. The figure below shows some of the probabilities for tosses starting at 1 for streaks of 2, 3, and 4 heads. It is an excellent exercise to walk through the enumeration process in order to double check the figures &#8211; I encourage you to give it a try.<br />
<center><br />
<img src="/attachments/2011/tosses/Figure3.png"/><br />
</center></p>
<h4>The Absence of a Streak</h4>
<p>Now that we can compute the probability of seeing a streak of <i>k</i> heads at toss <i>i</i>, we need to do just a bit more work to see the what the odds are of seeing that streak at any time in a sequence of <i>n</i> tosses. To get there, we need one more piece of information: the probability of <i>not</i> seeing a streak of <i>k</i> heads at toss <i>i</i> &#8211; in other words, the chances of a negative outcome.</p>
<p>It&#8217;s pretty easy to calculate that number &#8211; simply subtract the number of successful outcomes from the total number of outcomes. The sample sequences for values of <i>k</i> corresponding to 2, 3, and 4 are shown here:<br />
<center><br />
<img src="/attachments/2011/tosses/Figure4.png"/><br />
</center><br />
In order to make the final probability calculation a bit easier, I&#8217;m going to derive a value for the count of failures that simplifies future calculations. In the first line of the figure below we have the basic calculation &#8211; the number of failures at toss <i>i</i> is equal to the total number of outcomes less the number of successes. Both of those two values used to derive the failure count can be expressed in terms of an n-step Fibonacci number, and that is shown on the next line.</p>
<p>In the third line of the figure I simply break out the value first term of the equation into its two component parts. Now, in the next line, I expand the value of fib<sub>k</sub>(<i>i+1</i>) into its component parts, using the definition of the Fibonacci sequence. I keep that grouped in parentheses to make it clear that the new terms are the result of the expansion. Just as an example, if we were doing this expansion for a value of <i>k</i> equal to 3, we would expand fib<sub>3</sub>(<i>i+1</i>) into the sum of fib<sub>3</sub>(<i>i</i>), fib<sub>3</sub>(<i>i-1</i>), and fib<sub>3</sub>(<i>i-2</i>), using the identity of the n-step Fibonacci sequence.</p>
<p>Note that in line 3, the last term of the expanded Fibonacci sequence in parentheses is canceled by the last term overall, which was the number of successful outcomes. When we remove those two elements from the equation, line 5 has simplified into the identity of fib<sub>k</sub>(i+2). So we now have a reasonable number we can use to determine the count of failures at a given toss.<br />
<center><br />
<img src="/attachments/2011/tosses/Figure5.png"/><br />
</center></p>
<h4>Put it All Together</h4>
<p>With all these formulas in hand, we have the tools to determine the final probability we&#8217;ve been working towards: the probability that a sequence of <i>k</i> heads will appear in a sequence of <i>n</i> tosses. To do this, we calculate the cumulative probability that the event does not occur, and subtract that value from 1. The result will be the answer to the question.</p>
<p>The first equation below shows the general approach we take to this problem &#8211; multiplying the probability of failure at each toss. Once we plug in the actual formula for the failure count at each point, and the formula for the total number of outcomes, we see that most of the terms cancel out. We are left with fib<sub>k</sub>(<i>n+2</i>) on top, and 2<sup>n</sup> on the bottom. And that is the final formula that provides an answer to the question.<br />
<center><br />
<img src="/attachments/2011/tosses/Figure6.png"/><br />
</center><br />
Returning to the original supposition in the New York Times, all we need to do is calculate fib<sub>20</sub>(1,000,002) and then divide it by 2<sup>1,000,000</sup>.</p>
<p>Unfortunately, my calculator is not really up to this. Even a desktop calculator that could handle arbitrary precision math won&#8217;t normally have a fib<sub>k</sub> button. </p>
<p>If I had a copy of <a href="http://www.wolfram.com/mathematica/" class="newpage">Mathematica</a>, and knew how to use it, I think I could solve this with just a few lines of input. But I don&#8217;t, so I coded up a short solution in Java.</p>
<p>Java has two classes that enable me to solve this problem in relatively easy fashion: <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html" class="newpage">java.math.BigInteger</a> and <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html" class="newpage">java.math.BigDecimal</a>. BigInteger performs simple integer calculations with arbitrary precision, and BigDecimal supports floating point math.</p>
<p>My simple app, contained in <a href="/attachments/2011/tosses/Flipper.java" class="newpage">Flipper.java</a>, uses BigInteger to calculate fib<sub>k</sub>(n+2) and 2<sup>n</sup>, then uses BigDecimal to divide the two numbers and subtract the result from 1. Despite fact that the two intermediate results are over 300,000 digits each, the program ran in a very reasonable amount of time, less than an hour. (Optimization of this program would be a very interesting exercise.)</p>
<p>The output of the program is shown here:<br />
<code><br />
fib(20,1000002) = 614579313398524367786474463596 (301000 digits elided)<br />
2^1000000 = 99006562292958982506979236163 (301000 digits elided)<br />
Div = 0.379253961388950068663971868 (999980 digits elided)<br />
</code><br />
So at last, we know the correct result. If you flip a coin a million times, you have a 38% chance of seeing 20 heads in a row. A long way from the certainty claimed by the New York Times, and a bit off from my initial 60% value.</p>
<h4>Postscript</h4>
<p>Working out the details of this problem was a very enjoyable piece of math. When I first started in on the problem, I hoped I would be able to find a reference that simply told me how to calculate the number, but I had no luck. As I worked through the problem, I ran into the existence of the n-step Fibonacci numbers, which I had never heard of. Once I found the reference to them on the Wolfram Alpha page (linked above), I saw that the page had a terse note describing this problem, but with no details. With luck the next person trying to understand this problem will be able to make sense of it by reading this page.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2011/01/17/20-heads-in-a-row-what-are-the-odds/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>No Exceptions &#8211; With One Exception</title>
		<link>http://marknelson.us/2007/11/13/no-exceptions/</link>
		<comments>http://marknelson.us/2007/11/13/no-exceptions/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 05:04:46 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://marknelson.us/2007/11/20/no-exceptions/</guid>
		<description><![CDATA[







































<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/11/13/no-exceptions/' addthis:title='No Exceptions &#8211; With One Exception' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Exceptions are a necessary part of the C++ language, but for most programmers they are worse than worthless - they are unusable. When exceptions were first added to the language back in the days before standardization, they were seen as a brilliant improvement over the hideous setjmp/longjmp facility from ANSI C. Because exceptions unwind the [...]]]></description>
			<content:encoded><![CDATA[



















<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/11/13/no-exceptions/' addthis:title='No Exceptions &#8211; With One Exception' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>Exceptions are a necessary part of the C++ language, but for most programmers they are worse than worthless - they are unusable. When exceptions were first added to the language back in the days before standardization, they were seen as a brilliant improvement over the hideous <span class="inline_code">setjmp</span>/<span class="inline_code">longjmp</span> facility from ANSI C.  Because exceptions unwind the stack when they are thrown, they call destructors and clean up all of your untidy messes as they go.</p>
<table border="0" cellpadding="5" align="left">
<tr>
<td><center><img src="http://www.profcon.com/profcon/cargill/images/cargill.jpg">
<p/>Tom Cargill</center></td>
</tr>
</table>
<p>It didn't take long for somebody to notice that the emperor had no clothes. In 1994, Tom Cargill published a <a href="http://www.informit.com/content/images/020163371x/supplements/Exception_Handling_Article.html" class="newpage">prescient article</a> in the late <em>C++ Report</em>. Tom pointed out in detail just how difficult it was to write code that actually held up when exceptions were used. Scott Meyers flags this as one of his five <a href="http://www.artima.com/cppsource/top_cpp_publications.html" class="newpage">Most Important C++ Non-Book Publications...<em>Ever</em></a>. (Incidentally, Tom is responsible for a piece of wisdom that sounds like a an offhand quip: <a href="http://en.wikipedia.org/wiki/Ninety-ninety_rule" class="newpage">the ninety-ninety rule</a>. Any project manager who doesn't understand this rule all the way into his or her bones needs to quit. Now.)</p>
<p>The intervening years have not been kind to exceptions. The problems Tom foresaw in 1994 were real, and while they can be managed, writing exception-safe code is still fraught with peril. As a result, exceptions are basically unused by by most C++ programmers. After all, we have to deal with tough problems like writing safe multithreaded apps, understanding template metaprogramming, and dealing with the language's high-maintenance memory management. The last thing we need is to make use of a feature, that while useful, is basically impossible to use correctly.</p>
<p>Naturally, after this rather verbose introduction, you have probably guessed that I'm using this article to tell you about a simple little utility class I use to help me throw informative, easy to build exceptions in my C++ programs. Yes, exceptions are problematic, but there's one place I can use them with impunity: to cause a fatal error that aborts my program.<br />
<span id="more-110"></span></p>
<h4>Fatal Errors Considered Exceptional</h4>
<p>If you analyze Tom Cargill's article, or look into any of the additional work that has been done on exceptions up until today, you'll see that most of the nasty side effects are only a problem for a program that tries to keep running in a predictable and safe fashion while throwing and catching exceptions.</p>
<p>The side effects of code that isn't quite exception-safe include memory leaks, partially constructed objects, and invalid containers. All bad things. But the one place they usually don't matter to me is when things have degraded in my program to the point where I'm ready to throw in the towel anyway. That point is when I've encountered a fatal error and am aborting the program. At that point, I throw an exception, which generally percolates all the way up to <span class="inline_code">main()</span>, where it is caught, and error message is printed, and the program exits.</p>
<p>Handling fatal errors this way makes for much cleaner code. I generally assume that every method or function called in my program succeeds, and don't worry about creating special returns with error codes. I blithely march through call after call without checking results, secure in the knowledge that my code is working properly. I know this is the case, because if something went wrong, an exception would have been thrown, and my program would abort.</p>
<p>This error handling strategy is implemented entirely in <span class="inline_code">main()</span>, which generally follows this form in  one of my programs:</p>
<div class="igBar"><span id="lcpp-21"><a href="#" onclick="javascript:showPlainTxt('cpp-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-21">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span> <span class="kw4">int</span> argc, <span class="kw4">char</span> *argv<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// all the work is enclosed in this try block</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; ...</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">catch</span> <span class="br0">&#40;</span> <span class="kw4">const</span> std::<span class="me2">exception</span> &amp; e <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="kw3">cout</span> &lt;&lt;<span class="st0">"<span class="es0">\n</span>Fatal error: "</span> &lt;&lt;e.<span class="me1">what</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt;<span class="st0">"<span class="es0">\n</span><span class="es0">\n</span>"</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="kw3">cout</span> &lt;&lt;<span class="st0">"Hit enter to continue..."</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">string</span> temp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="me2">getline</span><span class="br0">&#40;</span> std::<span class="kw3">cin</span>, temp <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> -<span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
There are a couple of interesting points to note about the error handler you see here. First, because I am catching <span class="inline_code">std::exception</span>, I will catch any errors in the standard library, such as <span class="inline_code">bad_alloc</span>, <span class="inline_code">logic_error</span>, <span class="inline_code">runtime_error</span>, etc. It's a good idea to have a top-level <span class="inline_code">try</span>/<span class="inline_code">catch</span> block for these items anyway, so my use of exceptions for fatal errors forces good hygiene practices.</p>
<p>Second, I'm using the <span class="inline_code">what()</span> method to give human-readable feedback on exactly what happened to send my program off the rails. This method is defined as <span class="inline_code">virtual</span> for the base class <span class="inline_code">std::exception</span>, so all derived classes will support it. There's no requirement that they populate this string with great prose, but we can at least expect implementors to provide something informative here.</p>
<h4>My Exception Class</h4>
<p>When I first started using exceptions for fatal errors, I kept things simple by just throwing instances of <span class="inline_code">std::runtime_error</span> when I wanted to abort my program. I could pass the constructor of this object a descriptive string as it was constructed, and that same string would be returned when <span class="inline_code">what()</span> was called in the exception handler. Typical usage might look like this:</p>
<div class="igBar"><span id="lcpp-22"><a href="#" onclick="javascript:showPlainTxt('cpp-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-22">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;stdexcept&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">int</span> main<span class="br0">&#40;</span> <span class="kw4">int</span> argc, <span class="kw4">char</span> *argv<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> argc &lt;<span class="nu0">2</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw std::<span class="me2">runtime_error</span><span class="br0">&#40;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">"Usage:<span class="es0">\n</span><span class="es0">\n</span>"</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">"add_link_error url<span class="es0">\n</span>"</span> <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This code ensures that if the user leaves off the necessary command line argument, the <span class="inline_code">catch()</span> block will print out a usage statement and return an error value to the invoking shell, which is just what we want. I manage to handle faulty input with just one line of code, don't have to set any error variables, no if/then/break clause to print an error and then exit. It's tidy and works well.</p>
<p>Of course, it wasn't long until I ran into situations where I wanted to provide the user a little more information with a fatal error - information that had to be formatted at runtime. I ended up writing a lot of code that looked like this:</p>
<div class="igBar"><span id="lcpp-23"><a href="#" onclick="javascript:showPlainTxt('cpp-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-23">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">FILE</span> *fp = <span class="kw3">fopen</span><span class="br0">&#40;</span> <span class="st0">"database.txt"</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">"r"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span> !fp <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; std::<span class="me2">stringstream</span> s;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; s &lt;&lt;<span class="st0">"Error opening file, errno value of "</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &lt;&lt;errno</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &lt;&lt;<span class="st0">" translates to "</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &lt;&lt;strerror<span class="br0">&#40;</span> <span class="kw2">errno</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; throw std::<span class="me2">runtime_error</span><span class="br0">&#40;</span> s.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This more or less worked, but it added a lot of lines to the code, and as a rule, the fewer lines, the less typing, the more I like it. In this case I have to construct a separate <span class="inline_code">std::stringstream</span> object to hold the formatted error message (or a character buffer if I choose to go old school and use <span class="inline_code">vnsprintf() </span>, a second line to do the formatting, and a third line to construct and throw the exception.</p>
<p>In addition to writing three lines instead of one, I now have to enclose the whole thing in brackets, because the clause following the if statement is multiple lines, which makes the whole thing require five lines  of code instead of one! </p>
<h4>A Better Way</h4>
<p>I needed to find a better way to do this. I first dabbled with a class derived from <span class="inline_code">std::exception</span> that used <span class="inline_code">vnsprintf()</span> to format arguments, C-style. But there were a few disadvantages to this, the primary one being that it made it hard to take advantage of classes that have their own overrides to stream classes. In other words, if I've bothered to create a stream override so I can print the contents of <span class="inline_code">class foo</span>, it's easy to write that to a stream, but not so easy to insert it into a buffer being formatted with <span class="inline_code">vnsprintf()</span>.</p>
<p>So I determined that I wanted to have a class that that let me rewrite the code shown above so that it all fits on one line, like this:</p>
<div class="igBar"><span id="lcpp-24"><a href="#" onclick="javascript:showPlainTxt('cpp-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-24">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw4">FILE</span> *fp = <span class="kw3">fopen</span><span class="br0">&#40;</span> <span class="st0">"database.txt"</span>, </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">"r"</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span> !fp <span class="br0">&#41;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; throw fatal_error<span class="br0">&#40;</span><span class="br0">&#41;</span> &lt;&lt;<span class="st0">"Error opening file, errno value of "</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;errno</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;<span class="st0">" translates to "</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;strerror<span class="br0">&#40;</span> error <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
<center><i>My Goal For Use of <span class="inline_code">class fatal_error</span></i></center><br />
My first stab at getting this to work was to create a class that used multiple inheritance to create a class that inherits from both <span class="inline_code">std::exception</span> and <span class="inline_code">std::stringstream</span>:</p>
<div class="igBar"><span id="lcpp-25"><a href="#" onclick="javascript:showPlainTxt('cpp-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-25">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">class fatal_error </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; : <span class="kw2">public</span> std::<span class="me2">exception</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; , <span class="kw2">public</span> std::<span class="me2">stringstream</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">... </div>
</li>
</ol>
</div>
</div>
</div>
<p>
With this routine I'd just need to override the definition of <span class="inline_code">what()</span> and I would be in business. </p>
<p>But there was a fatal flaw in this problem: the absence of a copy constructor for <span class="inline_code">std::stringstream</span>. I had assumed that the copy constructor wouldn't be needed, as I was catching the exception object by reference. My thought was that the compiler would create a temporary object, pass it along to my <span class="inline_code">catch</span> clause, and all would be well.</p>
<p>No such luck. In this case, the compiler has the right to make a copy, and even if it doesn't make a copy, it has the right to insist on a copy constructor even if it is only considering the <i>possibility</i> of making a copy. Strike one.</p>
<h4>This One Works</h4>
<p>So I needed to make a version of my <span class="inline_code">fatal_error</span> class that doesn't try to call the copy constructor for <span class="inline_code">std::stringstream</span>. This is accomplished easily enough by using <i>composition</i> instead of <i>inheritance</i>:</p>
<div class="igBar"><span id="lcpp-26"><a href="#" onclick="javascript:showPlainTxt('cpp-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-26">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">class fatal_error </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; : <span class="kw2">public</span> std::<span class="me2">exception</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">...</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">private</span> :</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; mutable std::<span class="me2">stringstream</span> mStream;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This solves one problem, but creates another. Because <span class="inline_code">fatal_error</span> is no longer derived from <span class="inline_code">std::stringstream</span>, I've lost the overloaded operators that insert text into the object before it is thrown. In other words, my wished-for code shown above won't compile.</p>
<p>In this case, the solution is simple, I just add a template method to the class:</p>
<div class="igBar"><span id="lcpp-27"><a href="#" onclick="javascript:showPlainTxt('cpp-27'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-27">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">class fatal_error</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; : <span class="kw2">public</span> std::<span class="me2">exception</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">public</span> :</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; template&lt;typename T&gt;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; fatal_error&amp; operator&lt;&lt;<span class="br0">&#40;</span> <span class="kw4">const</span> T&amp; t <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; mStream &lt;&lt;t;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> *<span class="kw3">this</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">...</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Now the template class takes care of routing the stream insertion output into the <span class="inline_code">std::stringstream</span> member I've incorporated into my class. Note also that the overloaded insertion operator returns a reference to the <span class="inline_code">fatal_error</span> object, allowing me to chain insertions.</p>
<h4>Almost Done</h4>
<p>There only two more issues to deal with in order for this class to be ready for use. First, I need to deal with the possibility of a copy constructor being called as this exception is thrown. Because I can't copy the <span class="inline_code">std::stringstream</span> object from the old object to the new, I have to save off its contents into a mutable <span class="inline_code">std::string</span> member called mWhat. The result looks like this:</p>
<div class="igBar"><span id="lcpp-28"><a href="#" onclick="javascript:showPlainTxt('cpp-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-28">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">fatal_error::<span class="me2">fatal_error</span><span class="br0">&#40;</span> <span class="kw4">const</span> fatal_error &amp;that <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; mWhat += that.<span class="me1">mStream</span>.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Finally, I need a good version of <span class="inline_code">what()</span> that conforms to what is expected for <span class="inline_code">std::exception</span>. This is nice and easy:</p>
<div class="igBar"><span id="lcpp-29"><a href="#" onclick="javascript:showPlainTxt('cpp-29'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-29">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">virtual</span> <span class="kw4">const</span> <span class="kw4">char</span> *fatalError::<span class="me2">what</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> throw<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> mStream.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; mWhat += mStream.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; mStream.<span class="me1">str</span><span class="br0">&#40;</span> <span class="st0">""</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> mWhat.<span class="me1">c_str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h4>Wrapped Up</h4>
<p>With that, I have an exception that I can use to easily generate fatal error exceptions with as much data as I want. I only have to include a single header file in any code that uses the class, and I can create and throw the exception in a single line of code, which adheres to the C ideologoy of minimal typing.</p>
<p>Life is good.</p>
<p>Complete source code from the single file, <code>fatal_error.h</code>, is given below. This code should work in g++ 3.x code, and Visual Studio 2003 and 2005 programs. If you run into problems with different versions of various compilers, please let me know!</p>
<hr/>
<div class="igBar"><span id="lcpp-30"><a href="#" onclick="javascript:showPlainTxt('cpp-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-30">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// fatal_error.h</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include &lt;sstream&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">#include &lt;stdexcept&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co2">#include &lt;string&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// This class is designed to make it a little easier</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// to throw informative exceptions. It's a little lame,</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// but I do like to be able to write code like this</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// for fatal errors:</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// throw fatal_error() &lt;&lt;&quot;Game over, &quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;mHealth</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;&quot; health points!&quot;;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">// It works everywhere I've tested it, let's hope that it holds up.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class fatal_error : <span class="kw2">public</span> std::<span class="me2">exception</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> :</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; fatal_error<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// Need a copy constructor, because the runtime of the compiler is</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// allowed to insist on it when it throws an object of this type,</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// even if it doesn't actually make a copy. When I make a copy, I</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// need to capture whatever is in the stringstream object. Note:</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// in many cases, attempting to copy an iostream object leads to</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// errors, so the copy constructor here constructs a brand new</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// mstream object.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; fatal_error<span class="br0">&#40;</span> <span class="kw4">const</span> fatal_error &amp;that <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; mWhat += that.<span class="me1">mStream</span>.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// Virtual dtor needed? Not really, but here it is anyway.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">virtual</span> ~fatal_error<span class="br0">&#40;</span><span class="br0">&#41;</span> throw<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// When I finally get this object to an exception handler, </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// (hopefully catching by reference) I want to display the error</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// message that I've inserted. To do that, I just chapture</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// whatever is in the mWhat string object concatenated</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// with anything that might be in the stringstring mStream object,</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// and return it. (Odds are that only one of them will contain</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// anything, depending on whether or not the copy constructor </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// was called.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">virtual</span> <span class="kw4">const</span> <span class="kw4">char</span> *what<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span> throw<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> mStream.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mWhat += mStream.<span class="me1">str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mStream.<span class="me1">str</span><span class="br0">&#40;</span> <span class="st0">""</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> mWhat.<span class="me1">c_str</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// The template function used to create insertion operators for all</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">// of the various types of objects one might insert into this guy.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; template&lt;typename T&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; fatal_error&amp; operator&lt;&lt;<span class="br0">&#40;</span> <span class="kw4">const</span> T&amp; t <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; mStream &lt;&lt;t;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> *<span class="kw3">this</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">private</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;mutable std::<span class="me2">stringstream</span> mStream;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;mutable std::<span class="me2">string</span> mWhat;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2007/11/13/no-exceptions/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Yet Another Word Puzzle</title>
		<link>http://marknelson.us/2007/11/13/yet-another-word-puzzle/</link>
		<comments>http://marknelson.us/2007/11/13/yet-another-word-puzzle/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 20:58:45 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Articles]]></category>

		<guid isPermaLink="false">http://marknelson.us/2007/11/18/yet-another-word-puzzle/</guid>
		<description><![CDATA[















<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/11/13/yet-another-word-puzzle/' addthis:title='Yet Another Word Puzzle' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Dr. Dobb's PortalNovember, 2007 Article on DDJ site As I've confessed in the past, I'm a sucker for word puzzles. My recent post on a Will Shortz puzzle from NPR Morning Edition ended up provoking a surprising amount of comment, much of it in the vein of Watch me solve it better, faster, and with [...]]]></description>
			<content:encoded><![CDATA[







<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/11/13/yet-another-word-puzzle/' addthis:title='Yet Another Word Puzzle' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0" width="500">
<tr>
<td width="300"><img alt="DDJ Portal Logo" src="http://marknelson.us/attachments/misc/logo_ddj.gif" /></td>
<td><strong>Dr. Dobb's Portal</strong><br/>November, 2007<br />
         <a href="http://ddj.com/cpp/202806370" class="newpage">Article on DDJ site</a>
  </td>
</tr>
</table>
<p>As I've confessed in the past, I'm a sucker for word puzzles. My <a href="http://marknelson.us/2007/04/01/puzzling/" class="newpage">recent post</a> on a Will Shortz puzzle from NPR Morning Edition ended up provoking a surprising amount of <a href="http://thraxil.org/users/anders/posts/2007/10/30/A-Simple-Programming-Puzzle-Seen-Through-Three-Different-Lenses/" class="newpage">comment</a>, much of it in the vein of <i>Watch me solve it better, faster, and with more style using language XXX</i>.</p>
<p>I certainly enjoyed watching other people solve the problem, and found their solutions instructive. As the XP crowd has figured out, we programmers spend too much time working on our own problems and not enough time watching how other people work. There's a lot to learn, both good and bad, from getting a peek inside another person's head.</p>
<h4>Out of the Blue</h4>
<p>Which brings me to the puzzle at the center of this article.<br />
<span id="more-106"></span></p>
<table border="0" align="left">
<tr>
<td><center><img src="http://marknelson.us/attachments/2007/yet-another-word-puzzle/BethKatz.jpg" hspace="5"><br/>Beth Katz</center></td>
</tr>
</table>
<p>In what at first seemed to be an incident completely unrelated to word play, I had a pleasant email exchange with <a href="http://cs.millersville.edu/~katz/" class="newpage">Beth Katz</a>, who was teaching a Data Structures class at <a href="http://cs.millersville.edu/" class="newpage">Millersville University</a>. I happened to look at Beth's current homework assignment for her class, and you can imagine my reaction when I saw the problem she had posted for her class:</p>
<blockquote><p>We define word reduction as removing a single letter from a word while leaving the remaining letters in their original order so that the resulting sequence of characters is also a word. A good word can be reduced step-by-step until all that is left is <em>a</em> or <em>i</em>. Your program will answer the question: what is the biggest word in the given dictionary that can be reduced?</p></blockquote>
<p>Beth gave a short example of a good word: <i>planets</i>:</p>
<pre>
planets
plants
pants
pant
ant
an
a
</pre>
<p>As you can see, you can remove one letter at a time, and each time you are left with a valid word one character shorter. </p>
<p>This makes for an interesting problem indeed. I've read that the average English speaker has a vocabulary of perhaps 15,000 to 20,000 words, but many reasonable word lists have upwards of 100,000 English words. How many of these words qualify as <em>good </em>words? </p>
<p>As I discussed in the previous word puzzle article, the highly evolved pattern matching facility in the human mind is often pretty good at solving these problems, and I think this is the case (in a limited way) for this particular problem. If I give you a word (like <i>planets</i>) above, I think you'd be able to find a possible reduction path quickly, subject to the limitations of your own vocabulary.</p>
<p>But the human mind is not so good at certain variations on the same problems. Asking you for the biggest word that fits this pattern presents you with an almost impossible task. Basically, it requires you to be able to iterate through the words you know, ordered by length, and test each one. Unless you are subject to some pretty incredible flashes of insight, I think you're going to need a computer for this.</p>
<h4>Going Bottom Up</h4>
<p>Maybe the feeling isn't universal among programmers, but when I look at a problem, my first instinct is usually to try a top-down approach. For this problem, a top-down approach would mean identifying the longest words in the dictionary, then attempting to decompose them into successively shorter words.</p>
<p>This approach will work, but a little mental analysis shows that it might be a little resource heavy. Imagine that you are decomposing a 10 letter word by taking away one letter at a time. In the worst case, you might find all 9 shorter words in the dictionary, and then you could find all 720 8 letter words, and so on. Although in the general case you might only find one or two matches, particularly at the long lengths, even the potential for factorial growth leaves some room for concern.</p>
<p>So I took a shot at a bottom-up approach instead. It isn't usually my first choice, but I think you'll see that in this case it yields a much more satisfactory and efficient solution to the problem.</p>
<h5>The Inner Loop</h4>
<p>For this program to succeed, it must terminate its processing with a container that holds the longest good words in the dictionary. For this particular problem, my choice of container is the C++ <span class="inline_code">hash_set</span>, which is non-standard but universally implemented.</p>
<p>My bottom-up approach means that I will fill in the <span class="inline_code">hash_set</span> container for words of length 1 first, then words of length 2, and so on. For reasons of efficiency, it works out better if I keep a separate <span class="inline_code">hash_set</span> for each word length, so the <span class="inline_code">hash_set</span> objects are actually stored in a <span class="inline_code">map </span>that is indexed on the word size:</p>
<p><span class="inline_code">std::map&lt;size_t,hash_set&lt;std::string&gt; &gt; good_words</span></p>
<p>To fill in the hash for size <strong>i</strong>, I need a loop that iterates over all words of size <b>i</b>, removing one character at a time and then testing to see if the result is a good word of size <b>i-1</b>. If it is, I add it to <span class="inline_code">good_words[ i ]</span>. When I'm done iterating over all words of that size, I have a <span class="inline_code">hash_set</span> that contains all good words of that size, and I can move up to the next larger size.</p>
<p>So if we're testing words of size <b>i</b> the innermost part of the loop will look like this:</p>
<div class="igBar"><span id="lcpp-39"><a href="#" onclick="javascript:showPlainTxt('cpp-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-39">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">size_t</span> j = <span class="nu0">0</span> ; j &lt;i ; j++ <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; string test_word = word;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; test_word.<span class="me1">erase</span><span class="br0">&#40;</span> j, <span class="nu0">1</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> good_words<span class="br0">&#91;</span> i-<span class="nu0">1</span> <span class="br0">&#93;</span>.<span class="me1">find</span><span class="br0">&#40;</span> test_word <span class="br0">&#41;</span> != fail <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; good_words<span class="br0">&#91;</span> i <span class="br0">&#93;</span>.<span class="me1">insert</span><span class="br0">&#40;</span> word <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
In this inner part of the loop, we repeatedly remove a character from the word, testing to see if the resulting word appears in the list of words saved in the previous size hash set. If a match occurs, the word is inserted into the hash set for the current word size, and the loop breaks.</p>
<p>This bottom-up approach seems like the ideal way to build my lists of good words for a couple of reasons. First, I don't go to the expense of adding words to the hash sets unless they are good words. Second, determining that a given word is a good word only requires a test against the words at level i-1; I don't have to test for a complete path down to 'a' or 'i'.</p>
<h5>Building The Input Data</h4>
<p>In the previous section I mentioned that the innermost loop was going to be called as I iterated over all the dictionary words of a given size. So how do I get all the words of a given size?</p>
<p>The first thing that might occur to you is that you could read in all the words from the dictionary, then sort them by size. This approach would work, but given that the standard sort routines available to you in the C++ library are all going to work in O(n&middot;lgn) time, it might get kind of expensive as the dictionary grows to hundreds of thousands of words.</p>
<p>The good news is that with this data set we're in position to take advantage of a linear sort. Yes, we can sort data and do substantially better than O(n&middot;lgn) when we know that the  data to be sorted is constrained to a small set of values.</p>
<p>In this case, I just create one linked list for each word size, and as I read the input file, I add each input word to the front of the appropriate linked list. This would be a true linear algorithm if I constrained the input size to a fixed number, say 25, but for convenience I actually store the lists in a map that looks like this:</p>
<p><span class="inline_code">std::map&lt;size_t,std::list&lt;std::string&gt; &gt; words_by_length</span></p>
<p>As a result the input code runs in close to linear time. The actual loop that reads in the data is nice and simple:</p>
<div class="igBar"><span id="lcpp-40"><a href="#" onclick="javascript:showPlainTxt('cpp-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-40">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">while</span> <span class="br0">&#40;</span> input <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; string word;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; input&gt;&gt; word;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; words_by_length<span class="br0">&#91;</span> word.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#93;</span>.<span class="me1">push_back</span><span class="br0">&#40;</span> word <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; count++;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span> count % <span class="nu0">100</span> <span class="br0">&#41;</span> == <span class="nu0">0</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">cout</span> &lt;&lt;count &lt;&lt;<span class="st0">"<span class="es0">\r</span>"</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Once all the words are read in, I can access the list of words of a given size with a simple map lookup: <span class="inline_code">words_by_length[ i ]</span>.</p>
<h4>Word Lists</h4>
<p>One final detail before I can compile my library - I need some lists of words! Lists of words are not hard to come by, although coming up with a suitable one for this exercise might take some work.</p>
<p>One of the first places to look is is on your local Linux system. My system has a list of words in <code>/usr/share/dict/words</code>, which is used by the spell checker application, and possibly by the password app. One one of my systems, this dictionary has a whopping 483,524 words, which means it is packed with obscure words. Just as an example, a typical 12-character good word and its derivation found using <code>/usr/share/dict/words</code> yields this head-scratching sequence:</p>
<pre>
abranchiate
branchiate
branchiae
branchia
branchi
branch
ranch
rach
ach
ch
h
</pre>
<p>Probably the first thing you want to do with that file is go through and remove all the single letter words except 'a' and 'i', but even so, you're going to be boggled by some of what you see.</p>
<p>Another good alternative are the collection of word lists distributed on Project Gutenberg as the <a href="http://www.gutenberg.org/etext/3201" class="newpage">Moby Word List</a>. This includes a wide variety of lists of various sizes.</p>
<p>Beth Katz had <a href="http://cs.millersville.edu/~katz/cs362/examples/dictionaries/" class="newpage">several good dictionaries</a> listed along with her homework assignment, including a short one called <a href="http://cs.millersville.edu/~katz/cs362/examples/dictionaries/kids.dict" class="newpage">kids.dict</a> that is nice and short, making it good for debugging runs.</p>
<p>Finally, Google searches for "word lists" will turn up many other good choices.</p>
<h4>Wrapping it up</h4>
<p>Once I had the bottom-up good word builder working with the file-based word list, I was ready to put it all together. The core of <span class="inline_code">main()</span> now looks like this:</p>
<div class="igBar"><span id="lcpp-41"><a href="#" onclick="javascript:showPlainTxt('cpp-41'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-41">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">map&lt;size_t,list&lt;string&gt;&gt; words_by_length;</div>
</li>
<li class="li2">
<div class="de2">read_words<span class="br0">&#40;</span> argv<span class="br0">&#91;</span> <span class="nu0">1</span> <span class="br0">&#93;</span>, words_by_length <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">map&lt;size_t,hash_set&lt;string&gt;&gt; good_words;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw4">size_t</span> longest = build_up_words<span class="br0">&#40;</span> words_by_length,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;good_words <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">print_good_words<span class="br0">&#40;</span> longest,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; words_by_length,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; good_words <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Procedure <span class="inline_code">read_words()</span> simply reads all the words from the file into the map of <span class="inline_code">hash_set</span> containers called <span class="inline_code">word_by_length</span>, as described earlier.</p>
<p>Then <span class="inline_code">build_up_words</span> collects all the good words and stores them, organized by size, in the <span class="inline_code">map </span>of <span class="inline_code">hash_set</span> containers called <span class="inline_code">good_words</span>. The core of that routine was described earlier.</p>
<p>I show the results of the top two levels in <span class="inline_code">print_good_words()</span>, which is simple enough to read up on in the source code.</p>
<h4>The Results</h4>
<p>By now you are no doubt dying to see some results from the program. But first, to scope the size of the problem, here's the output from the program as the <span class="inline_code">good_words</span> hashes are populated on a run against <code>SINGLE.TXT</code>, a 354,985 word database:</p>
<pre>
Loading words from: SINGLE.TXT
Loaded 354985 words from SINGLE.TXT
Found 431 eligible words out of 431 total at length 2
Found 2075 eligible words out of 2092 total at length 3
Found 6213 eligible words out of 6758 total at length 4
Found 11322 eligible words out of 15047 total at length 5
Found 12495 eligible words out of 28473 total at length 6
Found 8939 eligible words out of 40094 total at length 7
Found 4295 eligible words out of 49528 total at length 8
Found 1210 eligible words out of 51216 total at length 9
Found 174 eligible words out of 43964 total at length 10
Found 20 eligible words out of 36082 total at length 11
Found 0 eligible words out of 28009 total at length 12
</pre>
<p>So that means about 13% of the words in this vocabulary were good words. I'm a little surprised that it's that high. To add some sanity to the mix, I removed all the single character words from <code>SINGLE.TXT</code> with the exception of 'a' and 'i', and the ratio went down to a more reasonable 8%.</p>
<p>You can also see, as you would expect, that the proportion of good words goes down at each level. At lengths 2, 3, and 4 nearly all words are good words, but by the time we get to length 11, we're down to less than one-tenth of one percent good.</p>
<p>Even with my modified version of <code>SINGLE.TXT</code>, you're bound to get plenty of esoteric words when working your way through the derivation of an 11 or 10 character good word. Of the 18 words of eleven characters, the derivation that works best with my vocabulary would be the following:</p>
<pre>
sparklingly
sparkingly
sparingly
springly
springy
spring
sprig
prig
pig
pi
i
</pre>
<p>With the more manageable scrabble.dict dictionary, containing 79,340 words, some of the first sequences that pop out include:</p>
<pre>
shopping hopping hoping oping ping pig pi i
breaches beaches baches aches aces ace ae a
marchese marches arches aches aces ace ae a
prawning pawning awning awing wing win in i
stablest stalest stales tales ales als as a
bravoing braving raving ravin rain ain in i
failings filings flings lings lins ins is i
relaters elaters elates elate late ate ae a
roadster roaster raster rater rate ate ae a
semioses semises seises seise seis sis is i
clambers lambers lamber lamer lame lam am a
claviers clavers lavers avers aves ave ae a
shrieves shrives shives hives hies his is i
stalkier talkier talker taker take tae ae a
statutes statues states tates ates ate ae a
swarming warming waring wring ring rin in i
brambled rambled ambled amble able ale ae a
stratous stratus status stats tats tas as a
paddlers paddles padles pales ales als as a
thirling tirling tiring iring ring rin in i
trucking trucing truing ruing ring rin in i
brawlier brawler bawler baler bale ale ae a
frilling filling filing fling ling lin in i
carouses arouses arouse arose arse are ae a
</pre>
<p>No doubt there are still plenty of obscure words here, but remember, this dictionary is probably composed of at least 50% words that aren't in your working vocabulary.</p>
<h4>Efficiency</h4>
<p>When run against a word list with 350K+ entries on my anemic notebook computer, it takes almost 10 seconds for the program to terminate, including display time. The vast majority of that time is spent checking words for goodness, which requires removing characters one at a time, then checking to see if their small descendants are in the word list.</p>
<p>Obviously, if you want to optimize this program for better performance, that's the place to do it.  My guess would be that the std::string class member to erase characters from a word is probably far from optimal, and could be replaced by a hand-coded routine designed to do the same task with much greater speed.</p>
<h4>Issues With Non-Standard Library Functions</h4>
<p>Because hashed containers did not make it into the original C++ standard, there is a somewhat higher level of peril when using them. Problems ranging from syntactic inconsistency to lack of performance guarantees definitely make <span class="inline_code">hash_set</span> and <span class="inline_code">hash_map</span> second class citizens compared to the other standard containers. I saw a good example of this when I first started work on this article.</p>
<p>When solving this problem, the first thing that seemed obvious to me was that we were going to be storing references to dictionary words in hash tables. And I thought it might be interesting to see how well the C++ hash classes were going to be able to handle input data with hundreds of thousands of words.</p>
<p>I thought a good test program would be one that simply reads in the text file and adds it to a hash set:</p>
<div class="igBar"><span id="lcpp-42"><a href="#" onclick="javascript:showPlainTxt('cpp-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-42">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">hash_set&lt;std::<span class="me2">string</span>&gt; words;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">while</span> <span class="br0">&#40;</span> input <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; std::<span class="me2">string</span> word;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; input&gt;&gt; word;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; words.<span class="me1">insert</span><span class="br0">&#40;</span> word <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; count++;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span> count % <span class="nu0">100</span> <span class="br0">&#41;</span> == <span class="nu0">0</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; std::<span class="kw3">cout</span> &lt;&lt;count &lt;&lt;<span class="st0">"<span class="es0">\r</span>"</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
Under Visual C++ .NET 2005 on a fairly slow laptop, I immediately saw a nasty problem with the Microsoft implementation of <span class="inline_code">hash_set<T></span>. Every time the counter hit a an even power of two, there was a bit of a pause. The pause grew longer and longer as the count grew larger, until by the time I was up to 128K words, it stretched out to many seconds.</p>
<p>Lesson learned. Hash table resizing can be expensive under some implementations of this non-standard class. Just resizing hash tables in any implementation can be difficult, but the additional requirements imposed on C++ library containers adds significantly to the work that must be done at this point. </p>
<p>I hoped that I would find a <span class="inline_code">reserve()</span> method or a constructor option that would let me preallocate a <span class="inline_code">hash_set</span> with perhaps 200K buckets, but this doesn't seem to be possible with Microsoft's implementation. The good news is that the <span class="inline_code">hash_set</span> replacement in <a href="http://en.wikipedia.org/wiki/Technical_Report_1" class="newpage">TR1</a>, <span class="inline_code">unordered_set</span>, will impose a requirement that conforming libraries allow for a bucket count as part of the container's constructor.</p>
<p>It turned out to not be too important, however. As I worked on the implementation of the algorithm, I drastically reduced the number of strings that were stored in any one hash, making this a moot point.</p>
<h4>Source Code</h4>
<p>You can download the <a href="http://marknelson.us/attachments/2007/yet-another-word-puzzle/Perfect.zip" class="newpage">project</a> here, including project files for Visual Studio 2003 and 2005, and a simple Makefile for gcc. The code has been checked under gcc 3.4, but I make no claims that it will work with all later versions of the library.</p>
<h4>References</h4>
<p>Ward, Grady. "Moby Word Lists by Grady Ward - Project Gutenberg." Main Page - Gutenberg. 12 Nov. 2007 <a href="http://www.gutenberg.org/etext/3201" class="newpage">http://www.gutenberg.org/etext/3201</a>.<br />
"ISO/IEC JTC1/SC22/WG21 - The C++ Standards Committee." Open Standards. 13 Nov. 2007 <a href="http://www.open-std.org/jtc1/sc22/wg21/" class="newpage">http://www.open-std.org/jtc1/sc22/wg21/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2007/11/13/yet-another-word-puzzle/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Byzantine Generals Problem</title>
		<link>http://marknelson.us/2007/07/23/byzantine/</link>
		<comments>http://marknelson.us/2007/07/23/byzantine/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 12:11:48 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Magazine Articles]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">/2006/08/13/byzantine/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/07/23/byzantine/' addthis:title='The Byzantine Generals Problem' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>This article presents the algorithm that solves the Byzantine General’s Problem, as first described by Lamport, Pease, and Shostak in 1982 <a href="http://research.microsoft.com/users/lamport/pubs/byz.pdf" class="newpage">[1]</a>. While Lamport’s algorithm is not particularly complex, programmers who aren’t used to working on distributed computation might find it difficult to implement. To accompany the explanation of the algorithm, I have included a C++ program designed for experimentation with the solution.]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/07/23/byzantine/' addthis:title='The Byzantine Generals Problem' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><p>This article presents the algorithm that solves the Byzantine General’s Problem, as first described by Lamport, Pease, and Shostak in 1982 <a href="http://research.microsoft.com/users/lamport/pubs/byz.pdf" class="newpage">[1]</a>. While Lamport’s algorithm is not particularly complex, programmers who aren’t used to working on distributed computation might find it difficult to implement. To accompany the explanation of the algorithm, I have included a C++ program designed for experimentation with the solution.</p>
<h4>Introduction</h4>
<p>The Byzantine General’s Problem is one of many in the field of agreement protocols. In 1982, Leslie Lamport described this problem in a paper written with Marshall Pease and Robert Shostak. Lamport framed his paper around a story problem after observing what he felt was an inordinate amount of attention received by Dijkstra’s Dining Philosophers problem <a href="http://research.microsoft.com/users/lamport/pubs/pubs.html#byz" class="newpage">[2]</a>.</p>
<p>This problem is built around an imaginary General who makes a decision to attack or retreat, and must communicate the decision to his lieutenants. A given number of these actors are traitors (possibly including the General.) Traitors cannot be relied upon to properly communicate orders; worse yet, they may actively alter messages in an attempt to subvert the process.</p>
<p>When we’re not dwelling in storybook land, the generals are collectively known as <i>processes</i>, the general who initiates the order is the <i>source process</i>, and the orders sent to the other processes are <i>messages</i>. Traitorous generals and lieutenants are <i>faulty processes</i>, and loyal generals and lieutenants are <i>correct processes</i>. The order to retreat or attack is a message with a single bit of information: a one or a zero.</p>
<p>In general, a solution to an agreement problem must pass three tests: <i>termination</i>, <i>agreement</i>, and <i>validity</i>. As applied to the Byzantine General’s problem, these three tests are:</p>
<ol>
<li>A solution has to guarantee that all correct processes eventually reach a decision regarding the value of the order they have been given.</li>
<li>
All correct processes have to decide on the same value of the order they have been given.</li>
<li>If the source process is a correct process, all processes have to decide on the value that was original given by the source process.</li>
</ol>
<p>Note that one interesting side effect of this is that if the source process is faulty, all other processes still have to agree on the same value. It doesn’t matter what value they agree on, they simply all have to agree. So if the General is subversive, all lieutenants still have to come to a common, unanimous decision.<br />
<span id="more-86"></span></p>
<h4>Difficulties</h4>
<p>This agreement problem doesn’t lend itself to an easy naïve solution.  Imagine, for example, that the source process is the only faulty process. It tells half the processes that the value of their order is zero, and the other half that their value is one.</p>
<p>After receiving the order from the source process, the remaining processes have to agree on a value that they will all decide on. The processes could quickly poll one another to see what value they received from the source process.</p>
<p>In this scenario, imagine the decision algorithm of a process which receives an initial message of zero from the source process, but sees that one of the other processes says that the correct value is one. Given the conflict, the process knows that either the source process is faulty, having given different values to two different peers, or the peer is faulty, and is lying about the value it received from the source process. </p>
<p>It’s fine to reach the conclusion that someone is lying, but making a final decision on who is the traitor seems to be an insurmountable problem. And in fact it can be proven that it is impossible to decide in some cases. The classic example used to show this is when there are only three processes: one source process and two peer processes. </p>
<p>In the two configurations shown in Figure 1 and Figure 2, the peer processes attempt to reach consensus by sending each other their proposed value after receiving it from the source process. In Figure 1, the source process (P<sub>1</sub>) is faulty, sending two different values to the peers. In Figure 2, P<sub>3</sub> is faulty, sending an incorrect value to the peer.</p>
<p>You can see the difficulty P<sub>2</sub> faces in this situation. Regardless of which configuration it is in, the incoming data is the same. He has no way to distinguish between the two configurations, and no way to know which of the two other processes to trust.<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure1.gif"><br />
Figure 1<br />
The case in which the source process is faulty</p>
<p><img src="http://marknelson.us/attachments/2007/byzantine/Figure2.gif"><br />
Figure 2<br />
The case in which P<sub>3</sub> is faulty<br />
</center><br />
This situation doesn’t necessarily get better just by throwing more non-faulty processes at the problem. A naïve algorithm as shown in Figure 1 and Figure 2 might have each process tell every other process what it received from P<sub>1</sub>. A process would then decide on the correct value by taking a simple majority of the values in its incoming messages.</p>
<p>Under the rules of this approach, it is easy to show that regardless of how many processes are in the system, a subversive source process with one collaborator can cause half the processes to choose to attack, while half the processes elect to retreat, leading to maximum confusion. </p>
<h4>The Lamport, Pease and Shostak Algorithm</h4>
<p>In 1982, Lamport, Pease, and Shostak published a fairly simple solution to this problem. The algorithm assumes that there are <i>n</i> processes, with <i>m</i> faulty processes, where <i>n > 3m</i>. Thus, for a scenario such as that in Figure 1 and 2 with 1 faulty process, there would have to be a minimum of 4 processes in the system to come to agreement. (For the rest of this article, <i>n</i> will always refer to the count of processes, and <i>m</i> will always refer to the number of faulty processes.)</p>
<p>The definition of the algorithm in the original paper is short and succinct, but at least in my experience, is somewhat confusing for programmers without a lot of experience in distributed algorithms.</p>
<p>Lamport’s algorithm is a recursive definition, with a base case for <i>m=0</i>, and a recursive step for m <i>> 0</i>:</p>
<p><center></p>
<table border="0">
<tr>
<td width="10%">&nbsp;</td>
<td>
<strong>Algorithm OM(0)</strong></p>
<ol>
<li>The general sends his value to every lieutenant.</li>
<li>Each lieutenant uses the value he receives from the general.</li>
</ol>
<p><strong>Algorithm OM(m), m > 0</strong></p>
<ol>
<li>The general sends his value to each lieutenant.</li>
<li>For each i, let v<sub>i</sub> be the value lieutenant i receives from the general. Lieutenant i acts as the general in Algorithm OM(m-1) to send the value v<sub>i</sub> to each of the n-2 other lieutenants.</li>
<li>For each i, and each j ≠ i, let v<sub>i</sub> be the value lieutenant i received from lieutenant j in step 2 (using Algorithm (m-1)). Lieutenant i uses the value majority (v<sub>1</sub>, v<sub>2</sub>, … v<sub>n</sub>).</li>
</ol>
</td>
</tr>
</table>
<p>Lamport’s Algorithm Definition<br />
</center><br />
To most programmers, this is going to look like a conventional recursive function definition, but it doesn’t quite fit into the mold you learned when studying the example of <i>factorial( n )</i>. </p>
<p>Lamport’s algorithm actually works in two stages. In the first step, the processes iterate through <i>m+1</i> rounds of sending and receiving messages. In the second stage of the algorithm, each process takes all the information it has been given and uses it to come up with its decision.</p>
<p>I found this to be non-obvious without quite a bit of study, which is the reason for this article.</p>
<h4>The First Stage</h4>
<p>The first stage of the algorithm is simply one of data gathering. The algorithm defines <i>m+1</i> rounds of messaging between all the processes. </p>
<p>In round 0, the General sends the order to all of its lieutenants. Having completed his work, the General now retires and stands by idly waiting for the remaining work to complete. Nobody sends any additional messages to the General, and the General won’t send any more messages.</p>
<p>In each of the remaining rounds, each lieutenant composes a batch of messages, each of which is a tuple containing a value and a path. The value is simply a 1 or a 0. The path is a string of process ids, &lt;ID<sub>1</sub>, ID<sub>2</sub>, …, ID<sub>n</sub>>. What the path means in this context is that in Round N,  PID<sub>N</sub> is saying that was told in round N-1 that P<sub>IDN-1</sub> was told by… P<sub>ID1</sub> that the command value was <i>v</i>. (This is very much like the classic party game in which a message is whispered from ear to ear through a chain of players, becoming slightly mangled along the way.) No path can contain a cycle. In other words, if ID1 is 1, no other ID in the string of process IDs will be a 1.</p>
<p>The message definition is easy in round 1. Each process broadcasts a message to all the other processes, including itself, but excluding the General, with the value it received from the General and its own process ID. </p>
<p>In subsequent rounds, things get more complicated. Each process takes all the messages it received from the previous round, appends its process ID where allowed, and sends those messages to all other processes, including itself. (The "where allowed"  just means that the process skips any messages where adding its process ID to the list would create a cycle in the string of process IDs.)</p>
<p>For example, let’s suppose that in Round 0 that P<sub>1</sub>, a faulty general told P<sub>2</sub>, P<sub>3</sub>, and P<sub>4</sub> that the command value was 0, and told P<sub>5</sub>, P<sub>6</sub>, and P<sub>7</sub> that the command value was 1. In round 1, the following messages would be sent:<br />
<center></p>
<table border="1" cellpadding="3">
<tr>
<td colspan="2">Sender=P<sub>2</sub></td>
<td colspan="2">Sender=P<sub>3</sub></td>
<td colspan="2">Sender=P<sub>4</sub></td>
<td colspan="2">Sender=P<sub>5</sub></td>
<td colspan="2">Sender=P<sub>6</sub></td>
<td colspan="2">Sender=P<sub>7</sub></td>
</tr>
<tr>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
<td>Dest</td>
<td>Msg</td>
</tr>
<tr>
<td>P<sub>2</sub></td>
<td>{0,12}</td>
<td>P<sub>2</sub></td>
<td>{0,13}</td>
<td>P<sub>2</sub></td>
<td>{0,14}</td>
<td>P<sub>2</sub></td>
<td>{1,15}</td>
<td>P<sub>2</sub></td>
<td>{1,16}</td>
<td>P<sub>2</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>3</sub></td>
<td>{0,12}</td>
<td>P<sub>3</sub></td>
<td>{0,13}</td>
<td>P<sub>3</sub></td>
<td>{0,14}</td>
<td>P<sub>3</sub></td>
<td>{1,15}</td>
<td>P<sub>3</sub></td>
<td>{1,16}</td>
<td>P<sub>3</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>4</sub></td>
<td>{0,12}</td>
<td>P<sub>4</sub></td>
<td>{0,13}</td>
<td>P<sub>4</sub></td>
<td>{0,14}</td>
<td>P<sub>4</sub></td>
<td>{1,15}</td>
<td>P<sub>4</sub></td>
<td>{1,16}</td>
<td>P<sub>4</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>5</sub></td>
<td>{0,12}</td>
<td>P<sub>5</sub></td>
<td>{0,13}</td>
<td>P<sub>5</sub></td>
<td>{0,14}</td>
<td>P<sub>5</sub></td>
<td>{1,15}</td>
<td>P<sub>5</sub></td>
<td>{1,16}</td>
<td>P<sub>5</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>6</sub></td>
<td>{0,12}</td>
<td>P<sub>6</sub></td>
<td>{0,13}</td>
<td>P<sub>6</sub></td>
<td>{0,14}</td>
<td>P<sub>6</sub></td>
<td>{1,15}</td>
<td>P<sub>6</sub></td>
<td>{1,16}</td>
<td>P<sub>6</sub></td>
<td>{1,17}</td>
</tr>
<tr>
<td>P<sub>7</sub></td>
<td>{0,12}</td>
<td>P<sub>7</sub></td>
<td>{0,13}</td>
<td>P<sub>7</sub></td>
<td>{0,14}</td>
<td>P<sub>7</sub></td>
<td>{1,15}</td>
<td>P<sub>7</sub></td>
<td>{1,16}</td>
<td>P<sub>7</sub></td>
<td>{1,17}</td>
</tr>
</table>
<p>Table 1<br />
Messages sent by all six lieutenant processes in round 1<br />
</center><br />
The number of messages goes up in in the second round. From the previous iteration, we know that each process now has six values that it received in the previous round – one message from each of the six other non-source processes – and it needs to send each of those messages to all of the other processes, which might mean each process would send 36 messages out.</p>
<p>In the previous table I showed the messages being sent to all six processes, which is fairly redundant, since the same messages are broadcast to all processes. For round 2, I’ll just show you the set of messages that each process sends to all of its neighbors.</p>
<p><center></p>
<table border="1" cellpadding="3">
<tr>
<td>Sender=P<sub>2</sub></td>
<td>Sender=P<sub>3</sub></td>
<td>Sender=P<sub>4</sub></td>
<td>Sender=P<sub>5</sub></td>
<td>Sender=P<sub>6</sub></td>
<td>Sender=P<sub>7</sub></td>
</tr>
<tr halign="center">
<td>{0,132}<br/>{0,142}<br/>{1,152}<br/>{1,162}<br/>{1,172}</td>
<td>{0,123}<br/>{0,143}<br/>{1,153}<br/>{1,163}<br/>{1,173}</td>
<td>{0,124}<br/>{0,134}<br/>{1,154}<br/>{1,164}<br/>{1,174}</td>
<td>{0,125}<br/>{0,135}<br/>{0,145}<br/>{1,165}<br/>{1,175}</td>
<td>{0,126}<br/>{0,136}<br/>{0,146}<br/>{1,156}<br/>{1,176}</td>
<td>{0,127}<br/>{0,137}<br/>{0,147}<br/>{1,157}<br/>{1,167}</td>
</tr>
</table>
<p>Table 2<br />
Messages sent by all six processes in round 2<br />
</center><br />
The six messages that P<sub>2</sub> received in round 1 were {0,12}, {0,13}, {0,14}, {1,15}, {1,16}, and {1,17}. According to the earlier definition, P<sub>2</sub> will append its process ID to the path and forward each resulting message to all other processes. The possible messages it could broadcast in round 2 are {0,122}, {0,132}, {0,142}, {1,152}, {1,162}, and {1,172}. The first message, {1,122} contains a cycle in the path value of the tuple, so it is tossed out, leaving five messages to be sent to all processes.</p>
<p>The first message that P<sub>2</sub> is sending in round 2, {0,132}, is equivalent to saying "P<sub>2</sub> is telling you that in round 1 P<sub>3</sub> told it that in round 0 that P<sub>1</sub> (the General) told it that the value was 0". The five messages shown in P<sub>2</sub>’s column in the table are sent to all six lieutenant processes, include itself.</p>
<p>It’s easy to see that as the number of processes increases, the number of messages being exchanged starts to go up rapidly. If there are N processes, each process sends N-1 messages in round 1, then (N-1)*(N-2) in round 2, (N-1)*(N-2)*(N-3) in round 3. That can add up to a lot of messages in a big system.</p>
<h4>The Second Stage</h4>
<p>While sending messages in each round, processes are also accumulating incoming messages. The messages are stored in a tree format, with each round of messages occupying one rank of the tree. Figure 3 shows the layout of the tree for a simple configuration with six processes, one of which can be faulty. Since m=1, there are just two rounds of messaging: the first, in which the general sends a value to each lieutenant process, and a second, in which each process broadcasts its value to all the other processes. Two rounds of messaging are equivalent to two ranks in the tree.</p>
<p>Each node in the tree has three elements: an input value, a path, and an output value. The input value and path are defined in the first stage of the algorithm - they are simply the messages received from the peer processes. The output value is left undetermined until the second stage of the algorithm, which I am defining here. Note that in the figure below, the output values are initially set to '?', indicating that they are presently unknown.<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure3.gif" alt="" /><br />
Figure 3<br />
The Tree Layout for 5 processes with 1 faulty process<br />
</center><br />
In Figure 3, there are six processes, and the General (P<sub>1</sub>) is faulty – sending a 1 to the first three lieutenants and 0 to the last two. The subsequent round of messaging results in P<sub>2</sub> having an information tree that looks just like that shown in Figure 3. (Because only the General is faulty, in this case all other processes will have an identical tree.)</p>
<p>Once a process has completed building its tree, it is ready to decide on a value. It does this by working its way up from the leaves of the tree, calculating the majority value at each rank and assigning it to the rank above it. The output value at each level is the third item in the data structure attached to each node, and those values are all undefined during the information gathering stage.</p>
<p>Calculating the output values is a three step process:</p>
<ol>
<li>Each leaf node in the tree (all values at rank m) copies its input value to the output value.</li>
<li>Starting at rank m-1 and working down to 0, the output value of each internal node is set to be the majority of the output values of all its children. In the event of a tie, an arbitrary tie-breaker is used to assign a default value. The same default value must be used by all processes.</li>
<li>When complete, the process has a decision value in the output of the sole node at rank 0.</li>
</ol>
<p>In Figure 3, step 1 of the process assigns the initial values to the leaf nodes. In the next step, the majority value of { 1, 1, 1, 0, 0 } is evaluated and returns a value of 1, which is assigned to the output value in rank 0. Because that is the top rank, the process is done, and P<sub>1</sub> decides on a value of 1.</p>
<p>Every lieutenant value in a given exercise will have the same paths for all its nodes, and in this case, since only the General is faulty, we know that all lieutenants will have the same input values on all its leaves. As a result, all processes will agree on the same value, 1, which fulfills the agreement property.</p>
<h4>A More Complicated Example</h4>
<p>Getting a good understanding of the algorithm really requires walking through an example that has at least three ranks. (Examples on the web, mostly extracted from lecture notes, nearly always have the simple two-rank example.) For this example, consider an example with <i>n=7</i> and <i>m=2</i>. We’ll continue with the convention that the General is P<sub>1</sub>, and instead of having a faulty general, we’ll have P<sub>6</sub> and P<sub>7</sub> be faulty processes. After the initial three rounds of information exchange, we have the three-ranked tree shown in Figure 4:<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure4.gif" alt="" /><br />
Figure 4<br />
A tree with <i>n=7</i>, <i>m=2</i>, and faulty processes P<sub>6</sub> and P<sub>7</sub><br />
</center><br />
The important thing to note in these trees is that I’ve inserted the value 'X' for the input values of any input value that comes from the two faulty processes. We don’t know what P<sub>6</sub> and P<sub>7</sub> might send in any given round, so in general, we’ll try to work through the algorithm without constricting their incorrect messages to any specific values.</p>
<p>You’ll see that at rank 1, the values from path 17 and 16 are both set to X. In the first round the two faulty processes communicated possibly false values to all other processes, and may have arbitrarily changed the values sent to different processes in order to skew the results.</p>
<p>As a result of those bad values in rank 1, we see their frequent occurrence in rank 2. The incorrect values show up not only in direct messages from the faulty processes, but also in any message from a correct process that includes a faulty process earlier in its path.</p>
<p>All in all, at the leaf nodes, we have 18 deceptive values at the leaf nodes, and only 12 accurate messages that trace their way all the way back to the general through nothing but correct processes. Obviously, if we just voted on the majority of the messages we had received, we would be susceptible to falling for the wrong value.</p>
<p>Fortunately, the layout of the tree guarantees that we will actually get a correct value. In Figure 4, the roll up of the output values hasn’t occurred yet, so every node has a question mark in the output value. In Figure 5, the output values are shown. The leaf rank has the output values set to the input values, with X used to indicate unknown values from faulty processes.</p>
<p>When the leaf rank is rolled up to the second rank, the nodes with paths 12, 13, 14, and 15 all have clear majority values of 0 for their output values, with 16 and 17 set to X, as their values are uncertain.</p>
<p>The final roll up to the top rank successfully sets the output value to 0, as four of the inputs are set to 0 and only 2 are set to X. Mission accomplished. And because of the way this was calculated, we know that the correct result will be achieved regardless of what deceptive values are sent by the two faulty processes.<br />
<center><br />
<img src="http://marknelson.us/attachments/2007/byzantine/Figure5.gif" alt="" /><br />
Figure 5<br />
The tree after calculating the output values<br />
</center></p>
<h4>The Sample Code</h4>
<p>I’ve included a simple C++ program that implements this algorithm, with extensive internal documentation. It has a <span class="inline_code">Process </span>class that is used to send and receive messages, as well as to roll up the decision tree. A <span class="inline_code">Traits </span>class is used to define the number of processes, the number of faulty processes, the source process, and what values the faulty processes send in various rounds.</p>
<p>To help with visualization, the program will output the tree for a given process in the format used by <i>dot</i>, part of the free Graphviz program. You can then use dot to create a nice picture of the output graph – all the figures in this article were created that way. I find that using the SVG format for the output produces very readable results.</p>
<p>As supplied, the program is set for values of <i>n=7</i> and <i>m=2</i>. Good exercises to perform while experimenting with it include:</p>
<ul>
<li>Attempt to invalidate the program or the algorithm by getting incorrect results with some particular combination of faulty messages.</li>
<li>Add a third faulty process and show that it is relatively easy to get invalid output when <i>n=7</i> and <i>m=2</i>.</li>
<li>Reduce <i>n</i> to 6 and show that it is relatively easy to get invalid output with two faulty processes.</li>
<li>Move up to <i>m=3</i> and <i>n=10</i>. Experiment with various combinations of faulty Generals and lieutenants and see if you can create incorrect results.</li>
</ul>
<h4>Note</h4>
<p>Most implementations of this algorithm include logic designed to deal with the case in which a faulty process fails to send a message. This is equivalent to simply having the faulty process send an arbitrary value, so I don’t treat it as a separate case.</p>
<h4>Source Code</h4>
<p><a href="/attachments/2007/byzantine/source.zip">source.zip</a>, which contains:</p>
<ul>
<li>main.cpp</li>
<li>README.TXT</li>
<li>VS2003/byzantine.sln
<li>
<li>VS2003/byzantine.vcproj
<li>VS2005/byzantine.sln
<li>
<li>VS2005/byzantine.vcproj
</ul>
<h4>References</h4>
<p>[1]  The Byzantine Generals Problem  (with Marshall Pease and Robert Shostak)<br />
ACM Transactions on Programming Languages and Systems 4, 3 (July 1982), 382-401.<br />
<a href="http://research.microsoft.com/users/lamport/pubs/byz.pdf" class="newpage">http://research.microsoft.com/users/lamport/pubs/byz.pdf</a></p>
<p>[2] The Writings of Leslie Lamport: <a href="http://research.microsoft.com/users/lamport/pubs/pubs.html#byz" class="newpage">http://research.microsoft.com/users/lamport/pubs/pubs.html#byz</a></p>
<p>Lynch, Nancy A. Distributed Algorithms. San Francisco, CA: Morgan Kaufmann, 1997. ISBN: 1558603484.</p>
<p>Graphviz – Graph Visualization Software. <a href="http://www.graphviz.org/" class="newpage">http://www.graphviz.org/</a></p>
<p>CS 6378: Advanced Operating Systems Section 081 Notes on Agreement Protocols (lecture notes by Neeraj Mittal, University of Texas at Dallas)  <a href="http://www.utdallas.edu/~neerajm/cs6378su07/agreement.pdf" class="newpage">http://www.utdallas.edu/~neerajm/cs6378su07/agreement.pdf</a></p>
<p><i>Update 01-November-2007</i><br />
<em>Distributed Computing With Malicious Processors w/o Crypto or Private Channels</em>, <a href="http://www.youtube.com/watch?v=xXhnJl4AVg0" class="newpage">YouTube Video of Google TechTalk</a>, <a href="http://webhome.cs.uvic.ca/~val/" class="newpage">Valerie King</a>, Department of Computer Science, University of Victoria, Victoria, BC, Canada</p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2007/07/23/byzantine/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Puzzling</title>
		<link>http://marknelson.us/2007/04/01/puzzling/</link>
		<comments>http://marknelson.us/2007/04/01/puzzling/#comments</comments>
		<pubDate>Mon, 02 Apr 2007 03:21:03 +0000</pubDate>
		<dc:creator>Mark Nelson</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Articles]]></category>

		<guid isPermaLink="false">/2007/04/01/puzzling/</guid>
		<description><![CDATA[



















<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/04/01/puzzling/' addthis:title='Puzzling' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div>Dr. Dobb's PortalApril, 2007 Article on DDJ site I'm an inveterate fan of wordplay of all sorts - puzzles, anagrams, crosswords. I've been known online by my anagrammatic name, SnorkelMan, all the way back to the ancient days of the text mode BBS. My continual hectoring of the staff at the Dallas Morning News over [...]]]></description>
			<content:encoded><![CDATA[









<div class="addthis_toolbox addthis_default_style" addthis:url='http://marknelson.us/2007/04/01/puzzling/' addthis:title='Puzzling' ><a class="addthis_button_twitter"></a><a class="addthis_button_favorites"></a><a class="addthis_button_print"></a><a class="addthis_button_facebook_like"></a><a class="addthis_button_google_plusone"></a><a class="addthis_button_compact"></a></div><table border="0" width="500">
<tr>
<td width="300"><img alt="DDJ Portal Logo" src="http://marknelson.us/attachments/misc/logo_ddj.gif" /></td>
<td><strong>Dr. Dobb's Portal</strong><br/>April, 2007<br />
         <a href="http://www.ddj.com/architect/198701685" class="newpage">Article on DDJ site</a>
  </td>
</tr>
</table>
<p>I'm an inveterate fan of wordplay of all sorts - puzzles, anagrams, crosswords. I've been known online by my anagrammatic name, SnorkelMan, all the way back to the ancient days of the text mode BBS. My continual hectoring of the staff at the Dallas Morning News over errors in their print version of the New York Times crossword puzzle led them to finally just give me the job of proofreading it. I spend way too much time on the crosswords and other puzzles, both online and in print. In other words, I'm a sucker for a good word puzzle.</p>
<p><center><br />
<img src="http://marknelson.us/attachments/2007/puzzling/WordplayArticle.jpg" alt="Wordplay Article from Dallas Morning News" /><br />
Figure 1 - Dallas Morning News Article<br />
</center></p>
<p>My sense of wordplay was naturally piqued this weekend when I heard the <a href="http://www.npr.org/templates/story/story.php?storyId=9264290">latest weekly puzzle challenge</a> from Will Shortz on NPR Weekend Edition. The challenge, from contributor David Edelheit, read as follows:</p>
<blockquote><p>
Take the names of two U.S. States, mix them all together, then rearrange the letters to form the names of two other U.S. States. What states are these?
</p></blockquote>
<p>As sometimes happens, when I heard this puzzle, and the answer didn't click immediately, my first thought was "I could write a program to solve this faster than I can figure it out myself."</p>
<p>That's a treacherous thought for a puzzler, because it immediately diverts that little thread in the back of your mind that is supposed to be solving the puzzle, instead putting it on the task of writing the program.</p>
<p>But it turned out to be an interesting problem in efficiency, and so I'm glad I went down that path. </p>
<h4>First Pass</h4>
<p>Most of my work these days is in C++, and while C++ doesn't have the world's best string manipulation facilities, I thought it had enough to do the job on this puzzle. Figuring that the problem was small enough to solve via brute force, I decided that the general course of the program would be to work my way through all 50*49/2 combinations of states, and test them against all 48*47/2 remaining combinations. That's just a little more than a million operations, which ought to be child's play. Thus, the basic program loop was going to look like this:</p>
<div class="igBar"><span id="lcpp-53"><a href="#" onclick="javascript:showPlainTxt('cpp-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-53">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> i = <span class="nu0">0</span> ; i &lt;<span class="nu0">49</span> ; i++ <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> j = i&nbsp; + <span class="nu0">1</span>; j &lt;<span class="nu0">50</span> ; j++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> m = <span class="nu0">0</span> ; m &lt;<span class="nu0">49</span> ; m++ <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> m != i &amp;&amp; m != j <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> n = m + <span class="nu0">1</span> ; n &lt;<span class="nu0">50</span> ; n++ <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> n != i &amp;&amp; n != j <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//compare state i and j against state m and n </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Still thinking brute force, I was looking for the simplest way to store the data so that it would be easy to compare, and turned to <code>std::multiset</code>. I knew that if I stored all the characters from states <code>i</code> and <code>j</code> in one <code>std::multiset<char></code> object, and all the characters from states <code>m</code> and <code>n</code> in another, I could quickly compare one against the other with a simple equality operator.</p>
<p>So in the above loop, I inserted these lines after the first two <code>for</code> statements:</p>
<div class="igBar"><span id="lcpp-54"><a href="#" onclick="javascript:showPlainTxt('cpp-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-54">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">multiset</span>&lt;char&gt; label1;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw4">char</span> *p = states<span class="br0">&#91;</span> i <span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">while</span> <span class="br0">&#40;</span>*p<span class="br0">&#41;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; label1.<span class="me1">insert</span><span class="br0">&#40;</span> *p++ <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">p = states<span class="br0">&#91;</span> j <span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">while</span> <span class="br0">&#40;</span>*p<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; label1.<span class="me1">insert</span><span class="br0">&#40;</span> *p++ <span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p>
(Note that I had snagged the names of the states from one of the first sites in a Google search, and inserted them into an array of character pointers called <code>states</code>.) </p>
<p>I inserted a similar definition for <code>label2</code> inside the second set of two <code>for</code> statements, which means all I had left to do was a simple comparison of <code>label1</code> against <code>label2</code>:</p>
<div class="igBar"><span id="lcpp-55"><a href="#" onclick="javascript:showPlainTxt('cpp-55'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-55">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span> label1 == label2 <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; std::<span class="kw3">cout</span> &lt;&lt;states<span class="br0">&#91;</span> i <span class="br0">&#93;</span> &lt;&lt;<span class="st0">", "</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;states<span class="br0">&#91;</span> j <span class="br0">&#93;</span> &lt;&lt;<span class="st0">", "</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;states<span class="br0">&#91;</span> m <span class="br0">&#93;</span> &lt;&lt;<span class="st0">", "</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;&lt;states<span class="br0">&#91;</span> n <span class="br0">&#93;</span> &lt;&lt;<span class="st0">"<span class="es0">\n</span>"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>I did a quick compile, tested the code, and sure enough, the contents of the multisets were indeed sorted concatentations of the letters of each pair of states. Time to run!</p>
<p>My first disappointment was seeing that, while the program was indeed running properly, it was going slow enough that it looked like it was going to take a sizable fraction of an hour to make it through the entire alphabet. I could just wait, but in this case I decided I could optimize faster than it would take to wait for the first results.</p>
<h4>Second Pass</h4>
<p>It's pretty obvious that calculating the concatenation of state <code>m</code> and <code>n</code> in the innermost loop is full of wasted cycles, since it is repeatedly calculating the same state values. I knew it was inefficient, but I didn't think it was going to matter too much.</p>
<p>Since it turned out that it did matter, I decided to precalculate all the values before entering the four-deep nested comparison loop, with code like this:</p>
<div class="igBar"><span id="lcpp-56"><a href="#" onclick="javascript:showPlainTxt('cpp-56'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-56">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1">std::<span class="me2">multiset</span>&lt;char&gt; letters<span class="br0">&#91;</span> <span class="nu0">50</span> <span class="br0">&#93;</span><span class="br0">&#91;</span> <span class="nu0">50</span> <span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> i = <span class="nu0">0</span> ; i &lt;<span class="nu0">49</span> ; i++ <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span> <span class="kw4">int</span> j = i + <span class="nu0">1</span> ; j &lt;<span class="nu0">50</span> ; j++ <span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">char</span> *p = states<span class="br0">&#91;</span> i <span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>*p<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; letters<span class="br0">&#91;</span> i <span class="br0">&#93;</span><span class="br0">&#91;</span> j <span class="br0">&#93;</span>.<span class="me1">insert</span><span class="br0">&#40;</span> *p++ <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; p = states<span class="br0">&#91;</span> j <span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span> *p <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;letters<span class="br0">&#91;</span> i <span class="br0">&#93;</span><span class="br0">&#91;</span> j <span class="br0">&#93;</span>.<span class="me1">insert</span><span class="br0">&#40;</span> *p++ <span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Then I didn't have to do any computation in my main loop, I just had to modify the comparison line in the innermost loop:</p>
<div class="igBar"><span id="lcpp-57"><a href="#" onclick="javascript:showPlainTxt('cpp-57'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C++:</span>
<div id="cpp-57">
<div class="cpp">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span> <span class="br0">&#40;</span> letters<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#91;</span>j<span class="br0">&#93;</span> == letters<span class="br0">&#91;</span>m<span class="br0">&#93;</span><span class="br0">&#91;</span>n<span class="br0">&#93;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; std::<span class="kw3">cout</span> &lt;&lt;states<span class="br0">&#91;</span> i <span class="br0">&#93;</span> &lt;&lt;<span class="st0">", "</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; ... </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This modified program did indeed speed things up considerably, bringing the run time down from a fraction of an hour to just a few seconds, even with a bit of progress tracing turned on.</p>
<h4>Third and Final Pass</h4>
<p>Although I could have stopped here, I thought it might be interesting to see how expensive the use of an associative container like <code>multiset </code>was compared to something simpler. I replaced the setup code so that it stored the data in an <code>std::vector<char></code> instead of a <code>multiset</code>, on the theory that the comparison operator would run much faster on a <code>vector</code>. I had to add a call to sort the data after inserting it in the <code>vector</code>, which would be executed 50 times, but nowhere near as many times as the comparison operator.</p>
<p>The results were more or less as I expected. When run under Windows with default Release optimization, the <code>vector</code> version of the program ran about twice as fast as the <code>multiset</code> version. When compiled with g++ 3.3.5 with <code>-O2</code>, I saw roughly the same ratio of execution speeds. </p>
<h4>No Spoilers</h4>
<p>I'm not going to spoil the puzzle for you by giving away the answer. Let's just say that it is a good word puzzle, and if you manage to arrive at the answer you'll see why. If you don't manage to solve it in your head, you can download the source, compile it, and get there by brute force, just like I did.</p>
<p><a href="http://marknelson.us/attachments/2007/puzzling/usa.cpp">usa.cpp</a></p>
<p>But I don't think it will count as a spoiler if I tell you that you don't need a computer program to solve this problem. </p>
]]></content:encoded>
			<wfw:commentRss>http://marknelson.us/2007/04/01/puzzling/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

