priority_queue 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Justin Balthrop
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,25 @@
1
+ = PriorityQueue
2
+
3
+ A very simple priority queue.
4
+
5
+ == Usage:
6
+
7
+ pq = PriorityQueue.new
8
+ pq[5] << 'foo'
9
+ pq[10] << 'unimportant foo'
10
+ pq[1] << 'important foo'
11
+
12
+ pq.shift # => 'important foo'
13
+ pq.shift # => 'foo'
14
+ pq.shift # => 'unimportant foo'
15
+ pq.shift # => nil
16
+
17
+ PriorityQueue is also enumerable.
18
+
19
+ == Install:
20
+
21
+ sudo gem install priority-queue -s http://gemcutter.org
22
+
23
+ == License:
24
+
25
+ Copyright (c) 2009 Justin Balthrop, Geni.com; Published under The MIT License, see License.txt
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |s|
8
+ s.name = "priority_queue"
9
+ s.summary = %Q{ A simple priority queue in Ruby. }
10
+ s.email = "code@justinbalthrop.com"
11
+ s.homepage = "http://github.com/ninjudd/priority_queue"
12
+ s.description = "A simple priority queue in Ruby."
13
+ s.authors = ["Justin Balthrop"]
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
18
+ end
19
+
20
+ Rake::TestTask.new do |t|
21
+ t.libs << 'lib'
22
+ t.pattern = 'test/**/*_test.rb'
23
+ t.verbose = false
24
+ end
25
+
26
+ Rake::RDocTask.new do |rdoc|
27
+ rdoc.rdoc_dir = 'rdoc'
28
+ rdoc.title = 'priority_queue'
29
+ rdoc.options << '--line-numbers' << '--inline-source'
30
+ rdoc.rdoc_files.include('README*')
31
+ rdoc.rdoc_files.include('lib/**/*.rb')
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |t|
37
+ t.libs << 'test'
38
+ t.test_files = FileList['test/**/*_test.rb']
39
+ t.verbose = true
40
+ end
41
+ rescue LoadError
42
+ end
43
+
44
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,36 @@
1
+ require 'thread'
2
+
3
+ class PriorityQueue
4
+ def initialize
5
+ clear
6
+ end
7
+
8
+ def clear
9
+ @queues = {}
10
+ end
11
+
12
+ def [](value)
13
+ @queues[value] ||= Queue.new
14
+ end
15
+
16
+ def priorities
17
+ @queues.keys.sort
18
+ end
19
+
20
+ def shift
21
+ priorities.each do |key|
22
+ if not self[key].empty?
23
+ return self[key].shift(true)
24
+ else
25
+ @queues.delete(key)
26
+ end
27
+ end
28
+ nil
29
+ end
30
+
31
+ def size
32
+ @queues.values.inject(0) do |size, queue|
33
+ size + queue.size
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{priority_queue}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Justin Balthrop"]
12
+ s.date = %q{2009-11-25}
13
+ s.description = %q{A simple priority queue in Ruby.}
14
+ s.email = %q{code@justinbalthrop.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/priority_queue.rb",
26
+ "priority_queue.gemspec",
27
+ "test/priority_queue_test.rb",
28
+ "test/test_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/ninjudd/priority_queue}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.5}
34
+ s.summary = %q{A simple priority queue in Ruby.}
35
+ s.test_files = [
36
+ "test/priority_queue_test.rb",
37
+ "test/test_helper.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
50
+
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class PriorityQueueTest < Test::Unit::TestCase
4
+ should "shift values in priority order" do
5
+ pq = PriorityQueue.new
6
+
7
+ pq[1] << :important_foo
8
+ pq[1] << :important_bar
9
+ pq[0] << :very_important_foo
10
+ pq[4] << :foo
11
+ pq[4] << :bar
12
+
13
+ assert_equal :very_important_foo, pq.shift
14
+ assert_equal :important_foo, pq.shift
15
+ assert_equal :important_bar, pq.shift
16
+ assert_equal :foo, pq.shift
17
+ assert_equal :bar, pq.shift
18
+ assert_equal nil, pq.shift
19
+ assert_equal nil, pq.shift
20
+ end
21
+
22
+ should "know it's own size" do
23
+ pq = PriorityQueue.new
24
+
25
+ pq[7] << :foo
26
+ pq[7] << :fu
27
+ pq[4] << :bar
28
+ pq[9] << :baz
29
+ pq[1] << :bap
30
+ pq[0] << :zap
31
+
32
+ assert_equal 6, pq.size
33
+
34
+ 3.times { pq.shift }
35
+ assert_equal 3, pq.size
36
+
37
+ 3.times { pq.shift }
38
+ assert_equal 0, pq.size
39
+
40
+ 3.times { pq.shift }
41
+ assert_equal 0, pq.size
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib/')
7
+ require 'priority_queue'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: priority_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Balthrop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-25 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple priority queue in Ruby.
17
+ email: code@justinbalthrop.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION
31
+ - lib/priority_queue.rb
32
+ - priority_queue.gemspec
33
+ - test/priority_queue_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/ninjudd/priority_queue
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: A simple priority queue in Ruby.
63
+ test_files:
64
+ - test/priority_queue_test.rb
65
+ - test/test_helper.rb