<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Pograph&#039;s Weblog</title>
	<atom:link href="http://pograph.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pograph.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Sun, 27 Nov 2011 07:09:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='pograph.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Pograph&#039;s Weblog</title>
		<link>http://pograph.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pograph.wordpress.com/osd.xml" title="Pograph&#039;s Weblog" />
	<atom:link rel='hub' href='http://pograph.wordpress.com/?pushpress=hub'/>
		<item>
		<title>My WWDC app wall (2010 version)</title>
		<link>http://pograph.wordpress.com/2010/08/24/my-app-wall-2010/</link>
		<comments>http://pograph.wordpress.com/2010/08/24/my-app-wall-2010/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 20:31:04 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[for fun]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=383</guid>
		<description><![CDATA[In case anyone still want it. Pre-compiled versions (Windows and OSX) Source code<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=383&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In case anyone still want it.</p>
<p><a href="http://drop.io/appwall2010">Pre-compiled versions (Windows and OSX)</a></p>
<p><a title="http://github.com/pograph/appwall2010" href="http://github.com/pograph/appwall2010">Source code</a></p>
<span style="text-align:center; display: block;"><a href="http://pograph.wordpress.com/2010/08/24/my-app-wall-2010/"><img src="http://img.youtube.com/vi/E3EDxFaxqDc/2.jpg" alt="" /></a></span>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/383/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=383&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2010/08/24/my-app-wall-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>A Search Engine for Python Source Code</title>
		<link>http://pograph.wordpress.com/2010/02/01/a-search-engine-for-python-source-code/</link>
		<comments>http://pograph.wordpress.com/2010/02/01/a-search-engine-for-python-source-code/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 07:51:38 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[search engine]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=376</guid>
		<description><![CDATA[Nullege.com finds python examples by name.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=376&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a title="python search engine" href="http://nullege.com">Nullege.com</a> finds python examples by name.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/376/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/376/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/376/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=376&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2010/02/01/a-search-engine-for-python-source-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>python2.6, email and py2exe</title>
		<link>http://pograph.wordpress.com/2009/07/02/python2-6-email-and-py2exe/</link>
		<comments>http://pograph.wordpress.com/2009/07/02/python2-6-email-and-py2exe/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 03:28:19 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[Web Programming]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=369</guid>
		<description><![CDATA[To make email module work with py2exe: 1. Use new lower case module names, not old ones. For example, email.mime.image.MIMEImage, instead of email.MIMEImage.MIMEImage. 2. import email.iterators, email.generator, and other staff that your exe complained missing.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=369&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To make email module work with py2exe:</p>
<p>1. Use new lower case module names, not old ones. For example, email.mime.image.MIMEImage, instead of email.MIMEImage.MIMEImage.</p>
<p>2. import email.iterators, email.generator, and other staff that your exe complained missing.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/369/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/369/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/369/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=369&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/07/02/python2-6-email-and-py2exe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>My own app wall</title>
		<link>http://pograph.wordpress.com/2009/06/24/my-own-app-wall/</link>
		<comments>http://pograph.wordpress.com/2009/06/24/my-own-app-wall/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 06:10:43 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[Web Programming]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=359</guid>
		<description><![CDATA[Download and run your own App Wall.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=359&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s app wall is cool. But it would be even better if we can have our own copy.</p>
<p>I spent some time and crafted one. It is in python, so you should be able to run it on any platform. But I&#8217;m not a UI guy, so don&#8217;t expect too much.﻿﻿﻿</p>
<span style="text-align:center; display: block;"><a href="http://pograph.wordpress.com/2009/06/24/my-own-app-wall/"><img src="http://img.youtube.com/vi/aMhR_vURCrk/2.jpg" alt="" /></a></span>
<p>You need pygame, PIL and feedparser to run it. Here is the link <a title="app wall source code" href="http://github.com/pograph/appwall/tree/master">[appwall]</a>, feel free to try or improve it.</p>
<p>Update:</p>
<p>For those who don&#8217;t want to install a bunch of staff to try it, here is <a href="http://webfakecall.com/pograph/appwall_win32.zip">a exe version</a>. My python installation on mac is totally a mess now. So, sorry, no app version.</p>
<p>Update2:</p>
<p>Fixed 2 bugs. The program is more stable now. Above links have been updated and point to newest version.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/359/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/359/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/359/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=359&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/06/24/my-own-app-wall/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>Build Mame with Visual Studio</title>
		<link>http://pograph.wordpress.com/2009/06/08/build-mame-with-visual-studio/</link>
		<comments>http://pograph.wordpress.com/2009/06/08/build-mame-with-visual-studio/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 23:35:27 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[code reading]]></category>
		<category><![CDATA[mame]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=354</guid>
		<description><![CDATA[The official document is a little out-dated. I met several problems during build. Here is how I eventually got a debug build: Install Mame build package &#38; DirectX SDK Open a Visual Studio Command Prompt (Start-&#62;All Programs-&#62;Microsoft Visual Studio 200x-&#62;Visual Studio Tools-&#62;Visual Studio 200x Command Prompt) cd /mame_src/ set path=%PATH%;/path/to/mingw/bin &#8220;c:\Program Files\Microsoft DirectX SDK (March [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=354&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The official document is a little out-dated. I met several problems during build. Here is how I eventually got a debug build:</p>
<blockquote><p>Install Mame build package &amp; DirectX SDK<br />
Open a Visual Studio Command Prompt (Start-&gt;All Programs-&gt;Microsoft Visual Studio 200x-&gt;Visual Studio Tools-&gt;Visual Studio 200x Command Prompt)<br />
cd /mame_src/<br />
set path=%PATH%;/path/to/mingw/bin<br />
&#8220;c:\Program Files\Microsoft DirectX SDK (March 2009)\Utilities\bin\dx_setenv.cmd&#8221; x86<br />
mingw32-make MSVC_BUILD=1 DEBUG=1 SYMBOLS=1 DIRECTINPUT=8 DIRECT3D=9<br />
Open vmamed.exe for debug</p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/354/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/354/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/354/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=354&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/06/08/build-mame-with-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>Notes on gzip and DEFLATE format</title>
		<link>http://pograph.wordpress.com/2009/06/04/notes-on-gzip-and-deflate-format/</link>
		<comments>http://pograph.wordpress.com/2009/06/04/notes-on-gzip-and-deflate-format/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 05:59:13 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[code reading]]></category>
		<category><![CDATA[gzip format]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=344</guid>
		<description><![CDATA[Explain gzip format with samples.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=344&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>.gz file is just a wrapper arround deflate format. Nothing special. See rfc1952 if you want.</p>
<p>The way that gzip compress files is easy to understand. 1. If a string repeats, we can simply say: rewind x bytes and copy y bytes from there. 2. Use Haffman code to further reduce file size.</p>
<p>However, deflate format is complicated, largely due to the way it represents Huffman trees. With some restrictions (see RFC1951 section 3.2.1 and 3.2.2), you only need to write down how many bits each leaf node has. Thus, a Huffman tree with N leaf nodes can be represented as an N-integer sequence.</p>
<p>Besides, gzip code is in an old fashioned format and not very reader friendly. Variables are named as b, kb, bb.  To undestand deflate format, I tried 3 files.</p>
<p>&gt; echo hello &gt; hello.txt<br />
&gt; gzip -c hello.txt &gt; hello.gz</p>
<p>The content of hello.gz is</p>
<pre>00000000  1F 8B 08 08 C0 C4 1D 4A 00 03 68 65 6C 6C 6F 2E .......J..hello.
00000010  74 78 74 00 CB 48 CD C9 C9 E7 02 00 20 30 3A 36 txt..H...... 0:6
00000020  06 00 00 00                                     ....</pre>
<p>Compressed data is CB 48 CD C9 C9 E7 02 00.  Deflate format is a bit stream. The RFC defined that when packing bits into bytes, store into LSB first. In our example, the first byte is CB (1100 1011). So the first bit is the right most 1.  According to the RFC, this means we already reached the last data block. As we expected, we have only one data block here.</p>
<p>The next field is a 2-bit data field. According to the RFC, data fields are stored in their original bit order.  So we got &#8217;01&#8242;, which means this data block is compressed with fixed Huffman codes. The way to pack bits into bytes are confusing for human, but easy for computers to carry out. See section 3.2.6 for coding schema. For ASCII code, the result is simply ascii+0&#215;30.</p>
<p>Now, we reached the real data. Since data are coded by Huffman codes, length of next field depends on its content. Lets cheat here. We know that we are going to get an &#8216;h&#8217;, which is 8-bit long. RFC says Huffman codes should be packed MSB first. That is to say, we need to reverse the bits to get the real Huffman code. Next 8-bit is 00011001 (left 3 bits from the 2nd byte). Revers it we get 10011000. Minus 0&#215;30 we get the ascii code for &#8216;h&#8217;, 1101000. Repeat this step, we&#8217;ll get &#8220;ello\n&#8221; and a termination mark (7 zero bits). Gzip&#8217;s implementation does not realy reverse the Huffman code, it use pre-computed tables to accelerate this step.</p>
<p>The 2nd example is<br />
&gt; echo hello hello &gt; hello2.txt<br />
&gt; gzip -c hello2.txt &gt; hello2.gz</p>
<p>The content of hello2.gz is</p>
<pre>00000000  1F 8B 08 08 22 BE 1E 4A 00 03 68 65 6C 6C 6F 32 ...."..J..hello2
00000010  2E 74 78 74 00 CB 48 CD C9 C9 57 C8 00 91 5C 00 .txt..H...W...\.
00000020  A5 6A 0A 44 0C 00 00 00                         .j.D....</pre>
<p>Following the procedure, we can easily get &#8220;hello h&#8221;. Then we got a 7-bit Huffman code, 0&#215;02, which represents 258. 258 means repeat a 4-byte string in the output buffer. The RFC call this kind of code (258-285) length code, but I&#8217;d rather call them repeat code. Next one or two fields specify offset. In our case, we get 4 and 1. Following the 2nd table in RFC section 3.2.5, we get offset=6. Rewind 6 bytes and read 4 bytes from output buffer, we get &#8220;ello&#8221;. In this case, gzip did some compression, but it didn&#8217;t do its best.</p>
<p>The 3rd file is a real case:<br />
&gt; wget http://www.ietf.org/rfc/rfc1951.txt<br />
&gt; gzip -c rfc1951.txt &gt; rfc1951.gz</p>
<p>The content of rfc1951.gz is:<br />
00000000  1F 8B 08 08 31 25 A2 31 00 03 72 66 63 31 39 35 &#8230;.1%.1..rfc195<br />
00000010  31 2E 74 78 74 00 B5 7D 6B 77 DB 46 92 E8 77 9D 1.txt..}kw.F..w.<br />
00000020  FB 23 7A 7D CF 9E 90 31 49 11 7C E8 69 E7 AC 2C .#z}&#8230;1I.|.i..,<br />
00000030  CB 8E 27 B2 D7 37 72 26 99 CC 7A E7 80 64 93 C4 ..&#8217;..7r&amp;..z..d..<br />
&#8230;&#8230;</p>
<p>This time, gzip use dynamic Huffman codes. To make it simpler, I would explan compress process before jumping into the file.<br />
1) generate 2 Huffman trees( called dictionary, one for alphabet and repeat marks, one for distance) and encoding the text file with these 2 trees.<br />
2) recall that a Huffman tree can be represented as a sequence of integers. The dictionary can be repsented as 2 sequences. Totally 256(alphabet) + 1 (termination code) + 29(repeat code) + 30(distance) =  316 elements. In this step, we encode the dictionary with another Huffman tree. Maximum value in dictionary is 15 (=maximum depth of alphabet and repeat code tree). We gave 16, 17, 18 special meanings (see RFC section 3.2.7). Thus, this new tree has 18 nodes.</p>
<p>Alright, let&#8217;s look at the file. Header saids we have only 1 block, use dynamic Huffman coding, has 22 + 257 = 279 nodes in alphabet and repeat code tree, 1 + 29 = 30 nodes in the distance tree and 11 + 4 = 15 nodes in the 2nd level Huffman tree. In the 2nd level tree, 8, 7, 9, 6, 10 are represented as 3-bit code. (Note: although 8 appears before 6 according to the RFC, 6 still has lower code then 8). Decode the tree, we get 6-&gt;000, 7-&gt;001, &#8230; Then, we can decode the alphabet and repeat code tree. 00-09 does not appear, 0A (\n) as 8-bit value, &#8230;, 32 (space) as 5-bit, &#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/344/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/344/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/344/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/344/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/344/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/344/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/344/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/344/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/344/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/344/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/344/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/344/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/344/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/344/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=344&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/06/04/notes-on-gzip-and-deflate-format/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>Openfire and Jwchat on Ubuntu</title>
		<link>http://pograph.wordpress.com/2009/05/01/openfire-and-jwchat-on-ubuntu/</link>
		<comments>http://pograph.wordpress.com/2009/05/01/openfire-and-jwchat-on-ubuntu/#comments</comments>
		<pubDate>Fri, 01 May 2009 01:28:11 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[jwchat]]></category>
		<category><![CDATA[openfire]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=332</guid>
		<description><![CDATA[I spent a whole afternoon to install onpenfire and jwchat. This post works for me. But to apply it on Ubuntu, you need to do a little bit more. 1) install apache2, jdk, etc. 2) install openfire. I didn&#8217;t use ubuntu package since I want to do some development later. Download it and extract to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=332&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I spent a whole afternoon to install onpenfire and jwchat. This <a href="http://www.igniterealtime.org/community/message/156841#156841">post</a> works for me. But to apply it on Ubuntu, you need to do a little bit more.</p>
<p>1) install apache2, jdk, etc.<br />
2) install openfire. I didn&#8217;t use ubuntu package since I want to do some development later. Download it and extract to any folder would work.<br />
3) sudo a2enmod proxy; sudo a2enmod proxy_http<br />
4) enable and setup mod_proxy. Add the following code to your &lt;VirtualHost&gt;. I use /etc/apache2/site-available/default<br />
&lt;VirtualHost *&gt;<br />
&#8230;.<br />
&lt;Proxy *&gt;<br />
Order allow,deny<br />
Allow from all<br />
&lt;/Proxy&gt;<br />
AddDefaultCharset UTF-8<br />
ProxyPass /http-bind/ http://yoursite.com:7070/http-bind/<br />
&lt;/VirtualHost&gt;<br />
Note: Don&#8217;t try to access yoursite.com:7070 from browser, you will get 400 or 404. This is fine.</p>
<p>5) Disable SASL in Jwchat.<br />
In jsjac.js, change the following line:<br />
var JSJACHBC_USE_BOSH_VER  = true; // change it to false</p>
<p>In jwchat.html, insert<br />
oArg.authtype = &#8216;nonsasl&#8217;;<br />
before<br />
con.connect(oArg);</p>
<p>6) Log into openfire console and add the following 2 server properties.<br />
xmpp.httpbind.client.requests.polling = 0<br />
xmpp.httpbind.client.requests.wait = 10</p>
<p>7) Restart openfire and run /etc/init.d/apache2 force-reload</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/332/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=332&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/05/01/openfire-and-jwchat-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>Thread Building Block test drive</title>
		<link>http://pograph.wordpress.com/2009/04/28/thread-building-block-test-drive/</link>
		<comments>http://pograph.wordpress.com/2009/04/28/thread-building-block-test-drive/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 00:01:20 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[parallel programming]]></category>
		<category><![CDATA[TBB]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=325</guid>
		<description><![CDATA[After reading the TBB book, I spent half day on playing the lib. My test is simple. Having 2 1000-element arrays, do some math on one array, store the result in the other, swap the arrays and do it again. Run this a few million times. I tried this program on my 2-core machine, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=325&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After reading the <a title="Intel Thread Building Blocks" href="http://www.amazon.com/Intel-Threading-Building-Blocks-Parallelism/dp/0596514808/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1215536190&amp;sr=1-1" target="_blank">TBB book</a>, I spent half day on playing the lib. My test is simple. Having 2 1000-element arrays, do some math on one array, store the result in the other, swap the arrays and do it again. Run this a few million times. I tried this program on my 2-core machine, the result is just as we expected:</p>
<table border="0">
<tbody>
<tr>
<td>Loop</td>
<td>Signal Thread</td>
<td>TBB on 2 cores</td>
</tr>
<tr>
<td>1,000,000</td>
<td>36.235s</td>
<td>18.418s</td>
</tr>
<tr>
<td>3,000,000</td>
<td>106.257</td>
<td>54.638s</td>
</tr>
</tbody>
</table>
<p>In this case, the small change of code worth it well. If this library was available when I was working on my last ray-tracing engine, my life would have been much easier. I wish someday, a similar library would be available on GPU.</p>
<p>I found another interesting thing during my test drive:</p>
<p>unsigned a, b = b0;<br />
unsigned n = &lt;a number you like&gt;;<br />
for(int i = 0; i &lt; LOOP; ++i) {<br />
a = b * b + n;<br />
b = a * a + n;<br />
}<br />
No matter what b0 is, a and b will fall into a pair of fixed numbers (s1, s2), which only depend on value of n; If n is a odd number, then a != b. Otherwise, a = b = one of s1 and s2. I can&#8217;t prove it in math but I&#8217;ve verified b0=[0, 1024) and n=(0, 1024].</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/325/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/325/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/325/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/325/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/325/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/325/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/325/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/325/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=325&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/04/28/thread-building-block-test-drive/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>Porting C++ code to Linux kernel</title>
		<link>http://pograph.wordpress.com/2009/04/05/porting-cpp-code-to-linux-kernel/</link>
		<comments>http://pograph.wordpress.com/2009/04/05/porting-cpp-code-to-linux-kernel/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 01:01:36 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[kernel programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Linux kernel]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=320</guid>
		<description><![CDATA[Notes and sample code on porting existing C++ kernel code to Linux<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=320&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We all know writing Linux kernel module in C++ is not a good idea, but sometimes we don&#8217;t have options, especially when porting a existing project from Windows to Linux.<br />
The main constrains are:<br />
1) There is no C++ runtime in kernel. Most features of C++ will work, virtual functions, templates, operators, etc. But their is no default implementation of new and delete operators, so you need to write your own.<br />
2) Stack space is very limited. In Linux kernel, default size of stack is 2 pages. That is 8k for 32-bit systems and 16k for 64-bit ones. Actually, stack is a little bit smaller than that, because the task structure for the thread also reside in these 2 pages. Due to this constrain, you can&#8217;t use exceptions in kernel, because exception needs too much space in stack. There are some open source C++ runtimes for kernel, which support exception, but I haven&#8217;t tried any of them.</p>
<p>Here are some tips I learned in a recent project:<br />
1) Implement you own new and delete operators.<br />
2) Do not use exceptions. If you are porting a Windows driver, probably you don&#8217;t have to worry about this.<br />
3) Do not use global non-trival variables. Since there is no C++ runtime in kernel, nobody will construct those variable for you. There are better ways to do it, singleton, factory, etc.<br />
4) Implement platform-dependent functions. Making C++ code work with kerenl headers is not a simple job. I wrote this part in C. If you know a better way, plese let me know.<br />
5) Compile your c++ code with -fno-builtin -fno-exceptions -fno-rtti -nostdinc, and link obj files to your platform-dependent part.</p>
<p>Ok, that&#8217;s it. I attached a sample project. If I missed anything, hopefully the code can explain it. (Why wordpress does not allow zip files? Is there any better way to post code including several files?)</p>
<p>// =======================================<br />
// new.h, implement new and delete</p>
<p>#ifndef NEW_H<br />
#define NEW_H</p>
<p>#include &#8220;kernel_api.h&#8221;</p>
<p>inline void *operator new(size_t s) {<br />
return my_alloc(s);<br />
}</p>
<p>inline void operator delete(void *p) {<br />
return my_free(p);<br />
}</p>
<p>// you may need other forms of new and delete<br />
#endif</p>
<p>// =========================================<br />
// kernel_api.h.<br />
#ifndef KERNEL_API_H<br />
#define KERNEL_API_H</p>
<p>#ifdef __cplusplus<br />
extern &#8220;C&#8221; {<br />
#endif</p>
<p>typedef unsigned int size_t;<br />
#define KERN_INFO    &#8220;&lt;6&gt;&#8221;<br />
#define NULL    0UL</p>
<p>extern void printk(const char *fmt, &#8230;);<br />
extern void *my_alloc(size_t size);<br />
extern void my_free(void *p);</p>
<p>#ifdef __cplusplus<br />
}<br />
#endif</p>
<p>#endif</p>
<p>//==========================================<br />
// kernel_api.c<br />
#include &lt;linux/module.h&gt;</p>
<p>void *my_alloc(size_t s) {<br />
return kmalloc(s, GFP_KERNEL);<br />
}</p>
<p>void my_free(void *p) {<br />
return kfree(p);<br />
}</p>
<p>//==========================================<br />
// interface.c, required interface for every kernel module<br />
#include &lt;linux/module.h&gt;<br />
#include &lt;linux/kernel.h&gt;<br />
#include &lt;linux/init.h&gt;<br />
#include &#8220;cppmod.h&#8221;</p>
<p>static int __init cppmod_init(void)<br />
{<br />
printk(KERN_INFO &#8220;cpp module installed\n&#8221;);<br />
return 0;<br />
}</p>
<p>static void __exit cppmod_exit(void)<br />
{<br />
printk(KERN_INFO &#8220;cpp module removed\n&#8221;);<br />
}</p>
<p>module_init(cppmod_init);<br />
module_exit(cppmod_exit);</p>
<p>MODULE_LICENSE(&#8220;GPL&#8221;);</p>
<p>//==========================================<br />
// cppmod.h, exported C interface from C++ code<br />
#ifndef CPP_MOD_H<br />
#define CPP_MOD_H</p>
<p>#ifdef __cplusplus<br />
extern &#8220;C&#8221; {<br />
#endif</p>
<p>extern int start_driver(void* data);<br />
extern void stop_driver(void);</p>
<p>#ifdef __cplusplus<br />
}<br />
#endif</p>
<p>#endif</p>
<p>//==========================================<br />
//  cppmod.cpp, finally, my C++ code<br />
#include &#8220;kernel_api.h&#8221;<br />
#include &#8220;new.h&#8221;<br />
#include &#8220;cppmod.h&#8221;</p>
<p>class Driver {<br />
public:<br />
Driver(unsigned int dev_id) : _id(dev_id) { printk(KERN_INFO &#8220;C++ driver started\n&#8221;); }<br />
~Driver(void) { printk(KERN_INFO &#8220;Goodbye C++ driver\n&#8221;); }<br />
private:<br />
unsigned int _id;<br />
};</p>
<p>static Driver *g_driver = NULL;</p>
<p>extern &#8220;C&#8221; int start_driver(void *data) {<br />
g_driver = new Driver(*(unsigned int*)data);<br />
if(!g_driver)<br />
return -1;<br />
return 0;<br />
}</p>
<p>extern &#8220;C&#8221; void stop_driver(void) {<br />
if(g_driver)<br />
delete g_driver;<br />
}</p>
<p>#####################################################<br />
# Makefile<br />
obj-m += mydrv.o<br />
mydrv-objs := interface.o cppmod.o kernel_api.o<br />
list-multi := mydrv.o</p>
<p>all: cppmod.o module<br />
module:<br />
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules</p>
<p>clean:<br />
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean</p>
<p>cppmod.o : cppmod.cpp new.h kernel_api.h cppmod.h<br />
$(CC) -c -o $@ -fno-builtin -fno-exceptions -fno-rtti -nostdinc $&lt;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/320/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=320&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/04/05/porting-cpp-code-to-linux-kernel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
		<item>
		<title>Object in Javascript, a C++ programmer&#8217;s point of view</title>
		<link>http://pograph.wordpress.com/2009/03/20/object-in-javascript-a-c-programmers-point-of-view/</link>
		<comments>http://pograph.wordpress.com/2009/03/20/object-in-javascript-a-c-programmers-point-of-view/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 04:59:22 +0000</pubDate>
		<dc:creator>pograph</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Javascript OOP C++]]></category>

		<guid isPermaLink="false">http://pograph.wordpress.com/?p=305</guid>
		<description><![CDATA[Explain Javascript OOP, which can be confusing for C++ users.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=305&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There is no shortage of books and blog posts talking about OOP in Javascript. But the more I read, the more confused I am. Because I am a C++ programmer and Javascript and C++ are so different. Until I found this <a title="this ppt" href="http://www.slideshare.net/stoyan/beginning-objectoriented-javascript-presentation" target="_self">ppt</a>. After read it a few times, played in Firebug, and read some open source Javascript projects, I think I understand the idea of Object now.</p>
<p>In this post, I use Firebug&#8217;s Javascript Console to demonstrate my codes and their result. Firebug is a Javascript debugger for Firefox. It is also a good place to test drive Javascript.</p>
<p>Except a few primitive types, everything in Javascript is an Object. Objects are implemented as map (hash table). Consider the following object:<br />
<code><span style="color:#0000ff;">var user = { name : 'John', age : 20 };</span></code><br />
You can either write<br />
<code><span style="color:#0000ff;">user.name</span></code><br />
or<br />
<code><span style="color:#0000ff;"> user['name']</span></code><br />
Both will give you the same result. Member of an object can be a primitive variable, an object, or a function. Actually, in Javascript, function is a kind of object.</p>
<p>In Javascript, objects can only be accessed by reference. That means if you assign an object to another, you just created another reference to the same object. In this aspect, Javascript behaves like Java. For example:<br />
<code><span style="color:#0000ff;"> &gt;&gt;&gt; var o1 = { id : 1 };<br />
&gt;&gt;&gt; var o2 = o1;  // o1 and o2 point to the same object<br />
&gt;&gt;&gt; var o2.id = 2;<br />
&gt;&gt;&gt; o2.id = 2;<br />
2<br />
&gt;&gt;&gt; o1.id    // o1.id get changed too<br />
2</span></code></p>
<p>As I said before, in Javascript, a function is an object. It is just a object that can be called. You can think of it as a functor in C++. Actually, function has more &#8220;functions&#8221; in Javascript. A function can be called in 2 ways, as a normal function or as a constructor. When you write<br />
<code><span style="color:#0000ff;"> a = myfunc();</span></code><br />
you are calling myfunc() as a normal function. Instead, if you write<br />
<code><span style="color:#0000ff;"> a = new myfunc();</span></code><br />
you are calling myfunc() as a constructor.</p>
<p>When called as a constructor, a function first create a object called &#8220;this&#8221;. Then, it go though the function as usual. When the function returns, &#8220;this&#8221; will be returned and the original return will be discarded. Consider the following code:<br />
<code><span style="color:#0000ff;"> function UserAccount(name, age) {<br />
    this.name = name;<br />
    this.age = age;<br />
    return "I'll not be returned when called as a constructor";<br />
} <br />
var user1 = new UserAccount('John', 20);</span></code><br />
user1 will be an object consists &#8220;name&#8221; and &#8220;age&#8221;.  Here, &#8220;this&#8221; is quite confusing for C++ programmers. It has nothing to do with &#8220;this&#8221; pointer in C++. <strong>&#8220;this&#8221; is a object that browser created for you when you call a function as a constructor. </strong>In the above sample, we modified &#8220;this&#8221; and &#8220;this&#8221; was passed to user1 implicitly.</p>
<p>Another widely used and confusing feature is prototype. Javascript implements inheritance by prototype. But in  practice, prototype is simply used as another way to initialize a object. Then, why another way? To get the answer, we need to analyze the underling implementation.</p>
<p> Every object has a hidden member called __proto__, which points to the object&#8217;s base object. When you try to read a member of an derived object, browser will search in the object&#8217;s properties first. If the derived object does not have the requested property, browser will search in its __proto__, then the __proto__&#8217;s __proto__. If you want to write into a non-existing member, browser will create the member and write into it. At the same time, every function has a property called &#8220;prototype&#8221;. By default, prototype is an empty object. You can modify it to fit your needs. When a function is called as a constructor, browser creates a object called &#8220;this&#8221; and points its &#8220;__proto__&#8221; to the function&#8217;s &#8220;prototype&#8221;. Since __proto__ is just a reference, the function&#8217;s prototype is shared by all object it constructed. The following code shows the underling implement of prototype:<br />
<code><span style="color:#0000ff;"> &gt;&gt;&gt; function UserAccount() {}<br />
&gt;&gt;&gt; UserAccount.prototype.name = 'John';<br />
"John"<br />
&gt;&gt;&gt; user1 = new UserAccount();    // user1 does not have name, but prototype has one.<br />
Object name=John<br />
&gt;&gt;&gt; user2 = new UserAccount();    // prototype is shared by user1 and user2<br />
Object name=John<br />
&gt;&gt;&gt; user1.name = 'Tom';        // user1 has its own name now<br />
"Tom"<br />
&gt;&gt;&gt; user2.name        // user2 is still using the prototype's name<br />
"John"<br />
&gt;&gt;&gt; UserAccount.prototype.name = 'Alice';    // change prototype's name<br />
"Alice"<br />
&gt;&gt;&gt; user1.name    // user1 unaffected<br />
"Tom"<br />
&gt;&gt;&gt; user2.name    // user2 is still using the prototype's name<br />
"Alice"</span></code></p>
<p>That&#8217;s it. Hopefully this post can help C++ users pick up Javascript easier.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pograph.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pograph.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pograph.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pograph.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pograph.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pograph.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pograph.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pograph.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pograph.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pograph.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pograph.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pograph.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pograph.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pograph.wordpress.com/305/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pograph.wordpress.com&amp;blog=2231104&amp;post=305&amp;subd=pograph&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pograph.wordpress.com/2009/03/20/object-in-javascript-a-c-programmers-point-of-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">pograph</media:title>
		</media:content>
	</item>
	</channel>
</rss>
