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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +66 -32
- data/lib/philiprehberger/priority_queue/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a631e281cd30a3bd485b87972243ee8f53420dd1fb86e16a2cf8b5d32493dbb5
|
|
4
|
+
data.tar.gz: 91ae0621673e00deef9dc027e2645468d0070d1d8bb38d453197a7c3326790e8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 32f67845be0fa02863266bf26fde40d6a425cff68e8b7b0271a69c21ad05842c68e6db0a587ed0ef7a65455070021d7308459bac122a6870159910129ca97c76
|
|
7
|
+
data.tar.gz: da04d30b1cdc936aa4b1a6f15c99feb1110b5dae3a456d8d9fa30ae8fa41a4d0a93bdd420ea83fdf03572bb1004c1483dcc54a5928c5cc611ff998aeec76feb7
|
data/CHANGELOG.md
CHANGED
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(
|
|
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
|
+
```
|
|
47
66
|
|
|
48
|
-
|
|
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(
|
|
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
|
-
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
|
-
|
|
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(
|
|
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
|
|
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
|
-
|
|
66
|
-
|
|
100
|
+
q2 = Philiprehberger::PriorityQueue::Queue.new
|
|
101
|
+
q2.push("c", priority: 2)
|
|
67
102
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
|
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
|