philiprehberger-priority_queue 0.1.7 → 0.1.10
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 +17 -0
- data/README.md +86 -34
- data/lib/philiprehberger/priority_queue/version.rb +1 -1
- 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: e95bdf19e31db232c54ac6ee645aa4a9023fc87140d2be6310c8469e4a5440d2
|
|
4
|
+
data.tar.gz: 944088fb4246573e985496a73fa8ca50a369da021274c050b8db926e3a6f7b4c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5c21fb11db45c81cc229b54ac6a8589000543141859a47abe483be620825e7662ce5b89228b278927e5665e842443300421641a9e494d372d3921f43bbcaad5a
|
|
7
|
+
data.tar.gz: 7efaf2fe7884442250b83e94e4c9e472f82b50e0ce7ee7ae8cc0f2390674cdd7827176de9c77baefd7df1a81b02ffca32260429545dbda04e9ea0566e0602db1
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,23 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.1.10] - 2026-03-31
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
- Standardize README badges, support section, and license format
|
|
14
|
+
|
|
15
|
+
## [0.1.9] - 2026-03-26
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
- Add Sponsor badge to README
|
|
19
|
+
- Fix License section format
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## [0.1.8] - 2026-03-24
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
- Add Usage subsections and expand API table in README
|
|
26
|
+
|
|
10
27
|
## [0.1.7] - 2026-03-24
|
|
11
28
|
|
|
12
29
|
### Fixed
|
data/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/philiprehberger/rb-priority-queue/actions/workflows/ci.yml)
|
|
4
4
|
[](https://rubygems.org/gems/philiprehberger-priority_queue)
|
|
5
|
-
[](https://github.com/philiprehberger/rb-priority-queue/commits/main)
|
|
6
6
|
|
|
7
7
|
Binary heap priority queue with min/max modes, custom comparators, and priority updates
|
|
8
8
|
|
|
@@ -31,45 +31,78 @@ require "philiprehberger/priority_queue"
|
|
|
31
31
|
|
|
32
32
|
# Min-heap (default) - lowest priority first
|
|
33
33
|
queue = Philiprehberger::PriorityQueue::Queue.new
|
|
34
|
-
queue.push(
|
|
35
|
-
queue.push(
|
|
36
|
-
queue.push(
|
|
34
|
+
queue.push("low", priority: 1)
|
|
35
|
+
queue.push("high", priority: 10)
|
|
36
|
+
queue.push("mid", priority: 5)
|
|
37
37
|
|
|
38
38
|
queue.pop # => "low"
|
|
39
39
|
queue.pop # => "mid"
|
|
40
40
|
queue.pop # => "high"
|
|
41
41
|
|
|
42
|
-
#
|
|
42
|
+
# Peek without removing
|
|
43
|
+
queue.push("item", priority: 1)
|
|
44
|
+
queue.peek # => "item"
|
|
45
|
+
queue.size # => 1
|
|
46
|
+
queue.empty? # => false
|
|
47
|
+
|
|
48
|
+
# Shovel operator for hash-based push
|
|
49
|
+
queue << { item: "task", priority: 3 }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Max-Heap Mode
|
|
53
|
+
|
|
54
|
+
Pass `mode: :max` to pop the highest priority first.
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
43
57
|
queue = Philiprehberger::PriorityQueue::Queue.new(mode: :max)
|
|
44
|
-
queue.push(
|
|
45
|
-
queue.push(
|
|
46
|
-
queue.
|
|
58
|
+
queue.push("task_a", priority: 3)
|
|
59
|
+
queue.push("task_b", priority: 7)
|
|
60
|
+
queue.push("task_c", priority: 1)
|
|
61
|
+
|
|
62
|
+
queue.pop # => "task_b"
|
|
63
|
+
queue.pop # => "task_a"
|
|
64
|
+
queue.pop # => "task_c"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Custom Comparator
|
|
47
68
|
|
|
48
|
-
|
|
69
|
+
Supply a block to define your own ordering. The block receives two priorities and must return `-1`, `0`, or `1`.
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
49
72
|
queue = Philiprehberger::PriorityQueue::Queue.new { |a, b| a.length <=> b.length }
|
|
50
|
-
queue.push(
|
|
51
|
-
queue.push(
|
|
52
|
-
queue.pop
|
|
73
|
+
queue.push("short", priority: "short")
|
|
74
|
+
queue.push("very long", priority: "very long")
|
|
75
|
+
queue.pop # => "short"
|
|
76
|
+
```
|
|
53
77
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
queue.
|
|
57
|
-
queue.peek # => "item"
|
|
58
|
-
queue.size # => 1
|
|
78
|
+
### Priority Updates
|
|
79
|
+
|
|
80
|
+
Change the priority of an item already in the queue. The heap re-balances automatically.
|
|
59
81
|
|
|
60
|
-
|
|
82
|
+
```ruby
|
|
61
83
|
queue = Philiprehberger::PriorityQueue::Queue.new
|
|
62
|
-
queue.push(
|
|
63
|
-
queue.
|
|
84
|
+
queue.push("task_a", priority: 10)
|
|
85
|
+
queue.push("task_b", priority: 5)
|
|
86
|
+
|
|
87
|
+
queue.change_priority("task_a", 1) # task_a is now highest priority
|
|
88
|
+
queue.pop # => "task_a"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Merging Queues
|
|
64
92
|
|
|
65
|
-
|
|
66
|
-
merged = queue1.merge(queue2)
|
|
93
|
+
Combine two queues into a new queue. The originals are unchanged.
|
|
67
94
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
95
|
+
```ruby
|
|
96
|
+
q1 = Philiprehberger::PriorityQueue::Queue.new
|
|
97
|
+
q1.push("a", priority: 1)
|
|
98
|
+
q1.push("b", priority: 3)
|
|
99
|
+
|
|
100
|
+
q2 = Philiprehberger::PriorityQueue::Queue.new
|
|
101
|
+
q2.push("c", priority: 2)
|
|
102
|
+
|
|
103
|
+
merged = q1.merge(q2)
|
|
104
|
+
merged.to_a # => ["a", "c", "b"]
|
|
105
|
+
merged.size # => 3
|
|
73
106
|
```
|
|
74
107
|
|
|
75
108
|
## API
|
|
@@ -77,15 +110,16 @@ queue.clear # removes all items
|
|
|
77
110
|
| Method | Description |
|
|
78
111
|
|--------|-------------|
|
|
79
112
|
| `Queue.new(mode: :min, &comparator)` | Create a priority queue; mode can be `:min` or `:max`; optional custom comparator block |
|
|
80
|
-
| `#push(item, priority:)` | Add an item with the given priority |
|
|
81
|
-
|
|
|
82
|
-
| `#
|
|
113
|
+
| `#push(item, priority:)` | Add an item with the given priority; returns `self` for chaining |
|
|
114
|
+
| `#<<(item:, priority:)` | Shovel operator; push via a hash with `:item` and `:priority` keys |
|
|
115
|
+
| `#pop` | Remove and return the highest-priority item; returns `nil` when empty |
|
|
116
|
+
| `#peek` | Return the highest-priority item without removing it; returns `nil` when empty |
|
|
83
117
|
| `#size` | Return the number of items in the queue |
|
|
84
118
|
| `#empty?` | Return `true` if the queue has no items |
|
|
85
|
-
| `#change_priority(item, new_priority)` | Update the priority of an existing item and re-heapify |
|
|
86
|
-
| `#to_a` | Return all items sorted by priority |
|
|
119
|
+
| `#change_priority(item, new_priority)` | Update the priority of an existing item and re-heapify; raises `ArgumentError` if item not found |
|
|
120
|
+
| `#to_a` | Return all items sorted by priority with FIFO tie-breaking |
|
|
87
121
|
| `#include?(item)` | Return `true` if the item is in the queue |
|
|
88
|
-
| `#clear` | Remove all items from the queue |
|
|
122
|
+
| `#clear` | Remove all items from the queue; returns `self` |
|
|
89
123
|
| `#merge(other)` | Return a new queue containing items from both queues |
|
|
90
124
|
|
|
91
125
|
## Development
|
|
@@ -96,6 +130,24 @@ bundle exec rspec
|
|
|
96
130
|
bundle exec rubocop
|
|
97
131
|
```
|
|
98
132
|
|
|
133
|
+
## Support
|
|
134
|
+
|
|
135
|
+
If you find this project useful:
|
|
136
|
+
|
|
137
|
+
⭐ [Star the repo](https://github.com/philiprehberger/rb-priority-queue)
|
|
138
|
+
|
|
139
|
+
🐛 [Report issues](https://github.com/philiprehberger/rb-priority-queue/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
|
|
140
|
+
|
|
141
|
+
💡 [Suggest features](https://github.com/philiprehberger/rb-priority-queue/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement)
|
|
142
|
+
|
|
143
|
+
❤️ [Sponsor development](https://github.com/sponsors/philiprehberger)
|
|
144
|
+
|
|
145
|
+
🌐 [All Open Source Projects](https://philiprehberger.com/open-source-packages)
|
|
146
|
+
|
|
147
|
+
💻 [GitHub Profile](https://github.com/philiprehberger)
|
|
148
|
+
|
|
149
|
+
🔗 [LinkedIn Profile](https://www.linkedin.com/in/philiprehberger)
|
|
150
|
+
|
|
99
151
|
## License
|
|
100
152
|
|
|
101
|
-
MIT
|
|
153
|
+
[MIT](LICENSE)
|
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.1.
|
|
4
|
+
version: 0.1.10
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- philiprehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-03-
|
|
11
|
+
date: 2026-03-31 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,
|