min_max 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/benchmarks/benchmarks.rb +2 -2
- data/lib/min_max/version.rb +1 -1
- data/lib/min_max.rb +19 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 012d1e5c570db138f41f8ac8556dde89598878d15fc9176a7006bdd33e5deaf5
|
4
|
+
data.tar.gz: 70a8c3a7bb6afdaf23260ccd56c87acd1bd85e49d40d9231fbb316bf6beb4917
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc3d399e2eae53e244c0bb3a5d502cf725e3b4d4b0d941e1c96d9f0ceeee367a4688ec191116c75d94bed5bbc4470c59626e53da13c85afb24fd44df1f6668bc
|
7
|
+
data.tar.gz: d98bef37722feef1534e85f709f95dcaf314dc5f9013b32e10d667c653b97c3274e8f210fa42d2fdeb6f1fdb63c9f4a62da2d1cfd44ae2178b1439d79c56e8a8
|
data/README.md
CHANGED
@@ -100,3 +100,14 @@ heap.count(item)
|
|
100
100
|
heap.contains?(item)
|
101
101
|
```
|
102
102
|
|
103
|
+
## Performance
|
104
|
+
You can run the `benchmarks/benchmarks.rb` file inside this repository for comparison to other popular heap libraries:
|
105
|
+
* [rb_heap](https://github.com/florian/rb_heap)
|
106
|
+
* [algorithms](https://github.com/kanwei/algorithms)
|
107
|
+
* [ruby-heap](https://github.com/general-CbIC/ruby-heap)
|
108
|
+
* [pqueue](https://github.com/rubyworks/pqueue)
|
109
|
+
|
110
|
+
min-max should be the fastest to pop from a large heap, often by a significant margin, while also offering both min and max operations from a single heap.
|
111
|
+
Some options are faster at pushing individual items, but the difference is within the same order of magnitude.
|
112
|
+
Batch pushing to min-max also significantly increases insert speed.
|
113
|
+
|
data/benchmarks/benchmarks.rb
CHANGED
@@ -73,13 +73,13 @@ pqueue = PQueue.new()
|
|
73
73
|
mm_hp = MinMax[]
|
74
74
|
|
75
75
|
Benchmark.bm do |x|
|
76
|
-
x.report("
|
76
|
+
x.report("push_pqueue_heap"){ data.each{|d| pqueue.push(d) } }
|
77
77
|
x.report("push_mm_heap"){ data.each{|d| mm_hp.push(d) } }
|
78
78
|
x.report("push_mm_heap_batches"){ mm_hp = MinMax[]; data.each_slice(1000){|d| mm_hp.push(*d) } }
|
79
79
|
end
|
80
80
|
|
81
81
|
Benchmark.bm do |x|
|
82
|
-
x.report("
|
82
|
+
x.report("pop_pqueue_heap"){ 100_000.times{|d| pqueue.pop } }
|
83
83
|
x.report("pop_mm_heap"){ 100_000.times{|d| mm_hp.pop_min } }
|
84
84
|
x.report("pop_mm_heap_batches"){ 1000.times{|d| mm_hp.pop_min(100) } }
|
85
85
|
end
|
data/lib/min_max/version.rb
CHANGED
data/lib/min_max.rb
CHANGED
@@ -6,7 +6,7 @@ require_relative "min_max/min_max"
|
|
6
6
|
class MinMax
|
7
7
|
class Error < StandardError; end
|
8
8
|
|
9
|
-
attr_reader :priority_blk, :storage
|
9
|
+
attr_reader :priority_blk, :storage
|
10
10
|
|
11
11
|
def self.[](*args, &blk)
|
12
12
|
new(*args, &blk)
|
@@ -17,7 +17,6 @@ class MinMax
|
|
17
17
|
s.instance_eval{
|
18
18
|
@priority_blk = (blk || proc{|x| x.respond_to?(:priority) ? x.priority : x.to_i })
|
19
19
|
@storage = Hash.new{|h,k| h[k] = [0, nil] }
|
20
|
-
@mtx ||= Mutex.new
|
21
20
|
}
|
22
21
|
s.push(*args)
|
23
22
|
}
|
@@ -51,19 +50,27 @@ class MinMax
|
|
51
50
|
popped.kind_of?(Array) ? popped.map{|p| retrieve(p) } : retrieve(popped)
|
52
51
|
end
|
53
52
|
|
54
|
-
def peek_min
|
55
|
-
|
56
|
-
peeked.kind_of?(Array) ? peeked.map{|p| retrieve(p, false) } : retrieve(popped, false)
|
53
|
+
def peek_min
|
54
|
+
retrieve(_peek_min, false)
|
57
55
|
end
|
58
56
|
|
59
|
-
def peek_max
|
60
|
-
|
61
|
-
|
57
|
+
def peek_max
|
58
|
+
retrieve(_peek_max, false)
|
59
|
+
end
|
60
|
+
|
61
|
+
def first
|
62
|
+
peek_min
|
63
|
+
end
|
64
|
+
|
65
|
+
def last
|
66
|
+
peek_max
|
62
67
|
end
|
63
68
|
|
64
69
|
def each(*args, &blk)
|
65
70
|
if block_given?
|
66
|
-
|
71
|
+
_each(*args) do |p|
|
72
|
+
blk[retrieve(p, false)]
|
73
|
+
end
|
67
74
|
else
|
68
75
|
to_enum(:each, *args)
|
69
76
|
end
|
@@ -82,15 +89,15 @@ class MinMax
|
|
82
89
|
end
|
83
90
|
|
84
91
|
def to_a_asc
|
85
|
-
|
92
|
+
_to_a_asc.map{|p| retrieve(p, false) }
|
86
93
|
end
|
87
94
|
|
88
95
|
def to_a_desc
|
89
|
-
|
96
|
+
_to_a_desc.map{|p| retrieve(p, false) }
|
90
97
|
end
|
91
98
|
|
92
99
|
def inspect
|
93
|
-
"MinMax[#{
|
100
|
+
"MinMax[#{each.first(10).map(&:to_s).join(", ")}#{size > 10 ? ", ..." : ""}]"
|
94
101
|
end
|
95
102
|
|
96
103
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: min_max
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wouter Coppieters
|
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: 3.3.11
|
96
96
|
requirements: []
|
97
|
-
rubygems_version: 3.
|
97
|
+
rubygems_version: 3.5.6
|
98
98
|
signing_key:
|
99
99
|
specification_version: 4
|
100
100
|
summary: A min max heap extension for Ruby
|