<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: 13.07.2007 Switch On Strings In C And C++</title>
	<atom:link href="http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/</link>
	<description>timj.testbit.eu/</description>
	<lastBuildDate>Fri, 10 Sep 2010 17:03:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: muppet</title>
		<link>http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/comment-page-1/#comment-57</link>
		<dc:creator>muppet</dc:creator>
		<pubDate>Sat, 14 Jul 2007 20:46:33 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/#comment-57</guid>
		<description>I&#039;m very interested here, because the &quot;inefficiency&quot; of lookups based on strings has driven many of my peers to managing number spaces and the resulting dependency and coupling issues that brings.

Why the statement expression instead of an inline function?

Why not use a hashing algorithm, instead?  Your source generator could scan the strings of interest and make symbols whose values are their hashes.  Then, assuming that compute_hash() has exactly the same algorithm as your code generator:

    switch (compute_hash(&quot;foo&quot;)) {
        case STRING_FOO_HASH: ... ;
        case STRING_BAR_HASH: ... ;
    }

Now you walk the string exactly once at runtime, to compute the hash, instead of doing logN comparisons in a bsearch.

A disadvantage is that your syntax of case SOMETHING(string_i_want_to_match) is more obvious, but it would remove the limitation of word-characters (which is a reason i can&#039;t apply your idea to my own problem ;-).

For that matter, you might just have a nice, intrinsically extensible hash table of string-&gt;handler function, but that&#039;s nowhere near as syntactically friendly as a switch statement...</description>
		<content:encoded><![CDATA[<p>I&#8217;m very interested here, because the &#8220;inefficiency&#8221; of lookups based on strings has driven many of my peers to managing number spaces and the resulting dependency and coupling issues that brings.</p>
<p>Why the statement expression instead of an inline function?</p>
<p>Why not use a hashing algorithm, instead?  Your source generator could scan the strings of interest and make symbols whose values are their hashes.  Then, assuming that compute_hash() has exactly the same algorithm as your code generator:</p>
<p>    switch (compute_hash(&#8220;foo&#8221;)) {<br />
        case STRING_FOO_HASH: &#8230; ;<br />
        case STRING_BAR_HASH: &#8230; ;<br />
    }</p>
<p>Now you walk the string exactly once at runtime, to compute the hash, instead of doing logN comparisons in a bsearch.</p>
<p>A disadvantage is that your syntax of case SOMETHING(string_i_want_to_match) is more obvious, but it would remove the limitation of word-characters (which is a reason i can&#8217;t apply your idea to my own problem ;-).</p>
<p>For that matter, you might just have a nice, intrinsically extensible hash table of string-&gt;handler function, but that&#8217;s nowhere near as syntactically friendly as a switch statement&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tim Janik</title>
		<link>http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/comment-page-1/#comment-56</link>
		<dc:creator>Tim Janik</dc:creator>
		<pubDate>Sat, 14 Jul 2007 16:23:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/#comment-56</guid>
		<description>&gt; What about gperf (http://www.gnu.org/software/gperf/) ?

for gperf and why it&#039;s not suitable, see:
  http://shum.huji.ac.il/~agay/sos/theory.html#gperf
in short, it has unwanted collisions with arbitrary strings and it&#039;d involve a hell of a lot more code then the simple binary lookup SOSID has.</description>
		<content:encoded><![CDATA[<p>&gt; What about gperf (<a href="http://www.gnu.org/software/gperf/" rel="nofollow">http://www.gnu.org/software/gperf/</a>) ?</p>
<p>for gperf and why it&#8217;s not suitable, see:<br />
  <a href="http://shum.huji.ac.il/~agay/sos/theory.html#gperf" rel="nofollow">http://shum.huji.ac.il/~agay/sos/theory.html#gperf</a><br />
in short, it has unwanted collisions with arbitrary strings and it&#8217;d involve a hell of a lot more code then the simple binary lookup SOSID has.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Florin</title>
		<link>http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/comment-page-1/#comment-55</link>
		<dc:creator>Florin</dc:creator>
		<pubDate>Sat, 14 Jul 2007 16:05:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/#comment-55</guid>
		<description>What about gperf (http://www.gnu.org/software/gperf/) ?</description>
		<content:encoded><![CDATA[<p>What about gperf (<a href="http://www.gnu.org/software/gperf/" rel="nofollow">http://www.gnu.org/software/gperf/</a>) ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fraggle</title>
		<link>http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/comment-page-1/#comment-54</link>
		<dc:creator>fraggle</dc:creator>
		<pubDate>Sat, 14 Jul 2007 11:58:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/#comment-54</guid>
		<description>Sounds like you just reinvented symbols.  I guess Greenspun&#039;s Tenth Rule is alive and well.</description>
		<content:encoded><![CDATA[<p>Sounds like you just reinvented symbols.  I guess Greenspun&#8217;s Tenth Rule is alive and well.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- This Quick Cache file was built for (  blogs.gnome.org/timj/2007/07/14/13072007-switch-on-strings-in-c-and-c/feed/ ) in 1.17733 seconds, on Feb 11th, 2012 at 6:44 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 11th, 2012 at 7:44 am UTC -->
