<?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>ShimPossible</title>
	<atom:link href="http://blog.shimpossible.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.shimpossible.com</link>
	<description>It's not impossible, it's ShimPossible</description>
	<lastBuildDate>Sun, 16 Aug 2009 13:08:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Legos and Optimization</title>
		<link>http://blog.shimpossible.com/2009/08/16/legos_and_optimization/</link>
		<comments>http://blog.shimpossible.com/2009/08/16/legos_and_optimization/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 13:08:31 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/?p=38</guid>
		<description><![CDATA[Everyone wants faster, simpler code.Â  &#8220;Optimization&#8221; doesn&#8217;t mean the same thing to everyone though.Â  For example a FSM.Â  This usually start out as a big mass of very simple states.Â  You then &#8220;optimize&#8221; redundant and nop states away, and combine states.Â  For example a simple FSM for most console games Logos -&#62; Loading screen -&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone wants faster, simpler code.Â  &#8220;Optimization&#8221; doesn&#8217;t mean the same thing to everyone though.Â  For example a <a href="http://en.wikipedia.org/wiki/Finite-state_machine">FSM</a>.Â  This usually start out as a big mass of very simple states.Â  You then &#8220;optimize&#8221; redundant and nop states away, and combine states.Â  For example a simple FSM for most console games</p>
<blockquote><p>Logos -&gt; Loading screen -&gt; Main menu -&gt; &#8230;.</p></blockquote>
<p>After main menu, you could go to any number of states such as playing the game, loading a save, configuration, etc&#8230;Â  The first three states though, will always follow the same path and can be combined down into one state.Â  This is great on paper and pencil because now you have less things to draw.Â  When translating this into the software world each &#8220;state&#8221; can be represented as its own object or method.Â  So we&#8217;ve also cut down on the typing we had to do in software.Â  Such an &#8220;optimization&#8221; was probably not a good thing though.Â Â  If the code has to change later we&#8217;ll have to break apart the state to put in a new state.</p>
<p>It&#8217;s best to leave the states as separate states (objects,methods) in the software.Â  This allows the code to easily be changed later if and when it does (because it will always change).Â  By having objects/methods do TOO much, it has a level of complexity that does not and should not be there.Â  Break things up into smaller, simpler parts.Â  Maybe you have already heard of <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic complexity</a> (Firefox hasn&#8217;t though, as it comes up as a spelling error.)Â  There are a number of tools on the web that will measure the this on each method/class in program.</p>
<p>If the three initial states were merged we&#8217;d end up with one state that was 3x more complex than the individual states.Â  In terms of maintainability, this isn&#8217;t a good thing.Â  Merging the states also did not make anything faster, and might have made things slower.Â  Instead of the state machine deciding which of the three states we are in, the new &#8216;optimized&#8217; state much decide which of the three states it is in, so there are two decision points instead of one.</p>
<p>Having lots of small methods, classes,objects in a program is not a bad thing.Â  Merging them down into larger objects is not a good thing.Â  Keep the code broken up as possible, or it will become unmaintainable mess of large objects with very specific (and sometimes redundant) functions .Â Â  Think of code like building blocks.Â  As long as the objects  are small you can mix and match them anywhere to build things.Â  The larger pieces can only be used for a few things and are limited in their use.Â  Look at some of the Lego sets you can buy.Â  Some are made with very generic parts that can be used for almost anything.Â  Others have very specific (often large) pieces that serve specific functions.Â  Such as a cockpit on the spaceship, or a fancy roof for a building.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2009/08/16/legos_and_optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python like .NET</title>
		<link>http://blog.shimpossible.com/2009/04/17/python-like-net/</link>
		<comments>http://blog.shimpossible.com/2009/04/17/python-like-net/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 23:00:48 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/?p=32</guid>
		<description><![CDATA[Python allows you to assign almost everything.Â  There doesn&#8217;t seem to be a difference between variables and functions.Â  You can have a class Foo with attributes bar and baz. These can be methods or plain varaibles, and you can re assign them at run time.Â Â  So Foo.bar = 10Â  and Foo.bar = { return a+1 [...]]]></description>
			<content:encoded><![CDATA[<p>Python allows you to assign almost everything.Â  There doesn&#8217;t seem to be a difference between variables and functions.Â  You can have a class <strong>Foo </strong>with attributes <strong>bar </strong>and <strong>baz</strong>. These can be methods or plain varaibles, and you can re assign them at run time.Â Â  So Foo.bar = 10Â  and Foo.bar = { return a+1 }Â  are both valid (though not valid python syntax).Â Â  This allows you to reassign methods/functions at run time.</p>
<p>.NET has a simular feature called <strong>Delegates</strong>.Â  These are basicly pointers to function.Â  Most people will use these for callbacks and events.Â  Something interesting you can do is use them in properties.</p>
<pre>class Foo
{
  delegate void Fn(string s);
  Fn bar;
  Fn baz
  public Fn Bar{
    get{ return bar; }
    set{ bar = value; }
  }
  public Fn Baz{
    get{ return baz; }
    set{ baz = value; }
  }
}</pre>
<p>Using this class you can now assign the class methods at runtime.</p>
<p>Calling the method:<br />
<code><br />
f.Baz("something")<br />
</code><br />
and reassign the function to print out a line<br />
<code><br />
f.Baz = f =&gt; Console.WriteLine(f);<br />
</code></p>
<p>The class looks just like it has normal methods, but they are not assignable.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2009/04/17/python-like-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun with timezones</title>
		<link>http://blog.shimpossible.com/2009/04/09/fun-with-timezones/</link>
		<comments>http://blog.shimpossible.com/2009/04/09/fun-with-timezones/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 23:50:47 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/?p=30</guid>
		<description><![CDATA[Using dates and times in a program is not always as straight forward as you might think.Â  The issue is with timezones and daylight savings.Â  Most people forget to use a timezone when they store dates.Â  This might not seem like an issue if you think your program will never be used in another timezone, [...]]]></description>
			<content:encoded><![CDATA[<p>Using dates and times in a program is not always as straight forward as you might think.Â  The issue is with timezones and daylight savings.Â  Most people forget to use a timezone when they store dates.Â  This might not seem like an issue if you think your program will never be used in another timezone, but it still is.</p>
<p>Timezones tell you how to convert your datetime from one timezone to the other.Â  10am in NYC is not the same 10am in Tokyo.Â  What makes this all the more fun is the wonder idea of daylight savings time.Â  Time will go backwards and forwards or seeming random days.Â  Suddenly there are 2 different 11:59pm.</p>
<p>When you look at a file in windows explort, did you ever think about the missing timezone on the dates?Â  You probably didn&#8217;t give it a second thought.Â  But you should.Â  You probably assume that it is showing the times in your current time zone.Â Â  Sadly you are more correct then you think.Â  I found out about this after I<a href="http://markmail.org/message/ebdzdl4ev7axmmbr#query:os.stat%20st_atime%201%20hour+page:1+mid:rzvcbnfuwleifeww+state:results"> read this link</a>.Â  It seems windows uses your CURRENT timezone to display the datetime.Â  Whats the problem with this?Â  If a file was created at 3/1 10:00pm and you look at the file in April, windows shows the file date as 11:00pm, even though the correct time should be 10:00pm.</p>
<p>Windows is smart enough to store the date in UTC/GMT/ZULU time, but when converting it back to your local timezone, it just uses your CURRENT timezone offset, instead of figuring out what it was on that date with DST factored in.Â  I wonder if someone was trying to optimize out a few CPU cycles.</p>
<p>So next time you are looking at the access times on your files, remember that they could be up to an hour off from what the actual time was.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2009/04/09/fun-with-timezones/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Swap with out the tmp</title>
		<link>http://blog.shimpossible.com/2009/01/27/swap-with-out-the-tmp/</link>
		<comments>http://blog.shimpossible.com/2009/01/27/swap-with-out-the-tmp/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 05:36:07 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/?p=28</guid>
		<description><![CDATA[When swapping variables most people use tmp = a a = b b = tmp This requires using an extra variable tmp to store A while it is overwritten with b.Â  Wouldn&#8217;t it be great if you could swap the two without using an extra variable?Â  The XOR swap algorithm is just what you need [...]]]></description>
			<content:encoded><![CDATA[<p>When swapping variables most people use</p>
<p><code>tmp = a<br />
a = b<br />
b = tmp<br />
</code></p>
<p>This requires using an extra variable tmp to store A while it is overwritten with b.Â  Wouldn&#8217;t it be great if you could swap the two without using an extra variable?Â  The <a href="http://en.wikipedia.org/wiki/XOR_swap_algorithm">XOR swap algorithm</a> is just what you need</p>
<p><code>a^=b^=a^=b</code></p>
<p>This ends up with A and B swapped.Â  Still 3 assignments but with out the extra variable.Â  If you want a good explaination of how that works read the wikipedia article.</p>
<p>A word of warning though, the above code only works in C/C++.Â Â  It will not work in an VM language like Java or .NET.Â Â Â  This has to do with how the languages are implemented.Â  Java and .NET are stack based VMs where as C/C++ is register based (and not a VM).Â  This means that values are pushed onto the stack and then poped off to calculate values.</p>
<p>For example</p>
<p><code>int a = 1;<br />
int b = 1;<br />
int c1 = (a++) + a;<br />
int c2 = b + (b++);<br />
</code></p>
<p>c1 will be set to 3 and c2 will be set to 2.Â Â  Written in C/C++ both will give the answer of 2.Â  The operator a++ will increment a by 1 but return its original value.Â  With a stack base, the 1st part of the equation, (a++), is calculated and the answer ( 1 ) push onto the stack.Â Â  The next part of the equation (a) is calculated and the answer (2) and push onto the static.Â Â  Why is the 2nd part of the equation 2?Â  When the add operation is run, it retrieves the actual values of the variable one of which is the returned &#8216;original value&#8217; of a and the other is the current value of A.</p>
<p>An alternate way of writing the XOR swap that works on stack based (and non stack based) VMs is as follows.</p>
<p><code>a^=b<br />
b^=a<br />
a^=b</code></p>
<p>Note:Â  This does not work if A and B are the same object.Â  You shouldn&#8217;t be swapping in the case any ways.</p>
<p>The short moral of the story is dont use shortcut code to put a bunch of commands on the same line.Â  It may not do what you think it will.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2009/01/27/swap-with-out-the-tmp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Launch Wrapper</title>
		<link>http://blog.shimpossible.com/2007/09/02/launch-wrapper/</link>
		<comments>http://blog.shimpossible.com/2007/09/02/launch-wrapper/#comments</comments>
		<pubDate>Mon, 03 Sep 2007 03:41:22 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/2007/09/02/launch-wrapper/</guid>
		<description><![CDATA[Here&#8217;s a small utility i wrote after having to keep adding the same command lines arguments to a java. This program is a wrapper around the one you want to launch. You can specify a prefix and a postfix to the arguments. You can also alter them through a regular expression. This is all done [...]]]></description>
			<content:encoded><![CDATA[<p><a id="p23" href="http://blog.shimpossible.com/wp-content/uploads/2007/09/wrapper.zip"> </a>Here&#8217;s a small utility i wrote after having to keep adding the same command lines arguments to a java.   This program is a wrapper around the one you want to launch.   You can specify a prefix and a postfix to the arguments.  You can also alter them through a regular expression.  This is all done through the .config file.   The basic idea is to rename your executable, point the wrapper to the renamed file, and rename wrapper.exe to the original name of your executable.</p>
<p><a title="Program launch Wrapper" rel="attachment" id="p23" href="http://blog.shimpossible.com/2007/09/02/launch-wrapper/program-launch-wrapper/" /><a title="Program launch wrapper" id="p25" href="http://blog.shimpossible.com/wp-admin/wrapper.zip">wrapper.zip</a><br />
Edit:Â  Updated exe to C++.Â Â Â  C#/.NET doesn&#8217;t allow exec like processes</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2007/09/02/launch-wrapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>White Screen of Death</title>
		<link>http://blog.shimpossible.com/2007/07/18/white-screen-of-death/</link>
		<comments>http://blog.shimpossible.com/2007/07/18/white-screen-of-death/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 01:02:48 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/2007/07/18/white-screen-of-death/</guid>
		<description><![CDATA[I bought an Acer laptop last year. It seemed like a pretty good deal at the time. It was 1000$ cheaper than other. Though, I seemed to have got what I paid for. It promptly died in December, slightly before Christmas. I sent it back ($20) to them to get fixed and they said they [...]]]></description>
			<content:encoded><![CDATA[<p>I bought an Acer laptop last year.   It seemed like a pretty good deal at the time.   It was 1000$ cheaper than other.   Though, I seemed to have got what I paid for.   It promptly died in December, slightly before Christmas.  I sent it back ($20) to them to get fixed and they said they replaced the mother board.</p>
<p>Since then, the screen turns white.  It just fizzles out and turns completely white.  This isn&#8217;t a software problem because it happens anytime, from the BIOS start screen, to inside XP.   This was only a few weeks after I had got it back from being repaired.  Sigh&#8230;   So I sent it back again (another $20) to be fixed.   Each time I sent it, its at least 2 weeks before I got it back.</p>
<p>Its been pretty good since then, until this week.   It started turning white again.   I only got it back a little over a month ago.  Not again.   So rather then send it back again (For another $20 and 2 weeks) I looked at what I could do.</p>
<p>Looking &#8220;White screen of death&#8221; up on the internet, showed that it could be problems with the LCD boards.   I was lucky enough to find a repair manual for the laptop though.   The manual showed some LCD parts above the keyboard.  This looked like a good place to start.   Since my screen was not almost permanently white, I figured it wouldn&#8217;t hurt to poke around.</p>
<p>After removing a cover, I had access to the connectors.   After poking and wiggling a few of them, the screen came back.   It seems the LCD cable is loose.   Its not connected to the motherboard completely, and comes partially loose.   Hmm..  seems someone didn&#8217;t put my computer back together quite that well after replacing the motherboard.  As far as I can see though, the connection is tight and all the way in.  It just isn&#8217;t staying in.   Bad design?</p>
<p>Anywho, instead replacing a pricey inverter or back light or controller card for the LCD, it was just a partially loose connection.  And to think, I could have sent it back to Acer to be fixed for another $20 and 2 weeks.</p>
<p>(Edit Dec 22 2007)</p>
<p>I notice a lot of people finding this page when searching for &#8216;acer white screen&#8217;.Â  From my findings, a white screen means that your LCD cable has partially loose.Â Â  Either take it into a shop to get fixed, or do it yourself.Â  To do it yourself, you probably need to open the top of your laptop above the keyboard.Â  Unplug the LCD connection and plug it back it, and secure it in place somehow.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2007/07/18/white-screen-of-death/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Faster flash</title>
		<link>http://blog.shimpossible.com/2007/05/02/faster-flash/</link>
		<comments>http://blog.shimpossible.com/2007/05/02/faster-flash/#comments</comments>
		<pubDate>Wed, 02 May 2007 17:24:56 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/2007/05/02/faster-flash/</guid>
		<description><![CDATA[I have been working on a project in Adobe flash for the last few months. It basically does a ton of animation and math on every screen. So i put all the code in onEnterFrame, the standard way to run continuous code, so it ran on each frame. Over the months I have done by [...]]]></description>
			<content:encoded><![CDATA[<p>I have been working on a project in Adobe flash for the last few months.  It basically does a ton of animation and math on every screen.  So i put all the code in onEnterFrame, the standard way to run continuous code, so it ran on each frame.  Over the months I have done by best to optimize it for speed and efficiency but it had still  ran slow, and used a lot of CPU power.   How can I make it faster I thought?</p>
<p>The first most obvious thing to try was to up the FPS.  Flash defaults it to 12, which seems rather slow for animation doesn&#8217;t it?   So I updated it to 30, which didn&#8217;t seem the to change anything.  60fps offered a slight improvement.  So it logically concludes that 120fps (the max you can set in flash) would give the best performance, and it seemed like it did.  But the program was still using a ton of CPU power, and had a very low fps.</p>
<p>Was there a way i could squeeze a few more fps out of the program?  Well there happens to be a function called updateAfterEvent()  which basically tells the program to issue another screen refresh immediately.  The only problem is that you can only call it from certain functions., one of which is NOT onEnterFrame.   It would only work if called from an event, such as a mouse movement or key press.  It also would work for a periodically ran function called through setInterval().   This looked like the option to try.</p>
<p>The function has this basic format:</p>
<p>setInterval(function, ms, params)</p>
<p>It takes a pointer to a function, the number of milliseconds in between calls to the function, and any params you would like to send it.  That sounded wonderful, just set a really low ms, and pass in the function that does the same thing my current onEnterFrame does, and it should run as fast as I want.   Well, not really.   The one gotcha with this function is that it will only call the function at most, as often as onEnterFrame is called.  The resolution of this timer is only as fine as the FPS in the movie.  Even if i set the ms to 0, it would still only be called as often as onEnterFrame.  Back the square one again, or was I?  The new difference now was that I could call updateAfterEvent in the function, because it was called through setInterval.  Yea!   So i put the new code in, removed the old onEnterFrame, left out updateAfterEVents, set the ms to 10 and watched to see if there was any improvement.</p>
<p>Suddenly there was a 10x speed up.  The animation wasn&#8217;t as sluggish and it responded to the uses key presses immediately.  That didn&#8217;t make any sense.  The same amount of code was being ran on every frame, the program should run the same speed.  I tried changing the ms down to 1 and the animation only got faster.  I changed the FPS of the program down to 12, and the animation was still just as smooth.   I wondered if the function was being called more often the the screen updated, so I turned the FPS down to 1.   A FPS of 1 resulted in jerky animation just as expected.  So the function was still only being called as often as the screen updated.  Turning the fps back to 12 (the default).  I ran the program and watched the cpu usage, it was only around 10-20%, instead of the whopping 70% it was using before.   Changing the FPS had the most effect on the CPU usage.  A higher FPS resulted in more processor usage.   But changing the MS in setInterval had barely  any effect on CPU usage.   Hmmm&#8230;.  So i went around and changed all my onEnterFrame to setInterval code.   This resulted in a much more responsive program, that had lower CPU usage.   I still don&#8217;t know what the problem with putting code in onEnterFrame is, but I dont think I&#8217;ll be useing onEnterFrame much anymore.</p>
<p>The moral of the story:</p>
<p>DO NOT USE onEnterFrame in flash, its slow.  Only use it if you absolutely must, but I don&#8217;t know why you would.<br />
Changing the FPS does NOT result in faster code.  Only MORE CPU usage.<br />
USE setInterval(&#8230;)  instead with a lowish FPS (such as 12)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2007/05/02/faster-flash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Non-repeat random playlist in constant time?</title>
		<link>http://blog.shimpossible.com/2007/01/13/non-repeat-random-playlist-in-constant-time/</link>
		<comments>http://blog.shimpossible.com/2007/01/13/non-repeat-random-playlist-in-constant-time/#comments</comments>
		<pubDate>Sat, 13 Jan 2007 06:55:14 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/2007/01/13/non-repeat-random-playlist-in-constant-time/</guid>
		<description><![CDATA[I&#8217;m sure you have all heard about the problems with certain mp3 players and their &#8216;random&#8217; play lists. Some people think they repeat some songs more than others. Well here a way to get a truly random shuffle of the list in constant time. That means it takes the same amount of time no matter [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure you have all heard about the problems with certain mp3 players and their &#8216;random&#8217; play lists.  Some people think they repeat some songs more than others.  Well here a way to get a truly random shuffle of the list in constant time.  That means it takes the same amount of time no matter how big the list is.</p>
<p>This class takes takes an Array of items (data) and divides it into two sections.  The first section is the already randomized items, the other is the items that haven&#8217;t been shuffled yet.  When you request the next item, it picks a random item from the remaining set and swaps it onto the end of the shuffled set.  When the non-shuffled set is empty, it starts from the beginning of the array again.  At this point the whole array would have been shuffled already so no more swapping will be needed.<br />
Here is the entire class in C#<br />
class Shuffle<br />
{<br />
Random r = new Random();<br />
Object[] data;</p>
<p>int split;  // where the split is in the array<br />
bool shuffled = false;  // has the entire array been shuffled<br />
public Shuffle(Object[] q)<br />
{<br />
split = 0;<br />
data = q;<br />
}</p>
<p>public Object Next()<br />
{<br />
if (split == data.Length)<br />
{<br />
split = 0;<br />
shuffled = true;<br />
}</p>
<p>if (!shuffled)<br />
{<br />
// find the next one<br />
int idx = r.Next(data.Length &#8211; split);<br />
// swap the positions<br />
Object tmp = data[split];<br />
data[split] = data[idx];<br />
data[idx] = tmp;<br />
}</p>
<p>return data[split++];<br />
}<br />
}This class shuffles the list in constant time.  It only shuffles when a new item is requested from the list, and only shuffles that one item.  This is many many times faster than shuffling all the items at once.</p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2007/01/13/non-repeat-random-playlist-in-constant-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combination Generation</title>
		<link>http://blog.shimpossible.com/2006/12/23/combination-generation/</link>
		<comments>http://blog.shimpossible.com/2006/12/23/combination-generation/#comments</comments>
		<pubDate>Sat, 23 Dec 2006 17:52:34 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/2006/12/23/combination-generation/</guid>
		<description><![CDATA[I ran across a wonderful article a while ago about a Combination / Permutation class that generated them as a stream.Â  It&#8217;s a great idea.Â  It makes unit testing pretty easy, as you can have the class make sure you hit all your test cases.Â  Sadly I wasn&#8217;t able to find the article again, at [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across a wonderful article a while ago about a Combination / Permutation class that generated them as a stream.Â  It&#8217;s a great idea.Â  It makes unit testing pretty easy, as you can have the class make sure you hit all your test cases.Â  Sadly I wasn&#8217;t able to find the article again, at least I don&#8217;t think i was able to find it.</p>
<p>Anyway, I went a made my own combination generator.Â  For those wondering what a Combination is, its arranging K items from a set of N items, order doesn&#8217;t matter.Â  So given 5 items, and only taking 3 at a time you would get</p>
<p>0,1,2<br />
0,1,3<br />
0,1,4<br />
0,2,3<br />
0,2,4<br />
0,3,4<br />
1,2,3<br />
1,2,4<br />
1,3,4<br />
2,3,4<br />
The way this works is quite simple.Â  Each column has a min and max value that it can hold.Â  The first column goes from 0-2 (or 0 to (N-K) ), the last column goes from N-K to N-1.Â  You can easily generalize this into column C can hold a value from C to N-K+C.Â  The value of N-K can be precomputed.Â  Armed with this knowledge you can now make all your combinations.Â  First start with the identity (0,1,2,&#8230;,n).Â  To get the next combination, start at the right most item, and move left until you hit the first item that can be incremented, in other words, the value is between C and N-K+C.Â  Increment this value, then move back right and set each of these columns equal to 1 more than the item at its left (ieÂ Â  c[i] = c[i-1]+1).</p>
<p>So given 0,3,4,Â  we start at the right (4).Â  4 is the max value for this column, so we move left.Â  3 is also the max value for this column, so we move left again.Â  0 is between 0 and 2, so we can still increment it.Â  We increment it to 1.Â  Now the array is 1,3,4.Â  Next we go back right and add set each column to one more than the column at its right, so we change the 3 into 1+1, which is 2.Â  We change the 4 into 2+1, which is 3.Â  This leaves us with the next combination of 1,2,3.</p>
<p>The last combination has the pattern of the first item equal to N-K (or 2 in our case).Â  When we reach this case we stop.</p>
<p>Ok, thats my little walk through on combinations. I&#8217;ll put something up about permutations next</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2006/12/23/combination-generation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>n-jugs</title>
		<link>http://blog.shimpossible.com/2006/11/09/n-jugs/</link>
		<comments>http://blog.shimpossible.com/2006/11/09/n-jugs/#comments</comments>
		<pubDate>Thu, 09 Nov 2006 19:35:49 +0000</pubDate>
		<dc:creator>shim</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://blog.shimpossible.com/2006/11/09/n-jugs/</guid>
		<description><![CDATA[Given a set of n jugs of different sizes, how would you dump water between them to get a specific amount? If you have ever watched Diehard III, you might have seen this with 2 jugs. In the movie they are given 4 and 5 gallon jugs and are asked to make 3 gallons. I [...]]]></description>
			<content:encoded><![CDATA[<p>Given a set of n jugs of different sizes, how would you dump water between them  to get a specific amount?   If you have ever watched  Diehard III, you might have seen this with 2 jugs.  In the movie they are given 4 and 5 gallon jugs and are asked to make 3 gallons.</p>
<p>I originally tried to write a program to solve this problem using a brute force method.  Needless to say, this was very slow and almost never ran to completion.  I thought about it for a while and finally figured it out.  It boils down to just solving a graph of connected nodes.  Since I wanted to learn ruby, I used this to learn the language.</p>
<p>The basic idea to solve this is:</p>
<p>Have we reach the end state (desired amount)?<br />
Loop through each from jug (i)<br />
Loop through each to jug (j)<br />
Calculate the new state of the jugs (water level in each)<br />
Have we seen this &#8220;state&#8221; before?  If not, store the new state.  If we have, then move on<br />
If this is a new state, go to step 1 with new state<br />
old state, continue looping</p>
<p>Obviously, this wont find the optimal solution, but it will find a solution.   If you want to find the optimal, you will have to store the steps to get to a state, and see if you have found a quicker path to a state.   If you want, I still have the ruby file to solve this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shimpossible.com/2006/11/09/n-jugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
