bogo 0.1.8 → 0.1.10
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 +4 -0
- data/bogo.gemspec +4 -2
- data/lib/bogo.rb +1 -0
- data/lib/bogo/lazy.rb +9 -0
- data/lib/bogo/priority_queue.rb +80 -0
- data/lib/bogo/version.rb +1 -1
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4b61b319c9395e4e644f827551a4af16deb76e9
|
4
|
+
data.tar.gz: dc1442544264f65268c3f7ef9125c9baeb763d00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5170908a533fab6acc5b7d757b24a0e59155913965bffb7bdb416fa4ce9e2ac87efa3739881c87e9cc5746ea152583e8f6ce21a6a13a49819f47f42d9c0a00f5
|
7
|
+
data.tar.gz: abd76ee307dc9fdc7761b7d5ed31d783ae7ce6208d5a712bdf134cbc9eee43464e9d40d75d3392c2909fb6eb8fdc215dd7d73792063998bf84c840bfccfd064f
|
data/CHANGELOG.md
CHANGED
data/bogo.gemspec
CHANGED
@@ -10,7 +10,9 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.description = 'Helper libraries'
|
11
11
|
s.require_path = 'lib'
|
12
12
|
s.license = 'Apache 2.0'
|
13
|
-
s.
|
14
|
-
s.
|
13
|
+
s.add_runtime_dependency 'hashie'
|
14
|
+
s.add_runtime_dependency 'multi_json'
|
15
|
+
s.add_development_dependency 'pry'
|
16
|
+
s.add_development_dependency 'minitest'
|
15
17
|
s.files = Dir['lib/**/*'] + %w(bogo.gemspec README.md CHANGELOG.md CONTRIBUTING.md LICENSE)
|
16
18
|
end
|
data/lib/bogo.rb
CHANGED
data/lib/bogo/lazy.rb
CHANGED
@@ -103,6 +103,15 @@ module Bogo
|
|
103
103
|
# Class methods for laziness
|
104
104
|
module ClassMethods
|
105
105
|
|
106
|
+
# Disable dirty state
|
107
|
+
def always_clean!
|
108
|
+
self.class_eval do
|
109
|
+
def dirty?; false; end
|
110
|
+
def valid_state; self; end
|
111
|
+
alias_method :dirty, :data
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
106
115
|
# Add new attributes to class
|
107
116
|
#
|
108
117
|
# @param name [String]
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'bogo'
|
2
|
+
|
3
|
+
module Bogo
|
4
|
+
# Specialized priority based queue
|
5
|
+
# @note does not allow duplicate objects to be queued
|
6
|
+
class PriorityQueue
|
7
|
+
|
8
|
+
# Create a new priority queue
|
9
|
+
#
|
10
|
+
# @return [self]
|
11
|
+
def initialize(*args)
|
12
|
+
@lock = Mutex.new
|
13
|
+
@queue = Hash.new
|
14
|
+
@block_costs = 0
|
15
|
+
@reverse_sort = args.include?(:highscore)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Push new item to the queue
|
19
|
+
#
|
20
|
+
# @param item [Object]
|
21
|
+
# @param cost [Float]
|
22
|
+
# @yield provide cost via proc
|
23
|
+
# @return [self]
|
24
|
+
def push(item, cost=nil, &block)
|
25
|
+
lock.synchronize do
|
26
|
+
if(queue[item])
|
27
|
+
raise ArgumentError.new "Item already exists in queue. Items must be unique! (#{item})"
|
28
|
+
end
|
29
|
+
unless(cost || block_given?)
|
30
|
+
raise ArgumentError.new 'Cost must be provided as parameter or block!'
|
31
|
+
end
|
32
|
+
@block_costs += 1 if cost.nil?
|
33
|
+
queue[item] = cost || block
|
34
|
+
sort!
|
35
|
+
end
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Object, NilClass] item or nil if empty
|
40
|
+
def pop
|
41
|
+
lock.synchronize do
|
42
|
+
sort! if @block_costs > 0
|
43
|
+
item, score = queue.first
|
44
|
+
@block_costs -= 1 if score.respond_to?(:call)
|
45
|
+
queue.delete(item)
|
46
|
+
item
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [Integer] current size of queue
|
51
|
+
def size
|
52
|
+
lock.synchronize do
|
53
|
+
queue.size
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def empty?
|
58
|
+
size == 0
|
59
|
+
end
|
60
|
+
|
61
|
+
# Sort the queue based on cost
|
62
|
+
def sort!
|
63
|
+
queue.replace(
|
64
|
+
Hash[
|
65
|
+
queue.sort do |x,y|
|
66
|
+
x,y = y,x if @reverse_score
|
67
|
+
(x.last.respond_to?(:call) ? x.last.call : x.last).to_f <=>
|
68
|
+
(y.last.respond_to?(:call) ? y.last.call : y.last).to_f
|
69
|
+
end
|
70
|
+
]
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
|
76
|
+
attr_reader :queue, :lock
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
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.10
|
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-
|
11
|
+
date: 2015-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
description: Helper libraries
|
42
70
|
email: code@chrisroberts.org
|
43
71
|
executables: []
|
@@ -54,6 +82,7 @@ files:
|
|
54
82
|
- lib/bogo/constants.rb
|
55
83
|
- lib/bogo/lazy.rb
|
56
84
|
- lib/bogo/memoization.rb
|
85
|
+
- lib/bogo/priority_queue.rb
|
57
86
|
- lib/bogo/smash.rb
|
58
87
|
- lib/bogo/utility.rb
|
59
88
|
- lib/bogo/version.rb
|