bogo 0.1.10 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4b61b319c9395e4e644f827551a4af16deb76e9
4
- data.tar.gz: dc1442544264f65268c3f7ef9125c9baeb763d00
3
+ metadata.gz: 1cfa543d2f3739d18a3df6fa420a46697880cc16
4
+ data.tar.gz: 8875ff3c7b44e4d4f8a9ede11a6ceaf8024d1611
5
5
  SHA512:
6
- metadata.gz: 5170908a533fab6acc5b7d757b24a0e59155913965bffb7bdb416fa4ce9e2ac87efa3739881c87e9cc5746ea152583e8f6ce21a6a13a49819f47f42d9c0a00f5
7
- data.tar.gz: abd76ee307dc9fdc7761b7d5ed31d783ae7ce6208d5a712bdf134cbc9eee43464e9d40d75d3392c2909fb6eb8fdc215dd7d73792063998bf84c840bfccfd064f
6
+ metadata.gz: ce49932b19e5bbe4e99adfdeb73b578aca860e9ea96b88901f31f67f00f69ae0baaa8278d1e824fc4861d09f75135c130703ac2ddae113481f4e1e078626b907
7
+ data.tar.gz: f475c41d8176aa3b003756e55be3b209f9d0f605aac70286e72f11ef33461e7dff63270514e69192de25c7e8f09df3b9017a2ce9aff7a06be9024a03bc151d81
@@ -1,3 +1,6 @@
1
+ ## v0.1.12
2
+ * Support multiple item push on `PriorityQueue`
3
+
1
4
  ## v0.1.10
2
5
  * Add `Lazy#always_clean!` to remove attribute state
3
6
  * Add `PriorityQueue`
data/README.md CHANGED
@@ -254,5 +254,46 @@ p data.dirty?
254
254
  p data.data
255
255
  ```
256
256
 
257
- ## Info
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
@@ -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
@@ -1,4 +1,4 @@
1
1
  module Bogo
2
2
  # Current library version
3
- VERSION = Gem::Version.new('0.1.10')
3
+ VERSION = Gem::Version.new('0.1.12')
4
4
  end
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.10
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-02-26 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie