<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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: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>Comments on: Growable Arrays</title>
	<atom:link href="http://programmingpraxis.com/2009/10/16/growable-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/10/16/growable-arrays/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Skrud</title>
		<link>http://programmingpraxis.com/2009/10/16/growable-arrays/#comment-727</link>
		<dc:creator><![CDATA[Skrud]]></dc:creator>
		<pubDate>Tue, 27 Oct 2009 19:08:49 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1428#comment-727</guid>
		<description><![CDATA[Here&#039;s another python solution. This one uses a wrapper class around the root node which keeps track of the current upper bound and uses that for put() and hirem(). I&#039;m sure hirem() could be cleaner, though. :-S

[sourcecode lang=&quot;python&quot;]
class GrowableArrayNode:
	def __init__(self, item=None):
		self.item = item
		self.left = None
		self.right = None
		
	def put(self, item, index):
		if index == 1:
			self.item = item
		elif index % 2 == 0:
			if not self.left:
				self.left = GrowableArrayNode()
			self.left.put(item, index/2)
		else:
			if not self.right:
				self.right = GrowableArrayNode()
			self.right.put(item, index/2)

	def get(self, index):
		if index == 1:
			return self.item
		
		if index % 2 == 0:
			return self.left.get(index / 2)
		else: return self.right.get(index / 2)
	
	def traverse(self):	
		if self.left:
			for x in self.left.traverse():
				yield x
		yield self
		if self.right:
			for x in self.right.traverse():
				yield x
				
	def hirem(self,index):
		if index / 2 == 1:
			if index % 2 == 0:
				self.left = None
			else:
				self.right = None
		elif index % 2 == 0:
			self.left.hirem(index / 2)
		else:
			self.right.hirem(index / 2)


class GrowableArray:
	def __init__(self):
		self.root = None
	
	def get(self,index):
		if self.root == None:
			return None
		
		return self.root.get(index)
	
	def put(self,item):
		if self.root == None:
			self.root = GrowableArrayNode()
			self.cur = 0
		self.cur += 1
		self.root.put(item,self.cur)
		
	def print_items(self):
		for node in self.root.traverse():
			print node.item
			
	def hirem(self):
		if self.root:
			self.root.hirem(self.cur)
			self.cur -= 1
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Here&#8217;s another python solution. This one uses a wrapper class around the root node which keeps track of the current upper bound and uses that for put() and hirem(). I&#8217;m sure hirem() could be cleaner, though. :-S</p>
<pre class="brush: python;">
class GrowableArrayNode:
	def __init__(self, item=None):
		self.item = item
		self.left = None
		self.right = None

	def put(self, item, index):
		if index == 1:
			self.item = item
		elif index % 2 == 0:
			if not self.left:
				self.left = GrowableArrayNode()
			self.left.put(item, index/2)
		else:
			if not self.right:
				self.right = GrowableArrayNode()
			self.right.put(item, index/2)

	def get(self, index):
		if index == 1:
			return self.item

		if index % 2 == 0:
			return self.left.get(index / 2)
		else: return self.right.get(index / 2)

	def traverse(self):
		if self.left:
			for x in self.left.traverse():
				yield x
		yield self
		if self.right:
			for x in self.right.traverse():
				yield x

	def hirem(self,index):
		if index / 2 == 1:
			if index % 2 == 0:
				self.left = None
			else:
				self.right = None
		elif index % 2 == 0:
			self.left.hirem(index / 2)
		else:
			self.right.hirem(index / 2)

class GrowableArray:
	def __init__(self):
		self.root = None

	def get(self,index):
		if self.root == None:
			return None

		return self.root.get(index)

	def put(self,item):
		if self.root == None:
			self.root = GrowableArrayNode()
			self.cur = 0
		self.cur += 1
		self.root.put(item,self.cur)

	def print_items(self):
		for node in self.root.traverse():
			print node.item

	def hirem(self):
		if self.root:
			self.root.hirem(self.cur)
			self.cur -= 1
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Doliner</title>
		<link>http://programmingpraxis.com/2009/10/16/growable-arrays/#comment-694</link>
		<dc:creator><![CDATA[Joe Doliner]]></dc:creator>
		<pubDate>Sat, 17 Oct 2009 00:09:34 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1428#comment-694</guid>
		<description><![CDATA[A python solution. joedoliner.com for comments and test code.
[sourcecode lang=&quot;python&quot;]
class exArray():
	key = None
	left = None
	right = None
	def get(self, index):
		if (index == 0):
			print &#039;index out of bounds&#039;
		elif (index == 1):
			if (self.key == None):
				print &#039;index out of bounds&#039;
			else:
				return self.key
		elif (index%2 == 0):
			if (self.left):
				return self.left.get(index &gt;&gt; 1)
			else:
				print &#039;index out of bounds&#039;
		else:
			if (self.right):
				return self.right.get(index &gt;&gt; 1)
			else:
				print &#039;index out of bounds&#039;
	def put(self, index, key):
		if (index == 0):
			print &#039;index out of bounds&#039;
		elif (index == 1):
			self.key = key
		elif (index%2 == 0):
			if (not self.left):
				self.left = exArray()
			self.left.put(index &gt;&gt; 1, key)
		else:
			if (not self.right):
				self.right = exArray()
			self.right.put(index &gt;&gt; 1, key)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>A python solution. joedoliner.com for comments and test code.</p>
<pre class="brush: python;">
class exArray():
	key = None
	left = None
	right = None
	def get(self, index):
		if (index == 0):
			print 'index out of bounds'
		elif (index == 1):
			if (self.key == None):
				print 'index out of bounds'
			else:
				return self.key
		elif (index%2 == 0):
			if (self.left):
				return self.left.get(index &gt;&gt; 1)
			else:
				print 'index out of bounds'
		else:
			if (self.right):
				return self.right.get(index &gt;&gt; 1)
			else:
				print 'index out of bounds'
	def put(self, index, key):
		if (index == 0):
			print 'index out of bounds'
		elif (index == 1):
			self.key = key
		elif (index%2 == 0):
			if (not self.left):
				self.left = exArray()
			self.left.put(index &gt;&gt; 1, key)
		else:
			if (not self.right):
				self.right = exArray()
			self.right.put(index &gt;&gt; 1, key)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis Growable Arrays &#171; Joe Doliner</title>
		<link>http://programmingpraxis.com/2009/10/16/growable-arrays/#comment-693</link>
		<dc:creator><![CDATA[Programming Praxis Growable Arrays &#171; Joe Doliner]]></dc:creator>
		<pubDate>Sat, 17 Oct 2009 00:08:05 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1428#comment-693</guid>
		<description><![CDATA[[...] Exercise from:  http://programmingpraxis.com/2009/10/16/growable-arrays/  [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Exercise from:  <a href="http://programmingpraxis.com/2009/10/16/growable-arrays/" rel="nofollow">http://programmingpraxis.com/2009/10/16/growable-arrays/</a>  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Posts about Programming from google blogs as of October 16, 2009 &#171; tryfly.com</title>
		<link>http://programmingpraxis.com/2009/10/16/growable-arrays/#comment-692</link>
		<dc:creator><![CDATA[Posts about Programming from google blogs as of October 16, 2009 &#171; tryfly.com]]></dc:creator>
		<pubDate>Fri, 16 Oct 2009 23:48:40 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1428#comment-692</guid>
		<description><![CDATA[[...]  [...]]]></description>
		<content:encoded><![CDATA[<p>[...]  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/10/16/growable-arrays/#comment-674</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 16 Oct 2009 12:43:05 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1428#comment-674</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/10/16/programming-praxis-growable-arrays/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
data Grow a = Empty &#124; Grow { val :: a, l :: Grow a, r:: Grow a }

walk :: (Int -&gt; Grow a -&gt; b) -&gt; Int -&gt; Grow a -&gt; b
walk f i = f (div i 2) . if even i then l else r

modify :: Grow a -&gt; (Int -&gt; Grow a -&gt; Grow a) -&gt; Int -&gt; Grow a -&gt; Grow a
modify d _ _ Empty         = d
modify _ f i g &#124; even i    = g { l = walk f i g }
               &#124; otherwise = g { r = walk f i g }

get :: Int -&gt; Grow a -&gt; Maybe a
get _ Empty = Nothing
get 1 g     = Just $ val g
get i g     = walk get i g

put :: Int -&gt; a -&gt; Grow a -&gt; Grow a
put 1 x Empty = Grow x Empty Empty
put 1 x g     = g { val = x }
put i x g     = modify (error &quot;array out of bounds&quot;) (`put` x) i g

hirem :: Int -&gt; Grow a -&gt; Grow a
hirem 1 = const Empty
hirem i = modify Empty hirem i
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/10/16/programming-praxis-growable-arrays/" rel="nofollow">http://bonsaicode.wordpress.com/2009/10/16/programming-praxis-growable-arrays/</a> for a version with comments):</p>
<pre class="brush: css;">
data Grow a = Empty | Grow { val :: a, l :: Grow a, r:: Grow a }

walk :: (Int -&gt; Grow a -&gt; b) -&gt; Int -&gt; Grow a -&gt; b
walk f i = f (div i 2) . if even i then l else r

modify :: Grow a -&gt; (Int -&gt; Grow a -&gt; Grow a) -&gt; Int -&gt; Grow a -&gt; Grow a
modify d _ _ Empty         = d
modify _ f i g | even i    = g { l = walk f i g }
               | otherwise = g { r = walk f i g }

get :: Int -&gt; Grow a -&gt; Maybe a
get _ Empty = Nothing
get 1 g     = Just $ val g
get i g     = walk get i g

put :: Int -&gt; a -&gt; Grow a -&gt; Grow a
put 1 x Empty = Grow x Empty Empty
put 1 x g     = g { val = x }
put i x g     = modify (error &quot;array out of bounds&quot;) (`put` x) i g

hirem :: Int -&gt; Grow a -&gt; Grow a
hirem 1 = const Empty
hirem i = modify Empty hirem i
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Growable Arrays &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2009/10/16/growable-arrays/#comment-673</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Growable Arrays &#171; Bonsai Code]]></dc:creator>
		<pubDate>Fri, 16 Oct 2009 12:42:54 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1428#comment-673</guid>
		<description><![CDATA[[...] today&#8217;s Programming Praxis we&#8217;re going to implement a growable array, which is a data structure with [...]]]></description>
		<content:encoded><![CDATA[<p>[...] today&#8217;s Programming Praxis we&#8217;re going to implement a growable array, which is a data structure with [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

