em-priority-queue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in em-priority-queue.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ em-priority-queue (0.0.1)
5
+ algorithms (~> 0.3.0)
6
+ eventmachine (>= 1.0.0.beta.3)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ algorithms (0.3.0)
12
+ diff-lcs (1.1.2)
13
+ eventmachine (1.0.0.beta.3)
14
+ rake (0.9.2)
15
+ rspec (2.6.0)
16
+ rspec-core (~> 2.6.0)
17
+ rspec-expectations (~> 2.6.0)
18
+ rspec-mocks (~> 2.6.0)
19
+ rspec-core (2.6.4)
20
+ rspec-expectations (2.6.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.6.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ algorithms (~> 0.3.0)
29
+ em-priority-queue!
30
+ eventmachine (>= 1.0.0.beta.3)
31
+ rake
32
+ rspec
data/README ADDED
@@ -0,0 +1 @@
1
+ EM Priority Queue
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ include Rake::DSL
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+ task :test => :spec
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "em-priority-queue/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "em-priority-queue"
7
+ s.version = EventMachine::PriorityQueue::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mike Lewis"]
10
+ s.email = ["ft.mikelewis@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Async Priority Queue}
13
+ s.description = %q{Aync Priority Queue}
14
+
15
+ s.rubyforge_project = "em-priority-queue"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "eventmachine", ">= 1.0.0.beta.3"
23
+ s.add_dependency "algorithms", "~> 0.3.0"
24
+
25
+
26
+ s.add_development_dependency 'rake'
27
+ s.add_development_dependency 'rspec'
28
+ end
@@ -0,0 +1,4 @@
1
+ require 'eventmachine'
2
+ require 'algorithms'
3
+
4
+ require 'em-priority-queue/priority_queue'
@@ -0,0 +1,55 @@
1
+ module EventMachine
2
+ class PriorityQueue
3
+ include Enumerable
4
+
5
+ def initialize(&blk)
6
+ block ||= lambda { |x, y| (x <=> y) == 1 }
7
+ @heap = Containers::Heap.new(&block)
8
+ @callbacks = []
9
+ end
10
+
11
+ def size
12
+ @heap.size
13
+ end
14
+
15
+ def push(obj, pri)
16
+ EM.schedule do
17
+ @heap.push(pri, obj)
18
+ @callbacks.shift.call(@heap.pop) until @heap.empty? || @callbacks.empty?
19
+ end
20
+ end
21
+
22
+ def clear
23
+ @heap.clear
24
+ end
25
+
26
+ def empty?
27
+ @heap.empty?
28
+ end
29
+
30
+ def has_priority?(priority)
31
+ @heap.has_key?(priority)
32
+ end
33
+
34
+ def next
35
+ @heap.next
36
+ end
37
+
38
+ def pop(*a, &c)
39
+ cb = EM::Callback(*a, &c)
40
+ EM.schedule do
41
+ if @heap.empty?
42
+ @callbacks << cb
43
+ else
44
+ cb.call @heap.pop
45
+ end
46
+ end
47
+ nil
48
+ end
49
+
50
+ def delete(pri)
51
+ @heap.delete(pri)
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module EventMachine
2
+ class PriorityQueue
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe EventMachine::PriorityQueue do
4
+
5
+ before do
6
+ @q = EM::PriorityQueue.new
7
+ end
8
+
9
+ context "Reg. Queue" do
10
+ it "should return 0 for size and be empty" do
11
+ @q.size.should == 0
12
+ @q.empty?.should == true
13
+ end
14
+
15
+ it "should should push things onto the queue" do
16
+ EM.run do
17
+ 5.times do |i|
18
+ @q.push(i, 1)
19
+ end
20
+ EM.stop
21
+ end
22
+
23
+ @q.size.should == 5
24
+ end
25
+
26
+ it "should pop elements after adding them" do
27
+ responses = []
28
+ EM.run do
29
+ 5.times do |i|
30
+ @q.push(i, 1)
31
+ end
32
+
33
+ 5.times do
34
+ @q.pop do |e|
35
+ responses << e
36
+ EM.stop if @q.empty?
37
+ end
38
+ end
39
+ end
40
+
41
+ responses.length.should == 5
42
+ [0,1,2,3,4].each do |n|
43
+ responses.should include(n)
44
+ end
45
+ end
46
+
47
+ it "should pop elements if there are callbacks waiting for pushes" do
48
+ responses = []
49
+ EM.run do
50
+ 3.times do
51
+ @q.pop do |e|
52
+ responses << e
53
+ EM.stop if responses.size == 3
54
+ end
55
+ end
56
+
57
+ 5.times do |n|
58
+ @q.push(n, 1)
59
+ end
60
+ end
61
+ responses.length.should == 3
62
+ [0,1,2].each do |n|
63
+ responses.should include(n)
64
+ end
65
+ end
66
+ end
67
+
68
+ context "Priority Queue" do
69
+ it "should give elements in the order of their priority" do
70
+ responses = []
71
+ EM.run do
72
+
73
+
74
+ @q.push("Mike", 20)
75
+ @q.push("Alex", 21)
76
+ @q.push("Bob", 22)
77
+ @q.push("Tim", 18)
78
+
79
+ 4.times do
80
+ @q.pop do |e|
81
+ responses << e
82
+ EM.stop if responses.size == 4
83
+ end
84
+ end
85
+ end
86
+
87
+ responses[0].should == "Bob"
88
+ responses[1].should == "Alex"
89
+ responses[2].should == "Mike"
90
+ responses[3].should == "Tim"
91
+
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'em-priority-queue'
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-priority-queue
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mike Lewis
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-16 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: eventmachine
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 62196357
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ - beta
35
+ - 3
36
+ version: 1.0.0.beta.3
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: algorithms
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ hash: 19
48
+ segments:
49
+ - 0
50
+ - 3
51
+ - 0
52
+ version: 0.3.0
53
+ type: :runtime
54
+ version_requirements: *id002
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ prerelease: false
58
+ requirement: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ description: Aync Priority Queue
84
+ email:
85
+ - ft.mikelewis@gmail.com
86
+ executables: []
87
+
88
+ extensions: []
89
+
90
+ extra_rdoc_files: []
91
+
92
+ files:
93
+ - .gitignore
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - README
97
+ - Rakefile
98
+ - em-priority-queue.gemspec
99
+ - lib/em-priority-queue.rb
100
+ - lib/em-priority-queue/priority_queue.rb
101
+ - lib/em-priority-queue/version.rb
102
+ - spec/em-priority-queue_spec.rb
103
+ - spec/spec_helper.rb
104
+ has_rdoc: true
105
+ homepage: ""
106
+ licenses: []
107
+
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project: em-priority-queue
134
+ rubygems_version: 1.3.7
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Async Priority Queue
138
+ test_files:
139
+ - spec/em-priority-queue_spec.rb
140
+ - spec/spec_helper.rb