<?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>StaticMethod &#187; map</title>
	<atom:link href="http://www.staticmethod.net/tag/map/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.staticmethod.net</link>
	<description>Things programmers might find interesting</description>
	<lastBuildDate>Thu, 04 Mar 2010 20:05:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript Map &#8211; Version 0.2</title>
		<link>http://www.staticmethod.net/2009/05/20/javascript-map-02/</link>
		<comments>http://www.staticmethod.net/2009/05/20/javascript-map-02/#comments</comments>
		<pubDate>Wed, 20 May 2009 17:41:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[map]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=79</guid>
		<description><![CDATA[After trying to use my previous map implementation in a Firefox extension I&#8217;ve been working on I realized that you can&#8217;t add random attributes to some objects you deal with in that world.  This made my previous approach unworkable. After doing some more searching, I found an approach that seems to work well so far.
The [...]]]></description>
			<content:encoded><![CDATA[<p>After trying to use my previous map implementation in a Firefox extension I&#8217;ve been working on I realized that you can&#8217;t add random attributes to some objects you deal with in that world.  This made my previous approach unworkable. After doing some more searching, I found an approach that seems to work well so far.</p>
<p>The basic idea is that we want to be able to generate an identifier for a random object consistently, then we can associate this identifier with a value.  The limitation is that the identifier needs to be a string (limitation of Javascript&#8217;s internal associative array/object construct), and that native javascript objects don&#8217;t have an internal mechanism for generating an ID (unlike Java&#8217;s Object class, which provides a hashCode() method).</p>
<p>The process to solve this problem has two steps in my current implementation:</p>
<ol>
<li>Store the &#8216;key&#8217; object in an array</li>
<li>Use the &#8216;key&#8217; object&#8217;s array index as the key value for the association</li>
</ol>
<p>This way, I can tell if a passed in &#8216;key&#8217; object has a value in my map by seeing if it&#8217;s in my internal array.  If it&#8217;s in there, I can look up it&#8217;s index&#8217;s value in my associative array (or anonymous object).  If I&#8217;m adding a new key/value pair, I insert the key object into my internal array and then use it&#8217;s index as the &#8216;key&#8217; for my associative array.  Removing a key/value pair from the map involves the following steps:</p>
<ol>
<li>Look up the index of the key object in my internal array</li>
<li>Remove the value that corresponds to the index from the associative array/anonymous object</li>
<li>Remove the key object from the internal array</li>
</ol>
<p>Here&#8217;s an example of the code that inserts a key/value pair into the map, this is a snip from the code, which is more object-oriented:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Internal holders</span>
<span style="color: #003366; font-weight: bold;">var</span> _internalMap <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> _internalArray <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/*
 * Private function for getting the key for an object
 */</span>
<span style="color: #003366; font-weight: bold;">function</span> getKey<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> _internalArray.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _internalArray.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        key <span style="color: #339933;">=</span> _internalArray.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// &quot;Keys&quot; will be of the form &quot;OR.x&quot; where 'x' is the index in the array</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;OR.&quot;</span> <span style="color: #339933;">+</span> key<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">/*
 * Internal put method
 */</span>
put <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> objKey <span style="color: #339933;">=</span> getKey<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _internalMap<span style="color: #009900;">&#91;</span>objKey<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The full implementation has get, remove, and size methods as well, and is built as a class, but this kind of shows the mechanism behind it all.  One thing to note is that this implementation relies on Javascript 1.6 (or higher) as the &#8216;indexOf()&#8217; array method was introduced in Javascript 1.6</p>
<p>I&#8217;ve attached the new version (version 0.2), you can find it here: <a rel="attachment wp-att-78" href="http://www.staticmethod.net/2009/05/20/javascript-map-02/jsmap-02/">JsMap Version 0.2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/20/javascript-map-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Javascript Map Implementation</title>
		<link>http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/</link>
		<comments>http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/#comments</comments>
		<pubDate>Mon, 11 May 2009 19:59:30 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Programming Problems]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[map]]></category>

		<guid isPermaLink="false">http://www.staticmethod.net/?p=59</guid>
		<description><![CDATA[One of the downsides to javascript (at least in my opinion) has been it&#8217;s lack of more intelligent collection classes, such as the Map.  Javascript&#8217;s array and object constructs are very flexible and useful in their own right, but sometimes you just need a way to map one object (as a key) to some arbitrary [...]]]></description>
			<content:encoded><![CDATA[<p>One of the downsides to javascript (at least in my opinion) has been it&#8217;s lack of more intelligent collection classes, such as the Map.  Javascript&#8217;s array and object constructs are very flexible and useful in their own right, but sometimes you just need a way to map one object (as a key) to some arbitrary value, and then have the possibility to look up that value later.  I was playing around with some ideas for how to get a simple map to work in javascript and came up with my JsMap class.  It&#8217;s a very small class, more of an exercise than anything, but maybe someone will find it useful.</p>
<p>This map class is very limited, as (as far as I can tell) javascript objects don&#8217;t have internal hashing methods or object ids that allow for simple identification in the context of a map.  In order to make up for this, my map class generates a random ID for a passed in &#8220;key&#8221; object and then uses that random ID as the key in the internal map.  The generated key is then stored in a variable <em>__jsKey </em>in the passed in &#8220;key&#8221; object.  When the &#8220;get&#8221; method on the map is called, it looks to see if the passed in &#8220;key&#8221; object has a <em>__jsKey</em> variable, and if so, looks up the value associated with it in the internal map.</p>
<p>Here is a link to the attachment page containing the javascript file, which I&#8217;ll update with changes as they&#8217;re made: [<a rel="attachment wp-att-57" href="http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/jsmap/">JsMap</a>].</p>
<p>Here&#8217;s a simple HTML test of the map class: [<a rel="attachment wp-att-58" href="http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/test/">JsMap Test/Example usage file</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.staticmethod.net/2009/05/11/simple-javascript-map-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
