min_max 0.1.6 → 0.1.7

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
  SHA256:
3
- metadata.gz: 012d1e5c570db138f41f8ac8556dde89598878d15fc9176a7006bdd33e5deaf5
4
- data.tar.gz: 70a8c3a7bb6afdaf23260ccd56c87acd1bd85e49d40d9231fbb316bf6beb4917
3
+ metadata.gz: 2b43d258b5b9ccddc81016bb4c69c186df448ab7c51ae712f136ceed6218a145
4
+ data.tar.gz: 8c22dd5c5ac6ced73a789dc6813f62bc51168e3e48e2e2ca1b689187be7af8ac
5
5
  SHA512:
6
- metadata.gz: bc3d399e2eae53e244c0bb3a5d502cf725e3b4d4b0d941e1c96d9f0ceeee367a4688ec191116c75d94bed5bbc4470c59626e53da13c85afb24fd44df1f6668bc
7
- data.tar.gz: d98bef37722feef1534e85f709f95dcaf314dc5f9013b32e10d667c653b97c3274e8f210fa42d2fdeb6f1fdb63c9f4a62da2d1cfd44ae2178b1439d79c56e8a8
6
+ metadata.gz: b02f4bdcbe8d60e8e2dfba61218a7958e8d97dd54a8939a0befcd363c796e049f11b7f7f58e014c8f8ead40773ea4ad9695fc69a89022e0bec6ac738ae4968ac
7
+ data.tar.gz: b55fc832a26ce35e955e275ea812b64de15f541fea714a57e107f078d287e3436a76a041a6152863ea1b9680282c0864096cb7784f1aaed525cf5b253b989465
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # MinMax Heap
2
2
 
3
- The MinMax Heap gem provides a high-performance minmax heap implementation for Ruby, written in Rust.
3
+ The MinMax Heap gem provides a high-performance minmax heap implementation for Ruby, written in Rust.
4
4
  The gem wraps the excellent [min-max-heap-rs](https://github.com/tov/min-max-heap-rs) Rust library.
5
- It allows for the creation of a min-max-heap and supporting operations like pushing and popping multiple items, iterating over heap items, and converting heaps to arrays.
5
+ It allows for the creation of a min-max-heap and supporting operations like pushing and popping multiple items, iterating over heap items, and converting heaps to arrays.
6
6
 
7
7
  ## Features
8
8
 
@@ -13,9 +13,9 @@ It allows for the creation of a min-max-heap and supporting operations like push
13
13
  - Convert heap to array with `#to_a`, `#to_a_asc` and `#to_a_desc`.
14
14
 
15
15
  ## Prequisites
16
- - You must have a working Rust compiler installed on your system.
16
+ - You must have a working Rust compiler installed on your system.
17
17
  ```bash
18
- curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs |
18
+ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs |
19
19
  ```
20
20
 
21
21
  ## Installation
@@ -79,6 +79,21 @@ heap.to_a_desc # => [5, 3, 1]
79
79
  heap.to_a # => Heap order
80
80
  ```
81
81
 
82
+ ## Iterate in order
83
+ ```ruby
84
+ heap.each_asc #<Enumerator: ...>
85
+ heap.each_desc #<Enumerator: ...>
86
+ ```
87
+
88
+ ## Peek at min and max items
89
+ ```ruby
90
+ heap.peek_min # => 1
91
+ heap.first # => 1
92
+
93
+ heap.peek_max # => 1
94
+ heap.last # => 1
95
+ ```
96
+
82
97
  ## Size
83
98
  ```ruby
84
99
  heap.size # => 4
@@ -110,4 +125,3 @@ You can run the `benchmarks/benchmarks.rb` file inside this repository for compa
110
125
  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
126
  Some options are faster at pushing individual items, but the difference is within the same order of magnitude.
112
127
  Batch pushing to min-max also significantly increases insert speed.
113
-
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MinMax
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  end
data/lib/min_max.rb CHANGED
@@ -16,7 +16,7 @@ class MinMax
16
16
  self._new.tap{|s|
17
17
  s.instance_eval{
18
18
  @priority_blk = (blk || proc{|x| x.respond_to?(:priority) ? x.priority : x.to_i })
19
- @storage = Hash.new{|h,k| h[k] = [0, nil] }
19
+ @storage = Hash.new{|h,k| h[k] = [0, nil] }.compare_by_identity
20
20
  }
21
21
  s.push(*args)
22
22
  }
@@ -24,13 +24,13 @@ class MinMax
24
24
 
25
25
  def push(*args)
26
26
  mapped = args.map do |a|
27
- hash = a.hash
28
- entry = self.storage[hash]
27
+ object_id = a.object_id
28
+ entry = self.storage[object_id]
29
29
  entry[0] += 1
30
30
  entry[1] ||= a
31
31
  [
32
32
  (self.priority_blk.call(a) rescue 0),
33
- hash
33
+ object_id
34
34
  ]
35
35
  end
36
36
  _push(mapped)
@@ -81,18 +81,30 @@ class MinMax
81
81
  end
82
82
 
83
83
  def count(val)
84
- counts.has_key?(val.hash) ? counts[val.hash] : 0
84
+ storage.has_key?(val.object_id) ? storage[val.object_id].first : 0
85
85
  end
86
86
 
87
87
  def contains?(val)
88
- counts.has_key?(val.hash) && counts[val.hash] > 0
88
+ storage.has_key?(val.object_id)
89
89
  end
90
90
 
91
91
  def to_a_asc
92
+ return to_enum(:to_a_asc) unless block_given?
92
93
  _to_a_asc.map{|p| retrieve(p, false) }
93
94
  end
94
95
 
95
96
  def to_a_desc
97
+ return to_enum(:to_a_desc) unless block_given?
98
+ _to_a_desc.map{|p| retrieve(p, false) }
99
+ end
100
+
101
+ def each_asc
102
+ return to_enum(:each_asc) unless block_given?
103
+ _to_a_asc.each{|p| yield retrieve(p, false) }
104
+ end
105
+
106
+ def each_desc
107
+ return to_enum(:each_desc) unless block_given?
96
108
  _to_a_desc.map{|p| retrieve(p, false) }
97
109
  end
98
110
 
@@ -101,11 +113,10 @@ class MinMax
101
113
  end
102
114
 
103
115
  private
104
- def retrieve(hash, remove=true)
105
- entry = self.storage[hash]
106
- self.storage.delete(hash) if remove && (entry[0] -= 1) == 0
116
+ def retrieve(object_id, remove=true)
117
+ entry = self.storage[object_id]
118
+ self.storage.delete(object_id) if remove && (entry[0] -= 1) == 0
107
119
  entry[1]
108
120
  end
109
121
 
110
122
  end
111
-
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: min_max
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wouter Coppieters
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-02-25 00:00:00.000000000 Z
10
+ date: 2025-01-03 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rb_sys
@@ -79,7 +78,6 @@ licenses:
79
78
  metadata:
80
79
  allowed_push_host: https://rubygems.org
81
80
  homepage_uri: https://github.com/wouterken/min_max
82
- post_install_message:
83
81
  rdoc_options: []
84
82
  require_paths:
85
83
  - lib
@@ -94,8 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
92
  - !ruby/object:Gem::Version
95
93
  version: 3.3.11
96
94
  requirements: []
97
- rubygems_version: 3.5.6
98
- signing_key:
95
+ rubygems_version: 3.6.2
99
96
  specification_version: 4
100
97
  summary: A min max heap extension for Ruby
101
98
  test_files: []