philiprehberger-priority_queue 0.4.0 → 0.5.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: 4fca90d36f6f8f63c1b89c458854eb61af26682c7dcf66aaa1da9023f8cba5ab
4
- data.tar.gz: a91b25347569be4e586c4c0ec1ce8c6d77b77d652df16ba39bce020c4560458c
3
+ metadata.gz: 5f4b5e7596b5e647e5e21d266d6532e93e045e36eb4373fb4588150220798f7b
4
+ data.tar.gz: 18dfd8320614129f2fef3980afdaa69e65052f081471252b35d6b1b70ee70f3a
5
5
  SHA512:
6
- metadata.gz: b280a1927ced0e7ac6706af14e03a319111c962d8d67cfe7d93ceab3ce05761a187698ec0b8d8d175c3fdb12f52fecf35640de99410242dee37a583e5bdf1322
7
- data.tar.gz: c52cb6190358eab1880f423df857c8165cadca1b98255c1c062a2c6bdfe54c027d0c29a492f9baf1481d78078f354c1190a7f48dbdf0d570ee5ae38d2fa7f57b
6
+ metadata.gz: 8f1818babf9c4a3b4f052c8a5fe7b7168f93dd8072933049209e6ecf87485e0769c7fe5dbea5fb166fb45543653a8acf77a53ef3699dd063e5cbf185c4a32629
7
+ data.tar.gz: 30d6bda5afc831cb70e0f8d5beb4ae4b5b37cb4e7512e4b236ad396b00811aec6c8233040e512910855338d0a088a7a538a64650c6f460faeb8f1d293a2ab9dd
data/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.5.0] - 2026-04-26
11
+
12
+ ### Added
13
+ - `Queue#peek_n(n)` — return the top `n` items in priority order without removing them; symmetric to `#pop_n`
14
+
10
15
  ## [0.4.0] - 2026-04-21
11
16
 
12
17
  ### Added
data/README.md CHANGED
@@ -154,6 +154,21 @@ queue.pop_n(5) # => ["c"]
154
154
  queue.pop_n(0) # => []
155
155
  ```
156
156
 
157
+ ### Peek N
158
+
159
+ Return the top `n` items in priority order without removing them. The queue is unchanged.
160
+
161
+ ```ruby
162
+ queue = Philiprehberger::PriorityQueue::Queue.new
163
+ queue.push("a", priority: 1)
164
+ queue.push("b", priority: 2)
165
+ queue.push("c", priority: 3)
166
+
167
+ queue.peek_n(2) # => ["a", "b"]
168
+ queue.peek_n(5) # => ["a", "b", "c"]
169
+ queue.size # => 3
170
+ ```
171
+
157
172
  ### Delete
158
173
 
159
174
  Remove a specific item by value.
@@ -230,6 +245,7 @@ merged.size # => 3
230
245
  | `#peek_priority` | Return just the top priority value; returns `nil` when empty |
231
246
  | `#drain` | Pop all items and return as array in priority order; empties the queue |
232
247
  | `#pop_n(n)` | Pop up to `n` items in priority order and return as array; returns `[]` for empty queue or `n == 0`; raises `ArgumentError` for negative `n` |
248
+ | `#peek_n(n)` | Return up to `n` items in priority order without removing them; returns `[]` for empty queue or `n == 0`; raises `ArgumentError` for negative `n` |
233
249
  | `#delete(item)` | Remove a specific item by value; returns the item or `nil` |
234
250
  | `#priorities` | Return sorted array of unique priority values in the queue |
235
251
  | `#each` | Yield `[item, priority]` pairs in priority order (Enumerable) |
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module PriorityQueue
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
@@ -69,6 +69,21 @@ module Philiprehberger
69
69
  result
70
70
  end
71
71
 
72
+ # Return the top +n+ items in priority order without removing them.
73
+ #
74
+ # When the queue is empty or +n+ is zero, returns +[]+. When +n+ is
75
+ # greater than or equal to the size, returns all items in priority order.
76
+ # The queue is not modified.
77
+ #
78
+ # @param n [Integer] the maximum number of items to return; must be non-negative
79
+ # @return [Array] the top +n+ items in priority order, or fewer when the queue is smaller
80
+ # @raise [ArgumentError] if +n+ is negative
81
+ def peek_n(n)
82
+ raise ArgumentError, "n must be non-negative, got #{n}" if n.negative?
83
+
84
+ to_a.first(n)
85
+ end
86
+
72
87
  def empty?
73
88
  @size.zero?
74
89
  end
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.0
4
+ version: 0.5.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-21 00:00:00.000000000 Z
11
+ date: 2026-04-27 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,