<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: swap code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 17:54:50 GMT</pubDate>
    <description>DZone Snippets: swap code</description>
    <item>
      <title>Pointer swap function</title>
      <link>http://snippets.dzone.com/posts/show/5423</link>
      <description>// A generic pointer swap function that works with all structs by calling swap(&amp;A, &amp;B), where A and B are struct pointers&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void swap(void **x, void **y) {&lt;br /&gt;	void *t = *x;&lt;br /&gt;	*x = *y;&lt;br /&gt;	*y = t;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 23 Apr 2008 10:39:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5423</guid>
      <author>rasmus (Rasmus Frederiksen)</author>
    </item>
    <item>
      <title>swap values without temporary variable</title>
      <link>http://snippets.dzone.com/posts/show/2676</link>
      <description>The XOR swap algorithm is an inefficient method of swapping two variables. http://en.wikipedia.org/wiki/Xor_swap_algorithm&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void XORSwap(void *x, void *y)&lt;br /&gt;{&lt;br /&gt;   *x ^= *y;&lt;br /&gt;   *y ^= *x;&lt;br /&gt;   *x ^= *y;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 23 Sep 2006 17:40:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2676</guid>
      <author>nevadalife (nevada)</author>
    </item>
    <item>
      <title>javascript image swap using DOM</title>
      <link>http://snippets.dzone.com/posts/show/1596</link>
      <description>Here is a javascript image swap that uses the DOM methods.&lt;br /&gt;It uses them to support swapping images with different sizes.&lt;br /&gt;Just changing the src property causes the swapped images to be distorted in IE.&lt;br /&gt;This script uses replaceChild instead.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var blnDOMSUPPORT = (document.getElementById) ? true : false;&lt;br /&gt;window.onload = window_load;&lt;br /&gt;&lt;br /&gt;function window_load()&lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;   if ( blnDOMSUPPORT )&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      document.getElementById('icon1').onclick = swapimage;&lt;br /&gt;      document.getElementById('icon2').onclick = swapimage;&lt;br /&gt;      document.getElementById('icon3').onclick = swapimage;&lt;br /&gt;      &lt;br /&gt;      }&lt;br /&gt;   else&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      document.images['icon1'].onclick = swapimage;&lt;br /&gt;      document.images['icon2'].onclick = swapimage;&lt;br /&gt;      document.images['icon3'].onclick = swapimage;&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;function swapimage()&lt;br /&gt;   {&lt;br /&gt;   &lt;br /&gt;   if ( blnDOMSUPPORT )&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      var imgMain = document.getElementById('mainimage');&lt;br /&gt;      var divParent;&lt;br /&gt;&lt;br /&gt;      // create new image element&lt;br /&gt;      var imgNew = document.createElement('img');&lt;br /&gt;&lt;br /&gt;      // give it an image&lt;br /&gt;      // here I use the src of the icon just for convenience&lt;br /&gt;      // you'd want to somehow determine the src of your desired image&lt;br /&gt;      imgNew.src = this.src;&lt;br /&gt;   &lt;br /&gt;      // give it an id&lt;br /&gt;      imgNew.id = 'mainimage';&lt;br /&gt;&lt;br /&gt;      // replace image&lt;br /&gt;      divParent = imgMain.parentNode;&lt;br /&gt;      divParent.replaceChild(imgNew, imgMain);&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;   else&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      // old school image swap&lt;br /&gt;      document.images['mainimage'].src = this.src;&lt;br /&gt;&lt;br /&gt;      alert( 'old school' );&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;   &lt;br /&gt;   }         &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;It would be used with HTML like this&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;div id="main"&gt;&lt;br /&gt;&lt;img id="mainimage" src="http://www.w3schools.com/css/smiley.gif"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div id="icons"&gt;&lt;br /&gt;&lt;img id="icon1" src="http://www.w3schools.com/css/smiley.gif"&gt;&lt;br /&gt;&lt;img id="icon2" src="http://www.w3schools.com/images/vxhtml.gif"&gt;&lt;br /&gt;&lt;img id="icon3" src="http://www.w3schools.com/images/print.gif"&gt;&lt;br /&gt;&lt;input type="button" value="Toggle DOM Support" onclick="window.blnDOMSUPPORT = !window.blnDOMSUPPORT;"&gt;&lt;br /&gt;&lt;/div&gt; &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 04:31:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1596</guid>
      <author>Will_Rickards (Will Rickards)</author>
    </item>
    <item>
      <title>Swap elements of an array in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/280</link>
      <description>&lt;code&gt;class Array&lt;br /&gt;    def swap!(a,b)&lt;br /&gt;         self[a], self[b] = self[b], self[a]&lt;br /&gt;    self&lt;br /&gt;    end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can now do stuff like..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;[1,2,3,4].swap!(2,3)  # = [1,2,4,3] etc..&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Many thanks to Sam Stephenson and technoweenie for their suggestions.</description>
      <pubDate>Fri, 13 May 2005 09:29:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/280</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
