philiprehberger-priority_queue 0.1.10 → 0.2.1

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: e95bdf19e31db232c54ac6ee645aa4a9023fc87140d2be6310c8469e4a5440d2
4
- data.tar.gz: 944088fb4246573e985496a73fa8ca50a369da021274c050b8db926e3a6f7b4c
3
+ metadata.gz: 550864e1c3c7c28c4d978059acd56e73d64fd084e9263f46afd5fbf52a86671e
4
+ data.tar.gz: 766b98241695a100ba12122f06c6a7d4cada3c28685643f08fae483b667da0ce
5
5
  SHA512:
6
- metadata.gz: 5c21fb11db45c81cc229b54ac6a8589000543141859a47abe483be620825e7662ce5b89228b278927e5665e842443300421641a9e494d372d3921f43bbcaad5a
7
- data.tar.gz: 7efaf2fe7884442250b83e94e4c9e472f82b50e0ce7ee7ae8cc0f2390674cdd7827176de9c77baefd7df1a81b02ffca32260429545dbda04e9ea0566e0602db1
6
+ metadata.gz: a76b48855cd5782999ec79ae730efc4ebeaad9a02f78b4b1d981979d3cdf7b9543108d53bf710163d526b09273f655a9dc0bcf716c42c870c6678994d2a4b29d
7
+ data.tar.gz: d31237580572959f8cbf0943504b96205df6b97678a4385dcf5963bf069b52f8eac8cbd45d644be6779c7cca1d98ec039e6e507844888f4d83ad9e61e0d00c59
data/CHANGELOG.md CHANGED
@@ -7,6 +7,27 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.1] - 2026-04-15
11
+
12
+ ### Fixed
13
+ - Set gemspec authors to `Philip Rehberger` and email to `me@philiprehberger.com`
14
+ - Update `required_ruby_version` to `>= 3.1.0` to match gemspec template
15
+
16
+ ## [0.2.0] - 2026-04-03
17
+
18
+ ### Added
19
+ - Include `Enumerable` module with `each` yielding `[item, priority]` pairs in priority order
20
+ - `push_many(items)` for batch pushing from array of hashes
21
+ - `peek_priority` to return just the top priority value
22
+ - `drain` to pop all items and return as array in priority order
23
+ - `delete(item)` to remove a specific item by value
24
+ - `priorities` to return sorted array of unique priority values
25
+
26
+ ## [0.1.11] - 2026-03-31
27
+
28
+ ### Added
29
+ - Add GitHub issue templates, dependabot config, and PR template
30
+
10
31
  ## [0.1.10] - 2026-03-31
11
32
 
12
33
  ### Changed
data/README.md CHANGED
@@ -88,6 +88,84 @@ queue.change_priority("task_a", 1) # task_a is now highest priority
88
88
  queue.pop # => "task_a"
89
89
  ```
90
90
 
91
+ ### Batch Push
92
+
93
+ Add multiple items at once from an array of hashes.
94
+
95
+ ```ruby
96
+ queue = Philiprehberger::PriorityQueue::Queue.new
97
+ queue.push_many([
98
+ { item: "email", priority: 2 },
99
+ { item: "backup", priority: 5 },
100
+ { item: "alert", priority: 1 }
101
+ ])
102
+
103
+ queue.pop # => "alert"
104
+ ```
105
+
106
+ ### Peek Priority
107
+
108
+ Return just the top priority value without the item.
109
+
110
+ ```ruby
111
+ queue = Philiprehberger::PriorityQueue::Queue.new
112
+ queue.push("task", priority: 7)
113
+ queue.peek_priority # => 7
114
+ ```
115
+
116
+ ### Drain
117
+
118
+ Pop all items and return them as an array in priority order.
119
+
120
+ ```ruby
121
+ queue = Philiprehberger::PriorityQueue::Queue.new
122
+ queue.push("c", priority: 3)
123
+ queue.push("a", priority: 1)
124
+ queue.push("b", priority: 2)
125
+
126
+ queue.drain # => ["a", "b", "c"]
127
+ queue.empty? # => true
128
+ ```
129
+
130
+ ### Delete
131
+
132
+ Remove a specific item by value.
133
+
134
+ ```ruby
135
+ queue = Philiprehberger::PriorityQueue::Queue.new
136
+ queue.push("a", priority: 1)
137
+ queue.push("b", priority: 2)
138
+
139
+ queue.delete("a") # => "a"
140
+ queue.size # => 1
141
+ ```
142
+
143
+ ### Priorities
144
+
145
+ Return a sorted array of unique priority values currently in the queue.
146
+
147
+ ```ruby
148
+ queue = Philiprehberger::PriorityQueue::Queue.new
149
+ queue.push("a", priority: 3)
150
+ queue.push("b", priority: 1)
151
+ queue.push("c", priority: 3)
152
+
153
+ queue.priorities # => [1, 3]
154
+ ```
155
+
156
+ ### Enumerable
157
+
158
+ The queue includes `Enumerable`, yielding `[item, priority]` pairs in priority order without modifying the queue.
159
+
160
+ ```ruby
161
+ queue = Philiprehberger::PriorityQueue::Queue.new
162
+ queue.push("x", priority: 10)
163
+ queue.push("y", priority: 5)
164
+
165
+ queue.map { |item, priority| "#{item}:#{priority}" } # => ["y:5", "x:10"]
166
+ queue.select { |_item, priority| priority > 7 } # => [["x", 10]]
167
+ ```
168
+
91
169
  ### Merging Queues
92
170
 
93
171
  Combine two queues into a new queue. The originals are unchanged.
@@ -121,6 +199,12 @@ merged.size # => 3
121
199
  | `#include?(item)` | Return `true` if the item is in the queue |
122
200
  | `#clear` | Remove all items from the queue; returns `self` |
123
201
  | `#merge(other)` | Return a new queue containing items from both queues |
202
+ | `#push_many(items)` | Batch push from array of hashes `[{ item: x, priority: n }, ...]`; returns `self` |
203
+ | `#peek_priority` | Return just the top priority value; returns `nil` when empty |
204
+ | `#drain` | Pop all items and return as array in priority order; empties the queue |
205
+ | `#delete(item)` | Remove a specific item by value; returns the item or `nil` |
206
+ | `#priorities` | Return sorted array of unique priority values in the queue |
207
+ | `#each` | Yield `[item, priority]` pairs in priority order (Enumerable) |
124
208
 
125
209
  ## Development
126
210
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module PriorityQueue
5
- VERSION = '0.1.10'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  end
@@ -5,6 +5,8 @@ require_relative 'priority_queue/version'
5
5
  module Philiprehberger
6
6
  module PriorityQueue
7
7
  class Queue
8
+ include Enumerable
9
+
8
10
  attr_reader :size
9
11
 
10
12
  def initialize(mode: :min, &comparator)
@@ -90,6 +92,55 @@ module Philiprehberger
90
92
  self
91
93
  end
92
94
 
95
+ def each(&block)
96
+ return enum_for(:each) unless block
97
+
98
+ sorted = @heap.sort { |a, b| compare_entries(a, b) }
99
+ sorted.each { |entry| block.call(entry[2], entry[0]) }
100
+ self
101
+ end
102
+
103
+ def push_many(items)
104
+ items.each { |h| push(h[:item], priority: h[:priority]) }
105
+ self
106
+ end
107
+
108
+ def peek_priority
109
+ return nil if empty?
110
+
111
+ @heap[0][0]
112
+ end
113
+
114
+ def drain
115
+ result = []
116
+ result << pop until empty?
117
+ result
118
+ end
119
+
120
+ def delete(item)
121
+ idx = @item_index[item]
122
+ return nil if idx.nil?
123
+
124
+ if idx == @size - 1
125
+ entry = @heap.pop
126
+ @size -= 1
127
+ @item_index.delete(item)
128
+ return entry[2]
129
+ end
130
+
131
+ swap(idx, @size - 1)
132
+ entry = @heap.pop
133
+ @size -= 1
134
+ @item_index.delete(item)
135
+ bubble_up(idx)
136
+ bubble_down(idx)
137
+ entry[2]
138
+ end
139
+
140
+ def priorities
141
+ @heap.map { |entry| entry[0] }.uniq.sort
142
+ end
143
+
93
144
  def merge(other)
94
145
  merged = self.class.new(&@comparator)
95
146
  @heap.each { |entry| merged.push(entry[2], priority: entry[0]) }
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-priority_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
- - philiprehberger
7
+ - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-03-31 00:00:00.000000000 Z
11
+ date: 2026-04-15 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,
15
15
  and FIFO tie-breaking.
16
16
  email:
17
- - philiprehberger@users.noreply.github.com
17
+ - me@philiprehberger.com
18
18
  executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
@@ -24,11 +24,11 @@ files:
24
24
  - README.md
25
25
  - lib/philiprehberger/priority_queue.rb
26
26
  - lib/philiprehberger/priority_queue/version.rb
27
- homepage: https://github.com/philiprehberger/rb-priority-queue
27
+ homepage: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-priority_queue
28
28
  licenses:
29
29
  - MIT
30
30
  metadata:
31
- homepage_uri: https://github.com/philiprehberger/rb-priority-queue
31
+ homepage_uri: https://philiprehberger.com/open-source-packages/ruby/philiprehberger-priority_queue
32
32
  source_code_uri: https://github.com/philiprehberger/rb-priority-queue
33
33
  changelog_uri: https://github.com/philiprehberger/rb-priority-queue/blob/main/CHANGELOG.md
34
34
  bug_tracker_uri: https://github.com/philiprehberger/rb-priority-queue/issues
@@ -41,7 +41,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: '3.1'
44
+ version: 3.1.0
45
45
  required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="