bogo 0.1.10 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +42 -1
- data/lib/bogo/priority_queue.rb +22 -0
- data/lib/bogo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cfa543d2f3739d18a3df6fa420a46697880cc16
|
4
|
+
data.tar.gz: 8875ff3c7b44e4d4f8a9ede11a6ceaf8024d1611
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce49932b19e5bbe4e99adfdeb73b578aca860e9ea96b88901f31f67f00f69ae0baaa8278d1e824fc4861d09f75135c130703ac2ddae113481f4e1e078626b907
|
7
|
+
data.tar.gz: f475c41d8176aa3b003756e55be3b209f9d0f605aac70286e72f11ef33461e7dff63270514e69192de25c7e8f09df3b9017a2ce9aff7a06be9024a03bc151d81
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -254,5 +254,46 @@ p data.dirty?
|
|
254
254
|
p data.data
|
255
255
|
```
|
256
256
|
|
257
|
-
##
|
257
|
+
## PriorityQueue
|
258
|
+
|
259
|
+
Queue items based on a given score. Score can be provided as a
|
260
|
+
numerical value, or as the result of a block. By default, the
|
261
|
+
item with the lowest score has the highest priority.
|
262
|
+
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
require 'bogo'
|
266
|
+
|
267
|
+
q = Bogo::PriorityQueue.new
|
268
|
+
q.push('a', 3)
|
269
|
+
q.push('b', 1)
|
270
|
+
puts q.pop
|
271
|
+
```
|
272
|
+
|
273
|
+
This will print "b" as it has the lowest score (highest priority). If
|
274
|
+
a high score is prefered to designate highest priority, that can be
|
275
|
+
set when initializing the instance of the queue:
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
require 'bogo'
|
279
|
+
|
280
|
+
q = Bogo::PriorityQueue.new(:highscore)
|
281
|
+
```
|
282
|
+
|
283
|
+
Scores can also be provided as blocks which will be evaluated prior
|
284
|
+
to `#pop`:
|
285
|
+
|
286
|
+
```ruby
|
287
|
+
require 'bogo'
|
288
|
+
|
289
|
+
q = Bogo::PriorityQueue.new(:highscore)
|
290
|
+
q.push('a'){ Time.now.to_i }
|
291
|
+
q.push('b', Time.now.to_i)
|
292
|
+
sleep(1)
|
293
|
+
puts q.pop
|
294
|
+
```
|
295
|
+
|
296
|
+
This will print "a" as its score will be higher when popped.
|
297
|
+
|
298
|
+
# Info
|
258
299
|
* Repository: https://github.com/spox/bogo
|
data/lib/bogo/priority_queue.rb
CHANGED
@@ -36,6 +36,28 @@ module Bogo
|
|
36
36
|
self
|
37
37
|
end
|
38
38
|
|
39
|
+
# Push multiple items onto the queue at once
|
40
|
+
#
|
41
|
+
# @param items [Array<Array<item, cost>>]
|
42
|
+
# @return [self]
|
43
|
+
def multi_push(items)
|
44
|
+
lock.synchronize do
|
45
|
+
items.each do |item_pair|
|
46
|
+
item, cost = item_pair
|
47
|
+
if(queue[item])
|
48
|
+
raise ArgumentError.new "Item already exists in queue. Items must be unique! (#{item})"
|
49
|
+
end
|
50
|
+
unless(cost.is_a?(Numeric) || cost.is_a?(Proc))
|
51
|
+
raise ArgumentError.new "Cost must be provided as parameter or proc! (item: #{item})"
|
52
|
+
end
|
53
|
+
@block_costs += 1 if cost.is_a?(Proc)
|
54
|
+
queue[item] = cost
|
55
|
+
end
|
56
|
+
sort!
|
57
|
+
end
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
39
61
|
# @return [Object, NilClass] item or nil if empty
|
40
62
|
def pop
|
41
63
|
lock.synchronize do
|
data/lib/bogo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bogo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|