<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: queue code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 13 Oct 2008 23:07:44 GMT</pubDate>
    <description>DZone Snippets: queue code</description>
    <item>
      <title>Using a Stack with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5981</link>
      <description>Source: &lt;a href="http://macdevelopertips.com/ruby/using-a-stack-with-ruby.html"&gt;Mac Developer Tips &#187; Using a Stack with Ruby&lt;/a&gt; [macdevelopertips.com]&lt;br /&gt;&lt;br /&gt;... creating a simple Stack class as follows:&lt;br /&gt;&lt;code&gt;class Stack&lt;br /&gt;&lt;br /&gt;  def initialize&lt;br /&gt;   @the_stack = []&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def push(item)&lt;br /&gt;    @the_stack.push item&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def pop&lt;br /&gt;    @the_stack.pop&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def count&lt;br /&gt;    @the_stack.length&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;Here is how you might use the stack:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;  stack = Stack.new&lt;br /&gt;  stack.push('abc')&lt;br /&gt;  stack.push(100)&lt;br /&gt;  stack.count&lt;br /&gt;  stack.pop()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;If need be, you could also add a few convenience methods such as clearing the stack or looking at the last element (without popping it off the stack):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;  def clear&lt;br /&gt;    @the_stack.clear&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def look&lt;br /&gt;    @the_stack.last&lt;br /&gt;  end&lt;/code&gt;</description>
      <pubDate>Sun, 24 Aug 2008 22:06:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5981</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Instruct a shared whiteboard to save and refresh</title>
      <link>http://snippets.dzone.com/posts/show/5284</link>
      <description>The following code used with the ProjectX API informs the client web browser that the whiteboard will be refreshed in 5 seconds. It then archives the current whiteboard information, formats it, and sends a message to each web browser to refresh their view.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;project name="whiteboardqueue"&gt;&lt;br /&gt;  &lt;methods&gt;&lt;br /&gt;    &lt;method name="create"&gt;&lt;br /&gt;      &lt;params&gt;&lt;br /&gt;        &lt;param var="type"&gt;ecmascript&lt;/param&gt;&lt;br /&gt;        &lt;param var="body"&gt;startRefresh(5)&lt;/param&gt;&lt;br /&gt;        &lt;param var="sender"&gt;system&lt;/param&gt;&lt;br /&gt;      &lt;/params&gt;&lt;br /&gt;    &lt;/method&gt;&lt;br /&gt;    &lt;method name="timer"&gt;&lt;br /&gt;      &lt;params&gt;&lt;br /&gt;        &lt;param var="timer"&gt;5&lt;/param&gt;&lt;br /&gt;      &lt;/params&gt;&lt;br /&gt;    &lt;/method&gt;&lt;br /&gt;    &lt;method name="archive_and_format"&gt;&lt;br /&gt;      &lt;params/&gt;&lt;br /&gt;    &lt;/method&gt;&lt;br /&gt;    &lt;method name="create"&gt;&lt;br /&gt;      &lt;params&gt;&lt;br /&gt;        &lt;param var="type"&gt;ecmascript&lt;/param&gt;&lt;br /&gt;        &lt;param var="body"&gt;reloadDocument()&lt;/param&gt;&lt;br /&gt;        &lt;param var="sender"&gt;system&lt;/param&gt;&lt;br /&gt;      &lt;/params&gt;&lt;br /&gt;    &lt;/method&gt;&lt;br /&gt;  &lt;/methods&gt;&lt;br /&gt;&lt;/project&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;*update 1:14am*&lt;br /&gt;The &lt;a href="http://rorbuilder.info/r/whiteboardqueue/index2.svg"&gt;whiteboard demo&lt;/a&gt; [rorbuilder.info] allows the user to draw using the mouse within the web browser which renders SVG. Tested on Flock and Firefox.&lt;br /&gt;&lt;br /&gt;*update 4:42pm 28 Mar 08*&lt;br /&gt;You can also view the &lt;a href="http://rorbuilder.info/whiteboardqueue/whiteboardqueue_data.xml?passthru=1"&gt;whiteboard message queue&lt;/a&gt; [rorbuilder.info].&lt;br /&gt;&lt;br /&gt;*update 6:29pm Mar 08*&lt;br /&gt;I've created a short url (http://rubyurl.com/vxHD) (to demonstrate the cleaning of the whiteboard) which redirects to this http://rorbuilder.info/api/projectx.cgi?xml_project=&lt;project name="whiteboardqueue"&gt;&lt;methods&gt;&lt;method name="create"&gt;&lt;params&gt;&lt;param var="type"&gt;ecmascript&lt;/param&gt;&lt;param var="body"&gt;startRefresh(5)&lt;/param&gt;&lt;param var="sender"&gt;system&lt;/param&gt;&lt;/params&gt;&lt;/method&gt;&lt;method name="timer"&gt;&lt;params&gt;&lt;param var="timer"&gt;5&lt;/param&gt;&lt;/params&gt;&lt;/method&gt;&lt;method name="archive_and_format"&gt;&lt;params/&gt;&lt;/method&gt;&lt;method name="create"&gt;&lt;params&gt;&lt;param var="type"&gt;ecmascript&lt;/param&gt;&lt;param var="body"&gt;reloadDocument()&lt;/param&gt;&lt;param var="sender"&gt;system&lt;/param&gt;&lt;/params&gt;&lt;/method&gt;&lt;/methods&gt;&lt;/project&gt;&lt;br /&gt;&lt;br /&gt;I've </description>
      <pubDate>Thu, 27 Mar 2008 19:14:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5284</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>a silly FIFO in Erlang (you should use OTP's queue.erl instead)</title>
      <link>http://snippets.dzone.com/posts/show/2748</link>
      <description>&lt;code&gt;&lt;br /&gt;-module(fifo).&lt;br /&gt;-export([new/0, loop/0, push/2, pop/1]).&lt;br /&gt;&lt;br /&gt;new() -&gt;&lt;br /&gt;    spawn(?MODULE, loop, []).&lt;br /&gt;&lt;br /&gt;loop() -&gt;&lt;br /&gt;    receive&lt;br /&gt;        {Pid,pop} -&gt;&lt;br /&gt;            Pid ! {self(),receive {push,X} -&gt;&lt;br /&gt;                                  X&lt;br /&gt;                          end),&lt;br /&gt;            loop()&lt;br /&gt;    end.&lt;br /&gt;&lt;br /&gt;push(Fifo,X) -&gt;&lt;br /&gt;    Fifo ! {push,X},&lt;br /&gt;    X.&lt;br /&gt;&lt;br /&gt;pop(Fifo) -&gt;&lt;br /&gt;    Fifo ! {self(),pop},&lt;br /&gt;    receive {Fifo,X} -&gt;&lt;br /&gt;            X&lt;br /&gt;    end.&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 01 Oct 2006 09:36:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2748</guid>
      <author>ayrnieu (Julian Fondren)</author>
    </item>
    <item>
      <title>priority-queue functions</title>
      <link>http://snippets.dzone.com/posts/show/1142</link>
      <description>&lt;code&gt;&lt;br /&gt;REBOL [&lt;br /&gt;    Title: "Cheasy (Cheap-n-Easy) Priority Queue"&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;pq-insert: func [&lt;br /&gt;    list [any-block!]   "The queue"&lt;br /&gt;    item&lt;br /&gt;    priority [integer!]&lt;br /&gt;][&lt;br /&gt;    sort/skip/reverse append list reduce [priority item] 2&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;pq-remove: func [&lt;br /&gt;    "Remove an item from the priority queue"&lt;br /&gt;    list [any-block!]   "The queue"&lt;br /&gt;    /index  "Remove a specific item"&lt;br /&gt;        idx [integer!] "The specific item to remove"&lt;br /&gt;][&lt;br /&gt;    remove/part either index [at list (idx * 2 - 1)][head list] 2&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;pq-first: func [&lt;br /&gt;    list [any-block!]   "The queue"&lt;br /&gt;][&lt;br /&gt;    ; skip over the priority value and return the actual value&lt;br /&gt;    ; that was inserted in the queue.&lt;br /&gt;    first next head list&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;priority-queue: make object! [&lt;br /&gt;    data: copy []&lt;br /&gt;&lt;br /&gt;    insert: func [item priority] [pq-insert data item priority]&lt;br /&gt;&lt;br /&gt;    remove: func [/index idx] [&lt;br /&gt;        either index [pq-remove/index data idx][pq-remove data]&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    first: does [pq-first data]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;pq: copy []&lt;br /&gt;print pq-insert pq "A" 1&lt;br /&gt;print pq-insert pq "B" 10&lt;br /&gt;print pq-insert pq "C" 100&lt;br /&gt;print pq-insert pq "D" 1000&lt;br /&gt;print pq-remove/index pq 3&lt;br /&gt;print pq-remove pq&lt;br /&gt;print mold pq&lt;br /&gt;print pq-insert pq "CC" 100&lt;br /&gt;print pq-insert pq "CCC" 100&lt;br /&gt;print pq-insert pq "D" 1000&lt;br /&gt;print pq-insert pq "CCCC" 100&lt;br /&gt;print pq-insert pq "CCCCC" 100&lt;br /&gt;print pq-remove pq&lt;br /&gt;print pq-insert pq "CCCCCC" 100&lt;br /&gt;print pq-insert pq "CCCCCCC" 100&lt;br /&gt;&lt;br /&gt;print ""&lt;br /&gt;&lt;br /&gt;pq: make priority-queue []&lt;br /&gt;print pq/insert"A" 1&lt;br /&gt;print pq/insert"B" 10&lt;br /&gt;print pq/insert"C" 100&lt;br /&gt;print pq/insert"D" 1000&lt;br /&gt;print pq/remove/index 3&lt;br /&gt;print pq/remove ;pq&lt;br /&gt;print mold pq/data&lt;br /&gt;print pq/insert"CC" 100&lt;br /&gt;print pq/insert"CCC" 100&lt;br /&gt;print pq/insert"D" 1000&lt;br /&gt;print pq/insert"CCCC" 100&lt;br /&gt;print pq/insert"CCCCC" 100&lt;br /&gt;print pq/remove ;pq/data&lt;br /&gt;print pq/insert"CCCCCC" 100&lt;br /&gt;print pq/insert"CCCCCCC" 100&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;halt&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 13 Jan 2006 04:00:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1142</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>Priority Queue</title>
      <link>http://snippets.dzone.com/posts/show/726</link>
      <description>&lt;code&gt;&lt;br /&gt;pq = PriorityQueue()&lt;br /&gt;&lt;br /&gt;pq.put(('b', 1))&lt;br /&gt;pq.put(('a', 1))&lt;br /&gt;pq.put(('c', 1))&lt;br /&gt;pq.put(('z', 0))&lt;br /&gt;pq.put(('d', 2))&lt;br /&gt;&lt;br /&gt;while not pq.empty():&lt;br /&gt;  print pq.get(),   &lt;br /&gt;# z b a c d&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Get the implementation of Priority Queue &lt;a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/87369&gt;here&lt;/a&gt;.</description>
      <pubDate>Fri, 16 Sep 2005 19:55:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/726</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
