<?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>bc-programming</title>
	<atom:link href="http://bc-programming.com/blogs/feed/" rel="self" type="application/rss+xml" />
	<link>http://bc-programming.com/blogs</link>
	<description>Programming, Possums, Ponies, and why you shouldn&#039;t mix any two.</description>
	<lastBuildDate>Wed, 16 May 2012 23:06:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Wallpaper Change Script: Gnome 3</title>
		<link>http://bc-programming.com/blogs/2012/05/wallpaper-change-script-gnome-3/</link>
		<comments>http://bc-programming.com/blogs/2012/05/wallpaper-change-script-gnome-3/#comments</comments>
		<pubDate>Wed, 16 May 2012 23:06:38 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=992</guid>
		<description><![CDATA[One of the most aggravating things when I switched to Linux Mint 12 from Mint 10 was that it seemed to offer a step backwards for a lot of functionality. I was using a program called &#8220;Desktop Drapes&#8221; to periodically cycle my wallpaper, but it no longer worked in Gnome 3. (there were some supposed [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/wallpaper-change-script-gnome-3/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>One of the most aggravating things when I switched to Linux Mint 12 from Mint 10 was that it seemed to offer a step backwards for a lot of functionality. I was using a program called &#8220;Desktop Drapes&#8221; to periodically cycle my wallpaper, but it no longer worked in Gnome 3. (there were some supposed plugins to fix this but they didn&#8217;t work). I tried a few others, but they universally failed.</p>
<p>&nbsp;</p>
<p>So I ended up writing a python script to do the job:</p>
<p>&nbsp;</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#!/usr/bin/python</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1">#Copyright (c) 2011 BASeCamp Corporation</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># All rights reserved.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="co1">#</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># Redistribution and use in source and binary forms, with or without</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># modification, are permitted provided that the following conditions</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># are met:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1">#</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="co1"># Redistributions of source code must retain the above copyright notice,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># this list of conditions and the following disclaimer.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1">#</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># Redistributions in binary form must reproduce the above copyright</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># notice, this list of conditions and the following disclaimer in the</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="co1"># documentation and/or other materials provided with the distribution.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1">#</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># Neither the name of the project&#8217;s author nor the names of its</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># contributors may be used to endorse or promote products derived from</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># this software without specific prior written permission.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="co1">#</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="co1"># HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="co1"># NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="co1"># SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">import</span> <span class="kw3">sys</span>,<span class="kw3">os</span>,<span class="kw3">random</span>,<span class="kw3">ConfigParser</span>,<span class="kw3">string</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">from</span> <span class="kw3">optparse</span> <span class="kw1">import</span> OptionParser</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#Wallpaper cycling script</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#written Sunday March 18th 2012</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#designed to be added to crontab with a delay</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">#example line in crontab:</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#*/4 * * * * /home/bc_programming/bin/cyclebg.py</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#when run, it will change the wallpaper to the next one in the cycle and exit.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#Author: BC_Programming</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#location of data path. we keep track of the wallpapers we have already cycled there.</span></div>
</li>
<li class="li1">
<div class="de1">datapath = <span class="kw3">os</span>.<span class="me1">path</span>.<span class="me1">expanduser</span><span class="br0">&#40;</span><span class="st0">&quot;~/.cyclebg/&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">datafile = datapath + <span class="st0">&quot;cycled_data.dat&quot;</span></div>
</li>
<li class="li2">
<div class="de2">wallpaperfile = datapath + <span class="st0">&quot;wallpapers.dat&quot;</span></div>
</li>
<li class="li1">
<div class="de1">disablefile = datapath + <span class="st0">&quot;disable.dat&quot;</span> <span class="co1">#if this file exists we are &quot;disabled&quot; and don&#8217;t change the wallpaper.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#command to run to change the bg.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#following is for Gnome 2</span></div>
</li>
<li class="li2">
<div class="de2"><span class="co1">#bgchangecmd = &#8216;dconf gsettings set org.gnome.desktop.background picture-uri &quot;file://%s&quot;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#gnome 3 command&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">bgchangecmd = <span class="st0">&#8216;gsettings set org.gnome.desktop.background picture-uri &quot;file:///%s&quot;&#8217;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#we need two lists: the list of possible wallpapers, and the wallpapers we&#8217;ve already listed.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> isdisabled<span class="br0">&#40;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> <span class="kw3">os</span>.<span class="me1">path</span>.<span class="me1">exists</span><span class="br0">&#40;</span>disablefile<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#returns the list of wallpapers we&#8217;ve already cycled through.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> getusedlist<span class="br0">&#40;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw1">not</span> <span class="kw3">os</span>.<span class="me1">path</span>.<span class="me1">exists</span><span class="br0">&#40;</span>datafile<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">#if the file doesn&#8217;t exist, then return an empty list.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> &nbsp;<span class="br0">&#91;</span><span class="br0">&#93;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">else</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">list</span><span class="br0">&#40;</span><span class="kw2">open</span><span class="br0">&#40;</span>datafile<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#clears the used wallpapers list.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> clearused<span class="br0">&#40;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw3">os</span>.<span class="me1">path</span>.<span class="me1">exists</span><span class="br0">&#40;</span>datafile<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">os</span>.<span class="me1">remove</span><span class="br0">&#40;</span>datafile<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> markused<span class="br0">&#40;</span>wallpaper<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#mark a wallpaper as viewed</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; writelist = <span class="kw2">open</span><span class="br0">&#40;</span>datafile,<span class="st0">&#8216;a&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; writelist.<span class="me1">write</span><span class="br0">&#40;</span>wallpaper<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">#returns all the wallpapers we are to cycle through.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> getwallpapers<span class="br0">&#40;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw1">not</span> <span class="kw3">os</span>.<span class="me1">path</span>.<span class="me1">exists</span><span class="br0">&#40;</span>wallpaperfile<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">return</span> &nbsp;<span class="br0">&#91;</span><span class="br0">&#93;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">else</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">return</span> <span class="kw2">list</span><span class="br0">&#40;</span><span class="kw2">open</span><span class="br0">&#40;</span>wallpaperfile<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co1">#routine that changes the background.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> changebackground<span class="br0">&#40;</span>picture<span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#changes the background to the specified image.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; cmdrun =bgchangecmd</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; cmdrun = cmdrun.<span class="me1">replace</span><span class="br0">&#40;</span><span class="st0">&quot;%s&quot;</span>,picture<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot; running &quot;</span>,cmdrun</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">os</span>.<span class="me1">system</span><span class="br0">&#40;</span>cmdrun<span class="br0">&#41;</span> &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> isdisabled<span class="br0">&#40;</span><span class="br0">&#41;</span>: <span class="kw3">sys</span>.<span class="me1">exit</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw3">sys</span>.<span class="me1">exit</span><span class="br0">&#40;</span>changewallpaper<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">def</span> changewallpaper<span class="br0">&#40;</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#retrieve the wallpapers and used wallpapers&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; used = getusedlist<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; papers = getwallpapers<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#exit if no wallpapers in main list&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>papers<span class="br0">&#41;</span>==<span class="nu0">0</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;No wallpapers! add wallpaper file paths to &quot;</span>, wallpaperfile, <span class="st0">&quot;!&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#now, remove all entries in papers that exist in used.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; removethese= <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;<span class="co1">#list of items to remove</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> iterateused <span class="kw1">in</span> papers:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> iterateused <span class="kw1">in</span> used:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;removethese = removethese + &nbsp;<span class="br0">&#91;</span>iterateused<span class="br0">&#93;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#now, remove the items&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span> iterateremove <span class="kw1">in</span> removethese:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; papers.<span class="me1">remove</span><span class="br0">&#40;</span>iterateremove<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>papers<span class="br0">&#41;</span>==<span class="nu0">0</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">#if papers list is empty, egads! clear the used list, since we&#8217;ve cycled through them all evidently.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; clearused<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">#and&#8230; well balls to it we&#8217;ll just call ourself.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; changewallpaper<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#now, the main logic. Choose a wallpaper&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; choosefrom = papers</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#shuffle the choosefrom listing&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">random</span>.<span class="me1">shuffle</span><span class="br0">&#40;</span>choosefrom<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">#take one of the items from the resulting shuffled list.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; chosen = choosefrom.<span class="me1">pop</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#add it to the list of seen wallpapers&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; markused<span class="br0">&#40;</span>chosen<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">#and attempt to change to it&#8230;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; changebackground<span class="br0">&#40;</span>chosen<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co1">#parse our options/arguments&#8230;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">parser</span> = OptionParser<span class="br0">&#40;</span>usage=<span class="st0">&quot;usage: %prog &nbsp;[options] &nbsp;&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; version=<span class="st0">&quot;%prog 1.0&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">parser</span>.<span class="me1">add_option</span><span class="br0">&#40;</span><span class="st0">&quot;-g&quot;</span>,<span class="st0">&quot;&#8211;gui&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">action=<span class="st0">&quot;store_true&quot;</span>, dest=<span class="st0">&quot;gui&quot;</span>, default=<span class="kw2">False</span>,</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">help</span>=<span class="st0">&quot;run GUI&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">global</span> options,args</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span>options,args<span class="br0">&#41;</span> = <span class="kw3">parser</span>.<span class="me1">parse_args</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">main<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The script is designed to be used by adding a like to crontab. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/wallpaper-change-script-gnome-3/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=992&amp;md5=8b1350b4fa797d0c7a27db710178b125" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/05/wallpaper-change-script-gnome-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F05%2Fwallpaper-change-script-gnome-3%2F&amp;language=en_GB&amp;category=software&amp;title=Wallpaper+Change+Script%3A+Gnome+3&amp;description=One+of+the+most+aggravating+things+when+I+switched+to+Linux+Mint+12+from+Mint+10+was+that+it+seemed+to+offer+a+step+backwards+for+a+lot+of+functionality....&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>BC&#8217;s storytime</title>
		<link>http://bc-programming.com/blogs/2012/05/bcs-storytime/</link>
		<comments>http://bc-programming.com/blogs/2012/05/bcs-storytime/#comments</comments>
		<pubDate>Thu, 10 May 2012 16:48:30 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=990</guid>
		<description><![CDATA[A Forum post I&#8217;ve decided to graduate to the enviable position of Blog post, Because it reaches my minimum de facto word count and because I haven&#8217;t had humourous stories for a while. For the record the first part is true, but I amend the story with a possible TV series adaptation. RANDOM STORY TIME. [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/bcs-storytime/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>A Forum post I&#8217;ve decided to graduate to the enviable position of Blog post, Because it reaches my minimum de facto word count and because I haven&#8217;t had humourous stories for a while. For the record the first part is true, but I amend the story with a possible TV series adaptation.</p>
<p>RANDOM STORY TIME. this happened almost 10 years ago (9, really) so it might not be 100% accurate, but it was pretty epic. Though it doesn&#8217;t have much to do with this topic. Than again this is a rather odd topic to find on this forum regardless so I thought I&#8217;d give it some content. No offense.</p>
<p>One time I was at a grocery store. Well, that&#8217;s not true. I&#8217;ve actually been to them several times, but this was one specific instance. Anyway, what I was getting is neither important, nor do I remember it, though I do remember I also got a few peanuts. This was odd because I&#8217;m not usually a peanut person and also because it helped me in a &#8220;crowning moment of awesome&#8221; to quote tvtropes. Anyway, as luck would have it one of the registers was staffed by one of my friends I had been working up the nerve to ask out. In actual fact that was partially why I chose that store, though the main reason was it was the closest. Nonetheless, opportunity smiled upon me. As I waited in line the customers ahead of me turned out to know her. (Which isn&#8217;t surprising, they turned out to be two of those pretentious and inexplicably popular girls that every high school seems to have, though not to the scale that TV and movies seem to suggest. Nonetheless,  They were your typical shallow B-words. Anyway In typical B-word fashion their exchanges with her consisted primarily of passive-aggressive comments. Some high-school crap which I don&#8217;t remember and wasn&#8217;t important, and was certainly &lt;highly&gt; inappropriate given the setting, but of course being the queen B-words of the school they felt they were above all things. I saw my chance when they said something to the accord of &#8220;how do you expect guys to notice you if you don&#8217;t look  <em> beautiful </em> &#8221; and with that last word she did one of those idiotic things where she shook her hair around. Pretentious git. (This made no sense because the girl in question was pretty bloody good looking if you ask me) I jumped into this, much to all three of their surprise since I was usually the quiet type. &#8220;Well, let&#8217;s see, I&#8217;ve talked to both of you (addressing the &#8220;customers&#8221;) and I&#8217;ve talked to her. With her I&#8217;ve had interesting conversations on a variety of subjects and issues, some of which she is clearly my intellectual better; with you two, on the other hand, I believe I would have had an equally intellectual conversation if I was to tape two cantaloupes to a piece of cardboard. Obviously, I cannot speak for all guys, but for myself I&#8217;d much rather associate with and be seen with somebody smart like her (at which point I pointed at the girl behind the counter) than either of you two, who on a good day might be able to combine your abilities to match this container of peanuts at checkers with some extensive tutelage&#8221;.</p>
<p>CROWNING MOMENT OF AWESOME. When I think of school, that event is the first thing I think of. Naturally, all three of them were speechless. I remember hearing the one person who was behind me in line giggling. They were already done their transaction so they left, and she started to ring me through. &#8220;So what am I smarter than you at, exactly?&#8221;. &#8220;That, you will need to find out on your own.&#8221;.<br />
e<br />
After that event, which was one of those stories that just BEGS to be told, our circle of friends started referring to those other two girls as &#8220;the cantaloupes&#8221;. It was awesome. Then she moved away and I haven&#8217;t seen her since. The end. It&#8217;s beautiful. I was considering having it made into a television series. With the appropriate plot changes to incorporate explosions. My idea was that it would occur in winter and somehow in the ten minutes I was in the store the entire thing would be covered and snowed in. I&#8217;m sure I can cover that with some stupid psychobabble. Maybe I could have a robot that I built that is an expert at human-cyborg relations to explain it. Anyway basically what happens is the weight on the ceiling is intense enough to cause a rafter from the high ceiling to break loose. I would swoop in and grab all three of them and take them to safety, without breaking a sweat, thanks to being ten feet tall and built like a tank. At this point a variant of the above scenario would occur, possibly ending with the drinking of maple syrup. As time wears on the individuals locked inside form alliances and separate into different tribes. dictated mostly by which areas they occupy. For example, there would be the produce tribe, the dairy tribe, and the delicatessen tribe. I make a nemesis, Tim, too. During the second season, everything changes however as we discover that the grocery store is in fact a starship, however the bridge of the ship is in one of those &#8220;employees only&#8221; doors and as such is protected by a magical force field allowing only employees to enter. This is where my character&#8217;s love interest (my past one, but no matter) comes in.(Oh yeah all the other people working there were turned into soup late in the first season by the raiders from the noodle isle.). She&#8217;s the only one that can go through the force field, but the moment she steps through it, the U.S.S Quality goes on red alert, which alerts the manager to her presence; he&#8217;s been locked in his office since the start of the series locked in his office, because he has a rare disorder that makes him want to tie people up and take them hostage for no reason. So of course she get&#8217;s captured and I yell one of those NOOOO things. I was thinking the scene could be a Piano solo by Randy Newman, much like the series intro. Of course, because I&#8217;m 10 feet tall and built like a tank (much like my real life self, of course) I manage to use the power of love to break through the employees only force field, with a little help from a bazooka I find in the firearms isle (I guess it&#8217;s an American grocery store), and an experimental laser I find in the spice isle, which is a clear case where somebody decided they didn&#8217;t want something and just plopped it on the nearest shelf. This only makes me angrier, of course. So I manage to break through and get the Manager to surrender his prisoner. At this point there is a tearful scene:</p>
<p>BC Played by The Rock: I&#8217;m here to rescue you! haha, I&#8217;ve always wanted to say that. Seriously though you alright? The manager didn&#8217;t try to force you to do anything cruel or unnatural, like watch I Love Lucy or anything, did he?<br />
BC&#8217;s old love interest whose name started with a K but I&#8217;ve embarrassingly forgotten:You&#8217;ve changed!<br />
BC Played by the Rock: yeah, I was able to find a change of clothes. It was touch and go there for a while, glad you noticed.<br />
K: No, I mean you&#8217;re different now. Now you use violence to solve problems instead of before, where you just sort of let other people deal with it<br />
BC Played by the Rock: yeah, it rules<br />
*this part would be in the spoiler commercial*<br />
K: I&#8217;m pregnant.<br />
BC: Cool, I don&#8217;t remember the fun part of that, though, which concerns me. Was it really that bad that I blocked it out of my memory?<br />
K: It&#8217;s Tim&#8217;s<br />
BC: Oh, that&#8217;s a relief. I was worried for a momen&#8230;. HEY WAIT A SECOND.</p>
<p>And cue ending credits of that episode. naturally played by Randy Newman.</p>
<p>I think it holds real promise myself. It&#8217;s better than Lost, at least. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/bcs-storytime/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=990&amp;md5=89d01198dd000db0140a20c5f977768b" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/05/bcs-storytime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F05%2Fbcs-storytime%2F&amp;language=en_GB&amp;category=software&amp;title=BC%26%238217%3Bs+storytime&amp;description=A+Forum+post+I%26%238217%3Bve+decided+to+graduate+to+the+enviable+position+of+Blog+post%2C+Because+it+reaches+my+minimum+de+facto+word+count+and+because+I+haven%26%238217%3Bt+had+humourous+stories...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>WordPress, Sourcecode, and escaping</title>
		<link>http://bc-programming.com/blogs/2012/05/wordpress-sourcecode-and-escaping/</link>
		<comments>http://bc-programming.com/blogs/2012/05/wordpress-sourcecode-and-escaping/#comments</comments>
		<pubDate>Sun, 06 May 2012 05:41:53 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[General Computing]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=987</guid>
		<description><![CDATA[Every single time I write a post that contains source code, it&#8217;s a bloody struggle. Take my previous &#8220;snippets&#8221; entry. Normally, I draft it and plan it in advance, and use Editpad Pro on my windows machine to write it out. Now, in a ideal universe where stuff works properly, I would be able to [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/wordpress-sourcecode-and-escaping/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>Every single time I write a post that contains source code, it&#8217;s a bloody struggle.</p>
<p>Take my previous &#8220;snippets&#8221; entry.</p>
<p>Normally, I draft it and plan it in advance, and use Editpad Pro on my windows machine to write it out. Now, in a ideal universe where stuff works properly, I would be able to just paste the code in without putting dumbass escapes- that&#8217;s what the CMS is supposed to do, manage my content- but no such luck. So, I replace all &lt; with &lt; and all &gt; with &gt; and all that drollish crap that people use software to avoid. It&#8217;s especially fun when the blog helpfully assumes I am using some secret tag when I use generics.</p>
<p>Anyway, first problem: well, some of my swapping was wrong. Second, everything was double-spaced thanks to the magic of me making the original textfile on a windows machine (crlf versus just cr).</p>
<p>I had to actually get the original source from the project I based the post on, perform the replacement, paste it into the proper tag, and hope the gods of blog software were smiling upon me.</p>
<p>Long story short- Putting code in posts is more painful than it should be. I sort of expected the add-in I use for highlighting and code blocks to have been smart enough to properly mark-up the text itself without intervention, but no such luck. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/wordpress-sourcecode-and-escaping/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=987&amp;md5=1ddcd9e341212b84b5c7b7df357e6696" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/05/wordpress-sourcecode-and-escaping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F05%2Fwordpress-sourcecode-and-escaping%2F&amp;language=en_GB&amp;category=software&amp;title=WordPress%2C+Sourcecode%2C+and+escaping&amp;description=Every+single+time+I+write+a+post+that+contains+source+code%2C+it%26%238217%3Bs+a+bloody+struggle.+Take+my+previous+%26%238220%3Bsnippets%26%238221%3B+entry.+Normally%2C+I+draft+it+and+plan+it+in+advance%2C+and...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>A Linux Rant</title>
		<link>http://bc-programming.com/blogs/2012/05/a-linux-rant/</link>
		<comments>http://bc-programming.com/blogs/2012/05/a-linux-rant/#comments</comments>
		<pubDate>Fri, 04 May 2012 07:25:01 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=979</guid>
		<description><![CDATA[Additional edit: This is exactly the same as what I posted on FB, so if you saw it there already, it doesn&#8217;t offer anything new Disclaimer: Linux is fine, but sometimes the little things just piss you off cumulatively. Linux Mint 12&#8242;s Wifi makes for an interesting case study. The connection is unstable and drops [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/a-linux-rant/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p> <i> Additional edit: This is exactly the same as what I posted on FB, so if you saw it there already, it doesn&#8217;t offer anything new </i> </p>
<p>Disclaimer: Linux is fine, but sometimes the little things just piss you off cumulatively. </p>
<p>Linux Mint 12&#8242;s Wifi makes for an interesting case study. The connection is unstable and drops out a lot. Each time that happens it pops up a notification at the bottom of the screen. In fact it&#8217;s showing right now &#8220;Disconnected &#8211; you are now offline&#8221;. The fun part is that the notification bar thing it has will even interfere with other windows. I thought I had broken X (the windowing environment) because no menus would show, but once I closed the notification suddenly it worked again.</p>
<p>My favourite part is that the connection/authorization dialog exists twice. First as a standard dialog. But also as this other weird themed dialog that grays the rest of the screen for some reason. Aside from the second one looking like the text was laid out by a mentally retarded koala bear (the text doesn&#8217;t even line up with the outline of the textbox, probably because I had the utter gall to change the system font from the default). Aside from that, there is no difference.</p>
<p>of course they both pop up while I&#8217;m in the middle of typing something, steal the focus, and then I start typing in the password box, you know, because even though it already has the password it has to ask me again just to be sure. then I have to backspace and retype it again.</p>
<p>And the best part is if I leave it unattended with the wifi on.</p>
<p>If I leave it for a few hours, and come back, it will literally be filled with authorization required dialogs. there was one time where I actually sat there for at least a good minute cancelling those dialogs. I ended up forcing everything closed by force-quitting the X-Server. The pattern is always the same, too.</p>
<p>-Leave system with wifi enabled<br />
-do something<br />
-come back to a bajillion &#8220;authorization required&#8221; dialogs. meanwhile, the connection is still at 2 signal bars so what it is talking about I&#8217;ve no clue. Usually I cannot actually see the desktop because at least half of those bajillion dialogs are the one that fades the screen for no reason, and when you have that many of them the background is just black.</p>
<p>So I press OK on the one on top, which helpfully has the password filled in already, making one wonder why the hell it&#8217;s asking me in the first place. This reveals of course the bajillionty other authorization dialogs of the &#8220;standard&#8221; style, all on top of one another. However for about 5 seconds absolutely nothing responds to mouse clicks, at which point I&#8217;m suddenly looking at a desktop with nothing but my wallpaper for another 5 seconds, until suddenly all the windows reappear, in what seems to be a random order. And then I have to click on the taskbar thing at the bottom to select one of the authorization required dialogs, press ok, press another, press OK, ad infinitum. It&#8217;s downright bloody ridiculous.</p>
<p>The more I use it the more convinced I become that Linux is nothing more than a hodgepodge of components for which interoperability is a gamble. (Does Program X work with Gnome? with ratpoison? does it work with KDE? Roll the dice and find out, assuming you can even get the damned thing to compile with the equally random collection of libraries you have). The fact that the whole &#8220;it&#8217;s more customizable&#8221; argument is utter tosh because most Linux zealots idea of &#8220;customization&#8221; is either changing the desktop wallpaper to a single background or writing some idiotic desktop panel applet, which will break, of course, the next time the desktop environment you wrote it for updates. Much like Desktop Drapes. &#8220;oh cool,a program to slideshow wallpapers&#8221; I thought, which was nice to have, and it worked well. With Mint 12 and Gnome 3 of course that program is utterly useless. It still launches and thinks it&#8217;s doing something but the wallpaper never changes because some retard decided that Gnome 3, for whatever reason, would disregard anything coming from a program trying to access it using the Gnome 3 functionality, why? I don&#8217;t know. maybe it regards those programs as uncultured. The end result for me was that, by upgrading from Mint 10 to Mint 12, I lost the ability to have a wallpaper slideshow. A feature that should be built into the god damned operating System. So what I ended up having to do was write a python script to do it, which was not fun because programming against any desktop environment seems to involve setting random environment variables and crossing your fingers that some random daemon running on the system will do something useful with that change, then discovering that was the &#8220;old way&#8221; and Gnome 3 does it a &#8220;better way&#8221; and by better way they mean that it does the exact same thing but requires a more verbose syntax and relying on yet another external program which itself probably just sets an environment variable. meanwhile, as I write this, the authorization dialog once again jumped at me, insisting I give it information it already bloody well has, so I had to delete a entire phrase I had typed, retype the password, and press enter. Then I wrote the previous sentence, at which point the notification dialog popped up informing me that it was unable to connect. Well thanks I figured that out when you showed me that dialog designed by a banana plant, thank you very much. And the mouse was near the bottom of the screen so for reasons that are beyond the comprehension of us mere mortals the notification managed to steal the focus as well. After breaking from my chain of thought to deal with the most idiotic and pain in the ass UX I have ever had the displeasure of having to use, the authorization dialog popped up once more, even though the connection had been established (which again, flies in the face of the &#8220;connection failed&#8221; which it had JUST BLOODY TOLD ME). If you are going to notify me of something, make sure that notification is actually bloody accurate and doesn&#8217;t come ten minutes after I disconnected and reconnected, because then it makes you seem a little slow. &#8220;OH! you disconnected! your OFFLINE!&#8221; Yes, thank you Network manager, that was ten minutes ago &#8220;OH I NEED your PASSWORD for this access point even though I already have it thought I&#8217;d make sure&#8221;, &#8220;oh ok, here it is&#8221; &#8220;ok, now just enter it about 500 more times in these other dialogs I put up in the last hour while you were away.&#8221;</p>
<p>This all combines nicely with the absolute refusal of syndaemon to do what it is supposed to, which is make it so while I am typing taps on the touchpad don&#8217;t move the cursor. Instead what it&#8217;s function seems to be, even though I set it to disable tap-clicks for 5 seconds, is randomly make it so the mouse doesn&#8217;t move. I&#8217;ve had to kill X to get the mouse working, and sometimes that doesn&#8217;t work. The word &#8220;working&#8221; in my last sentence was helpfully inserted into the authorization dialog that decided to steal the damned window focus for the millionth bloody time. </p>
<p>Of course, the answer in the Linux community is to move to another upstream. &#8220;Oh, Mint is going to go with Debian instead of Ubuntu as a base&#8221;. Well, that&#8217;s great, but I don&#8217;t see how that solves the elementary UX problems like notifications and random unnecessary dialogs stealing the bloody keyboard focus for no good reason at all, or popping up the same dialog a MILLION BLOODY TIMES. How hard was it to put  in a bit of code to check if you were already asking for a password? Actually, how about if you already have a saved password, you don&#8217;t prompt at all, that would be great. And it would be just awesome if it actually tried to connect to the access point I told it to and for which I&#8217;ve given it the password, instead of going &#8220;OH I found this other access point with a higher signal so I&#8217;ll just give it the password you gave me for the other one&#8221; and then it comes back with a look of surprise when it discovers the password isn&#8217;t in fact a skeleton key for all the access points in the area but only for the one access point I told it to bloody connect to. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/a-linux-rant/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=979&amp;md5=722e87dbabb8e8ef028d094a4fd98f10" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/05/a-linux-rant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F05%2Fa-linux-rant%2F&amp;language=en_GB&amp;category=software&amp;title=A+Linux+Rant&amp;description=Additional+edit%3A+This+is+exactly+the+same+as+what+I+posted+on+FB%2C+so+if+you+saw+it+there+already%2C+it+doesn%26%238217%3Bt+offer+anything+new+Disclaimer%3A+Linux+is+fine%2C+but...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>Snippets.</title>
		<link>http://bc-programming.com/blogs/2012/05/snippets/</link>
		<comments>http://bc-programming.com/blogs/2012/05/snippets/#comments</comments>
		<pubDate>Thu, 03 May 2012 22:58:10 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[General Computing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Choose]]></category>
		<category><![CDATA[ClampValue]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[IComparable]]></category>
		<category><![CDATA[Select]]></category>
		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=975</guid>
		<description><![CDATA[Naturally, as we write programs we create a small set of useful functions. BASeBlock has been no exception. I&#8217;ve created quite a few functions that offer generic functionality that could be used elsewhere. Here, I share some of them. Value Clamping Clamping values is a very common activity. It started to get on my nerves, [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/snippets/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>Naturally, as we write programs we create a small set of useful functions. BASeBlock has been no exception. I&#8217;ve created quite a few functions that offer generic functionality that could be used elsewhere. Here, I share some of them.</p>
<h3> Value Clamping </h3>
<p>Clamping values is a very common activity. It started to get on my nerves, repeating code to make sure a given value was within a range. As a result I conceived of a generic function that could be used for any IComparable.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;public <span class="kw4">static</span> T ClampValue&lt;T&gt; <span class="br0">&#40;</span>T Value, T min, T max<span class="br0">&#41;</span> &nbsp;where T:IComparable</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; &nbsp; &nbsp; <span class="co1">//cast to IComparable</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; IComparable cvalue = <span class="br0">&#40;</span>IComparable<span class="br0">&#41;</span>Value;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; IComparable cmin = <span class="br0">&#40;</span>IComparable<span class="br0">&#41;</span>min;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; IComparable cmax = <span class="br0">&#40;</span>IComparable<span class="br0">&#41;</span>max;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//return (T)(cvalue.CompareTo(cmin)&lt; 0 ?cmin:cvalue.CompareTo(cmax)&gt;0?max:Value);</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>cvalue.<span class="me1">CompareTo</span><span class="br0">&#40;</span>cmin<span class="br0">&#41;</span> &lt; <span class="nu0">0</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> min;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>cvalue.<span class="me1">CompareTo</span><span class="br0">&#40;</span>cmax<span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> max;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Value;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The basic idea is fairly simple. First, in order to clamp the value, we will need to be able to compare them, so we constrain the function to accepting only type T&#8217;s that implement that interface. The first step is casting each value to an IComparable; then we use those variables to compare and return the appropriate value. if the value is larger than max, max is returned; if it is smaller than min, min is returned. otherwise, value is returned unchanged. This function is most useful for numbers, but it can also have interesting implications and usage cases for other classes that are comparable, even strings.</p>
<h3>  choosing N items from a Enumerable list of S </h3>
</p>
<p>This also came up quite a lot- some parts of the game needed to randomly choose some set of values from a larger set of values. Naturally this gave birth to another generic routine for the purpose:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">public <span class="kw4">static</span> T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;Choose&lt;T&gt;<span class="br0">&#40;</span>IEnumerable&lt;T&gt; ChooseArray, <span class="kw4">int</span> numselect<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; Random rgen = new Random<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;returnarray = new T <span class="br0">&#91;</span>numselect<span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; SortedList&lt;double , T&gt; sorttest = new SortedList&lt;double, T&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; foreach <span class="br0">&#40;</span>T loopvalue in ChooseArray<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sorttest.<span class="me1">Add</span><span class="br0">&#40;</span>rgen.<span class="me1">NextDouble</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, loopvalue<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; var usearray = sorttest.<span class="me1">ToArray</span><span class="br0">&#40;</span><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> i = <span class="nu0">0</span>; i &lt; numselect; i++<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; returnarray <span class="br0">&#91;</span>i<span class="br0">&#93;</span> &nbsp;= usearray <span class="br0">&#91;</span>i<span class="br0">&#93;</span> .<span class="me1">Value</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> returnarray;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The idea is simple- make a SortedList that sorts the given listing using a random value as a key, then take the top numselect items off the top. This code will not work properly if numselect is larger than the size of the enumeration, but using count to clamp the size of the array would enumerate twice.</p>
<p>It&#8217;s probably possible to make this faster- possibly much faster- since we only need numselect elements. The core idea here is to shuffle the input array and choose two elements. One flawed approach for shuffling an array is to choose a random index and swap it with another random index, but this has myriad problems since it doesn&#8217;t really guarantee that everything is shuffled, and the result could very well have runs of the original card order.</p>
<p>Now, what if we had three objects we wanted to randomly choose from, and we wanted one of them to be chosen more frequently? One way of doing this is to use the above choose function and add duplicate entries. However, this could be tricky if you had odd requirements. This is where the Select&lt;T&gt; function would come in. this function is designed to accept an array and a corresponding array of probability weightings; if all the weightings are equal, than the result should be similar to what we get from Choose. Select accomplishes this  by keeping it simple. each array element is essentially assigned a given range within the total, and a random number is generated from the complete total of all weightings. For example, if we had the following elements:</p>
<table>
<th> # </th>
<th> Name </th>
<th> Weight </th>
<tr>
<td> 1 </td>
<td> Billy </td>
<td> 15 </td>
</tr>
<tr>
<td> 2 </td>
<td> Thomas </td>
<td> 35 </td>
</tr>
<tr>
<td> 3 </td>
<td> Jack </td>
<td> 70 </td>
</tr>
<tr>
<td> 4 </td>
<td> Selmac </td>
<td> 40 </td>
</tr>
<tr>
<td> 5 </td>
<td> Patrick </td>
<td> 80 </td>
</tr>
</table>
<p>We can see that if we generate a value between 0 and  240, than if it is between 0 and 15, we choose Billy, if it is between 15 and 50, we choose Thomas, etc.</p>
<p>Here is the code for the Select Function:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">static</span> T Select&lt;T&gt;<span class="br0">&#40;</span>T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;items, <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;Probabilities<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">return</span> Select<span class="br0">&#40;</span>items,Probabilities,new Random<span class="br0">&#40;</span><span class="br0">&#41;</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="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">static</span> T Select&lt;T&gt;<span class="br0">&#40;</span>T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;items, <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;Probabilities,Random rgen<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//first, sum all the probabilities.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//we do this manually because we will also build a corresponding list of the sums up to that element.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">float</span> getsum = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;sumulations = new <span class="kw4">float</span> <span class="br0">&#91;</span>Probabilities.<span class="me1">Length</span> + <span class="nu0">1</span><span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; Probabilities.<span class="me1">Length</span>; i++<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumulations <span class="br0">&#91;</span>i<span class="br0">&#93;</span> &nbsp;= getsum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getsum += Probabilities <span class="br0">&#91;</span>i<span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumulations <span class="br0">&#91;</span>sumulations.<span class="me1">Length</span><span class="nu0">-1</span><span class="br0">&#93;</span> &nbsp;= getsum; <span class="co1">//add this last value in&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//get a percentage using nextDouble. we use doubles, just in case the probabilities array uses rather large numbers to attempt to prevent</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//abberations as a result of floating point errors.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> usepercentage = rgen.<span class="me1">NextDouble</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//convert this percentage into a value we can use, that corresponds to the sum of float values:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">float</span> searchtotal = <span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>usepercentage * getsum<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//now find the corresponding index and return the corresponding value in the items array.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; Probabilities.<span class="me1">Length</span>; i++<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>searchtotal &gt; sumulations <span class="br0">&#91;</span>i<span class="br0">&#93;</span> &nbsp;&amp;&amp; searchtotal &lt; sumulations <span class="br0">&#91;</span>i + <span class="nu0">1</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> items <span class="br0">&#91;</span>i<span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">default</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>A short test Main() routine that can be used to&#8230; test it:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">void</span> Main<span class="br0">&#40;</span><span class="kw4">string</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;args<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> totalcount = <span class="nu0">50000</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">String</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;names = new <span class="kw4">string</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;<span class="br0">&#123;</span> <span class="st0">&quot;Bill&quot;</span>, <span class="st0">&quot;Tom&quot;</span>, <span class="st0">&quot;Dick&quot;</span> <span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;probability = new <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;<span class="br0">&#123;</span> <span class="nu0">50</span>, <span class="nu0">20</span>, <span class="nu0">30</span> <span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dictionary &lt;string , int&gt; &nbsp;countrunner = new Dictionary &lt;/string&gt; &nbsp;&lt;string , int&gt; <span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;running simulations&#8230;&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &gt; totalcount; i++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">String</span> Selected = Select<span class="br0">&#40;</span>names, probability<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!countrunner.<span class="me1">ContainsKey</span><span class="br0">&#40;</span>Selected<span class="br0">&#41;</span><span class="br0">&#41;</span> countrunner.<span class="me1">Add</span><span class="br0">&#40;</span>Selected, <span class="nu0">0</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countrunner <span class="br0">&#91;</span>Selected<span class="br0">&#93;</span> ++;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Completed. Results:&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var iterate in countrunner<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span>iterate.<span class="me1">Key</span> + <span class="st0">&quot;<span class="es0">\t</span><span class="es0">\t</span><span class="es0">\t</span>&quot;</span> + iterate.<span class="me1">Value</span>.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span> + <span class="st0">&quot; &quot;</span> + <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>iterate.<span class="me1">Value</span><span class="br0">&#41;</span>/<span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span>totalcount<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">ReadKey</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;&lt;/string&gt; &nbsp;</div>
</li>
</ol>
</div>
<p>The test is created in such a way that the values can be thought of directly as percentages. The resulting output shows that after a run the occurences of each lie around the percentages as given; Bill appears 50 percent of the time on average, Tom 20 percent, Dick 30 percent, etc. Obviously there is no requirement that the values add up to 100, but the values end up as percentages anyway. (choosing the values 100,40, and 60 for the probability array results in similar results).</p>
<p>This particular method has a bit of a &#8220;problem&#8221;; what if we run it repeatedly on the same array? Then we are constantly recreating the sumulations array and calculating the totals. How can we cache it? Easy- we use a Dictionary and keep weak references to the given array.</p>
<p>But all is not that simple! This is a generic method and the type T could easily change between calls- so what do we do? Well, it is possible to create a static object that contains a Dictionary indexed by a Type that has a value that is a KeyValuePair&lt;weakreference ,List&lt;T&gt;&gt;, and then inspect the Dictionary for the appropriate values, make sure the cache doesn&#8217;t get to big, dispose ofthe WeakReferences that point to arrays that have since been destroyed, blah blah, tricky business. We don&#8217;t want that, because for one thing it will be a pain to write- and for another, it will probably be slower overall to begin with. Instead, how about absolving the method itself from the responsibility, and having a ref parameter that can accept a calculated sum array. </p>
<p>For me, the above testing Main() routine took 200ms to execute, on average (I placed calls to System.Diagnostics.Stopwatch members before and after the loop). There was no appreciable difference in speed. Thnakfully, however, the extra logic did not slow it down, either.</p>
<p>The speed improvement can be seen when we have a lot more members. With 5000 members in the probability and Values arrays, and executing a Select on them 50,000 times, the average time was around 5-6 seconds. When the Main function instead gave a float []  ref as the third parameter, the average time dropped to one second. The code for the revised version of the procedure. This also adds some overloads:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">static</span> T Select&lt;T&gt;<span class="br0">&#40;</span>T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;items, <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;Probabilities<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Select<span class="br0">&#40;</span>items, Probabilities, new Random<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">static</span> T Select&lt;T&gt;<span class="br0">&#40;</span>T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;items, <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;Probabilities, Random rgen<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;sumulator = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Select<span class="br0">&#40;</span>items, Probabilities, rgen, ref sumulator<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">static</span> T Select&lt;T&gt;<span class="br0">&#40;</span>T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;items, <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;Probabilities, ref <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;sumulations<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Select<span class="br0">&#40;</span>items, Probabilities, new Random<span class="br0">&#40;</span><span class="br0">&#41;</span>, ref sumulations<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">static</span> T Select&lt;T&gt;<span class="br0">&#40;</span>T <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;items, <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;Probabilities,Random rgen, ref <span class="kw4">float</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> &nbsp;sumulations<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//first, sum all the probabilities; unless a cached value is being given to us.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//we do this manually because we will also build a corresponding list of the sums up to that element.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">float</span> getsum = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>sumulations ==<span class="kw2">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumulations = new <span class="kw4">float</span> <span class="br0">&#91;</span>Probabilities.<span class="me1">Length</span> + <span class="nu0">1</span><span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</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> i = <span class="nu0">0</span>; i &lt; Probabilities.<span class="me1">Length</span>; i++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumulations <span class="br0">&#91;</span>i<span class="br0">&#93;</span> &nbsp;= getsum;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getsum += Probabilities <span class="br0">&#91;</span>i<span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumulations <span class="br0">&#91;</span>sumulations.<span class="me1">Length</span><span class="nu0">-1</span><span class="br0">&#93;</span> &nbsp;= getsum; <span class="co1">//add this last value in&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getsum = sumulations <span class="br0">&#91;</span>sumulations.<span class="me1">Length</span><span class="nu0">-1</span><span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//get a percentage using nextDouble. we use doubles, just in case the probabilities array uses rather large numbers to attempt to prevent</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//abberations as a result of floating point errors.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">double</span> usepercentage = rgen.<span class="me1">NextDouble</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//convert this percentage into a value we can use, that corresponds to the sum of float values:</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">float</span> searchtotal = <span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>usepercentage * getsum<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//now find the corresponding index and return the corresponding value in the items array.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw4">int</span> i = <span class="nu0">0</span>; i &lt; Probabilities.<span class="me1">Length</span>; i++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>searchtotal &gt; sumulations <span class="br0">&#91;</span>i<span class="br0">&#93;</span> &nbsp;&amp;&amp; searchtotal &lt; sumulations <span class="br0">&#91;</span>i + <span class="nu0">1</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> items <span class="br0">&#91;</span>i<span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">default</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>A lot of the above could even be implemented as Extension methods to the appropriate classes, making it seamless.</p>
<p>Since I am working on a game, dealing with vectors and speeds and whatnot is common. One frequent requirement is for items to move at a random angle at a random speed within a given range. The obvious base case here is creating a Vector given a angle and a magnitude:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;public <span class="kw4">static</span> PointF GetVelocity<span class="br0">&#40;</span><span class="kw4">double</span> speed, <span class="kw4">double</span> angle<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">return</span> new PointF<span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>Math.<span class="me1">Cos</span><span class="br0">&#40;</span>angle<span class="br0">&#41;</span> * speed<span class="br0">&#41;</span>, <span class="br0">&#40;</span><span class="kw4">float</span><span class="br0">&#41;</span><span class="br0">&#40;</span>Math.<span class="me1">Sin</span><span class="br0">&#40;</span>angle<span class="br0">&#41;</span> * speed<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p> This uses standard trigonometry to calculate what would be the X and Y axes of a fictitious triangle with the given direction as it&#8217;s hypoteneuse. Extending from this, we simply create a few extra routines that perform the randomizations:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">public <span class="kw4">static</span> PointF GetRandomVelocity<span class="br0">&#40;</span><span class="kw4">double</span> minspeed, <span class="kw4">double</span> maxspeed, <span class="kw4">double</span> angle<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> GetRandomVelocity<span class="br0">&#40;</span>minspeed,maxspeed,angle,new Random<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">public <span class="kw4">static</span> PointF GetRandomVelocity<span class="br0">&#40;</span><span class="kw4">double</span> minspeed, <span class="kw4">double</span> maxspeed, <span class="kw4">double</span> angle, Random rgen<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> GetVelocity<span class="br0">&#40;</span>minspeed + <span class="br0">&#40;</span>rgen.<span class="me1">NextDouble</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="br0">&#40;</span>maxspeed-minspeed<span class="br0">&#41;</span><span class="br0">&#41;</span>,angle<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">public <span class="kw4">static</span> PointF GetRandomVelocity<span class="br0">&#40;</span><span class="kw4">double</span> minspeed, <span class="kw4">double</span> maxspeed, Random rgen<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">return</span> GetRandomVelocity<span class="br0">&#40;</span>minspeed,maxspeed, Math.<span class="me1">PI*</span><span class="nu0">2</span>*rgen.<span class="me1">NextDouble</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,rgen<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p> easy as 3.1415926! </p>
<p>Another interesting endeavour is simplifying the otherwise messy world of Object serialization, such as easily converting a Serializable Object to and from a Stream:</p>
<p> [code] </p>
<p>  public static void ObjectToStream&lt;T&gt;(T saveme, Stream outstream) where T : ISerializable</p>
<p>    {</p>
<p>        BinaryFormatter bf = getFormatter();</p>
<p>        using(GZipStream gz = new GZipStream(outstream,CompressionMode.Compress))</p>
<p>        {</p>
<p>            bf.Serialize(gz, saveme);</p>
<p>        }</p>
<p>    }</p>
<p>    public static T StreamToObject&lt;t&gt;(Stream instream) where T : ISerializable</p>
<p>    {</p>
<p>        BinaryFormatter bf = getFormatter();</p>
<p>        using(GZipStream gz = new GZipStream(instream,CompressionMode.Decompress))</p>
<p>        {</p>
<p>            return (T)bf.Deserialize(gz);</p>
<p>        }</p>
<p>    }</p>
<p> [/code] </p>
<p>This also plonks in a bit of compression through the use of a GZipStream.</p>
<p>Anyway, that&#8217;s a quick sampling of a few snippets of possible usefulness <img src='http://bc-programming.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/05/snippets/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=975&amp;md5=18c3d5fcfcf9cff1f4e1afe63ebea456" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/05/snippets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F05%2Fsnippets%2F&amp;language=en_GB&amp;category=software&amp;title=Snippets.&amp;description=Naturally%2C+as+we+write+programs+we+create+a+small+set+of+useful+functions.+BASeBlock+has+been+no+exception.+I%26%238217%3Bve+created+quite+a+few+functions+that+offer+generic+functionality+that+could...&amp;tags=.NET%2CC%23%2CChoose%2CClampValue%2CGenerics%2CIComparable%2CProgramming%2CSelect%2Cstatic%2Cblog" type="text/html" />
	</item>
		<item>
		<title>A response to something I read in the paper</title>
		<link>http://bc-programming.com/blogs/2012/04/a-response-to-something-i-read-in-the-paper/</link>
		<comments>http://bc-programming.com/blogs/2012/04/a-response-to-something-i-read-in-the-paper/#comments</comments>
		<pubDate>Sun, 29 Apr 2012 15:50:33 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[conspiracy]]></category>
		<category><![CDATA[electromagnetism]]></category>
		<category><![CDATA[evil]]></category>
		<category><![CDATA[exploded weevil]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=968</guid>
		<description><![CDATA[So I read the paper today. One of the letters to the editor was possibly the stupidest thing I had read in quite a long while, and required a response. I considered writing to the editor myself, but a newspaper is hardly the best place to conduct a flamewar. Besides, no doubt other people would [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/a-response-to-something-i-read-in-the-paper/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>So I read the paper today. One of the letters to the editor was possibly the stupidest thing I had read in quite a long while, and required a response. I considered writing to the editor myself, but a newspaper is hardly the best place to conduct a flamewar. Besides, no doubt other people would be searching for their cited evidence and come up empty, just as I had. Anyway, here it is.</p>
<blockquote><p> As someone who is electro-hyper-sensitive, I’m really angry that yet another public service is being denied to me. </p></blockquote>
<p>That&#8217;s right- in the first sentence they essentially claim to have super-powers.</p>
<blockquote><p> Public buildings – libraries, universities, government offices, hospitals, medical clinics, community centres – and privately-owned restaurants and coffee shops are all infested with “wireless connectivity syndrome”: microwave-emitting cellphones, i-Somethings, Blue Whatevers, Wi-Fi, cordless phones, wireless spy cameras, smart meters, etc. </p></blockquote>
<p>&#8220;Microwave&#8221; is a broad term, used to indicate any EM emission between 300Mhz and 300Ghz. This causes confusion amongst those intend on spreading Feay r, Uncertainty, and Doubt because they either do not realize that the spectrum used for cooking and heating items in a Microwave oven are in fact a tiny subset, and are far more powerful than anything emitted by devices such as cellphones or 802.11 networking devices. The common refutation is that Microwaves are &#8220;cumulative&#8221;. But this is simply false; the specific wavelength used for microwave ovens is designed (or rather discovered by accident) to excite water molecules, which is how it achieves the desired effect of heating or cooking food.
 </p>
<blockquote><p> The World Health Organization classified microwave radiation from these gadgets as a Class 2B carcinogen. </p></blockquote>
<p>What the writer isn&#8217;t mentioning here is that &#8220;Class 2B&#8221; carcinogens are &#8220;possible causes&#8221;; not verified. More importantly, it is in good company as a class 2B carcinogen; pickled vegetables are in the same category, but anybody crusading against the use of pickles and olives would be classified as insane.
 </p>
<blockquote><p> I can’t access those services because my body literally feels microwave radiation inside those buildings. First I itch, then get dizzy and nauseated, lose focus and memory, develop headaches. </p></blockquote>
<p>
the idea of electro-sensitivity is absurd, double-blind studies have shown that proclaimed &#8220;electro-sensitive&#8221; people report discomfort when they  <i> think </i>  there is an EM field. It comes as no surprise that these people were crusaders against the evils of newer technology long before they got their &#8220;super-powers&#8221;.
 </p>
<blockquote><p> but long-term effects – DNA damage (cancer, sterility), immune system failure, neurological disorders –will affect us equally. </p></blockquote>
<p>
We have been exposed to various forms of electromagnetic radiation, and in more powerful amounts, for over a hundred years. Power lines, electrical equipment (toasters, ovens, etc) all emit Electromagnetic radiation; junction boxes, light-bulbs, flourescent lights, Television screens, etc all emit EM radiation in similar wavelengths to those used by these later technologies that are being crusaded against. I often wonder why it is only the newer devices being singled-out, particularly since they use far less powerful emissions than those devices we have used for many years without ill effects.
 </p>
<p>
 Metal doesn&#8217;t always reflect radio waves and EM emissions, for move wavelengths, it absorbs them, because metals have free electrons they absorb EM emissions of most spectral frequencies. (how do you think antennas work?). devices designed to reflect EM emissions need to be designed to do so, and even then will only work at specific wavelengths, such as those- again- used with Microwave ovens.
 </p>
<p>
It&#8217;s fun to overgeneralize and assume that because the frequencies emitted by a Microwave oven&#8217;s magnetron are reflected by metal, that all EM emissions are, but it&#8217;s patently false. Only specific frequencies of EM radiation are reflected by Metals; Microwave ovens happen to use one of this subset specifically because of that property. However, Wireless-G, 802.11, and similar EM devices use wavelengths that are absorbed by metals.
 </p>
<blockquote><p> I don’t understand why WorkSafeB.C. allows such gross violations of their own regulations (employees may not be exposed to Class 2B carcinogens if safer options are available) and why our provincial government knowingly microwaves its employees and taxpayers. </p></blockquote>
<p>Once more, the alarmist jumps out, using the term &#8220;microwave&#8221; in order to try to connect 802.11g radiation with the EM radiation used for cooking in microwave ovens, the disparity is alarming between these, however; the former- particularly with cellphones and 802.11- are far lower power and of a completely different frequency than the latter, so creating any sort of connection between them is pure FUD. Additionally, I was unable to find any citation as to the regulations stating anything like that; probably because that &#8220;regulation&#8221; doesn&#8217;t exist. Considering such a regulation would be required to prevent employees from eating pickles or olives on the job I&#8217;m inclined to believe it&#8217;s invented.
 </p>
<p>
The curious thing about this is that the spreaders of such Dark-Age insipid FUD on this issue simply don&#8217;t understand some basic principles; first, the EM radiation that they are crusading against is about a million times weaker than that used in a microwave, and the radiation used in a microwave is about a million times weaker than the radiation we are exposed to in direct sunlight. And yet, they seem to have no problems with sunlight.
 </p>
<p>
I propose that the entire thing is based in unfamiliarity and resistance to newer technologies, rather than any genuine concern for health; The sun can cause health problems, but even a second&#8217;s worth of exposure is more powerful than a lifetime of using cellphones and other devices. The primary difference is that the Sun has &#8220;been there&#8221;; it&#8217;s an established element. Cellphones and other devices, to many people, are new, unfamiliar, and intrinsically dangerous, not because people don&#8217;t understand them, but because those people don&#8217;t understand them, nor do they usually understand the principles of electromagnetism, deriving their arguments instead from google searches and unbibliographically sound sources.
 </p>
<p>Here is an interesting link about &#8220;electro-sensitives&#8221; and their crusade:
 </p>
<p>http://www.skepdic.com/electrosensitives.html</p>
<p>
My favourite is still the bit about Class 2B carcinogen, and how they said it like it meant it was a deadly poison to be avoided; meanwhile, other things that are classified as Class 2B carcinogens include pickled vegetables. OOOOH SCARY. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/a-response-to-something-i-read-in-the-paper/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=968&amp;md5=eb23289afac7b6face5330d312f939e3" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/04/a-response-to-something-i-read-in-the-paper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F04%2Fa-response-to-something-i-read-in-the-paper%2F&amp;language=en_GB&amp;category=software&amp;title=A+response+to+something+I+read+in+the+paper&amp;description=So+I+read+the+paper+today.+One+of+the+letters+to+the+editor+was+possibly+the+stupidest+thing+I+had+read+in+quite+a+long+while%2C+and+required+a+response....&amp;tags=conspiracy%2Celectromagnetism%2Cevil%2Cexploded+weevil%2Cblog" type="text/html" />
	</item>
		<item>
		<title>ReferenceCounted List Update</title>
		<link>http://bc-programming.com/blogs/2012/04/referencecounted-list-update/</link>
		<comments>http://bc-programming.com/blogs/2012/04/referencecounted-list-update/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 23:46:47 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=965</guid>
		<description><![CDATA[A quick update on the functionality I was trying to use ReferenceCounted List for in BASeBlock. I ended up not using the class at all. I actually ended up just using a different method- instead of the powerup changing the drawattributes when it starts and ends, instead it is simply called each frame- basically the [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/referencecounted-list-update/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>A quick update on the functionality I was trying to use ReferenceCounted List for in BASeBlock.</p>
<p>I ended up not using the class at all. I actually ended up just using a different method- instead of the powerup changing the drawattributes when it starts and ends, instead it is simply called each frame- basically the character is like &#8220;OK, before I draw- you abilities got anything to add&#8221; and it will change what it wants as needed. The gamecharacter resets it after drawing so if the ability is removed it will &#8220;revert&#8221; to normal appearance.</p>
<p>Also, this is possibly my shortest blog post ever. In order to bulk it up- I made some changes to the main page as well as the theme of the blog. The main page&#8217;s various images were tweaked, and the blog has had it&#8217;s font&#8217;s changed because the font it was using was annoying. (Windows Vista and later will be unaffected by the change). BASeBlock now has a &#8220;BuilderShotPowerup&#8221; thingamajig that let&#8217;s you shoot blocks and build stuff, which I think will be useful for building a &#8220;bridge&#8221; for the GameCharacter, and other purposes. (This idea was Mulreay&#8217;s, btw, he has great ideas). I tweaked a lot of stuff such as the editor&#8217;s sound data list editor now showing progress and being less annoying to use, and a few general tweaks and minor fixes here and there. I documented it all in the changelog which I will post when I finally upload the new version. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/referencecounted-list-update/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=965&amp;md5=7031a957034c3d762c0967da9136ea3d" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/04/referencecounted-list-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F04%2Freferencecounted-list-update%2F&amp;language=en_GB&amp;category=software&amp;title=ReferenceCounted+List+Update&amp;description=A+quick+update+on+the+functionality+I+was+trying+to+use+ReferenceCounted+List+for+in+BASeBlock.+I+ended+up+not+using+the+class+at+all.+I+actually+ended+up+just...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>&#8220;First world problems&#8221;</title>
		<link>http://bc-programming.com/blogs/2012/04/first-world-problems/</link>
		<comments>http://bc-programming.com/blogs/2012/04/first-world-problems/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 23:31:34 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[idiots]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=963</guid>
		<description><![CDATA[A completely random rant&#8230; Here&#8217;s another thing that pisses me off. You complain about something- maybe it was a poor mannered UPS delivery person, a bad eBay experience (like say it randomly changes languages) or how your car won&#8217;t start, or your coffee maker is broken, etc. Time, and TIME AGAIN I hear people classify [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/first-world-problems/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>A completely random rant&#8230;</p>
<p>Here&#8217;s another thing that pisses me off. You complain about something- maybe it was a poor mannered UPS delivery person, a bad eBay experience (like say it randomly changes languages) or how your car won&#8217;t start, or your coffee maker is broken, etc.</p>
<p>Time, and TIME AGAIN I hear people classify these as &#8220;First world problems&#8221;. It&#8217;s not even really a proper response. It&#8217;s not even something that falls in that category. a &#8220;First world problem&#8221; might be something like a hangnail or some shit, or some teenage girl complaining about their like &#8220;OMG my nail is like, lopsided&#8221; but when  you have legitimate issues that the peers in your immediate vicinity aren&#8217;t facing, trying to broaden the scope and basically say &#8220;well, sorry buddy, I know that your car won&#8217;t start so you have to walk 3 hours t owork, but that doesn&#8217;t matter because some poor sod in Africa lost his leg to a land mine&#8221;  or some other shit. Yes, it sucks in third world countries. But here&#8217;s the thing- that is why they are called  [i] fucking third world countries [/i] . And the thing is, that there are a number of rich folks- the government bureaucrats- in those countries that have plenty of what could be called &#8220;first-world&#8221; problems, so the statement isn&#8217;t even legitimate. Imagine if companies decided this. Your phone service, after three weeks, still hasn&#8217;t been hooked up, so you call them. They tell you that is a first-world problem. Great. That still doesn&#8217;t address the fucking issue. eBay, or another site, randomly changes it&#8217;s language on you in the middle of a purchase. You complain on facebook, or whatever. Some other dick says &#8220;first world problems?&#8221;. yeah, that&#8217;s for that, asshole. How about instead of making unhelpful changes to scope you actually say something constructive. I mean- you could say that for damn near anything in the First world. That is why it&#8217;s called a &#8220;First world problem&#8221; but the thing is that people say it in a derogatory manner- as if to say &#8220;your problem is insigificant because a lot of people in the world are suffering more&#8221; but that isn&#8217;t the case. And the fact is that the very same people making these &#8220;accusations&#8221; are in fact doing so through first-world communication methods- such as comments on a social networking site, or blog, or what-have-you. So I wonder who is more objective- the person who is basing their opinions and complaints on first-hand experience, or the person who is colouring all their responses and thoughts with some ridiculous overtone to &#8220;worry about the third world&#8221; because they saw some touching second-hand documentary on National Geographic. I haven&#8217;t been in the third world, all I know about it is that it sucks terribly. </p>
<p>Thing is, that we cannot really solve the &#8220;problem&#8221; of the third world; it is a huge conglomeration that has appeared over the years due to varying socio-economic pressures in the region as well as problems of unstable governments, and most importantly the fact that they don&#8217;t really have anything there. It&#8217;s a fucking desert, pretty much. Suddenly, the fact that they have problems getting fresh water and food makes more sense. It&#8217;s because they are in the fucking desert. Of course most of them would have trouble leaving that desert, but that doesn&#8217;t change that simple fact. And more importantly any relief aid would be temporary, because the realy solution is to  <i> get the fuck out of the desert </i> . Anyway, I&#8217;ve rambled somewhat. Not all developing countries are in a desert- some just have corrupt governments. But I think a more relevant question to ask is- can a single person fix all the corrupted governments, turn a desert into a rainforest, and solve all those problems?</p>
<p>No. Of course not. That makes the scope change of suggesting otherwise pretty damned petty, doesn&#8217;t it. It&#8217;s like saying &#8220;I have nothing constructive to add to your complain about eBay, so I will instead make an idiotic gesture and extend the scope of your problem to make it seem insignificant&#8221;. Well no flipping duh it would seem insignificant. hell in the context of the galaxy Earth is pretty damned insigificant, but you don&#8217;t see everybody pulling that reasoning out of their ass. &#8220;Hello, I notice that even though I paid the bill, you&#8217;ve charged me a late fee&#8221; &#8220;Well you know what Mister, in the grand scheme of things Earth isn&#8217;t relevant&#8221;. It&#8217;s a  cop out no matter who says it.</p>
<p>It&#8217;s like that bullshit thing that parents always say when their kids say they are full &#8220;well there are kids starving in Africa&#8221; or something similar. Well guess what- that kid is not the one you are talking to. If you care so god-damned much about the kids starving in Africa, why the hell aren&#8217;t you donating time or money to help them? Instead you bring it up when your own kid won&#8217;t finish their food, which raises the other issue that we all wonder why kids (in the first-world) are becoming fatter on average, I wonder if maybe it&#8217;s related to parents forcing their kids to eat more when they are full? </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/first-world-problems/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=963&amp;md5=bdc293720f43d42a32a1dc40bf2dd347" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/04/first-world-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F04%2Ffirst-world-problems%2F&amp;language=en_GB&amp;category=software&amp;title=%26%238220%3BFirst+world+problems%26%238221%3B&amp;description=A+completely+random+rant%26%238230%3B+Here%26%238217%3Bs+another+thing+that+pisses+me+off.+You+complain+about+something-+maybe+it+was+a+poor+mannered+UPS+delivery+person%2C+a+bad+eBay+experience+%28like+say...&amp;tags=blog" type="text/html" />
	</item>
		<item>
		<title>A ReferenceCounted List</title>
		<link>http://bc-programming.com/blogs/2012/04/a-referencecounted-list/</link>
		<comments>http://bc-programming.com/blogs/2012/04/a-referencecounted-list/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 02:52:02 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[BASeBlock]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Sound]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=957</guid>
		<description><![CDATA[Here I explore the design and implementation of a Generic class designed to replace variables of it's Generic Type and add capability, while not forcing significant rewrites of code that uses the "old" type.]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/a-referencecounted-list/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><h3> BASeBlock &#8211; Music Manipulation Lessons </h3>
<p>As with most games, BASeBlock has music. Originally, I implemented a naive approach to have &#8220;Multiple music&#8221; playing; a simple stack. The multiple music idea is sort of like<br />
 how games might change the music to a boss music when a boss appears, and change it back when they die, or how a certain powerup might change the music while  you have it.</p>
<p>This implementation sat for a while. it used a stack based approach- the Sound manager had PushMusic and PopMusic methods.</p>
<p>However, several critical flaws in this approach became clear after I added an invulnerability powerup. Everything seemed to work fine, (get the powerup, you&#8217;re invulnerable and while you are there is different music) however, the problem became clear when I, while still invulnerable, released a boss. The boss music would start playing; however, while the boss was alive, the invincible power would run out; it would &#8220;PopMusic&#8221; which would revert the music from the boss music to the starman music, and then only when the boss died would it go back to normal. This is obviously not intended. The ideal case would be:</p>
<ol>
<li> Player starts level. Level Music is playing. </li>
<li> Player gets invulnerability powerup; invincible powerup music plays. </li>
<li> While invincible, the player, or something causes a boss to spawn. </li>
<li> when the boss is spawned, the invulnerable music can continue until the power runs out; at which point it plays the boss music, or, the boss music can replace the invincible music. The former is probably overall a better idea.
</li>
<li> Either way: the music must fit. Invincible music should only play while the player has said powerup; and boss music should only play when there is a boss.
</li>
</ol>
<p>Obviously, my approach failed miserably; it worked fine, but I had only had a single &#8220;active&#8221; piece of music at a time; how do you manage multiples?</p>
<p>After some thought, I considered the idea of &#8220;reference counting&#8221; or keeping track of how many times a given piece of music was requested to play. a boss spawning would increment the boss music by one, a second one with the same music would make it two; each time this happens, the sound manager could re-evaluate which piece of music to play based on finding the maximum reference count.</p>
<p>With this idea, I rearchitected some of the code within the SoundManager. The SoundManager (technically cNewSoundManager, since it was a rewrite of a strongly coupled version I had before) is essentially a class that, well, manages sound and music. I have a interface class that allows for different actual implementations of the details of playing sound (&#8220;Driver&#8221; classes, of you will) The Manager class itself merely deals with the details based on that basic functionality, which exposes a few critical events, such as music stopping and whatnot. The original &#8220;PushMusic&#8221; and &#8220;PopMusic&#8221; stack based approach used a small data class, shown here:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;private class ActiveMusicData</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">string</span> Name;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public iActiveSoundObject ActiveSound<span class="br0">&#123;</span>get;set;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public iSoundSourceObject Source <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public ActiveMusicData<span class="br0">&#40;</span><span class="kw4">String</span> pName,iActiveSoundObject pActiveSound, iSoundSourceObject pSource<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActiveSound = pActiveSound;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Source = pSource;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = pName;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</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;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>A minor explanation may be necessary; iActiveSoundObject is an interface class that is implemented by the  &#8220;driver&#8221;; same for iSoundSourceObject; the details of how they work isn&#8217;t important, just  that their interface methods do what the interface definition says. A Active Sound object is something that is &#8220;active&#8221; usually, this means it is playing, but it could also be paused. A Sound Source object can be used to &#8220;spawn&#8221; Active Sound Objects; in order to actually play music or sound, a iActiveSoundSource object is required. Rather than discard this class I extended from it. Arguably, I could have simply changed the actual class itself but that could always be done later:</p>
<p><div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; private class TemporaryMusicData:ActiveMusicData,IComparable&lt;TemporaryMusicData&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public TemporaryMusicData<span class="br0">&#40;</span><span class="kw4">String</span> pName, iActiveSoundObject pActiveSound, iSoundSourceObject pSource<span class="br0">&#41;</span>:base<span class="br0">&#40;</span>pName,pActiveSound,pSource<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">int</span> Occurences; <span class="co1">//reference count; we add one to this when a &quot;temporary&quot; music is played. and subtract one when it is &quot;stopped&quot;.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//we play the music with the highest &quot;reference count&quot;; items are removed when their &quot;reference count&quot; is zero.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">int</span> CompareTo<span class="br0">&#40;</span>TemporaryMusicData other<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> Occurences.<span class="me1">CompareTo</span><span class="br0">&#40;</span>other.<span class="me1">Occurences</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Again, another private class. The  Implementation of IComparable <temporarymusicdata>  is vestigial from when I was flailing around trying to shoehorn the old stack-based approach into the new  reference counted method using a SortedDictionary. Then I realized it was stupid and just made the data structure a normal dictionary.</p>
<p> </temporarymusicdata>  </p>
<p>Dictionary&lt;String,TemporaryMusicData&gt; to be precise; This indexes the TemporaryMusicData instances by Name (Key); the Name/Key is used by the sound Manager to index Sound sources, so getting the appropriate source is easy given a name, and it&#8217;s guaranteed to be unique since the listing is taken from the file system itself, and the loading routine has other considerations to prevent duplicate entries (and error handling for duplicate key Exceptions if they do occur). The Occurences field is basically the entire purpose here; when &#8220;Temporary&#8221; music is told to play, it merely increments the field for the appropriate entry at the Named Index; then both the Stop and Play routines will call another routine that Ensures that the item with the maximum occurences is playing. The implementation for the relevant routines:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;private <span class="kw4">String</span> OriginalSoundName; <span class="co1">//allocated when TemporaryMusicData is empty when music is pushed.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; private iSoundSourceObject OriginalSoundObject = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//private SortedList&lt;TemporaryMusicData, TemporaryMusicData&gt; TempMusicData = new SortedList&lt;TemporaryMusicData, TemporaryMusicData&gt;();</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; private Dictionary&lt;String, TemporaryMusicData&gt; TempMusicData = new Dictionary&lt;string, TemporaryMusicData&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// stops music played with the PlayTemporaryMusic function.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;MusicName&quot;&gt;&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public iActiveSoundObject StopTemporaryMusic<span class="br0">&#40;</span><span class="kw4">String</span> MusicName<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MusicName = MusicName.<span class="me1">ToUpper</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!TempMusicData.<span class="me1">Any</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> mPlayingMusic; <span class="co1">//no elements in Temporary Music Dictionary, so nothing to stop.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TemporaryMusicData incrementData = getTemporaryMusicData<span class="br0">&#40;</span>MusicName<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; incrementData.<span class="me1">Occurences</span>&#8211;; <span class="co1">//add one to occurences.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>incrementData.<span class="me1">Occurences</span> == <span class="nu0">0</span><span class="br0">&#41;</span> TempMusicData.<span class="me1">Remove</span><span class="br0">&#40;</span>MusicName<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> PlayMax<span class="br0">&#40;</span><span class="nu0">1</span>.0f, <span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// Plays &quot;temporary&quot; music; for example the music from a power up.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;MusicName&quot;&gt;&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public iActiveSoundObject PlayTemporaryMusic<span class="br0">&#40;</span><span class="kw4">String</span> MusicName,<span class="kw4">float</span> volume, bool loop<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MusicName = MusicName.<span class="me1">ToUpper</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//the idea is simple, we want to allow for the following:</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Player starts game. Music playing is standard level music or something.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//player get&#8217;s a power up, which has it&#8217;s own music. this powerup is timed.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//player causes something else to have different music; for example, they could spawn a boss or something.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//when the powerup runs out, the music will revert to the normal game music; and when the boss is killed, it will go back to the</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//music that was playing from the powerup.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//This attempts to mitigate this behaviour.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//step one: if the list is empty&#8230;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!TempMusicData.<span class="me1">Any</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//empty list. Initialize OriginalSoundName&#8230;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OriginalSoundObject = mPlayingMusicSource;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OriginalSoundName = scurrentPlayingMusic; </div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//step two: is there an element in the sorted list for MusicName?</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TemporaryMusicData incrementData = getTemporaryMusicData<span class="br0">&#40;</span>MusicName<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; incrementData.<span class="me1">Occurences</span>++; <span class="co1">//add one to occurences.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> PlayMax<span class="br0">&#40;</span>volume, loop<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</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;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; private TemporaryMusicData getTemporaryMusicData<span class="br0">&#40;</span><span class="kw4">string</span> MusicName<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MusicName=MusicName.<span class="me1">ToUpper</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TemporaryMusicData incrementData;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>TempMusicData.<span class="me1">ContainsKey</span><span class="br0">&#40;</span>MusicName<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; incrementData = TempMusicData <span class="br0">&#91;</span>MusicName<span class="br0">&#93;</span> ;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; incrementData = new TemporaryMusicData<span class="br0">&#40;</span>MusicName, <span class="kw2">null</span>, mSoundSources <span class="br0">&#91;</span>MusicName<span class="br0">&#93;</span> <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TempMusicData.<span class="me1">Add</span><span class="br0">&#40;</span>MusicName, incrementData<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> incrementData;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; private iActiveSoundObject PlayMax<span class="br0">&#40;</span><span class="kw4">float</span> volume, bool loop<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//now, find the one with the maximum occurences.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!TempMusicData.<span class="me1">Any</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mPlayingMusic.<span class="me1">Stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//play default.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scurrentPlayingMusic = OriginalSoundName;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mPlayingMusic = OriginalSoundObject.<span class="me1">Play</span><span class="br0">&#40;</span>loop,volume<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> mPlayingMusic;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> currentmax = <span class="kw4">int</span>.<span class="me1">MinValue</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TemporaryMusicData tmduse = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var iterate in TempMusicData<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>iterate.<span class="me1">Value</span>.<span class="me1">Occurences</span> &gt; currentmax<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentmax = iterate.<span class="me1">Value</span>.<span class="me1">Occurences</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmduse = iterate.<span class="me1">Value</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>scurrentPlayingMusic != tmduse.<span class="me1">Name</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mPlayingMusic.<span class="me1">Stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scurrentPlayingMusic = tmduse.<span class="me1">Name</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mPlayingMusic = tmduse.<span class="me1">Source</span>.<span class="me1">Play</span><span class="br0">&#40;</span>loop, volume<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> mPlayingMusic;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> mPlayingMusic;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>So far, this has worked well.</p>
<p>However, more recently I found that I also need the same sort of &#8220;reference count&#8221; management for other things related to powerups, such as the &#8220;DrawAttributes&#8221; of various objects. But it would be foolish to clutter up that code with this sort of thing. Surely there is some way that I can add the feature with little to no changes to existing code? Turns out, that leveraging a few C# features, this is relatively easily accomplished.</p>
<p>Consider the Nullable&lt;T&gt; class. Any struct or value type can be made &#8220;Nullable&#8221; using it; there is even a shortcut in the language syntax for type definitions to use it, by appending a question mark, (Nullable <int>  is equivalent to int?). What we want is a way to- generically- make a class &#8220;reference counted&#8221; so that only the value that has the highest reference count &#8220;is&#8221; the value. The Nullable&lt;T&gt; type can be implicitly cast to type T in most contexts; so you can change a T to a Nullable&lt;T&gt; type with few code changes, which is what we are after.</p>
<p> </int>  </p>
<p><h3> Enter ReferenceCounted&lt;T&gt; </h3>
</p>
<p>ReferenceCounted&lt;T&gt; is the name of the class that I created (or, as I write this, am creating) for this purpose. My original idea was to use implicit cast operators to make it a simple type change; assignments to the object of the &#8220;old type&#8221; (type T) would &#8220;automatically&#8221; be added to the reference list; going the other way, the ReferenceCounted&lt;T&gt; Type would be implicitly cast to T by way of taking the T value it currently has with the highest reference Count. This hit a snag, however; the second cast, thankfully, would work fine, but the first would not have the proper information; the cast operator is a static routine and wouldn&#8217;t have access to the ReferenceCounted&lt;T&gt; Object that is being assigned.</p>
<p>somewhat miffed but not surprised (it would be silly to provide for overloading of the assignment operator, but in this case I wish there was an exception), I didn&#8217;t give up; I just thought about it a little. And it hit me- I don&#8217;t need to overload the assignment operator to overload assignment; I could overload the addition operator and implement the &#8220;assignment&#8221; code there; this is what the Event classes do for event hooking; and I could use -= to remove &#8220;references&#8221;. Arguably, this would take more code and wouldn&#8217;t be quite as clean as I was hoping, but for the most part the actual reference counting logic would be out of the way, handled mostly by the implicit cast to T.</p>
<p>After some effort&#8230; it was made. Here is the source code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">using System.<span class="me1">Collections</span>.<span class="me1">Generic</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Diagnostics</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Drawing</span>.<span class="me1">Imaging</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Linq</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Text</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">namespace System.<span class="me1">Collections</span>.<span class="me1">Generic</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// Implements a reference counted list of T, allowing for implicit conversion to type T.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// A &quot;Reference counted list&quot; keeps track of both the items in the list as well as a reference count for each.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="co1">/// When an item is added, it&#8217;s reference count is incremented. If the item already exists, the existing item has it&#8217;s reference count incremented.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// Equality is determined by default using the Equals Method, but a predicate can be assigned.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; class ReferenceCounted&lt;T&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; private T lastitem = <span class="kw1">default</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; private Dictionary&lt;T, int&gt; ReferenceDictionary= new Dictionary&lt;T, int&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// Returns the Dictionary holding the Elements.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;returns&gt;&lt;/returns&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public Dictionary&lt;T, int&gt; getReferenceDictionary<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="kw1">return</span> ReferenceDictionary; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// creates a new ReferenceCounted List Object.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public ReferenceCounted<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// creates a new ReferenceCounted List Object, with the given IEqualityComparer as the comparison predicate.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;compareobject&quot;&gt;&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public ReferenceCounted<span class="br0">&#40;</span>IEqualityComparer&lt;T&gt; compareobject<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//initialize Dictionary with given IEqualityComparer interface.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReferenceDictionary = new Dictionary&lt;T, int&gt;<span class="br0">&#40;</span>compareobject<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// adds the given element to this list; which will either add an nonexistent item to the list and set it&#8217;s reference count to 1 or</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// increment the value of the existing element for the given value, as determined by any IEqualityComparer predicate given in the constructor.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;value&quot;&gt;Value to add or increment in the list.&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">void</span> AddElement<span class="br0">&#40;</span>T value<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//adds an element to this ReferenceCounted object.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//remove the previous item&#8230; or dereference it, rather.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!ReferenceDictionary.<span class="me1">Comparer</span>.<span class="me1">Equals</span><span class="br0">&#40;</span>lastitem, <span class="kw1">default</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RemoveElement<span class="br0">&#40;</span>lastitem<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastitem = <span class="kw1">default</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//first, does the given item exist in our dictionary already?</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!ReferenceDictionary.<span class="me1">ContainsKey</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//if not, we add it. Add it with a value 0, since it will be incremented after this if.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReferenceDictionary.<span class="me1">Add</span><span class="br0">&#40;</span>value, <span class="nu0">0</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//due to the above condition we know the element exists; increment it&#8217;s reference count.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReferenceDictionary <span class="br0">&#91;</span>value<span class="br0">&#93;</span> ++;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _Dirty = <span class="kw2">true</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &quot;removes&quot; the given element from this list. This is accomplished by decrementing the appropriate keyed item in the dictionary.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// If the element is not in this dictionary, this method has no effect.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">void</span> RemoveElement<span class="br0">&#40;</span>T value<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!ReferenceDictionary.<span class="me1">ContainsKey</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span>; <span class="co1">//nothing to do if no item.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReferenceDictionary <span class="br0">&#91;</span>value<span class="br0">&#93;</span> &#8211;;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//if reference count is zero: remove it.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>ReferenceDictionary <span class="br0">&#91;</span>value<span class="br0">&#93;</span> &nbsp;== <span class="nu0">0</span><span class="br0">&#41;</span> ReferenceDictionary.<span class="me1">Remove</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _Dirty = <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; T _CurrentMaximum=<span class="kw1">default</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; bool _Dirty = <span class="kw2">true</span>; <span class="co1">//whether the max is out of date.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// retrieves the item in this list with the maximum reference count.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// If multiple items have the maximum, only the first one encountered will be returned.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// This method also caches the result; calls only calculate a new maximum if necessary. (if the list was changed since the last one was cached).</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;returns&gt;&lt;/returns&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public T getMaxReferenced<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//find the item with the maximum value and return it&#8217;s key.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!_Dirty<span class="br0">&#41;</span> <span class="kw1">return</span> _CurrentMaximum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//otherwise, we&#8217;re &quot;dirty&quot; and need to re-find the maximum again.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> maxfound_int = <span class="kw4">int</span>.<span class="me1">MinValue</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T maxfound_T = <span class="kw1">default</span><span class="br0">&#40;</span>T<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var iterate in ReferenceDictionary<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>iterate.<span class="me1">Value</span> &gt; maxfound_int<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxfound_int = iterate.<span class="me1">Value</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxfound_T = iterate.<span class="me1">Key</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _CurrentMaximum = maxfound_T;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _Dirty = <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> _CurrentMaximum;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//Operator+; designed to be used like events- rather than code using = to &quot;assign&quot; a ReferenceCounted&lt;T&gt; value,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//it will use +=</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> ReferenceCounted&lt;T&gt; operator+<span class="br0">&#40;</span>ReferenceCounted&lt;T&gt; firstvalue, T secondvalue<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstvalue.<span class="me1">AddElement</span><span class="br0">&#40;</span>secondvalue<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> firstvalue; <span class="co1">//return the ReferenceCounted&lt;T&gt; object.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// Operator that calls RemoveElement. The result will be the ReferenceCounted object itself.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;firstvalue&quot;&gt;&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;secondvalue&quot;&gt;&lt;/param&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;returns&gt;&lt;/returns&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> ReferenceCounted&lt;T&gt; operator -<span class="br0">&#40;</span>ReferenceCounted&lt;T&gt; firstvalue, T secondvalue<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstvalue.<span class="me1">RemoveElement</span><span class="br0">&#40;</span>secondvalue<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> firstvalue;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&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;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// implicitly converts this list to the type of it&#8217;s component. </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;/summary&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;param name=&quot;value&quot;&gt;&lt;/param&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">/// &lt;returns&gt;&lt;/returns&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> implicit operator T<span class="br0">&#40;</span>ReferenceCounted&lt;T&gt; value<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> value.<span class="me1">getMaxReferenced</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&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;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<h3> Aftermath </h3>
</p>
<p>Pleased I had created a nice implementation, I set about creating the Comparison routine. Unfortunately, to my horror and surprise, the class which I wanted to use in conjunction with this class in one instance, ImageAttributes, had no way of getting it&#8217;s ColorMatrix. This presented an issue since I didn&#8217;t want added ColorMatrix values to mess about with the image, and the results could be less than extraordinary unless I cached each ImageAttributes.</p>
<p>And that was the entire purpose. However I decided to consider how else to acheive my goal; the goal here was to prevent powerups from changing the state of GameObject&#8217;s appearance in a manner that prevented them from undoing it. So, for example, powerups might have a limited duration, and the results from a overlap of two powerups could result in a confusing ending state for the object. The idea was to replace the GameObject class&#8217;s &#8220;DrawAttributes&#8221; field with a ReferenceCounted <drawattributes> ; then in it&#8217;s draw routine, it would assign use that in the appropriate method which would implicitly cast it to the maximum referenced item in the list. All I needed was a way to compare DrawAttributes aside from as references; but the ImageAttributes class, sadly, does not provide this functionality.</p>
<p> </drawattributes>  </p>
<p>So how do I address this?</p>
<p>I considered possibilities, and the problem, a bit more thoughtfully. Evidently, the ReferenceCounted&lt;T&gt; class would be very useful for it, but what would I use it for.</p>
<p>I&#8217;ve decided- though not yet attempted to implement- that I would use the ReferenceCounted class to keep track of  <i> The powerups themselves </i>  rather than a few fields of the gameobject. Since the powerup classes are what would result in the unwanted behaviour, it makes sense. So how does it work? Well, the framework basically allows a GameCharacter to have a list of GameCharacterAbilities; the GameCharacter calls the draw function of each when it draws, and it calls a frame function when it&#8217;s own frame function is called. My idea is to change that to a ReferenceCounted&lt;GameCharacterAbility&gt;. The code could then be changed to only call PerformFrame and Draw for the one with the highest reference count, or something similar.</p>
<p>The other possibility is to change the GameObject&#8217;s DrawAttributes field to a read-only property that is created &#8220;on the fly&#8221; from another new ColorMatrix field; the ColorMatrix item could be a ReferenceCounted object and therefore the use of that object in the property would use the implicit conversion operator. I&#8217;m trying to avoid this, even though I cannot foresee a circumstance where the ImageAttributes class provides something that I can&#8217;t do with a ColorMatrix (oh it does, but nothing I know how to do) I prefer to keep all my roads open, so to speak. If there was a way to compare the innards of the ImageAttributes, I could just change the DrawAttributes field to a ReferenceCounted object and make a new comparer, but it&#8217;s unfortunately not that simple. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/a-referencecounted-list/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=957&amp;md5=23eee2ae8a4329a1dee685da93596f1a" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/04/a-referencecounted-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F04%2Fa-referencecounted-list%2F&amp;language=en_GB&amp;category=software&amp;title=A+ReferenceCounted+List&amp;description=BASeBlock+%26%238211%3B+Music+Manipulation+Lessons+As+with+most+games%2C+BASeBlock+has+music.+Originally%2C+I+implemented+a+naive+approach+to+have+%26%238220%3BMultiple+music%26%238221%3B+playing%3B+a+simple+stack.+The+multiple+music+idea...&amp;tags=BASeBlock%2CC%23%2CGame+Programming%2CGenerics%2CMusic%2CSound%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Trolls are fun.</title>
		<link>http://bc-programming.com/blogs/2012/04/trolls-are-fun/</link>
		<comments>http://bc-programming.com/blogs/2012/04/trolls-are-fun/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 04:58:59 +0000</pubDate>
		<dc:creator>BC_Programming</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://bc-programming.com/blogs/?p=953</guid>
		<description><![CDATA[Well, my posts keep getting deleted off the MC forums (arguably mostly because they are responses to a troll, but damn, trolls can be amusing). Anyway, there was a discussion where some members seemed to agree that RAM never equals performance. My post on the matter was: What the hell is wrong with you people? [...]]]></description>
			<content:encoded><![CDATA[<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/trolls-are-fun/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div><p>Well, my posts keep getting deleted off the MC forums (arguably mostly because they are responses to a troll, but damn, trolls can be amusing).</p>
<p>Anyway, there was a discussion where some members seemed to agree that RAM never equals performance. My post on the matter was:</p>
<blockquote><p>
What the hell is wrong with you people?</p>
<p>Run Vista/7 on a system with 512MB of RAM.</p>
<p>Upgrade that system to 4GB. The performance improves. If RAM doesn&#8217;t increase performance, than this is only explanable because of MAGIC.</p>
<p>RAM improves performance, but only to a certain point. (which is what you guys are saying, just not very well).</p>
<p>For example, even for Minecraft, RAM matters; less physical memory means more swapping means more jerkiness loading chunks. But beyond 4GB, I doubt MC would see any performance improvement, at least not directly; superfetch might cache chunk file data, which would increase chunk loading, and it can only do that with extra memory which wouldn&#8217;t otherwise exist, so there is that; but there is a point of diminishing returns. However to state that those returns do not exist is foolish. MC would run better with 32GB of RAM than with 16 or 8; but the difference would be nearly inperceptible, and certainly not worth the massive price difference.
 </p></blockquote>
<p>In response, I got the following:</p>
<blockquote><p>
Hey look dumbo fumbo is back&#8230;<br />
YOU ARE STUPID!<br />
READ THE POSTS<br />
IT SAYS: Ram isn&#8217;t everything, though each system should get a fair amount of it.<br />
CentrallyProcessed just said that.<br />
No one mentioned 512mb of ram, what are you 2002?<br />
No one needs a 10 minute wall of text&#8230;<br />
You probably got that off WIkipedia, since that is pure bs in there.<br />
The only time you need 16gb of ram for minecraft is using a biomebuster&#8230;<br />
Plus Minecraft only takes up 912mb of ram&#8230;<br />
Press f3 and see that&#8230;<br />
Unless you change it of course.<br />
If Notch made it so that minecraft takes 912mb of ram, then you can&#8217;t argue with him.
 </p></blockquote>
<p>Which though an obvious troll, could not go unpunished:</p>
<blockquote><p>
Hey look dumbo fumbo is back&#8230;</p>
</blockquote>
<p>AHHH MY FEELINGS!</p>
<blockquote><p>
YOU ARE STUPID!</p>
</blockquote>
<p>Well that&#8217;s not very nice, but I suppose a true friend would be honest. Perhaps everybody else is just trying to protect me from that truth. I commend you on being a true friend and pointing out where I falter. Thank you.</p>
<blockquote><p> READ THE POSTS</p>
</blockquote>
<p>I did. In fact, the one  I was responding to- which was echoed in a few other posts, was quoted quite clearly in my posts.<br />
 [quote] IT SAYS: Ram isn&#8217;t everything, though each system should get a fair amount of it. [/quote]<br />
I find this curious. You say I am stupid, but you are unable to use the appropriate pluralizations. You speak of &#8220;posts&#8221; (plural) but then you say &#8220;it says&#8221;. Obviously this is just a slip-up, easily forgiven, but it makes it difficult to understand what specifically you mean, since I wasn&#8217;t addressing all the posts, only those that shared in the thought that I felt was mistaken that more RAM never speeds up a PC. One could argue also that I was attempting to dissolve what certainly looked to be a circle-jerk, too.</p>
<blockquote><p> CentrallyProcessed just said that. </p></blockquote>
<p>He also said:</p>
<p>&#8220;It entirely depends on the CPU and GPU&#8221; < <it doesn't.</p>
<p>And others said:<br />
&#8220;RAM does not equate to performance.&#8221; << it does. always.</p>
<blockquote><p> No one mentioned 512mb of ram, what are you 2002?</p>
<p>They didn&#8217;t have computers in 10 A.D so I don&#8217;t follow your logic. If I was that old I&#8217;d probably be pretty famous. </p>
<p>People mentioned Memory. are you arguing then, that 512MB of memory is not memory? At what size does RAM become RAM? 512MB is a valid amount of memory, and when you add memory to a system with 512MB of memory using a modern OS, performance improves. This is an undeniable fact. replacing 4GB of memory in a system with 8GB will improve performance as well; same with 16GB over 8GB. Whether it improves to a point that makes it worth that investment is questionable, but the improvements do exist.</p>
<blockquote><p> No one needs a 10 minute wall of text&#8230; </p></blockquote>
<p>Actually, the length of text is typically measured in characters, or words- not time.</p>
<blockquote><p> You probably got that off WIkipedia, since that is pure bs in there. </p></blockquote>
<p>Possibly; I am certainly open to refutations of that which I said that are based on intelligent arguments, observations, and other such evidence. Unfortunately, calling me a &#8220;dumbo fumbo&#8221; does not constitute either an intelligent argument, and observation, or evidence, so I cannot rightly accept that as a valid refutation.</p>
<blockquote><p> The only time you need 16gb of ram for minecraft is using a biomebuster&#8230; </p></blockquote>
<p>I never said anything about minecraft needing any particular amount of Memory. I said performance would be better, for the most part because of the better utilization of the memory in most cache schemes for keeping file data- in minecraft&#8217;s case chunk information- in the standby pool. Whether that increase is negligible was not debated; in fact I specifically pointed out that the difference would not be worth the investment. However whether something is worth the investment and whether there is a difference that exists are two separate points.</p>
<blockquote><p> Plus Minecraft only takes up 912mb of ram&#8230;<br />
Press f3 and see that&#8230; </p></blockquote>
<p>The minecraft F3 debug screen displays the size of the java heap, which directly relates to the size of data that is being used by the uncollected game objects, but has no bearing on the other structures used by the Virtual Machine, nor does it contain memory consumed by objects resurrected during finalization (which hopefully doesn&#8217;t happen). The real information on the memory usage can be readily seen using task manager, process explorer, or, on linux, ps -a -v. I started minecraft on my windows system when I started this post, about 5 minutes ago at this point, though don&#8217;t put too much weight on that, I&#8217;m also conversing in IRC and uploading a few files via FTP); it has been paused since. it&#8217;s virtual memory usage- according to process explorer- is now idling at 1.2GB; This was sitting in one place, so that is essentially the minimum it will probably use, at least at the various settings I&#8217;ve chosen. if I was to play the game, memory usage would undoubtedly go up as new objects were created, and more chunks were kept in memory and lazily unloaded. However, whether Minecraft uses more memory than is installed cannot be used as the single judge of whether it would improve performance.</p>
<p>You see, Windows, like all modern operating systems, is a protected mode, pre-emptive multi-tasking environment. Processes share memory; this is done by mapping physical memory into the virtual address space of each process, so they all think they &#8220;own&#8221; memory. this also isolates the memory of each process (&#8216;protecting&#8217; the memory of each application from other applications, giving us the moniker &#8220;protected mode&#8221;.). Of course, in order to act on memory, it needs to be in physical memory. Memory data in is swapped to and from physical memory as needed by way of a pagefile; if you run an application that allocated over 2 gigabytes of memory,  but leave it sitting there, eventually none of it&#8217;s data will be in physical RAM, instead replaced by the data of other applications that needed it, and entirely swapped into the pagefile. If you then switch to that application and it accesses some of what it allocated, the required pages will fault, and the memory manager will swap that data back into physical memory.</p>
<p>The advantage here is that because various applications, services, and other applications run at the same time, you cannot think in terms of what a single application would need, because you never have a single application running.</p>
<p>Also, of particular note is that much of the performance improvement from more memory on modern systems is seen by way of disk caching technologies such as Superfetch; more memory means that more of the &#8220;unloaded&#8221; chunks that minecraft has saved to disk will in fact be saved in memory, meaning that reloading that data from the disk (from the perspective of the game) will occur much faster. These benefits can be seen by comparing a 4GB and a 8GB system; wandering around a world, chunk loading will be slowed by I/O, but returning to some areas will load faster, because those chunk files&#8217; data is still in the cache. with more memory, more of that data can be cached for faster access. Chunk management is a central task performed by minecraft; it is indisputable that improving the speed of that chunk management would improve the overall performance of the game itself. The fact that the memory that is used to create that increased performance isn&#8217;t mapped into the virtual address space of the game is not relevant.</p>
<blockquote><p>
If Notch made it so that minecraft takes 912mb of ram, then you can&#8217;t argue with him.
 </p></blockquote>
<p>Notch didn&#8217;t make it that way. that is how the Java Virtual Machine manages the default java heap size. However, as I noted, the java heap size in no way reflects either the total commit charge of the process nor does it have any bearing on the availability of memory for use in tasks such as Superfetch. While I was amused by your attempts at derogatory comments toward me, I refuse to respond in kind. Which is most certainly a benefit in your favour, for my ability with language would certainly come up with something more creative than &#8220;dumbo fumbo&#8221; which in no way makes sense.</p>
<p>See, the best way to insult somebody is to take advantage of their weaknesses. At the moment, I am unemployed, (well, not legally, but let&#8217;s not confuse the issue, we&#8217;re trying to insult me, not write a book) so if I may suggest, you should certainly make light of that. Perhaps an insult such as &#8220;if you&#8217;re so smart why don&#8217;t you have a job&#8221; or if you want to condense it, &#8220;dumbo jobless&#8221;. however I think we might be best to eliminate the &#8220;dumbo&#8221; element altogether, particular since it, at least in my case, provides the image of elephants with big ears, which is actually kind of funny. My ears are not of a disproportionate size, either, so it doesn&#8217;t really fit, nor am I sensitive about the size of my ears. However, I&#8217;m sure you can think of something! Good luck. I look forward to your intelligent witticisms. Or perhaps you will simply make light of how long my post is! You could surely do that, and it saves you even reading. the bad part is that if somebody does read it, they will see this last part, see your reply, and realize that I in fact predicted your behaviour, which could reflect poorly on your character as being far too predictable.</p>
<p>Unfortunately the post was deleted, both mine and the trolls, which was sort of shame. Good thing I drafted it in gedit. Past here for posterity. Also because it has some interesting info on memory management. </p>
<div id="fcbk_share"><div class="fcbk_like">
										<div id="fb-root"></div>
										<script src="http://connect.facebook.net/en_US/all.js#appId=224313110927811&amp;xfbml=1"></script>
										<fb:like href="http://bc-programming.com/blogs/2012/04/trolls-are-fun/" send="false" layout="button_count" width="450" show_faces="false" font=""></fb:like>
									</div></div> <p><a href="http://bc-programming.com/blogs/?flattrss_redirect&amp;id=953&amp;md5=9f62ba8ec8af61f4ab0d9f1df874fa1d" title="Flattr" target="_blank"><img src="http://bc-programming.com/blogs/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://bc-programming.com/blogs/2012/04/trolls-are-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=bc_programming&amp;popout=1&amp;url=http%3A%2F%2Fbc-programming.com%2Fblogs%2F2012%2F04%2Ftrolls-are-fun%2F&amp;language=en_GB&amp;category=software&amp;title=Trolls+are+fun.&amp;description=Well%2C+my+posts+keep+getting+deleted+off+the+MC+forums+%28arguably+mostly+because+they+are+responses+to+a+troll%2C+but+damn%2C+trolls+can+be+amusing%29.+Anyway%2C+there+was+a+discussion...&amp;tags=blog" type="text/html" />
	</item>
	</channel>
</rss>

