data_structures_rmolinari 0.5.5 → 0.5.7

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: 02563eb882aace27085e0db092d5a266c413c1864fe3c74d23d9c2af194f2d87
4
+ data.tar.gz: 2153f481d92569fbee02caea9df2177d9f7465d18e39d3764c4e92fc65a7cae6
5
5
  SHA512:
6
- metadata.gz: 5c2a7ba2ca4630ae925358130d4b85297ea081d37ce032fe0db5e2b13d9c84cbef5be58f9d82027feb63cc476b59200c993d608f48a9db8871594415544aab32
7
- data.tar.gz: fc5ad5901038397b7d42eb44ee2b199654612f043c2eeceeb05b380da54c9e4db858612daf3dff7eb8ae580c0af0905ddc237867580f44057a0d9df6e063df6c
6
+ metadata.gz: dbe47a34b03026dd0f82e5a3ba75a31c2a24a03b1d4d32fe94c0b9760e53c9171a964bec7fa04a7cc675bd980c938781c75e4f09817f4cac6b866f1aebf2483a
7
+ data.tar.gz: 9a80a2984dd8ed2caa63393b0a363c3e3e7414a79217bbb5daf1015a9c17b0842ff534d366eef05adb311ed7ef889a171140af282e3aee71619be3d9483e93c7
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.5.7] 2024-01-04
6
+
7
+ - Heap
8
+ - addd top_priority method
9
+
10
+ ## [0.5.6] 2024-01-04
11
+
12
+ - Heap
13
+ - add update_by_delta method
14
+
5
15
  ## [0.5.5] 2023-12-19
6
16
 
7
17
  Support Ruby v3.2.
data/README.md CHANGED
@@ -53,17 +53,19 @@ operations:
53
53
 
54
54
  - `insert(item, priority)`, insert the given item with the stated priority.
55
55
  - By default, items must be distinct.
56
- - `top`, returning the element with smallest priority
56
+ - `top`, return the element with smallest priority
57
+ - `top_priority`, return the _priority_ of the top element
57
58
  - `pop`, return the element with smallest priority and remove it from the structure
58
59
  - `update(item, priority)`, update the priority of the given item, which must already be in the heap
60
+ - `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
61
 
60
- `top` is O(1). The others are O(log n) where n is the number of items in the heap.
62
+ `top` and `top_priority` are O(1). The others are O(log n) where n is the number of items in the heap.
61
63
 
62
64
  By default we have a min-heap: the top element is the one with smallest priority. A configuration parameter at construction can make
63
65
  it a max-heap.
64
66
 
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.
67
+ Another configuration parameter allows the creation of a "non-addressable" heap. This makes it impossible to call `update` or
68
+ `update_by_delta`, but allows the insertion of duplicate items (which is sometimes useful) and slightly faster operation overall.
67
69
 
68
70
  See https://en.wikipedia.org/wiki/Binary_heap and the paper by Edelkamp, Elmasry, and Katajainen [[EEK2017]](#references).
69
71
 
@@ -101,6 +101,13 @@ class DataStructuresRMolinari::Heap
101
101
  @data[root].item
102
102
  end
103
103
 
104
+ # Return the priority of the item at the top of the heap
105
+ def top_priority
106
+ raise 'Heap is empty!' unless @size.positive?
107
+
108
+ @data[root].priority
109
+ end
110
+
104
111
  # Return the top of the heap and remove it, updating the structure to maintain the necessary properties.
105
112
  # @return (see #top)
106
113
  def pop
@@ -137,6 +144,21 @@ class DataStructuresRMolinari::Heap
137
144
  check_heap_property if @debug
138
145
  end
139
146
 
147
+ # Update the priority of the given element by changing the priority by the given amount.
148
+ #
149
+ # @param element the item whose priority we are updating. It is an error to update the priority of an element not already in the
150
+ # heap
151
+ # @param the amount by which to change the priority. The existing priority must respond sensibly to += with this value
152
+ def update_by_delta(element, delta)
153
+ raise LogicError, 'Cannot update priorities in a non-addressable heap' unless @addressable
154
+ raise DataError, "Cannot update priority for value #{element} not already in the heap" unless contains?(element)
155
+
156
+ idx = @index_of[element]
157
+ old = @data[idx].priority
158
+
159
+ update(element, old + delta)
160
+ end
161
+
140
162
  # Filter the value at index up to its correct location. Algorithm from Edelkamp et. al.
141
163
  private def sift_up(idx)
142
164
  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.7
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.