philiprehberger-priority_queue 0.1.10 → 0.2.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 +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +84 -0
- data/lib/philiprehberger/priority_queue/version.rb +1 -1
- data/lib/philiprehberger/priority_queue.rb +51 -0
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4b651b4373f8d4da469ba23f030935a1cfe4acc6ee9b2876900d9bddd096f43d
|
|
4
|
+
data.tar.gz: 49c7c9ed171f003c839929b0af6875f0a0611e63a26b0c7c74584d0456518026
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1d5e7ea68204ea32eb3caeb06463f6782b28b44147029839a56ce75010e0cb3400fda69f32d97f4113eef247252aa5925808da025fef416119d8fce7a2c5001f
|
|
7
|
+
data.tar.gz: 5067adc0908c771d46959704071c658846d262e6a3d089c8cfe15fd919fa7be4e0d2b84b103b39f333707ea187a3a746245ef50a715e3d186d810393c5d30b1b
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,21 @@ 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.0] - 2026-04-03
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Include `Enumerable` module with `each` yielding `[item, priority]` pairs in priority order
|
|
14
|
+
- `push_many(items)` for batch pushing from array of hashes
|
|
15
|
+
- `peek_priority` to return just the top priority value
|
|
16
|
+
- `drain` to pop all items and return as array in priority order
|
|
17
|
+
- `delete(item)` to remove a specific item by value
|
|
18
|
+
- `priorities` to return sorted array of unique priority values
|
|
19
|
+
|
|
20
|
+
## [0.1.11] - 2026-03-31
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
- Add GitHub issue templates, dependabot config, and PR template
|
|
24
|
+
|
|
10
25
|
## [0.1.10] - 2026-03-31
|
|
11
26
|
|
|
12
27
|
### 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
|
|
|
@@ -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,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.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- philiprehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-04-04 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,
|
|
@@ -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://
|
|
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://
|
|
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
|