<?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"
	>
<channel>
	<title>Comments on: New Category: Programming (this week: swap and concept_check)</title>
	<atom:link href="http://chip.kcubes.com/2007/12/22/new-category-programming-this-week-swap-and-concept_check/feed/" rel="self" type="application/rss+xml" />
	<link>http://chip.kcubes.com/2007/12/22/new-category-programming-this-week-swap-and-concept_check/</link>
	<description>Tech commentary of thoughts, challenges, how-to's, and the mundane.</description>
	<pubDate>Fri, 21 Nov 2008 02:20:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	</channel>
</rss>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Chip's Technical Blog</title>
<link rel="SHORTCUT ICON" href="http://chip.kcubes.com/favicon.ico"/>
<link type="text/css" title="default" rel="stylesheet" href="http://chip.kcubes.com/chip.css"/>
<style type="text/css">
<!--

-->
</style>
<!--ol {display: block; padding-left: 0em; margin: 0 0 1em 1em; }-->
  <link rel="alternate" type="application/rss+xml" title="Chip&#8217;s Technical Blog RSS Feed" href="http://chip.kcubes.com/feed/" />
  <link rel="pingback" href="http://chip.kcubes.com/blog/xmlrpc.php" />
        </head>
<body>
	<div id="navbar">
	<!--<span style="display: run-in; ">Navigation: </span>-->
	<a href="http://chip.kcubes.com/">Home</a><a href="http://chip.kcubes.com/resume/">Resume</a><a href="http://chip.kcubes.com/research/">Research</a><a href="http://chip.kcubes.com/personal/">Personal</a><a href="http://chip.kcubes.com/killian_young/">Killian/Young</a><a href="http://www.kcubes.com/gallery/v/chip">Picture Album</a><a href="http://chip.kcubes.com/links.php">Links</a><a href="/2007/12/22/new-category-programming-this-week-swap-and-concept_check/feed/?printable=true">Printer-friendly</a>	</div>
<!--<hr WIDTH="100%" style="margin:0 0;padding: 0 0">-->
	<div id="withsidebar">
		<div id="sidebar">
			<img src="/chipPic.jpg" alt="Picture of Me"/>        <div class="box">
		<ul>

			<li> Search posts:
				<form method="get" id="searchform" action="http://chip.kcubes.com/">
<div><input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
			</li>

			<!-- Author information is disabled per default. Uncomment and fill in your details if you want to use it.
			<li><h2>Author</h2>
			<p>A little something about you, the author. Nothing lengthy, just an overview.</p>
			</li>
			-->

			<li>
						</li>

			
			<li><h2>Archives</h2>
				<ul>
					<li><a href='http://chip.kcubes.com/2008/09/' title='September 2008'>September 2008</a></li>
	<li><a href='http://chip.kcubes.com/2008/07/' title='July 2008'>July 2008</a></li>
	<li><a href='http://chip.kcubes.com/2008/06/' title='June 2008'>June 2008</a></li>
	<li><a href='http://chip.kcubes.com/2008/05/' title='May 2008'>May 2008</a></li>
	<li><a href='http://chip.kcubes.com/2008/04/' title='April 2008'>April 2008</a></li>
	<li><a href='http://chip.kcubes.com/2007/12/' title='December 2007'>December 2007</a></li>
	<li><a href='http://chip.kcubes.com/2007/11/' title='November 2007'>November 2007</a></li>
	<li><a href='http://chip.kcubes.com/2007/10/' title='October 2007'>October 2007</a></li>
	<li><a href='http://chip.kcubes.com/2007/09/' title='September 2007'>September 2007</a></li>
	<li><a href='http://chip.kcubes.com/2007/08/' title='August 2007'>August 2007</a></li>
	<li><a href='http://chip.kcubes.com/2007/07/' title='July 2007'>July 2007</a></li>
				</ul>
			</li>

			<li class="categories"><h2>Categories</h2><ul>	<li class="cat-item cat-item-3"><a href="http://chip.kcubes.com/tags/howto/" title="View all posts filed under How-to">How-to</a> (3)
</li>
	<li class="cat-item cat-item-4"><a href="http://chip.kcubes.com/tags/musings/" title="View all posts filed under Musings">Musings</a> (8)
</li>
	<li class="cat-item cat-item-6"><a href="http://chip.kcubes.com/tags/programming/" title="View all posts filed under Programming">Programming</a> (1)
</li>
	<li class="cat-item cat-item-5"><a href="http://chip.kcubes.com/tags/software/" title="View all posts filed under Software">Software</a> (10)
</li>
	<li class="cat-item cat-item-1"><a href="http://chip.kcubes.com/tags/uncategorized/" title="View all posts filed under Uncategorized">Uncategorized</a> (3)
</li>
</ul></li>
			          </ul>
          </div>
		</div>
		<div id="content">
			<h2><a href="http://chip.kcubes.com/">Chip&#8217;s Technical Blog</a></h2>
<div class="description">Tech commentary of thoughts, challenges, how-to&#8217;s, and the mundane.<br/><i>Note: to see my personal journal, formerly on this page, visit my <a href="/personal/">personal page</a>.</i>
</div>

<div class="box">
	<div class="widecolumn">

	
		<div class="navigation">
			<div class="alignleft">&laquo; <a href="http://chip.kcubes.com/2007/12/06/server-cookies-and-i-dont-think-they-quite-understand-advertising/">Server Cookies, and I don&#8217;t think they quite understand advertising&#8230;</a></div>
			<div class="alignright"><a href="http://chip.kcubes.com/2008/04/21/validating-email-addresses/">Validating email addresses</a> &raquo;</div>
		</div>

		<div class="post" id="post-33">
			<h2><a href="http://chip.kcubes.com/2007/12/22/new-category-programming-this-week-swap-and-concept_check/" rel="bookmark" title="Permanent Link: New Category: Programming (this week: swap and concept_check)">New Category: Programming (this week: swap and concept_check)</a></h2>

			<div class="entry">
				<p>I&#8217;m starting a new category for programming tips.  I jokingly referred to something as the C++ feature of the week (for Mace development) with one of our developers, and he responded that he needed to subscribe.  And it seemed like a good idea, so now I think I&#8217;ll start trying to blog about these new features I learn about.</p>
<p>So to start it off, there are two C++ features of the week for this past week:</p>
<ol>
<li><b>STL collection swap.</b>  The C++ STL Collections contain a <code>swap()</code> method, which takes another collection (of the same type) as a parameter.   The method does what&#8217;s expected &#8212; to swap the one collection&#8217;s elements with the other.  What makes this an interesting function is that it does it in constant time.  It doesn&#8217;t require constructing, copying, or otherwise wasting time with the two collections.  It just does pointer copies of internal collection state.  To see how this is useful, consider these two cases I&#8217;ve applied this to:
<ul>
<li>Maps within maps.  In one case, a Mace programmer had a map from an int to a vector.  In this case, the int represented the number of things in the vector (admittedly, this is a bit of a simplification).  So, when removing something from the vector, you would remove the vector from the map, then re-add it with the new key.  (This is because for other good reasons, the key of a map entry cannot be changed).  Because of the cost of this removal and re-addition, the programmer had originally implemented this using pointers, which, while correct and efficient, gave some of our other tools problems, and so we wanted to re-write it without using pointers.  <code>swap()</code> made this possible.  To do the update, use this code:<br />
<code><br />
void removeElement(IntVectorMap&#038; ivmap, int size) {<br />
    IntVectorMap::iterator i = ivmap.find(size);<br />
    assert(i != ivmap.end());<br />
    i->second.pop_front();<br />
    ivmap[size-1].swap(i->second);<br />
    ivmap.erase(i);<br />
}<br />
</code><br />
This does involve a construction of a new collection, but moves the elements of the collection quite efficiently.  Since we will erase the original map entry, causing the old vector to cease to exist, the fact that it now holds no elements does not matter.
</li>
<li>
The second case was one where we wanted to iterate through a set, but other code might be adding things to the set at the same time.  To maintain code safety, we must not lose newly added things, so we can process them later.  The original design involved making a copy of the set, then clearing the original set, and iterating over the copy.  Once again, <code>swap()</code> is the right tool here too.<br />
<code><br />
void processSet(IntSet&#038; s) {<br />
    IntSet t;<br />
    t.swap(s);<br />
    for(IntSet::iterator i = t.begin(); ...) {<br />
       ...<br />
    }<br />
    if (!s.empty()) { processSet(s); }<br />
}<br />
</code><br />
As an added bonus, if you need to hold a lock to touch S, you can simply acquire the lock and do the switch, a very fast operation, the release the lock.
</li>
</ul>
</li>
<li><a href="http://www.boost.org/libs/concept_check/concept_check.htm"><b>boost::concept_check.</b></a>  We were updating our serialization code, but found that the compiler error messages on template errors are hard-to-decipher (to be generous).  These errors were caused by one of two problems in one of our cases.  First, we had added a new template parameter, and inserted it before some existing ones.  In code which hadn&#8217;t been updated though, if they provided the older optional template parameters, the compiler would get very confused, and report error messages which could not be deciphered, and pointed to lines of code which didn&#8217;t make any sense.  In the other error case, the default template parameter might not work with other types passed.  (Specifically, it was a parameter telling how to serialize a collection, and the collection elements might not have been serializable.)  This message was a little easier to decipher, complaining about types which could not be serialized, but still didn&#8217;t point to the right lines of code.
<p>Using boost&#8217;s concept check, we were able to help both of these problems.  In the first case, we wrote a base class for all valid parameters of the template, then used a concept check to make sure the template parameter was convertible to the base class.   Passing in the older parameter now would generate a shorter, easier to understand message, and the concept check library makes sure that the line of code makes sense.  In the second case, we had to write our own concept checker, which would essentially just write code that needed to be able to compile (in this case, instantiating the type, serializing it, and deserializing it).  Again, the concept_check library would make sure the error message was pointed to in the right place.
</li>
</ol>
<p>That&#8217;s all for this edition.  Watch the programming category if you want to see other programming tips.</p>

				
				<p class="postmetadata alt">
					<small>
						This entry was posted
												on Saturday, December 22nd, 2007 at 4:53 am						and is filed under <a href="http://chip.kcubes.com/tags/programming/" title="View all posts in Programming" rel="category tag">Programming</a>.
						You can follow any responses to this entry through the <a href='http://chip.kcubes.com/2007/12/22/new-category-programming-this-week-swap-and-concept_check/feed/'>RSS 2.0</a> feed.

													You can <a href="#respond">leave a response</a>, or <a href="http://chip.kcubes.com/2007/12/22/new-category-programming-this-week-swap-and-concept_check/trackback/" rel="trackback">trackback</a> from your own site.

						
					</small>
				</p>

			</div>
		</div>

	
<!-- You can start editing here. -->


			<!-- If comments are open, but there are no comments. -->

	 


<h3 id="respond">Leave a Reply</h3>


<form action="http://chip.kcubes.com/blog/wp-comments-post.php" method="post" id="commentform">


<p><input type="text" name="author" id="author" value="" size="22" tabindex="1" />
<label for="author"><small>Name (required)</small></label></p>

<p><input type="text" name="email" id="email" value="" size="22" tabindex="2" />
<label for="email"><small>Mail (will not be published) (required)</small></label></p>

<p><input type="text" name="url" id="url" value="" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>


<!--<p><small><strong>XHTML:</strong> You can use these tags: <code>&lt;a href=&quot;&quot; title=&quot;&quot;&gt; &lt;abbr title=&quot;&quot;&gt; &lt;acronym title=&quot;&quot;&gt; &lt;b&gt; &lt;blockquote cite=&quot;&quot;&gt; &lt;cite&gt; &lt;code&gt; &lt;del datetime=&quot;&quot;&gt; &lt;em&gt; &lt;i&gt; &lt;q cite=&quot;&quot;&gt; &lt;strike&gt; &lt;strong&gt; </code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
<input type="hidden" name="comment_post_ID" value="33" />
</p>

</form>



	
	</div>
</div>


<div id="inlinefooter">
<!-- If you'd like to support WordPress, having the "powered by" link someone on your blog is the best way, it's our only promotion or advertising. -->
	<p>
		Chip&#8217;s Technical Blog is proudly powered by
		<a href="http://wordpress.org/">WordPress</a>
		<br /><a href="http://chip.kcubes.com/feed/">Entries (RSS)</a>
		and <a href="http://chip.kcubes.com/comments/feed/">Comments (RSS)</a>.
		<!-- 16 queries. 0.395 seconds. -->
	</p>
</div>

<!-- Gorgeous design by Michael Heilemann - http://binarybonsai.com/kubrick/ -->

		
		</div>
	</div>
<div id="footer">
<b>Contact: </b>
[<a href="mailto:chip_killian@acm.org">@ ACM.org</a>]
[<a href="mailto:ckillian+web@gmail.com">@ Gmail.com</a>]
</div>
</body>
</html>
