data_structures_rmolinari 0.5.5 → 0.5.6
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 +3 -2
- data/lib/data_structures_rmolinari/heap.rb +15 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fbafd7af3c7bd2d1eee3303fd8b8a759b0d67fc29bfdca3b7f67b626e9fe175
|
4
|
+
data.tar.gz: 5a870554e38c61b1e1af64d2205bc002cf7cb731e5412606543ffdce014342b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcdaae719324c5b8b7914eb6a982563d58ee10170d52280c58429e8d9b5a7e5224079350ab15ad618b5cacbcdf340c17feec2b34616e17847d5b0e16a7569d50
|
7
|
+
data.tar.gz: 446e2190ff33f65c9cb6cedd6043af506aa6f395f279e54520b2ba1c0c04fbd8504615f1a94e09232edefee9b0cb0d55363aa20b6504467d70f6766b19698464
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -56,14 +56,15 @@ operations:
|
|
56
56
|
- `top`, returning the element with smallest priority
|
57
57
|
- `pop`, return the element with smallest priority and remove it from the structure
|
58
58
|
- `update(item, priority)`, update the priority of the given item, which must already be in the heap
|
59
|
+
- `update_by_delta(item, delta)`, update the priorityof the given item by adding delta to the priority; the item must already be in the heap
|
59
60
|
|
60
61
|
`top` is O(1). The others are O(log n) where n is the number of items in the heap.
|
61
62
|
|
62
63
|
By default we have a min-heap: the top element is the one with smallest priority. A configuration parameter at construction can make
|
63
64
|
it a max-heap.
|
64
65
|
|
65
|
-
Another configuration parameter allows the creation of a "non-addressable" heap. This makes it impossible to call `update
|
66
|
-
allows the insertion of duplicate items (which is sometimes useful) and slightly faster operation overall.
|
66
|
+
Another configuration parameter allows the creation of a "non-addressable" heap. This makes it impossible to call `update` or
|
67
|
+
`update_by_delta`, but allows the insertion of duplicate items (which is sometimes useful) and slightly faster operation overall.
|
67
68
|
|
68
69
|
See https://en.wikipedia.org/wiki/Binary_heap and the paper by Edelkamp, Elmasry, and Katajainen [[EEK2017]](#references).
|
69
70
|
|
@@ -137,6 +137,21 @@ class DataStructuresRMolinari::Heap
|
|
137
137
|
check_heap_property if @debug
|
138
138
|
end
|
139
139
|
|
140
|
+
# Update the priority of the given element by changing the priority by the given amount.
|
141
|
+
#
|
142
|
+
# @param element the item whose priority we are updating. It is an error to update the priority of an element not already in the
|
143
|
+
# heap
|
144
|
+
# @param the amount by which to change the priority. The existing priority must respond sensibly to += with this value
|
145
|
+
def update_by_delta(element, delta)
|
146
|
+
raise LogicError, 'Cannot update priorities in a non-addressable heap' unless @addressable
|
147
|
+
raise DataError, "Cannot update priority for value #{element} not already in the heap" unless contains?(element)
|
148
|
+
|
149
|
+
idx = @index_of[element]
|
150
|
+
old = @data[idx].priority
|
151
|
+
|
152
|
+
update(element, old + delta)
|
153
|
+
end
|
154
|
+
|
140
155
|
# Filter the value at index up to its correct location. Algorithm from Edelkamp et. al.
|
141
156
|
private def sift_up(idx)
|
142
157
|
return if idx == root
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_structures_rmolinari
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rory Molinari
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: must_be
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
- !ruby/object:Gem::Version
|
123
123
|
version: '0'
|
124
124
|
requirements: []
|
125
|
-
rubygems_version: 3.4.
|
125
|
+
rubygems_version: 3.4.5
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: Several miscellaneous data structures I have implemented to learn about them.
|