philiprehberger-priority_queue 0.1.7 → 0.1.8

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: b6c6189abf62a5cd7a03eec4626b908cfb8fbfd954e8183dbb5ebe43f421105f
4
- data.tar.gz: 8af43691f143f9c673f56fed46d90486d9e0616ca938939bd99e5e62262b0bb9
3
+ metadata.gz: a631e281cd30a3bd485b87972243ee8f53420dd1fb86e16a2cf8b5d32493dbb5
4
+ data.tar.gz: 91ae0621673e00deef9dc027e2645468d0070d1d8bb38d453197a7c3326790e8
5
5
  SHA512:
6
- metadata.gz: fbb8db631237032ac270c43fe2c5a1f9e9c33a906a4e2c60def195451ade5d930196ffdaf44cd3342d21228a870c98d493ff31764fe0f7587e488b74a82003e7
7
- data.tar.gz: 12603ff8ef9d42bbac4fb3f364dab1b6edda0904962752e5ada257a022ec060f801d27175a7954c0849a2bd7499d78d9899d2a578bd6b1019ad26a32761a95cc
6
+ metadata.gz: 32f67845be0fa02863266bf26fde40d6a425cff68e8b7b0271a69c21ad05842c68e6db0a587ed0ef7a65455070021d7308459bac122a6870159910129ca97c76
7
+ data.tar.gz: da04d30b1cdc936aa4b1a6f15c99feb1110b5dae3a456d8d9fa30ae8fa41a4d0a93bdd420ea83fdf03572bb1004c1483dcc54a5928c5cc611ff998aeec76feb7
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.1.8] - 2026-03-24
11
+
12
+ ### Changed
13
+ - Add Usage subsections and expand API table in README
14
+
10
15
  ## [0.1.7] - 2026-03-24
11
16
 
12
17
  ### Fixed
data/README.md CHANGED
@@ -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('low', priority: 1)
35
- queue.push('high', priority: 10)
36
- queue.push('mid', priority: 5)
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
- # Max-heap - highest priority first
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('task_a', priority: 3)
45
- queue.push('task_b', priority: 7)
46
- queue.pop # => "task_b"
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
+ ```
47
66
 
48
- # Custom comparator
67
+ ### Custom Comparator
68
+
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('short', priority: 'short')
51
- queue.push('very long', priority: 'very long')
52
- queue.pop # => "short"
73
+ queue.push("short", priority: "short")
74
+ queue.push("very long", priority: "very long")
75
+ queue.pop # => "short"
76
+ ```
53
77
 
54
- # Peek without removing
55
- queue = Philiprehberger::PriorityQueue::Queue.new
56
- queue.push('item', priority: 1)
57
- queue.peek # => "item"
58
- queue.size # => 1
78
+ ### Priority Updates
59
79
 
60
- # Change priority
80
+ Change the priority of an item already in the queue. The heap re-balances automatically.
81
+
82
+ ```ruby
61
83
  queue = Philiprehberger::PriorityQueue::Queue.new
62
- queue.push('task', priority: 10)
63
- queue.change_priority('task', 1) # now highest priority in min-heap
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
92
+
93
+ Combine two queues into a new queue. The originals are unchanged.
94
+
95
+ ```ruby
96
+ q1 = Philiprehberger::PriorityQueue::Queue.new
97
+ q1.push("a", priority: 1)
98
+ q1.push("b", priority: 3)
64
99
 
65
- # Merge queues
66
- merged = queue1.merge(queue2)
100
+ q2 = Philiprehberger::PriorityQueue::Queue.new
101
+ q2.push("c", priority: 2)
67
102
 
68
- # Other operations
69
- queue.include?('task') # => true
70
- queue.to_a # => items sorted by priority
71
- queue.empty? # => false
72
- queue.clear # removes all items
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
- | `#pop` | Remove and return the highest-priority item |
82
- | `#peek` | Return the highest-priority item without removing it |
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module PriorityQueue
5
- VERSION = '0.1.7'
5
+ VERSION = '0.1.8'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-priority_queue
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - philiprehberger