em-priority-queue 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- em-priority-queue (0.0.2)
4
+ em-priority-queue (1.1.0)
5
5
  algorithms (~> 0.3.0)
6
6
  eventmachine (>= 1.0.0.beta.3)
7
7
 
@@ -9,8 +9,11 @@ GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
11
  algorithms (0.3.0)
12
+ chronic (0.6.6)
13
+ delorean (1.1.1)
14
+ chronic
12
15
  diff-lcs (1.1.2)
13
- eventmachine (1.0.0.beta.3)
16
+ eventmachine (1.0.0.beta.4)
14
17
  rake (0.9.2)
15
18
  rspec (2.6.0)
16
19
  rspec-core (~> 2.6.0)
@@ -25,8 +28,7 @@ PLATFORMS
25
28
  ruby
26
29
 
27
30
  DEPENDENCIES
28
- algorithms (~> 0.3.0)
31
+ delorean (>= 1.1.1)
29
32
  em-priority-queue!
30
- eventmachine (>= 1.0.0.beta.3)
31
33
  rake
32
34
  rspec
data/README.md CHANGED
@@ -59,6 +59,64 @@ EventMachine Priority Queue
59
59
  responses[3] # Bob
60
60
 
61
61
 
62
+ **FIFO (first in, first out)**
63
+
64
+ When values have the same priority, you may want to use FIFO to prioritize even more. This way, em-priority-queue will pop the items in the order that they were pushed.
65
+
66
+ @q = EM::PriorityQueue.new(:fifo => true)
67
+
68
+ responses = []
69
+ EM.run do
70
+ @q.push("Mike", 20)
71
+ @q.push("Alex", 21)
72
+ @q.push("Bob", 20)
73
+ @q.push("Tim", 18)
74
+ @q.push("Carol", 20)
75
+
76
+ 5.times do
77
+ @q.pop do |e|
78
+ responses << e
79
+ EM.stop if responses.size == 5
80
+ end
81
+ end
82
+ end
83
+
84
+ responses[0] # Alex
85
+ responses[1] # Mike
86
+ responses[2] # Bob
87
+ responses[3] # Carol
88
+ responses[4] # Tim
89
+
90
+ **FIFO with custom priority**
91
+
92
+ @q = EM::PriorityQueue.new(:fifo => true) {|x,y| x < y}
93
+
94
+ responses = []
95
+ EM.run do
96
+
97
+
98
+ @q.push("Mike", 20)
99
+ @q.push("Alex", 21)
100
+ @q.push("Bob", 20)
101
+ @q.push("Tim", 18)
102
+ @q.push("Carol", 20)
103
+
104
+ 5.times do
105
+ @q.pop do |e|
106
+ responses << e
107
+ EM.stop if responses.size == 5
108
+ end
109
+ end
110
+ end
111
+
112
+ responses[0] # Tim
113
+ responses[1] # Mike
114
+ responses[2] # Bob
115
+ responses[3] # Carol
116
+ responses[4] # Alex
117
+
118
+
119
+
62
120
 
63
121
 
64
122
  To see all examples/cases, see the spec file.
@@ -25,4 +25,5 @@ Gem::Specification.new do |s|
25
25
 
26
26
  s.add_development_dependency 'rake'
27
27
  s.add_development_dependency 'rspec'
28
+ s.add_development_dependency "delorean", ">= 1.1.1"
28
29
  end
@@ -2,9 +2,20 @@ module EventMachine
2
2
  class PriorityQueue
3
3
  include Enumerable
4
4
 
5
- def initialize(&blk)
5
+ def initialize(opts={}, &blk)
6
6
  blk ||= lambda { |x, y| (x <=> y) == 1 }
7
- @heap = Containers::Heap.new(&blk)
7
+ fifo_blk = nil
8
+ @fifo = !!opts[:fifo]
9
+ if @fifo
10
+ fifo_blk = lambda do |x,y|
11
+ if x[0] == y[0]
12
+ x[1] < y[1]
13
+ else
14
+ blk.call(x[0], y[0])
15
+ end
16
+ end
17
+ end
18
+ @heap = Containers::Heap.new(&(fifo_blk || blk))
8
19
  @callbacks = []
9
20
  end
10
21
 
@@ -13,6 +24,7 @@ module EventMachine
13
24
  end
14
25
 
15
26
  def push(obj, pri)
27
+ pri = [pri, Time.now.to_i] if @fifo
16
28
  EM.schedule do
17
29
  @heap.push(pri, obj)
18
30
  @callbacks.shift.call(@heap.pop) until @heap.empty? || @callbacks.empty?
@@ -1,5 +1,5 @@
1
1
  module EventMachine
2
2
  class PriorityQueue
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -77,10 +77,10 @@ describe EventMachine::PriorityQueue do
77
77
  @q.push("Tim", 18)
78
78
 
79
79
  4.times do
80
- @q.pop do |e|
81
- responses << e
82
- EM.stop if responses.size == 4
83
- end
80
+ @q.pop do |e|
81
+ responses << e
82
+ EM.stop if responses.size == 4
83
+ end
84
84
  end
85
85
  end
86
86
 
@@ -90,8 +90,89 @@ describe EventMachine::PriorityQueue do
90
90
  responses[3].should == "Tim"
91
91
 
92
92
  end
93
- it "should give elements in the order of their priority - custom ordering schema" do
94
- @q = EM::PriorityQueue.new {|x,y| x < y}
93
+
94
+ it "should create a fifo priority queue if specified" do
95
+ fifo_queue = EM::PriorityQueue.new(:fifo => true)
96
+ responses = []
97
+ EM.run do
98
+ fifo_queue.push("Checking1", 20)
99
+ Delorean.jump 30
100
+ fifo_queue.push("Other1", 16)
101
+ Delorean.jump 30
102
+ fifo_queue.push("Other2", 18)
103
+ Delorean.jump 30
104
+ fifo_queue.push("Checking2", 20)
105
+ Delorean.jump 30
106
+ fifo_queue.push("Checking3", 20)
107
+ Delorean.jump 30
108
+ fifo_queue.push("Checking4", 20)
109
+ Delorean.jump 30
110
+ fifo_queue.push("Checking5", 20)
111
+ Delorean.jump 30
112
+ fifo_queue.push("Checking6", 20)
113
+ Delorean.jump 30
114
+ fifo_queue.push("Other3", 33)
115
+ Delorean.jump 30
116
+ fifo_queue.push("Checking7", 20)
117
+ Delorean.jump 30
118
+ fifo_queue.push("Checking8", 20)
119
+ Delorean.jump 30
120
+
121
+ 11.times do
122
+ fifo_queue.pop do |e|
123
+ responses << e
124
+ EM.stop if responses.size == 4
125
+ end
126
+ end
127
+
128
+ end
129
+
130
+ responses.first(5).should == ["Other3", "Checking1", "Checking2", "Checking3", "Checking4"]
131
+
132
+ end
133
+ it "should create a fifo priority queue if specified - custom ordering schema" do
134
+ fifo_queue = EM::PriorityQueue.new(:fifo => true) {|x,y| x < y}
135
+ responses = []
136
+ EM.run do
137
+ fifo_queue.push("Checking1", 20)
138
+ Delorean.jump 30
139
+ fifo_queue.push("Other1", 16)
140
+ Delorean.jump 30
141
+ fifo_queue.push("Other2", 18)
142
+ Delorean.jump 30
143
+ fifo_queue.push("Checking2", 20)
144
+ Delorean.jump 30
145
+ fifo_queue.push("Checking3", 20)
146
+ Delorean.jump 30
147
+ fifo_queue.push("Checking4", 20)
148
+ Delorean.jump 30
149
+ fifo_queue.push("Checking5", 20)
150
+ Delorean.jump 30
151
+ fifo_queue.push("Checking6", 20)
152
+ Delorean.jump 30
153
+ fifo_queue.push("Other3", 33)
154
+ Delorean.jump 30
155
+ fifo_queue.push("Checking7", 20)
156
+ Delorean.jump 30
157
+ fifo_queue.push("Checking8", 20)
158
+ Delorean.jump 30
159
+
160
+ 11.times do
161
+ fifo_queue.pop do |e|
162
+ responses << e
163
+ EM.stop if responses.size == 4
164
+ end
165
+ end
166
+
167
+ end
168
+
169
+ responses.first(5).should == ["Other1", "Other2", "Checking1", "Checking2", "Checking3"]
170
+
171
+ end
172
+
173
+
174
+ it "should give elements in the order of their priority - custom ordering schema" do
175
+ @q = EM::PriorityQueue.new {|x,y| x < y}
95
176
  responses = []
96
177
  EM.run do
97
178
 
@@ -102,10 +183,10 @@ describe EventMachine::PriorityQueue do
102
183
  @q.push("Tim", 18)
103
184
 
104
185
  4.times do
105
- @q.pop do |e|
106
- responses << e
107
- EM.stop if responses.size == 4
108
- end
186
+ @q.pop do |e|
187
+ responses << e
188
+ EM.stop if responses.size == 4
189
+ end
109
190
  end
110
191
  end
111
192
 
@@ -113,8 +194,65 @@ describe EventMachine::PriorityQueue do
113
194
  responses[1].should == "Mike"
114
195
  responses[2].should == "Alex"
115
196
  responses[3].should == "Bob"
197
+ end
198
+ end
116
199
 
200
+ context "README demos" do
201
+ it "should run the first fifo example" do
202
+ @q = EM::PriorityQueue.new(:fifo => true)
203
+
204
+ responses = []
205
+ EM.run do
206
+
207
+
208
+ @q.push("Mike", 20)
209
+ @q.push("Alex", 21)
210
+ @q.push("Bob", 20)
211
+ @q.push("Tim", 18)
212
+ @q.push("Carol", 20)
213
+
214
+ 5.times do
215
+ @q.pop do |e|
216
+ responses << e
217
+ EM.stop if responses.size == 5
218
+ end
219
+ end
220
+ end
221
+
222
+ responses[0].should == "Alex"
223
+ responses[1].should == "Mike"
224
+ responses[2].should == "Bob"
225
+ responses[3].should == "Carol"
226
+ responses[4].should == "Tim"
117
227
  end
118
228
 
229
+ it "should run the second fifo example" do
230
+ @q = EM::PriorityQueue.new(:fifo => true) {|x,y| x < y}
231
+
232
+ responses = []
233
+ EM.run do
234
+
235
+
236
+ @q.push("Mike", 20)
237
+ @q.push("Alex", 21)
238
+ @q.push("Bob", 20)
239
+ @q.push("Tim", 18)
240
+ @q.push("Carol", 20)
241
+
242
+ 5.times do
243
+ @q.pop do |e|
244
+ responses << e
245
+ EM.stop if responses.size == 5
246
+ end
247
+ end
248
+ end
249
+
250
+ responses[0].should == "Tim" # Tim
251
+ responses[1].should == "Mike" # Mike
252
+ responses[2].should == "Bob" # Bob
253
+ responses[3].should == "Carol" # Carol
254
+ responses[4].should == "Alex" # Alex
255
+
256
+ end
119
257
  end
120
258
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,9 @@
1
1
  $:.unshift File.dirname(__FILE__) + '/../lib'
2
2
  require 'em-priority-queue'
3
+ require 'delorean'
4
+
5
+ RSpec.configure do |config|
6
+ config.include Delorean
7
+ config.after(:each) { back_to_the_present }
8
+
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-priority-queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-30 00:00:00.000000000 Z
12
+ date: 2011-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
16
- requirement: &2153108280 !ruby/object:Gem::Requirement
16
+ requirement: &2152680760 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.0.0.beta.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153108280
24
+ version_requirements: *2152680760
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: algorithms
27
- requirement: &2153107780 !ruby/object:Gem::Requirement
27
+ requirement: &2152680260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.3.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153107780
35
+ version_requirements: *2152680260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &2152578500 !ruby/object:Gem::Requirement
38
+ requirement: &2152679880 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152578500
46
+ version_requirements: *2152679880
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &2152574420 !ruby/object:Gem::Requirement
49
+ requirement: &2152679420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,18 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2152574420
57
+ version_requirements: *2152679420
58
+ - !ruby/object:Gem::Dependency
59
+ name: delorean
60
+ requirement: &2152678920 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.1.1
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2152678920
58
69
  description: Asynchronous Priority Queue with EventMachine
59
70
  email:
60
71
  - ft.mikelewis@gmail.com