<?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>polygonal</title>
	<atom:link href="http://lab.polygonal.de/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://lab.polygonal.de</link>
	<description>Blog of Michael Baczynski</description>
	<lastBuildDate>Fri, 01 Mar 2013 09:32:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Reassembling the polygonal library</title>
		<link>http://lab.polygonal.de/?p=2900</link>
		<comments>http://lab.polygonal.de/?p=2900#comments</comments>
		<pubDate>Wed, 25 Jul 2012 22:22:19 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=2900</guid>
		<description><![CDATA[I&#8217;ve just finished restructuring my library into smaller parts &#8211; this was necessary because the previous monolithic approach has become really hard to maintain. Now, I can push updates more frequently and adding new stuff should be also much easier. The various parts of the library are now hosted on github: https://github.com/polygonal. I apologize for [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve just finished restructuring my library into smaller parts &#8211; this was necessary because the previous monolithic approach has become really hard to maintain. Now, I can push updates more frequently and adding new stuff should be also much easier.<br />
The various parts of the library are now hosted on github: <a href="https://github.com/polygonal" title="polygonal on github" target="_blank">https://github.com/polygonal</a>.<br />
I apologize for any inconvenience and hope the reorganization does not break that much code.<br />
BTW, the library now fully supports <a href="http://haxe.org/" title="Haxe" target="_blank">Haxe</a> 2.10 :)</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=2900</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Using sprintf with Haxe</title>
		<link>http://lab.polygonal.de/?p=1939</link>
		<comments>http://lab.polygonal.de/?p=1939#comments</comments>
		<pubDate>Mon, 30 Apr 2012 18:39:21 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=1939</guid>
		<description><![CDATA[In contrast to ActionScript, Haxe allows you to customize the trace() function to better fit your requirements. For example, you can format or delegate the input to your favorite logger, resulting in less verbose code. My library supports the sprintf syntax, so let&#8217;s take a look on how to use it seamlessly with trace(). First, [...]]]></description>
				<content:encoded><![CDATA[<p>In contrast to ActionScript, Haxe allows you to customize the <em>trace()</em> function to better fit your requirements. For example, you can format or delegate the input to your favorite logger, resulting in less verbose code.<br />
My library supports the <a title="sprintf" href="http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/" target="_blank">sprintf</a> syntax, so let&#8217;s take a look on how to use it seamlessly with <em>trace()</em>.</p>
<p>First, we need to redefine the behavior of the <em>trace()</em> function, which is done by calling <em>Root.init()</em>:</p>
<pre class="prettyprint">class Main
{
    public static function main() {
        de.polygonal.core.Root.init(onInit, true);
    }

    static function onInit() {
        trace("entry point...");
    }
}</pre>
<p>From now on, <em>trace()</em> calls are redirected to<em> Root.log().debug(&#8230;)</em>, and the default log handler writes to flashlog when targeting Flash. Compiling and running the above example, the log file should look like this:</p>
<pre>001 DEBUG line 0154 Root::_onInit()               log initialized.
002 DEBUG line 0016 Test::new()                   entry point...</pre>
<p>BTW, I&#8217;m using a wonderful little log viewer called <a title="BareTail" href="http://www.baremetalsoft.com/baretail/" target="_blank">Baretail</a>. We are now ready to trace out sprintf formatted data. This is the regular way&#8230;</p>
<pre class="prettyprint">import de.polygonal.core.fmt.Sprintf;

trace(Sprintf.format("My name is %s.", ["Michael"])); //My name is Michael.</pre>
<p>&#8230;but thanks to Haxe it all boils down to this:</p>
<pre class="prettyprint">trace("My name is %s.", "Michael"); //My name is Michael.</pre>
<p>Here are some more examples:</p>
<pre class="prettyprint">trace("x=%02d", 3); //x=03

trace("PI equals %.3f", Math.PI); //PI equals 3.142

trace("%#x", 255); //0xff

trace("%.2f%%", 0.95); //0.95%

trace("[%+10.3f]", 1.1); //[    +1.100]</pre>
<p>And if you want to get rid of all trace statements, you simply compile with <code>--no-traces</code> ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=1939</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A fast notification system for event-driven design</title>
		<link>http://lab.polygonal.de/?p=2548</link>
		<comments>http://lab.polygonal.de/?p=2548#comments</comments>
		<pubDate>Tue, 24 Apr 2012 13:08:55 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Haxe]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=2548</guid>
		<description><![CDATA[Back in 2009 I started to work on a custom solution for dispatching and listening to events. At this time I switched to the Haxe language, and so my main motivation was to write a cross-platform system. I also never really liked the event dispatcher in Flash – too java-ish and boilerplate heavy for my taste. I guess this is why other [...]]]></description>
				<content:encoded><![CDATA[<p>Back in 2009 I started to work on a custom solution for dispatching and listening to events. At this time I switched to the Haxe language, and so my main motivation was to write a cross-platform system. I also never really liked the event dispatcher in Flash – too java-ish and boilerplate heavy for my taste. I guess this is why other libraries like <a title="AS3 Signals" href="https://github.com/robertpenner" target="_blank">AS3 Signals</a>, or <a title="hsl" href="http://lib.haxe.org/t/hsl" target="_blank">HSL</a> (Haxe Signaling Library) appeared.</p>
<p>In short, here are the key facts:</p>
<ol>
<li>I’m using the classic <strong>Observer pattern</strong>, because it’s simple and widely known.</li>
<li>Instead of listening to single events, I’m registering with multiple related events at once (&#8220;<strong>event group</strong>&#8220;).</li>
<li>Updates are not identified by strings, but are represented as <strong>integers</strong>.</li>
<li>The observers are managed in a <strong>linked list</strong>.</li>
</ol>
<p>Notice that in the following text, the terms event/update, listener/observer and event dispatcher/observable are used interchangeably. The source code is hosted on <a href="http://github.com/polygonal/core/tree/master/src/de/polygonal/core/event" title="polygonal library on github" target="_blank">github</a>.<br />
Let’s break down each point:</p>
<h4>1. The interface</h4>
<p>My design follows the observer pattern – chances are good that it&#8217;s the first design pattern covered in many books so I won&#8217;t go into much detail.<br />
Actually, it&#8217;s quite simple: An observable manages a list of observers. Whenever the state of the observable changes, the observable iterates over all observers and calls observer.update(). The entire system consists of three components:</p>
<ol>
<li><em>de.polygonal.core.event.IObservable</em>, an interface which merely defines three methods:<br/><br/>
<pre class="prettyprint">interface IObservable
{
    function attach(o:IObserver, mask:Int = 0):Void;

    function detach(o:IObserver, mask:Int = 0):Void;

    function notify(type:Int, userData:Dynamic = null):Void;
}</pre>
</li>
<li><em>de.polygonal.core.event.IObserver</em>, an interface with a single update() method:<br/><br/>
<pre class="prettyprint">interface IObserver
{
    function update(type:Int, source:IObservable, userData:Dynamic):Void;
}</pre>
</li>
<li><em>de.polygonal.core.event.Observable</em>, a ready-to-use implementation of IObservable.</li>
</ol>
<p>Thinking in flash terms, 2. and 3. correlate with <em>flash.events.IEventDispatcher</em> and <em>flash.events.EventDispatcher</em>. There is however, no <em>flash.events.Event</em> object. Instead the observer has to pull this information from the <em>source</em> (always points to the object that triggered the update) or use the <em>userData</em> parameter, which is often used for convenience (e.g. keyboard events store the current key code in it). Since I prefer short names, <em>addEventListener()</em>, <em>removeEventListener()</em> and <em>dispatchEvent()</em> are simply called <em>attach()</em>, <em>detach()</em> and <em>notify()</em>.</p>
<h4>2. Event groups</h4>
<p>We often have to set up listeners for similar events. For example, let&#8217;s assume we want to download a file from a server – in most cases a simple &#8220;onLoadComplete&#8221; event is not enough: we also need to handle errors or visualize the download progress. So the main idea is to replace multiple calls to <em>addEventListener()</em> with a single call to <em>attach()</em>, as you need those events anyway. This drastically reduces boilerplate code.<br />
A good example is the <em>Resource</em> class from my library, responsible for loading all kind of external assets. By attaching to a resource object, you get all important updates at once:</p>
<pre class="prettyprint">import de.polygonal.core.event.IObservable;
import de.polygonal.core.event.IObserver;
import de.polygonal.core.io.Resource;
import de.polygonal.core.io.ResourceEvent;
import de.polygonal.core.io.ResourceType;

/**
 * Example: loading an image using the Resource class.
 */
class Main implements IObserver {
    static function main() {
        new Main();
    }

    public function new() {
        var url = "http://polygonal.github.com/doc/ds/images/polygonal.png";
        var res = new Resource(new flash.net.URLRequest(url));
        res.attach(this); //listen to updates
        res.load();
    }

    public function update(type:Int, source:IObservable, userData:Dynamic):Void {
        trace("received event: " + ResourceEvent.getName(type));
        switch (type) {
            case ResourceEvent.COMPLETE:
                var res:Resource = cast source;
                var data:flash.display.Bitmap = source.content;
            
            case ResourceEvent.PROGRESS:
                var progress:Float = userData; //[0,1]
            ...
        }
    }
}</pre>
<p>Compiling and running this example, the trace statement prints out all updates like this:</p>
<pre>TestLoading.hx:23: received event: START
TestLoading.hx:23: received event: PROGRESS
TestLoading.hx:23: received event: PROGRESS
TestLoading.hx:23: received event: PROGRESS
TestLoading.hx:23: received event: PROGRESS
TestLoading.hx:23: received event: HTTP_STATUS
TestLoading.hx:23: received event: COMPLETE</pre>
<p>So far, so good. But what about a situation where you don’t use all those updates? Let’s consider the case that we have tons of observers listening to user input events. We are only interested in keyboard updates, and both key and mouse events are defined in a single event group. Mouse position updates are fired at a high rate and might slow down your application. The solution is to register with a subset of events only by explicitly naming them:</p>
<pre class="prettyprint">myObserver.attach(myObservable, UIEvent.KEY_DOWN | UIEvent.KEY_UP);</pre>
<p>This will only invoke <em>myObserver.update()</em> for those two event types. If we don&#8217;t need an event at all (usually application-wide), we can blacklist the event on the dispatcher and disable it completely, further improving performance:</p>
<pre class="prettyprint">myObservable.mute(UIEvent.MOUSE_MOVE);</pre>
<h4>3. Why integers?</h4>
<p>Using integers for identifying events has some advantages:</p>
<ul>
<li>We can pack multiple properties – in this case a group id and an update id.</li>
<li>We can efficiently check against multiple updates by combining them with bitwise OR or test if an update belongs to a particular group:<br/><br/>
<pre class="prettyprint">public function update(type:Int, source:IObservable, userData:Dynamic):Void
{
    if (UIEvent.has(type))
    {
        //handle ui updates
        if (type &amp; (UIEvent.KEY_DOWN | UIEvent.KEY_UP) &gt; 0)
        {
            //handle key updates
        }
    }
    if (ResourceEvent.has(type))
    {
        //handle resource updates
    }
}</pre>
</li>
<li>We can register with/unregister from multiple events at once in an elegant way:<br/><br/>
<pre class="prettyprint">myObservable.attach(myObserver, Event.A | Event.B | Event.C); //register with A,B,C
myObservable.detach(myObserver, Event.A | Event.B); //unregister from A,B only</pre>
</li>
<li>Comparing integers is wickedly fast. It won’t make a big difference, but it’s good to know ;).</li>
</ul>
<p>However, there is a major drawback: one has to make sure integers are unique across the application! Assigning ids by hand is prone to errors and dangerous, so I hand it over to the compiler and let a macro do all the work. In short, a macro is a piece of code written in the Haxe language itself that modifies existing code or generates new one at compile-time. This way, creating an event boils down to this:</p>
<pre class="prettyprint">@:build(de.polygonal.core.event.ObserverMacro.create([
	KEY_DOWN
	KEY_UP,
	KEY_REPEAT,
	MOUSE_DOWN,
	MOUSE_UP,
	MOUSE_WHEEL,
	MOUSE_MOVE]))
class UIEvent {}</pre>
<p>The syntax might look a bit strange at first, but it’s just an array of field names that gets passed to the build method. The macro then generates those fields, encodes and assigns integer values and adds some convenience methods, like for printing out the human readable name of an update. Before <a title="Haxe got Macros !" href="http://ncannasse.fr/blog/haxe_got_macros" target="_blank">Nicolas added the macro-system</a>, I defined them manually, which was a total <a title="old revision of ResourceLoaderEvent" href="http://code.google.com/p/polygonal/source/browse/trunk/src/lib/de/polygonal/core/io/ResourceLoaderEvent.hx?r=40" target="_blank">mess</a>.</p>
<p>Speaking about the format: each 32-bit integer stores a group id and an update type. 5 bits are reserved for the group id, while the remaining bits represent a bit field for the update type. Therefore, a total of 32 groups (2^5-1) and 27 individual types (32-5) can be encoded. In total the system supports 32&#215;27=135 unique events which should be enough for any application. By sacrificing one update bit, we get over 1500 different types.</p>
<h4>4. Observer storage</h4>
<p>Another decision I made is to use linked lists instead of arrays for storing observers. This way there is no need to make a copy of the observer list before dispatching updates. Arrayed implementations do this, because the observer list can be modified by observers themselves:</p>
<pre class="prettyprint">public function update(type:Int, source:IObservable, userData:Dynamic):Void
{
    source.detach(this);
}</pre>
<p>The above code updates the observer list instantly, but due to the nature of linked list, the update phase won&#8217;t break.<br />
Notice that when an observer calls<em> notify()</em> on its observable, the current update stops while the new update is carried out to all observers, and resumed as soon as the nested iteration has been completed. Let&#8217;s assume we have 3 observers: [A, B, C] – when B invokes an update the order will be: [A, B, [A, B, C], C]. Now, when C detaches from its source (when <em>C.update()</em> is called for the first time), C will disappear from all &#8220;levels&#8221;, and the last C at the root level won&#8217;t receive an update. In practice this shouldn&#8217;t be a problem and it’s usually the desired behavior.</p>
<p>Using linked lists require that observer objects are “boxed”: a helper objects has to be allocated for storing the observer and list pointers. To help with performance, I allocate and reuse those helper structures once an observer gets detached from its observable:</p>
<pre class="prettyprint">myObservable.attach(myObserverA); //allocate helper structure for A
//...
myObservable.detach(myObserverA); //cache it
//...
myObservable.attach(myObserverB); //reuse helper structure for B
//...</pre>
<p>The cache size is set to 10 by default, but you can easily customize this value by providing a different size to the constructor:</p>
<pre class="prettyprint">var x = new Observable(50);</pre>
<h4>Performance</h4>
<p>First: I can&#8217;t imagine a situation where dispatching events could be the bottleneck of an application. That said, it still does not hurt to provide an efficient solution. The most important thing for Flash is to use typed function calls, which are roughly 10x faster. This is achieved by forcing each observer to implement the <em>IObserver</em> interface:<br />
<img src="wp-content/assets/120423/chart1.png" alt="typed v untyped function calls" /><br />
Also, iterating a linked-list is very fast in Flash. Compared to <em>flash.events.EventDispatcher</em>, the overall result is pretty good:<br />
<img src="wp-content/assets/120423/chart2.png" alt="EventDispatcher v Observable" /></p>
<p>To sum it up, the system is optimized for updates fired at a high rate and listeners with a short live span, e.g.<br />
an AI system, were lots of agents are interacting with other agents by sending messages.</p>
<h4>Tips</h4>
<ol>
<li><em>Observable.dump(</em>) prints out all observers in use along with their number of observers. Useful for profiling.</li>
<li><em>Observable.release()</em> destroys all observable objects and removes all observers to make sure that the garbage collector can free up all used resources.</li>
<li><em>Observable.bind(func)</em> can be used to bind a function to an update. The binding is active as long as the closure returns true:
<pre class="prettyprint">var f = function()
{
     trace('update!');
     return true;
}
Observable.bind(f, myObservable);</pre>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=2548</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Introduction to ds</title>
		<link>http://lab.polygonal.de/?p=2513</link>
		<comments>http://lab.polygonal.de/?p=2513#comments</comments>
		<pubDate>Wed, 11 Jan 2012 14:42:22 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Haxe]]></category>
		<category><![CDATA[data structures]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=2513</guid>
		<description><![CDATA[&#8220;Introduction to ds&#8221; is a slide presentation/manual about ds which I started last year. I never had enough time to finish it quickly so I wrote it now and then and just recently finished it. The presentation explains the concepts and design decisions behind ds, provides short code examples to demonstrate basic usage and covers [...]]]></description>
				<content:encoded><![CDATA[<p>&#8220;Introduction to ds&#8221; is a slide presentation/manual about ds which I started last year. I never had enough time to finish it quickly so I wrote it now and then and just recently finished it.</p>
<p>The presentation explains the concepts and design decisions behind ds, provides short code examples to demonstrate basic usage and covers each data structure in a few words. I tried to squeeze in as much information as possible but due to the complexity of the topic it&#8217;s impossible to go into detail – each data structure alone could easily fill a presentation…</p>
<p>Code examples are all in haXe language, but converting to AS3 is simply a matter of removing type parameters &lt;T&gt; and renaming Int to int, Float to Number and so on.</p>
<p>Download:<br />
<a title="Introduction to ds" href="wp-content/assets/120111/introduction_to_ds.pdf" target="_blank">&#8220;Introduction to ds&#8221; (pdf)</a></p>
<p>Or watch it on slideshare.net:<br />
<a href="http://www.slideshare.net/polygonal/introduction-to-ds">http://www.slideshare.net/polygonal/introduction-to-ds</a></p>
<p>I hope you find it useful! (if you are an experienced game programmer you will be probably bored ;))</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=2513</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linked list performance</title>
		<link>http://lab.polygonal.de/?p=1826</link>
		<comments>http://lab.polygonal.de/?p=1826#comments</comments>
		<pubDate>Mon, 19 Dec 2011 09:49:11 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Haxe]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=1826</guid>
		<description><![CDATA[It&#8217;s a fact that haXe creates swf files running faster compared to mxmlc. Amazingly, haXe is also faster at compile time. But to which extent are haXe generated swf files faster? Optimizing code especially pays off for &#8220;low-level stuff&#8221; like data structures, the building blocks of many algorithms. With this in mind, I picked the doubly linked [...]]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s a fact that haXe creates swf files running faster compared to mxmlc. Amazingly, haXe is also faster at compile time. But to which extent are haXe generated swf files faster? Optimizing code especially pays off for &#8220;low-level stuff&#8221; like data structures, the building blocks of many algorithms. With this in mind, I picked the doubly linked list class from my <a title="ds" href="http://code.google.com/p/polygonal/wiki/DataStructures" target="_blank">ds</a> library (extensively used in almost all my projects) and played around with different optimization levels to figure out how those translate into run time improvements of the flash platform.</p>
<p>So, which options do we have?<br />
First, we can create &#8220;true generic classes&#8221; at compile time by adding <code>-D generic</code> to the haXe command line. We gain almost 40%. Why? Because we get rid of dynamic access (*) – perfect for AVM2, which is a statically typed runtime. You can read more about this <a title="flash 9 optimizations" href="http://ncannasse.fr/blog/flash_9_optimizations?lang=en" target="_blank">here</a>.<br />
Next, let&#8217;s turn on function inlining, so we don&#8217;t have to deal with the overhead of calling many small functions. Inlining is always enabled by default, but you can turn it off by compiling with <code>--no-inline</code>, just so you can see the difference. This increases performance by another 70%.<br />
So far we have twice the performance, thanks to smart compiler optimizations. Regarding linked lists (and many other linked structures in ds) we have one secret weapon left: &#8220;node caching&#8221;, a feature that was introduced in ds version 1.12 back in 2010. By passing a size parameter to the constructor we can reserve additional memory to minimize costly instantiation of linked list nodes:</p>
<pre class="prettyprint">var reservedSize = 1000;
var list = new de.polygonal.ds.DLL(reservedSize);</pre>
<p>From now on, every time you remove an element from the list, the node object storing that element is kept for later reuse – ready to store another incoming element. The resulting behavior is an incrementally filled object pool. And don&#8217;t worry if the size exceeds the predefined size – in this case objects are simply created on-the-fly. And here are the final numbers:</p>
<p><img src="wp-content/assets/111218/chart.png" alt="benchmark results" /></p>
<p>Notice that due to additional bytecode instructions for debugging, input validation and assert statements the debug build is awfully slow, so better not use it for deploying your application ;). The release build is effectively what you get when compiling <code>ds_release.swc</code> with mxmlc, on par with other libraries like the LinkedList class found in the as3commons <a title="as3commons Collection" href="http://www.as3commons.org/as3-commons-collections/index.html" target="_blank">Collection</a> package. Actually, haXe compiled SWC files are a bit faster because the generated bytecode is more efficient and private methods are still inlined, but in order to unleash the full potential of the flash player there is currently no way around haXe.</p>
<h4>About &#8220;-D generic&#8221;</h4>
<p>This is a custom conditional compilation switch which is available in ds. If defined, classes defining linked structures will implement the marker interface <code>haxe.rtti.Generic</code>. This tells the compiler to create specialized classes for each type parameter. Here is a simplified example:</p>
<pre class="prettyprint">
class Container&lt;T&gt;
#if generic
implements haxe.rtti.Generic
#end
{
    public var value:T;
    public function new() {}
}</pre>
<p>If we create an object of type Container like so&#8230;</p>
<pre class="prettyprint">
var myFloatContainer = new Container&lt;Float&gt;();
</pre>
<p>&#8230;the compiled result will look like this&#8230;</p>
<pre class="prettyprint">
class Container_Float
{
    public var value:Float;
    public function new() {}
}
</pre>
<p>&#8230;instead of:</p>
<pre class="prettyprint">
class Container
{
    public var value:Dynamic;
    public function new() {}
}
</pre>
<p>Now everything is strictly typed and we don&#8217;t need slow dynamic access.</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=1826</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A* pathfinding with ds</title>
		<link>http://lab.polygonal.de/?p=1815</link>
		<comments>http://lab.polygonal.de/?p=1815#comments</comments>
		<pubDate>Fri, 22 Jul 2011 15:11:23 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Haxe]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[graph]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=1815</guid>
		<description><![CDATA[I recently did a complete rewrite of my graph-based A* pathfinder example because I received a lot of questions on how to implement path-finding using the new ds library. So here is an updated demo that works with ds 1.32: Interactive demo: A* pathfinding with ds I&#8217;m transforming the point cloud with delaunay triangulation into [...]]]></description>
				<content:encoded><![CDATA[<p>I recently did a complete rewrite of my <a href="./?p=185" target="_blank">graph-based A* pathfinder example</a> because I received a lot of questions on how to implement path-finding using the new ds library. So here is an updated demo that works with <a href="http://code.google.com/p/polygonal/wiki/DataStructures">ds 1.32</a>:</p>
<p><a class="demo" title="Interactive demo: A* pathfinding with ds" href="wp-content/assets/110722/flash.html" target="_blank">Interactive demo: A* pathfinding with ds</a></p>
<p>I&#8217;m transforming the point cloud with delaunay triangulation into a graph structure. Then the system computes and draws the shortest path between two selected points.</p>
<h3>Compile instructions</h3>
<p>Running and examining the example is really easy:</p>
<ol>
<li>Use the automated installer to install haXe from <a href="http://haxe.org/download" target="_blank">http://haxe.org/download</a>.</li>
<li>Download the <a href="http://haxe.cmt.tc/" target="_blank">latest haXe nightly build</a> and overwrite the existing &#8216;haxe.exe&#8217; and &#8216;std&#8217; folder with the downloaded version.</li>
<li>Install the polygonal library by opening the command prompt and typing:<br />
<code>$ haxelib install polygonal</code>.</li>
<li>Sources should now be in {haxe_install_dir}/lib/polygonal/1,18/src/impl/sandbox/ds/astar, where {haxe_install_dir} is usually C:/Motion-Twin/haxe on Win7.<br />
The demo can be then compiled with:<br />
<code>$ cd C:\Motion-Twin\haxe\lib\polygonal\1,18\build<br />
$ haxe.exe compile-ds-examples.hxml</code></li>
</ol>
<h3>Extending the Graph class</h3>
<p>You have basically two options to extend the functionality of the Graph object: by composition or inheritance. While I highly recommend to use composition whenever possible, I&#8217;ve also included a version using inheritance &#8211; just so you see the difference.</p>
<p>The composition version looks like this:<br />
<img src="wp-content/assets/110722/composition.png" alt="A* using composition" /></p>
<p>The <em>Graph</em> object manages <em>GraphNode</em> objects, and each <em>GraphNode</em> holds a <em>Waypoint</em> object, which defines the world position of the waypoint as well as intermediate data used by the A* algorithm. Notice that <em>GraphNode</em> and <em>Waypoint</em> are cross-referencing each other as a <em>Waypoint</em> object has to query the graph for adjacent nodes. As a result, you have a clean separation between the data structure (<em>Graph</em>, <em>GraphNode</em>) and the algorithm (<em>AStar</em>, <em>Waypoint</em>) and don&#8217;t need object casting, which is good news because casting is a rather slow operation.</p>
<p>Now let&#8217;s look at the version using inheritance:<br />
<img src="wp-content/assets/110722/inheritance.png" alt="A* using inheritance" /><br />
Here, Waypoint directly subclasses GraphNode. Since the Graph is defined to work with GraphNode objects, we need a lot of (unsafe) down-casts to access the Waypoint class. Furthermore, the use of <em>haxe.rtti.Generic</em> will be very restricted or even impossible (implementing this marker interface generates unique classes for each type to avoiding dynamic).</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=1815</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Heaps and Priority Queues in ds 1.31</title>
		<link>http://lab.polygonal.de/?p=1710</link>
		<comments>http://lab.polygonal.de/?p=1710#comments</comments>
		<pubDate>Mon, 11 Apr 2011 19:03:57 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[data structures]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=1710</guid>
		<description><![CDATA[I recently revised and improved the Heap and PriorityQueue classes, mainly because the difference between both has always been somewhat blurry. The new implementations are included in ds 1.31, so let&#8217;s talk a bit about the changes. The Heap class By definition, a heap is a dense binary tree for which every parent node has [...]]]></description>
				<content:encoded><![CDATA[<p>I recently revised and improved the Heap and PriorityQueue classes, mainly because the difference between both has always been somewhat blurry. The new implementations are included in <a href="http://code.google.com/p/polygonal/wiki/DataStructures">ds 1.31</a>, so let&#8217;s talk a bit about the changes.</p>
<h3>The Heap class</h3>
<p>By definition, a heap is a dense binary tree for which every parent node has a value that is less (or greater; henceforward I assume elements are sorted in ascending order) than or equal to any of its children. A heap defines a set of simple operations:</p>
<ul>
<li>Insert element</li>
<li>Query smallest element</li>
<li>Remove smallest element</li>
</ul>
<p>In the old implementation, insertion was done by calling enqueue(), and removal by calling dequeue(). This was a bit misleading because a heap is not a queue, instead it&#8217;s used as an efficient data structure for implementing priority queues. Thus the existing heap API has been modified and now consists of the methods <strong>add()</strong>, <strong>top()</strong> and <strong>pop()</strong>, corresponding to the old methods enqueue(), front() and dequeue(), respectively. I&#8217;ve also added some additional methods:</p>
<ul>
<li><strong>replace()</strong> &#8211; replaces the smallest element with a new element</li>
<li><strong>change()</strong> &#8211; updates an existing element after it has been changed (restores heap condition)</li>
<li><strong>sort()</strong> &#8211; returns a sorted array of all elements (runs the heap sort algorithm)</li>
<li><strong>bottom()</strong> &#8211; finds and returns the largest element</li>
<li><strong>repair()</strong> &#8211; rebuilds the entire heap</li>
</ul>
<p>Note that replace() and change() is a lot faster than a combined remove() and add() operation. Also, bottom() runs in O(n) wheres top() is O(1). This can be further improved by a <em>Min-Max Heap</em>, which I may add in a future release.</p>
<p>Last but not least I did some optimizations, lowering memory requirements and almost doubling performance. And you don’t have to enable element-removal capabilities through the constructor as the heap now always supports removal of arbitrary elements.</p>
<h3>The PriorityQueue class</h3>
<p>As mentioned above, heaps are used for implementing priority queues and as the name implies, it’s a queue ADT so PriorityQueue now implements the Queue interface, defining the following methods:</p>
<ul>
<li><strong>enqueue()</strong> &#8211; adds an element</li>
<li><strong>dequeue()</strong> &#8211; removes the element with the highest priority</li>
<li><strong>peek()</strong> &#8211; returns the element with the highest priority (without removing it)</li>
<li><strong>back()</strong> &#8211; returns the element with the lowest priority (without removing it)</li>
</ul>
<p>The Heap class requires that every element provides a comparison function by implementing the <em>Heapable</em> interface. This is not needed for the PriorityQueue class, because here the elements are sorted using integer keys, resulting in a much better performance. So if you are only interested in managing prioritized data this is the perfect choice, while the Heap class is best used as an all-round tool for solving common problems like finding the min, max or k-th largest element.</p>
<h3>Usage</h3>
<p>Elements to be inserted into a heap have to implement the <em>Heapable</em> interface, which is defined in haXe like so:</p>
<pre class="prettyprint">
interface Heapable implements Comparable
{
    /**
     * Tracks the position inside a binary heap.
     * This value should never be changed by the user.
     */
    var position:Int;
}
</pre>
<p>Declaring a field in an interface is not supported in ActionScript 3.0, so when you inspect the compiled class from an SWC file, it has become an empty marker interface. So don&#8217;t forget to define the position field as you don&#8217;t get compile-time errors. I could have defined position as a getter/setter style function, but I don&#8217;t want to give up all those nice haXe features because it has become my primary language for a long time now. So the AS3 implementation of a heap element would look like this:</p>
<pre class="prettyprint">
package
{
    import de.polygonal.ds.Heapable;
    
    class HeapElement implements Heapable
    {
        public var value:int;
        public var position:int;

        public function HeapElement(value:int)
        {
            this.value = value;
        }

        public function compare(other:Object):int
        {
            return other.value - value;
        }

        public function toString():String
        {
            return &quot;&quot; + value;
        }
    }
}
</pre>
<p>Similary, elements to be inserted into a PriorityQueue have to implement <em>Prioritizable</em> like this:</p>
<pre class="prettyprint">
package
{
    import de.polygonal.ds.Prioritizable;
    
    class PriorityQueueElement implements Prioritizable
    {
        public var priority:Int;
        public var position:Int;
        public function PriorityQueueElement() {}
    }
}
</pre>
<p>Once we have a heap element defined, we can use the Heap like so:</p>
<pre class="prettyprint">
...
var heap = new Heap();
var a:HeapElement = new HeapElement(2);
var b:HeapElement = new HeapElement(5);
var c:HeapElement = new HeapElement(9);

//insert elements
heap.add(a);
heap.add(b);
heap.add(c);

//dump all elements
trace(heap);

//query smallest element
trace(heap.top());

//change element
a.value += 4;
heap.change(a);

//remove smallest element
trace(heap.pop());

//remove largest element
heap.remove(heap.bottom());
...
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=1710</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Yo, Ho! Welcome on deck, deque!</title>
		<link>http://lab.polygonal.de/?p=1472</link>
		<comments>http://lab.polygonal.de/?p=1472#comments</comments>
		<pubDate>Thu, 03 Mar 2011 23:25:57 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=1472</guid>
		<description><![CDATA[The newly released ds library (version 1.3) now includes an efficient deque implementation. What is a deque? A deque is a double-ended queue and pronounced &#8220;deck&#8221;. In contrast to a queue, which allows insertions at one end and removals at the opposite end, a deque allows insertions and removals at both ends. The new Deque [...]]]></description>
				<content:encoded><![CDATA[<p>The newly released ds library (version 1.3) now includes an efficient deque implementation.</p>
<h3>What is a deque?</h3>
<p>A deque is a <a href="http://en.wikipedia.org/wiki/Double-ended_queue">double-ended queue</a> and pronounced &#8220;deck&#8221;. In contrast to a queue, which allows insertions at one end and removals at the opposite end, a deque allows insertions and removals at <em>both</em> ends. The new <strong>Deque</strong> interface defines four methods to modify the Deque along with two methods to access the element at the front (head of the list) and back (tail of the list):</p>
<pre class="prettyprint">
interface Deque&lt;T&gt;
{
    function front():T;
    function back():T;
    
    function pushFront(x:T):Void;
    function popFront():T;
    
    function pushBack(x:T):Void;
    function popBack():T;
}
</pre>
<p>There are two common ways to implement a deque &#8211; by using a doubly-linked list or by using arrays. While the first option is very simple and straightforward, the latter one is more of a challenge because there are many approaches which all have their pros and cons.</p>
<h3>Linked implementation &#8211; de.polygonal.ds.LinkedDeque</h3>
<p>This is basically just a stripped down, lightweight version of the doubly linked list class DLL. A doubly-linked list is capable of doing insertions &#038; removals in constant time, but the operation itself is rather slow, since we need to instantiate a node object that stores the element and manage pointers to prevent the list from falling apart.<br />
Another important thing to watch out is that the LinkedDeque class requires considerable more memory &#8211; for each and every element we need a container object for storing the &#8220;cargo&#8221; (+16 bytes), a reference to the previous and next node (+4 bytes each) and finally a reference that points to the element (+4 bytes). So in Flash we need a total of 28 extra bytes per element. This means that 86% of the space is wasted on nodes and pointers!<br />
Despite those shortcomings it&#8217;s still useful for small to average sizes; and to minimize costly node object instantiation the LinkedDeque class (and all linked structures in ds) comes with built-in node pooling &#8211; all you have to do is to create a LinkedDeque object with a reserved size greater than zero:</p>
<pre class="prettyprint">
var deque = new LinkedDeque&lt;Int&gt;(100); //reuses up to 100 nodes
</pre>
<p>This is very useful if the maximum size is known in advance, as performance nearly doubles. The benchmarks below were all done with &#8220;node-caching&#8221; turned on.</p>
<h3>Arrayed implementation &#8211; de.polygonal.ds.ArrayedDeque</h3>
<p>While most sites explain how a doubly-linked list relates to a Deque, there is not much information available on how to efficiently implement a deque on top of an array. My first attempt used a circular array similar to the ArrayedQueue class. It seemed to be a good solution until I realized that resizing the deque would be slow and difficult to implement so I discarded this idea and started from scratch. This time I decided to use an array of arrays (turned out the C++ STL deque uses this approach) and I was very happy with the result.<br />
Let me briefly explain how it works. Data is organized in chunks or blocks of memory. A block is simply a fixed-sized array and all blocks are stored in a separate, dynamic array. Upon initialization the deque only contains a single block but once it fills up an additional block is allocated: adding elements to the back calls something like &#8220;blockListArray.push(newBlock)&#8221; and adding elements to the front calls &#8220;blockListArray.unshift(newBlock)&#8221;.<br />
Inserting elements at the beginning of an array is slow but in this case it doesn&#8217;t matter because the list of blocks is usually very small and it doesn&#8217;t happen very often. Therefore we can say that the ArrayedDeque performs modifications at both ends in <a href="http://stackoverflow.com/questions/200384/constant-amortized-time">amortized constant time</a>.</p>
<h3>Performance</h3>
<p>So how do both classes perform? Showing raw numbers would be pointless without being able to compare them to some reference values. For this reason I&#8217;ve created a minimal deque implementation called &#8220;NaiveDeque&#8221;:</p>
<pre class="prettyprint">
class NaiveDeque&lt;T&gt; implements Deque&lt;T&gt;
{
    var _data:Array&lt;T&gt;;
    
    public function new() {
        _data = new Array&lt;T&gt;();
    }
    
    public function front():T {
        return _data[0];
    }
    
    public function pushFront(x:T):Void {
        _data.unshift(x);
    }
    
    public function popFront():T {
        return _data.shift();
    }
    
    public function back():T {
        return _data[_data.length - 1];
    }
    
    public function pushBack(x:T):Void {
        _data.push(x);
    }
    
    public function popBack():T {
        return _data.pop();
    }
}
</pre>
<p>As you see it just uses what the Flash language has to offers. Clearly, the problem which the code above is that modifications at the beginning of the array are slow because a lot of memory is shifted around, either to fill a gap or to make room for a new element. So we kinda have the worst-case at the front and the best-case at the back. It&#8217;s not very fair to use this as a reference but on the other hand it shows how things can be drastically improved by using some elbow grease to write clever data structures. Here are the results:</p>
<p><img src="wp-content/assets/110304/chart.png" alt="deque benchmark" /></p>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr class="head">
<td colspan="1">size</td>
<td colspan="1">1000</td>
<td colspan="1">2000</td>
<td colspan="1">3000</td>
<td colspan="1">4000</td>
<td colspan="1">5000</td>
</tr>
<tr class="even">
<td>NaiveDeque</td>
<td>1.0</td>
<td>1.0</td>
<td>1.0</td>
<td>1.0</td>
<td>1.0</td>
</tr>
<tr class="uneven">
<td>LinkedDeque</td>
<td>1.65</td>
<td>4.4</td>
<td>6.6</td>
<td>7.2</td>
<td>7.7</td>
</tr>
<tr class="even">
<td>ArrayedDeque</td>
<td>5.0</td>
<td>9.3</td>
<td>11.7</td>
<td>14.8</td>
<td>19.3</td>
</tr>
<tr class="footer">
<td colspan="6">Speed relative to &#8220;naive deque&#8221;, higher is better</td>
</tr>
</table>
<p>The benchmark results were taken by equally distributing n elements at both ends, like this:</p>
<pre class="prettyprint">
//insert...
for (i in 0...n)
{
    if ((i &#038; 1) == 0)
        deque.pushBack(x);
    else
        deque.pushFront(x);
}
//remove...
for (i in 0...n)
{
    if ((i &amp; 1) == 0)
        deque.popBack();
    else
        deque.popFront();
}
</pre>
<h3>Usage</h3>
<p>One real-world application that comes to my mind is a software application&#8217;s list of undo operations; but as a deque is a very flexible abstract data structure it can be used in countless algorithms. The deque classes are all <a href="http://www.polygonal.de/doc/ds/">documented</a> so I hope it&#8217;s clear how to use them. If you have problems feel free to contact me.</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=1472</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Simple 2D Molehill Example</title>
		<link>http://lab.polygonal.de/?p=1433</link>
		<comments>http://lab.polygonal.de/?p=1433#comments</comments>
		<pubDate>Sun, 27 Feb 2011 21:38:00 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[molehill]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=1433</guid>
		<description><![CDATA[Here is a minimalistic FlashDevelop AS3 project that demonstrates how to use the Molehill API to draw a single 2D sprite on the screen. You can get the project here: fdproject-mole2d.zip. Assuming you have followed the instructions below the result should look like this: Flash: Simple 2D Molehill Example To compile the example you need [...]]]></description>
				<content:encoded><![CDATA[<p>Here is a minimalistic <a href="http://www.flashdevelop.org/wikidocs/index.php?title=Main_Page">FlashDevelop</a> AS3 project that demonstrates how to use the Molehill API to draw a single 2D sprite on the screen. You can get the project here: <a href="wp-content/assets/110227/fdproject-mole2d.zip">fdproject-mole2d.zip</a>.<br />
Assuming you have followed the instructions below the result should look like this:</p>
<p><a class="demo" title="Flash: Simple 2D Molehill Example" href="wp-content/assets/110227/flash.html" target="_blank">Flash: Simple 2D Molehill Example</a></p>
<p>To compile the example you need the Incubator version of the Flash Player, a new FlexSDK (&#8220;Hero&#8221;) and an updated playerglobal.swc which includes the Molehill API.<br />
All files can be downloaded from <a href="http://labs.adobe.com/">Adobe Labs</a>.<br />
After downloading and unzipping the SDK to the folder {flexSDK} create a directory named &#8220;10.1&#8243; in {flexSDK}frameworkslibsplayer and copy the new playerglobal.swc into it. Next fire up FlashDevelop and in Settings->AS3Context update the Flex SDK Location so it points to the new SDK. Finally just unzip &#038; extract the FD project and hit F5 to compile.</p>
<p>Here are the basic steps to draw a textured quad:</p>
<ul>
<li>Create a vertex buffer with 4 points to define a quad.</li>
<li>Create an index buffer that keys into this vertex buffer to define two triangles.</li>
<li>Create and upload a texture.</li>
<li>Compile the vertex and fragment programs (I&#8217;m using the AGAL mini-compiler from Adobe).</li>
</ul>
<p>Then on every frame:</p>
<ul>
<li>Assign the vertex and fragment program.</li>
<li>Transform the sprite and create a model-view-projection matrix for the vertex program. Since this is a 2D example we have to set up an orthographic projection.</li>
<li>Draw the triangles.</li>
</ul>
<p>The mvp transformation could be done by the vertex shader but to keep it stupid simple I&#8217;m creating the matrix in AS3. Also note that we need to flip the y-axis and shift the origin to the upper-left corner.</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=1433</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>Fast hash tables</title>
		<link>http://lab.polygonal.de/?p=1325</link>
		<comments>http://lab.polygonal.de/?p=1325#comments</comments>
		<pubDate>Mon, 01 Nov 2010 20:10:36 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://lab.polygonal.de/?p=1325</guid>
		<description><![CDATA[The Dictionary class &#8211; a dead end for other targets Recently I started to adjust my code with the C++ and JavaScript haXe target in mind. I have to say it’s really amazing to see your code running on different platforms. This offers an incredible amount of flexibility! While almost everything worked out of the [...]]]></description>
				<content:encoded><![CDATA[<h3>The Dictionary class &#8211; a dead end for other targets</h3>
<p>Recently I started to adjust my code with the C++ and JavaScript haXe target in mind. I have to say it’s really amazing to see your code running on different platforms. This offers an incredible amount of flexibility! While almost everything worked out of the box, the biggest hurdle was to find a replacement for the flash.utils.Dictionary class, which I was using extensively in my library.<br />
Although the haXe API already offers a Hash and IntHash class (both using the Dictionary for the flash target), I’ve decided to implement my own hash tables so they blend in nicely into my <strong><a href="http://code.google.com/p/polygonal/wiki/DataStructures">ds</a></strong> library.</p>
<h3>What are hash tables?</h3>
<p>A <a href="http://en.wikipedia.org/wiki/Hash_table">hash table</a> is a very important and fundamental data structure in computer science. It offers rapid insertion, access and deletion of sparse data. This is achieved by distributing the data amongst a fixed number of slots using a <a href="http://en.wikipedia.org/wiki/Hash_function">hash function</a>.</p>
<p>There are many ways of implementing hash tables, but from my experience (and from reading dozens of papers) the easiest solution is something called a ‘<em>standard-chain hash table</em>’, which simply uses linked lists as a collision resolution scheme. When linked lists are replaced with dynamic arrays, the result is called an ‘<em>array hash table</em>’, which offers a smaller memory footprint because it eliminates nodes and pointers all together and is more cache friendly.</p>
<p>Other implementations include ‘<em>bucketized cuckoo hash tables</em>’ or ‘<em>open-address hash tables</em>’ using techniques like linear probing or double hashing for collision resolution. Those are interesting if you are into computer science, but from the point of view of a game programmer, they don’t offer any advantages, but have drawbacks of some kind (problems with high load factor, difficult to implement, complex hashing and so on).</p>
<p>The hash table implementation which I’ve included in the ds library is somewhere in between a standard-chain hash table and an array hash table &#8211; the data is stored in an array while still offering basic advantages of a linked list, namely removal in constant time and move-front-on-access by pointer manipulation.</p>
<p>So the procedure for searching a key in my code is:</p>
<ol>
<li>Hash the key to acquire a slot.</li>
<li>Check if the slot is empty. If it’s empty, the key does not exist and the search fails.</li>
<li>Otherwise scan the array a key at a time until a match is found or until the array is exhausted (search fails).</li>
<li>If the key was found, move it to the front of the list by pointer manipulation. This way further queries to the same key take less time (optional).</li>
</ol>
<h3>Implementation overview</h3>
<p>The latest version of my ds library now contains a bunch of additional interfaces and classes:<br />
Classes implementing de.polygonal.ds.Map:</p>
<ul>
<li>IntIntHashTable &#8211; A hash table using 32-bit integers for both keys and values.</li>
<li>IntHashTable&lt;T&gt; &#8211; A generic hash table using 32-bit integer keys.</li>
<li>HashTable&lt;K, T&gt; &#8211; A generic hash table. Keys have to implement Hashable.</li>
<li>HashMap &#8211; The existing wrapper for the Dictionary class, which was updated so it now implements Mapinstead of Collection. Flash only!</li>
</ul>
<p>Classes implementing de.polygonal.ds.Set:</p>
<ul>
<li>IntHashSet &#8211; Similar to IntIntHashTable, this is a set for integer values.</li>
<li>HashSet&lt;T&gt; &#8211; A generic set. Values have to implement Hashable.</li>
<li>ListSet&lt;T&gt; &#8211; A simple set backed up by a list; this is a replacement for the former Set class that now defines an interface.</li>
</ul>
<p>IntIntHashTable and IntHashSet are the most important ones since they provide the core functionality used in other classes. IntHashTable, HashTable and HashSet extend their behavior via composition.</p>
<p>The core classes use ‘alchemy’ memory and lots of inlining to achieve very good performance. I’ve documented all classes, and I hope it’s not that difficult to understand. Some notes:</p>
<ul>
<li>Although hash tables implement a Map interface, they allow multiple entries of the same key following the FIFO rule. If you want to make sure that the hash table contains unique keys, use the setIfAbsent(key, val) method or manually check for existence using hasKey(key) before calling set(key, val). Example:<br/><br/>
<pre class="prettyprint">//duplicate key
var hash = new IntIntHashTable(1024);
hash.set(1, 2);
hash.set(1, 3);
trace(hash.hasKey(1)); //true
trace(hash.get(1)); //2
hash.clr(1);
trace(hash.hasKey(1)); //true
trace(hash.get(1)); //3

//ensure unique keys
var success = myHashTable.setIfAbsent(1, "value1"); //success == true
var success = myHashTable.setIfAbsent(1, "value2"); //success == false
//or
if (!myHashTable.hasKey(1)) myHashTable.set(1, "value");</pre>
</li>
<li>As you know, the Dictionary lets you use the object ‘identity’ as a key, and not the value returned from calling toString() on it. This behavior is not available in JavaScript and other targets. So objects used as keys in a HashTable or as values in a HashSet have to implement the Hashable interface or extend from the abstract class HashableItem. Hashable defines a single method getKey() (or just a field key if using haXe) returning an unique integer value to identify the object. This is a little awkward, but I currently don’t see any other (simple) solution. Example:<br/><br/>
<pre class="prettyprint">class Foo implements Hashable {
  public var key(default, null):Int;
  public function new() { key = HashKey.next(); }
}
//or
class Foo extends HashableItem {
  public function new() { super(); }
}
...
var item = new Foo();
myHashTable.set(foo, "myValue");</pre>
</li>
<li>For the AS3 users out there, I’m providing the following swc files (xxx stands for debug or release):
<ul>
<li>ds_xxx_alchemy.swc: ds version with alchemy support for FP10.</li>
<li>ds_xxx_.swc: ds version without alchemy support but using Vectors for FP10.</li>
<li>ds_xxx_fp9.swc: ds version without alchemy support but using Arrays for FP9.</li>
</ul>
<p>When using the alchemy version, it’s important to call free() to release all used memory once the object is no longer needed.</li>
</ul>
<h3>The Dictionary class</h3>
<p>If you want to take a look at the hash table implementation used in the Dictionary, you can do so by downloading the Tamarin source code and open the file core/MultinameHashtable.cpp or core/avmplusHashtable.cpp. From my understanding (please correct me if I’m wrong!) flash uses an open-addressed hash table with <a href="http://en.wikipedia.org/wiki/Quadratic_probing">quadratic probing</a> for resolving collisions. This means that keys are directly stored within hash slots, while my hash table is open: in the case of a hash collision, a single slot stores multiple entries, which must be searched sequentially. Also the Tamarin implementation automatically rehashes the hash table when it’s full (load factor &gt; 0.75), while I’m only allocating more capacity and leave rehashing to the user, because it’s an expensive operation and performance only slightly degrades when the load factor goes beyond one.</p>
<h3>Performance</h3>
<p>So here are the numbers for the IntIntHashTable. All results were done with the latest Flash Player for Windows. You can compile &amp; run the <a href="wp-content/assets/101101/benchmark.zip">benchmark</a> on your own, but I guess you have to adjust the number of iterations or you get a script timeout.</p>
<p>The chart below shows how the HashTable behaves when a slot contains more than one key. A load factor of 1 means that each slot contains only one key/value pair. A load factor of 2 means each slot has two pairs that have to be searched sequentially and so on.</p>
<p><img src="wp-content/assets/101101/chart1.png" alt="hash table benchmark" /></p>
<p>So it&#8217;s very predictable. A load factor of 10 makes the hash table 10x slower.</p>
<p>The chart and table below compares read/write/access operations between a IntIntHashTable and the Dictionary. Both structures are preallocated to minimize the time it takes to find a key.</p>
<p><img src="wp-content/assets/101101/chart2.png" alt="hash table benchmark" /></p>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr class="head">
<td colspan="1"><small>size</small></td>
<td colspan="1"><small>1024</small></td>
<td colspan="1"><small>2048</small></td>
<td colspan="1"><small>4096</small></td>
<td colspan="1"><small>8192</small></td>
<td colspan="1"><small>16384</small></td>
<td colspan="1"><small>32768</small></td>
<td colspan="1"><small>65536</small></td>
<td colspan="1"><small>131072</small></td>
</tr>
<tr class="even">
<td><small>IntIntHashTable</small></td>
<td><small>0,08</small></td>
<td><small>0,14</small></td>
<td><small>0,29</small></td>
<td><small>0,57</small></td>
<td><small>1,15</small></td>
<td><small>2,29</small></td>
<td><small>4,75</small></td>
<td><small>10,03</small></td>
</tr>
<tr class="uneven">
<td><small>Dictionary</small></td>
<td><small>0,84</small></td>
<td><small>1,54</small></td>
<td><small>3,3</small></td>
<td><small>6,7</small></td>
<td><small>16,5</small></td>
<td><small>44,76</small></td>
<td><small>97,72</small></td>
<td><small>218,32</small></td>
</tr>
<tr class="even">
<td><small>x times faster</small></td>
<td><small>10x</small></td>
<td><small>11x</small></td>
<td><small>11x</small></td>
<td><small>11x</small></td>
<td><small>14x</small></td>
<td><small>19x</small></td>
<td><small>20x</small></td>
<td><small>21x</small></td>
</tr>
<tr class="footer">
<td colspan="9"><small>Results are in milliseconds</small></td>
</tr>
</tbody>
</table>
<p>The last chart compares the generic HashTable implementation against the Dictionary class. The difference is not too big, still the HashTable is about 20-30% faster.</p>
<p><img src="wp-content/assets/101101/chart3.png" alt="hash table benchmark" /></p>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr class="head">
<td colspan="1">size</td>
<td colspan="1">1024</td>
<td colspan="1">2048</td>
<td colspan="1">4096</td>
<td colspan="1">8192</td>
<td colspan="1">16384</td>
<td colspan="1">32768</td>
<td colspan="1">65536</td>
<td colspan="1">131072</td>
</tr>
<tr class="even">
<td>HashTable</td>
<td>0,28</td>
<td>0,57</td>
<td>1,15</td>
<td>2,3</td>
<td>4,7</td>
<td>9,28</td>
<td>20,55</td>
<td>39,45</td>
</tr>
<tr class="uneven">
<td>Dictionary</td>
<td>0,37</td>
<td>0,72</td>
<td>1,35</td>
<td>2,73</td>
<td>5,6</td>
<td>13,48</td>
<td>27,45</td>
<td>52,48</td>
</tr>
<tr class="even">
<td>x times faster</td>
<td>1,32x</td>
<td>1,26x</td>
<td>1,17x</td>
<td>1,19x</td>
<td>1,19x</td>
<td>1,45x</td>
<td>1,34x</td>
<td>1,33x</td>
</tr>
<tr class="footer">
<td colspan="9">Results are in milliseconds</td>
</tr>
</tbody>
</table>
<h3>Conclusion</h3>
<p>While in most cases you won&#8217;t notice a big difference I have some applications in mind like hierarchical spatial hash tables for collision pruning where the extra speed should be very noticeable. So what&#8217;s next for ds? Probably a <em>deque</em> structure and lots of bug fixing of course ;).</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.polygonal.de/?feed=rss2&#038;p=1325</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
