<?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/"
		>
<channel>
	<title>Comments on: I guess PriorityBlockingDeque wasn&#8217;t high on the priority list..</title>
	<atom:link href="http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/feed/" rel="self" type="application/rss+xml" />
	<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/</link>
	<description>The internet, design patterns, frameworks and Java</description>
	<lastBuildDate>Wed, 08 Sep 2010 10:18:29 -0700</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-12369</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Fri, 05 Oct 2007 10:58:14 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-12369</guid>
		<description>Heh, and I read them carefully but misunderstand what they mean. Well, anyway, that&#039;s sorted out. Thanks for the input!</description>
		<content:encoded><![CDATA[<p>Heh, and I read them carefully but misunderstand what they mean. Well, anyway, that&#8217;s sorted out. Thanks for the input!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BrightByte</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-12368</link>
		<dc:creator>BrightByte</dc:creator>
		<pubDate>Fri, 05 Oct 2007 10:48:31 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-12368</guid>
		<description>Hah, no it doesn&#039;t confirm what I said. I should have read the docs carefully. Collections.sort is smarter than i thought - while a naive binary search on a LinkedList would be O(n * log(n)), Collections.sort does it in n steps with only log(n) comparisons, as opposed to n steps and n comparisons for the linear scan i suggested. I guess this is made possible by the fact that ListIterator can move both ways.

Another mistake I made is: ListIterator does haven an insert method (add). Sorry.

So, as it turns out, your LinkedList/Collections.sort based implementation seems to be optimal (or at least, slightly better than what I suggested), and I should read the documentation more closely :)

Thanks for this great blog, btw.</description>
		<content:encoded><![CDATA[<p>Hah, no it doesn&#8217;t confirm what I said. I should have read the docs carefully. Collections.sort is smarter than i thought &#8211; while a naive binary search on a LinkedList would be O(n * log(n)), Collections.sort does it in n steps with only log(n) comparisons, as opposed to n steps and n comparisons for the linear scan i suggested. I guess this is made possible by the fact that ListIterator can move both ways.</p>
<p>Another mistake I made is: ListIterator does haven an insert method (add). Sorry.</p>
<p>So, as it turns out, your LinkedList/Collections.sort based implementation seems to be optimal (or at least, slightly better than what I suggested), and I should read the documentation more closely <img src='http://chaoticjava.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for this great blog, btw.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-12361</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Fri, 05 Oct 2007 09:15:20 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-12361</guid>
		<description>What you said brought up a memory and after careful checking I remembered why I didn&#039;t use ArrayList in the first place (as it&#039;s obviously the more efficient one) - it doesn&#039;t have &lt;code&gt;pollFirst&lt;/code&gt; and &lt;code&gt;pollLast&lt;/code&gt; functionality at all, as it doesn&#039;t implement the &lt;code&gt;Deque&lt;/code&gt; interface, unlike &lt;code&gt;LinkedList&lt;/code&gt;.

However, a Deque implementation using a ring-array is definitely possible, and I&#039;m sure there&#039;s a ring-array implementation somewhere out there to be used instead of re-writing it.

However, as to what you said, take a look at the &lt;code&gt;Collections.sort&lt;/code&gt; documentation:

&lt;blockquote&gt;
This method runs in log(n) time for a &quot;random access&quot; list (which provides near-constant-time positional access). If the specified list does not implement the RandomAccess interface and is large, this method will do an iterator-based binary search that performs O(n) link traversals and O(log n) element comparisons.
&lt;/blockquote&gt;

If I understand this correctly, this verifies what you said.</description>
		<content:encoded><![CDATA[<p>What you said brought up a memory and after careful checking I remembered why I didn&#8217;t use ArrayList in the first place (as it&#8217;s obviously the more efficient one) &#8211; it doesn&#8217;t have <code>pollFirst</code> and <code>pollLast</code> functionality at all, as it doesn&#8217;t implement the <code>Deque</code> interface, unlike <code>LinkedList</code>.</p>
<p>However, a Deque implementation using a ring-array is definitely possible, and I&#8217;m sure there&#8217;s a ring-array implementation somewhere out there to be used instead of re-writing it.</p>
<p>However, as to what you said, take a look at the <code>Collections.sort</code> documentation:</p>
<blockquote><p>
This method runs in log(n) time for a &#8220;random access&#8221; list (which provides near-constant-time positional access). If the specified list does not implement the RandomAccess interface and is large, this method will do an iterator-based binary search that performs O(n) link traversals and O(log n) element comparisons.
</p></blockquote>
<p>If I understand this correctly, this verifies what you said.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BrightByte</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-12357</link>
		<dc:creator>BrightByte</dc:creator>
		<pubDate>Fri, 05 Oct 2007 08:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-12357</guid>
		<description>Using an ArrayList would give you O(log(n)) complexity for add, but means O(n) complexity for removing the head of the queue (at least I think so - again, I have not looked at how ArrayList is implemented, exactly). 

So, assuming an equal total number of add and remove/poll operations, the options appear to be: Use a LinkedList, with O(n) for add and O(1) for remove, giving you an amortized complexity of O(n), or using an ArrayList with O(log(n)) for add and O(n) for remove/poll, again giving you an amortized complexity of O(n), but with more overhead in practice (because you need n+log(n) steps and, as you mentioned, possibly for growing the array). 

It would be interesting to run a benchmark to see which one is actually better for a &quot;practical&quot; size of the queue and number of add/remove operations.</description>
		<content:encoded><![CDATA[<p>Using an ArrayList would give you O(log(n)) complexity for add, but means O(n) complexity for removing the head of the queue (at least I think so &#8211; again, I have not looked at how ArrayList is implemented, exactly). </p>
<p>So, assuming an equal total number of add and remove/poll operations, the options appear to be: Use a LinkedList, with O(n) for add and O(1) for remove, giving you an amortized complexity of O(n), or using an ArrayList with O(log(n)) for add and O(n) for remove/poll, again giving you an amortized complexity of O(n), but with more overhead in practice (because you need n+log(n) steps and, as you mentioned, possibly for growing the array). </p>
<p>It would be interesting to run a benchmark to see which one is actually better for a &#8220;practical&#8221; size of the queue and number of add/remove operations.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-12306</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Thu, 04 Oct 2007 19:11:33 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-12306</guid>
		<description>You&#039;re right, however, I could always change it to ArrayList and get it over with - receive the log(n) efficiency on one hand but might stumble upon bad efficiency during add() methods that exceed the size.

For that case, I could probably use the capacity to create the list at the right size right from the start (unless it&#039;s &lt;code&gt;Integer.MAX_VALUE&lt;/code&gt;, of course).</description>
		<content:encoded><![CDATA[<p>You&#8217;re right, however, I could always change it to ArrayList and get it over with &#8211; receive the log(n) efficiency on one hand but might stumble upon bad efficiency during add() methods that exceed the size.</p>
<p>For that case, I could probably use the capacity to create the list at the right size right from the start (unless it&#8217;s <code>Integer.MAX_VALUE</code>, of course).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BrightByte</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-12305</link>
		<dc:creator>BrightByte</dc:creator>
		<pubDate>Thu, 04 Oct 2007 19:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-12305</guid>
		<description>I havn&#039;t looks at how LinkedList and Collection.sort are implemented in detail, but doing a binary search on a linked list seems to be a bad idea to me. Accessing index i takes i steps on a linked list, so a binary search would be in O(n * log(n)). Simple linear scanning would be faster (O(n)). It&#039;s kind of sad that ListIterator doesn&#039;t have an insert method - that would avoid the issue that you have to scan twice: once iterating over the list to find the right place to insert your element, then again to actually insert it, using add(int, Object).

Anyway, by not doing a binary search but simply scanning for the place to insert the new item, i believe you can bring the complexity of add from O(n*log(n)) down to O(n). Still not great, but better...</description>
		<content:encoded><![CDATA[<p>I havn&#8217;t looks at how LinkedList and Collection.sort are implemented in detail, but doing a binary search on a linked list seems to be a bad idea to me. Accessing index i takes i steps on a linked list, so a binary search would be in O(n * log(n)). Simple linear scanning would be faster (O(n)). It&#8217;s kind of sad that ListIterator doesn&#8217;t have an insert method &#8211; that would avoid the issue that you have to scan twice: once iterating over the list to find the right place to insert your element, then again to actually insert it, using add(int, Object).</p>
<p>Anyway, by not doing a binary search but simply scanning for the place to insert the new item, i believe you can bring the complexity of add from O(n*log(n)) down to O(n). Still not great, but better&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-11664</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Wed, 26 Sep 2007 21:27:49 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-11664</guid>
		<description>You&#039;re right. I&#039;ve changed it and checked it in.</description>
		<content:encoded><![CDATA[<p>You&#8217;re right. I&#8217;ve changed it and checked it in.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hanson Char</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-11244</link>
		<dc:creator>Hanson Char</dc:creator>
		<pubDate>Mon, 24 Sep 2007 09:47:56 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-11244</guid>
		<description>&gt;with a Collections.sort call when adding elements

There is no need to sort when adding elements.  Sorting is necessary only when the list is initially constructed.  Subsequently adding elements only requires a binary search + insert.  That will yield ln(n) instead of n*ln(n) time.</description>
		<content:encoded><![CDATA[<p>&gt;with a Collections.sort call when adding elements</p>
<p>There is no need to sort when adding elements.  Sorting is necessary only when the list is initially constructed.  Subsequently adding elements only requires a binary search + insert.  That will yield ln(n) instead of n*ln(n) time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Avah</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-10721</link>
		<dc:creator>Avah</dc:creator>
		<pubDate>Wed, 19 Sep 2007 04:56:26 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-10721</guid>
		<description>I&#039;ve created a test for it, and sure enough it fails with duplicate values; I&#039;ve re-implemented it, this time using a LinkedList with a Collections.sort call when adding elements, and now it accepts duplicates.

I&#039;ve checked it in so it should be available now.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve created a test for it, and sure enough it fails with duplicate values; I&#8217;ve re-implemented it, this time using a LinkedList with a Collections.sort call when adding elements, and now it accepts duplicates.</p>
<p>I&#8217;ve checked it in so it should be available now.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hanson Char</title>
		<link>http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/comment-page-1/#comment-10693</link>
		<dc:creator>Hanson Char</dc:creator>
		<pubDate>Wed, 19 Sep 2007 01:53:11 +0000</pubDate>
		<guid isPermaLink="false">http://chaoticjava.com/posts/i-guess-priorityblockingdeque-wasnt-high-on-the-priority-list/#comment-10693</guid>
		<description>&gt;implementing PriorityDeque using a List and 
&gt;Collections methods would probably prove to be 
&gt;better conceptually, if not in performance, since it
&gt;will permit duplicate values, something that 
&gt;NavigableSet does not.
&gt;What do you think? 

That&#039;s basically what I was trying to suggest :)</description>
		<content:encoded><![CDATA[<p>&gt;implementing PriorityDeque using a List and<br />
&gt;Collections methods would probably prove to be<br />
&gt;better conceptually, if not in performance, since it<br />
&gt;will permit duplicate values, something that<br />
&gt;NavigableSet does not.<br />
&gt;What do you think? </p>
<p>That&#8217;s basically what I was trying to suggest <img src='http://chaoticjava.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
