philiprehberger-priority_queue 0.3.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81ecb740f0ef3871c9662a0bf53f627784e039ad638c3e772fae8cbf9664ec52
4
- data.tar.gz: 8233105bc0db893faa7a53696c2283165d4bc6b5f10f86c02771a1101ddb1a76
3
+ metadata.gz: 4fca90d36f6f8f63c1b89c458854eb61af26682c7dcf66aaa1da9023f8cba5ab
4
+ data.tar.gz: a91b25347569be4e586c4c0ec1ce8c6d77b77d652df16ba39bce020c4560458c
5
5
  SHA512:
6
- metadata.gz: 47adacec5e5bd08c5cd6dd2a45500f5e5901372df0171b1aaceb6aefe0e162fc4504e67b345a067ee818685333ca158eb3440a7fe9173c4716e2ed18f1586537
7
- data.tar.gz: 53380b7734ed65fcbf26b80644caef0268b2c187182a8168792aba4fa855433a4dcca840060a9f673972cb4250fc3e1cc996e75474bfcb8a07ac35168d03da59
6
+ metadata.gz: b280a1927ced0e7ac6706af14e03a319111c962d8d67cfe7d93ceab3ce05761a187698ec0b8d8d175c3fdb12f52fecf35640de99410242dee37a583e5bdf1322
7
+ data.tar.gz: c52cb6190358eab1880f423df857c8165cadca1b98255c1c062a2c6bdfe54c027d0c29a492f9baf1481d78078f354c1190a7f48dbdf0d570ee5ae38d2fa7f57b
data/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.0] - 2026-04-21
11
+
12
+ ### Added
13
+ - `Queue#bulk_push` — insert multiple items from a hash
14
+ - `Queue#priority_of` — lookup priority by item
15
+ - `Queue#find` — priority-ordered search
16
+
17
+ ### Fixed
18
+ - `bug_report.yml` — require Ruby version; add Gem version input per guide
19
+
10
20
  ## [0.3.0] - 2026-04-15
11
21
 
12
22
  ### Added
data/README.md CHANGED
@@ -103,6 +103,18 @@ queue.push_many([
103
103
  queue.pop # => "alert"
104
104
  ```
105
105
 
106
+ ### Bulk Insertion
107
+
108
+ Insert multiple items from a hash, look up the priority of an item, and search in priority order.
109
+
110
+ ```ruby
111
+ queue = Philiprehberger::PriorityQueue::Queue.new
112
+ queue.bulk_push("email" => 2, "backup" => 5, "alert" => 1)
113
+
114
+ queue.priority_of("backup") # => 5
115
+ queue.find { |_item, priority| priority > 1 } # => ["email", 2]
116
+ ```
117
+
106
118
  ### Peek Priority
107
119
 
108
120
  Return just the top priority value without the item.
@@ -221,6 +233,9 @@ merged.size # => 3
221
233
  | `#delete(item)` | Remove a specific item by value; returns the item or `nil` |
222
234
  | `#priorities` | Return sorted array of unique priority values in the queue |
223
235
  | `#each` | Yield `[item, priority]` pairs in priority order (Enumerable) |
236
+ | `#bulk_push(items_hash)` | Insert multiple items from a hash `{ item => priority, ... }`; returns `self`; raises `ArgumentError` for non-Hash input |
237
+ | `#priority_of(item)` | Return the priority of the first matching item (O(n) linear scan); returns `nil` if not present |
238
+ | `#find(&block)` | Yield `[item, priority]` pairs in priority order and return the first pair for which the block is truthy; returns `nil` if none match, or an `Enumerator` when no block is given |
224
239
 
225
240
  ## Development
226
241
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module PriorityQueue
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
@@ -153,6 +153,48 @@ module Philiprehberger
153
153
  @heap.map { |entry| entry[0] }.uniq.sort
154
154
  end
155
155
 
156
+ # Insert multiple items from a hash in one call.
157
+ #
158
+ # @param items_hash [Hash] a mapping of item to priority
159
+ # @return [self] the queue, for chaining
160
+ # @raise [ArgumentError] if +items_hash+ is not a Hash
161
+ def bulk_push(items_hash)
162
+ raise ArgumentError, "Expected a Hash, got #{items_hash.class}" unless items_hash.is_a?(Hash)
163
+
164
+ items_hash.each { |item, priority| push(item, priority: priority) }
165
+ self
166
+ end
167
+
168
+ # Look up the priority of the first matching item.
169
+ #
170
+ # Uses a linear scan (O(n)) over internal storage and returns
171
+ # the priority associated with the first entry whose item is
172
+ # +==+ to the argument.
173
+ #
174
+ # @param item [Object] the item to look up
175
+ # @return [Object, nil] the priority of the first match, or +nil+ when not present
176
+ def priority_of(item)
177
+ entry = @heap.find { |e| e[2] == item }
178
+ entry.nil? ? nil : entry[0]
179
+ end
180
+
181
+ # Find the first [item, priority] pair in priority order for which
182
+ # the block returns a truthy value.
183
+ #
184
+ # @yield [item, priority] pairs in priority order
185
+ # @yieldparam item [Object] the item
186
+ # @yieldparam priority [Object] the priority
187
+ # @return [Array, nil] the first matching +[item, priority]+ pair, or +nil+ when none match
188
+ # @return [Enumerator] if no block is given
189
+ def find(&block)
190
+ return enum_for(:find) unless block
191
+
192
+ each do |item, priority|
193
+ return [item, priority] if block.call(item, priority)
194
+ end
195
+ nil
196
+ end
197
+
156
198
  def merge(other)
157
199
  merged = self.class.new(&@comparator)
158
200
  @heap.each { |entry| merged.push(entry[2], priority: entry[0]) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-priority_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-15 00:00:00.000000000 Z
11
+ date: 2026-04-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A binary heap-based priority queue supporting min-heap, max-heap, and
14
14
  custom comparator modes. Features O(log n) push/pop, priority changes, merge operations,