data_structures_rmolinari 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ec5653a5bec033196042ad1af1e850877696eb02a1c37a6efdf1c89ce8e726c
4
- data.tar.gz: 88893649d4329549fd2d2994f9b643614b46eb1d2d5b541418d6ad183f8f9f02
3
+ metadata.gz: 1fbafd7af3c7bd2d1eee3303fd8b8a759b0d67fc29bfdca3b7f67b626e9fe175
4
+ data.tar.gz: 5a870554e38c61b1e1af64d2205bc002cf7cb731e5412606543ffdce014342b4
5
5
  SHA512:
6
- metadata.gz: 5c2a7ba2ca4630ae925358130d4b85297ea081d37ce032fe0db5e2b13d9c84cbef5be58f9d82027feb63cc476b59200c993d608f48a9db8871594415544aab32
7
- data.tar.gz: fc5ad5901038397b7d42eb44ee2b199654612f043c2eeceeb05b380da54c9e4db858612daf3dff7eb8ae580c0af0905ddc237867580f44057a0d9df6e063df6c
6
+ metadata.gz: fcdaae719324c5b8b7914eb6a982563d58ee10170d52280c58429e8d9b5a7e5224079350ab15ad618b5cacbcdf340c17feec2b34616e17847d5b0e16a7569d50
7
+ data.tar.gz: 446e2190ff33f65c9cb6cedd6043af506aa6f395f279e54520b2ba1c0c04fbd8504615f1a94e09232edefee9b0cb0d55363aa20b6504467d70f6766b19698464
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.5.6] 2024-01-04
6
+
7
+ - Heap
8
+ - add update_by_delta method
9
+
5
10
  ## [0.5.5] 2023-12-19
6
11
 
7
12
  Support Ruby v3.2.
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`, but
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.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: 2023-12-19 00:00:00.000000000 Z
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.10
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.