pairing_heap 2.0.0 → 3.0.0

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: afe0b373b7c015091b7d7f19ba3d8d0004d1f7c257aad0fe678cf235dc288541
4
- data.tar.gz: d3b93b4741c9255b67f0ca232cb881a32fa965b4390c903c4eda96b992961176
3
+ metadata.gz: eb8c2535dddd1392b4d868d5d5c48e7aec6d3de835b5ea780823a367b12d4a6f
4
+ data.tar.gz: 8dda83047006373651250a522ca1e1aa114987bf17b110d0b25d1147acf3a053
5
5
  SHA512:
6
- metadata.gz: d039d5f4a3b996a5f7b1f3193be96291ffa8d53537e45fa4fb6d61fbb83e9a1b897156c64918da8f08b3f0c78e905ef5e4588598495e3c655319e84a201bb037
7
- data.tar.gz: 29669debc6da2984b31e986c63ae36776bd48ccf6febab79d0a8f74b304c1242d65aabeba2b2ff7362476448722c4c52bb6c170a8ebbd857d1c3e930e41a79c4
6
+ metadata.gz: 39cb442615fa8da23e1d4fd1743b937dc9d8410014a2b7f2610de3cb3cad9dc3f3d9cf851e70cb943a81e658efaa70f7d588f29db4a2851a803dfafdbb6e19bb
7
+ data.tar.gz: bf9d597bbc03ebbf4aeae439ad18f04922fac7247b3046ec63afe89728020811f317e704955bbcdecefd82587de0c5152334f14e307beb9b1be024fb03234a07
@@ -12,10 +12,10 @@ jobs:
12
12
  runs-on: ${{ matrix.os }}
13
13
  steps:
14
14
  - uses: actions/checkout@v3
15
- - uses: ruby/setup-ruby@v1
15
+ - name: Set up ruby
16
+ uses: ruby/setup-ruby@v1
16
17
  with:
17
18
  ruby-version: ${{ matrix.ruby }}
18
- - run: |
19
- gem install bundler -v 2.3.6
20
- bundle install
21
- bundle exec rake
19
+ bundler-cache: true
20
+ - name: Run the default task
21
+ run: bundle exec rake
data/.standard.yml CHANGED
@@ -1,3 +1,3 @@
1
1
  ruby_version: 3.2.0
2
2
  ignore:
3
- - 'test/fib.rb'
3
+ - 'test/benchmark/fib.rb'
data/Gemfile.lock CHANGED
@@ -1,16 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pairing_heap (2.0.0)
4
+ pairing_heap (3.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
- codecov (0.6.0)
11
- simplecov (>= 0.15, < 0.22)
12
10
  docile (1.4.0)
13
11
  json (2.6.3)
12
+ json (2.6.3-java)
14
13
  language_server-protocol (3.17.0.2)
15
14
  minitest (5.15.0)
16
15
  parallel (1.22.1)
@@ -36,7 +35,7 @@ GEM
36
35
  rubocop (>= 1.7.0, < 2.0)
37
36
  rubocop-ast (>= 0.4.0)
38
37
  ruby-progressbar (1.11.0)
39
- simplecov (0.21.2)
38
+ simplecov (0.22.0)
40
39
  docile (~> 1.1)
41
40
  simplecov-html (~> 0.11)
42
41
  simplecov_json_formatter (~> 0.1)
@@ -49,14 +48,17 @@ GEM
49
48
  unicode-display_width (2.3.0)
50
49
 
51
50
  PLATFORMS
51
+ universal-java-17
52
+ x86_64-darwin-19
52
53
  x86_64-darwin-21
53
54
  x86_64-darwin-22
55
+ x86_64-linux
54
56
 
55
57
  DEPENDENCIES
56
- codecov (= 0.6.0)
57
58
  minitest (~> 5.0)
58
59
  pairing_heap!
59
60
  rake (~> 13.0)
61
+ simplecov (~> 0.22.0)
60
62
  standard (~> 1.20)
61
63
 
62
64
  BUNDLED WITH
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PairingHeap
4
- VERSION = "2.0.0"
4
+ VERSION = "3.0.0"
5
5
  end
data/lib/pairing_heap.rb CHANGED
@@ -83,7 +83,7 @@ module PairingHeap
83
83
  # @param elem Element to be pushed
84
84
  # @param priority Priority of the element
85
85
  # @raise [ArgumentError] if the element is already in the heap
86
- # @return [PairingHeap]
86
+ # @return [self]
87
87
  def push(elem, priority = elem)
88
88
  raise ArgumentError, "Element already in the heap" if @nodes.key?(elem)
89
89
 
@@ -101,16 +101,20 @@ module PairingHeap
101
101
 
102
102
  # Returns the element at the top of the heap
103
103
  # Time Complexity: O(1)
104
+ # @return [Object]
105
+ # @return [nil] if the heap is empty
104
106
  def peek
105
107
  @root&.elem
106
108
  end
107
109
 
108
110
  # @return [Object]
111
+ # @return [nil] if the heap is empty
109
112
  def peek_priority
110
113
  @root&.priority
111
114
  end
112
115
 
113
116
  # @return [Array(Object, Object)]
117
+ # @return [Array(nil, nil)] if the heap is empty
114
118
  def peek_with_priority
115
119
  [@root&.elem, @root&.priority]
116
120
  end
@@ -137,9 +141,10 @@ module PairingHeap
137
141
  # Removes element from the top of the heap and returns it
138
142
  # Time Complexity: O(N)
139
143
  # Amortized time Complexity: O(log(N))
140
- # @raise [ArgumentError] if the heap is empty
144
+ # @return [Object] The top element
145
+ # @return [nil] If the heap is empty
141
146
  def pop
142
- raise ArgumentError, "Cannot remove from an empty heap" if @root.nil?
147
+ return nil if @root.nil?
143
148
 
144
149
  elem = @root.elem
145
150
  @nodes.delete(elem)
@@ -153,18 +158,22 @@ module PairingHeap
153
158
  end
154
159
  alias_method :dequeue, :pop
155
160
 
161
+ # @see #pop
156
162
  # @return [Object]
163
+ # @return [nil] if the heap is empty
157
164
  def pop_priority
158
165
  node = @root
159
166
  pop
160
- node.priority
167
+ node&.priority
161
168
  end
162
169
 
170
+ # @see #pop
163
171
  # @return [Array(Object, Object)]
172
+ # @return [Array(nil, nil)] If the heap is empty
164
173
  def pop_with_priority
165
174
  node = @root
166
175
  pop
167
- [node.elem, node.priority]
176
+ [node&.elem, node&.priority]
168
177
  end
169
178
 
170
179
  # Changes a priority of element to a more prioritary one
@@ -173,7 +182,7 @@ module PairingHeap
173
182
  # @param elem Element
174
183
  # @param priority New priority
175
184
  # @raise [ArgumentError] if the element is not in the heap or the new priority is less prioritary
176
- # @return [PairingHeap]
185
+ # @return [self]
177
186
  def change_priority(elem, priority)
178
187
  node = @nodes[elem]
179
188
  raise ArgumentError, "Provided element is not in heap" if node.nil?
@@ -195,7 +204,7 @@ module PairingHeap
195
204
  # Time Complexity: O(N)
196
205
  # Amortized Time Complexity: O(log(N))
197
206
  # @raise [ArgumentError] if the element is not in the heap
198
- # @return [PairingHeap]
207
+ # @return [self]
199
208
  def delete(elem)
200
209
  node = @nodes[elem]
201
210
  raise ArgumentError, "Provided element is not in heap" if node.nil?
@@ -221,14 +230,21 @@ module PairingHeap
221
230
  self
222
231
  end
223
232
 
233
+ # Check if element is in the heap
234
+ # Time Complexity: O(1)
235
+ # @return [Boolean]
236
+ def include?(key)
237
+ @nodes.key?(key)
238
+ end
239
+ alias_method :exists?, :include?
240
+
224
241
  # Returns priority of the provided element
225
242
  # Time Complexity: O(1)
226
- # @raise [ArgumentError] if the element is not in the heap
227
243
  # @return [Object]
244
+ # @return [nil] If element does not exist
228
245
  def get_priority(elem)
229
246
  node = @nodes[elem]
230
- raise ArgumentError, "Provided element is not in heap" if node.nil?
231
- node.priority
247
+ node&.priority
232
248
  end
233
249
 
234
250
  # Returns a pair where first element is success flag, and second element is priority
@@ -242,11 +258,22 @@ module PairingHeap
242
258
  [true, node.priority]
243
259
  end
244
260
 
245
- # Returns enumerator of elements. No order guarantees are provided.
246
- # @return [Enumerator]
261
+ # Returns enumerator of elements.
262
+ # @note There are no order guarantees.
263
+ # @yieldparam [Object] element Element in the heap
264
+ # @return [Enumerator<Object>]
247
265
  def each
248
266
  return to_enum(__method__) { size } unless block_given?
249
- NodeVisitor.visit_node(@root) { |x| yield x.elem }
267
+ @nodes.each_value { |node| yield node.elem }
268
+ end
269
+
270
+ # Returns enumerator of elements.
271
+ # @note There are no order guarantees.
272
+ # @return [Enumerator<Array(Object, Object)>] if no block given
273
+ # @yieldparam [Array(Object, Object)] element Element in the heap with its priority
274
+ def each_with_priority
275
+ return to_enum(__method__) { size } unless block_given?
276
+ @nodes.each_value { |node| yield [node.elem, node.priority] }
250
277
  end
251
278
 
252
279
  private
@@ -294,7 +321,7 @@ module PairingHeap
294
321
  # Time Complexity: O(1)
295
322
  # @param elem Element to be pushed
296
323
  # @param priority Priority of the element
297
- # @return [PairingHeap]
324
+ # @return [self]
298
325
  def push(elem, priority = elem)
299
326
  node = Node.new(elem, priority)
300
327
  @root = if @root
@@ -310,16 +337,20 @@ module PairingHeap
310
337
 
311
338
  # Returns the element at the top of the heap
312
339
  # Time Complexity: O(1)
340
+ # @return [Object]
341
+ # @return [nil] If the heap is empty
313
342
  def peek
314
343
  @root&.elem
315
344
  end
316
345
 
317
346
  # @return [Object]
347
+ # @return [nil] If the heap is empty
318
348
  def peek_priority
319
349
  @root&.priority
320
350
  end
321
351
 
322
352
  # @return [Array(Object, Object)]
353
+ # @return [Array(nil, nil)] If the heap is empty
323
354
  def peek_with_priority
324
355
  [@root&.elem, @root&.priority]
325
356
  end
@@ -344,9 +375,10 @@ module PairingHeap
344
375
  # Removes an element from the top of the heap and returns it
345
376
  # Time Complexity: O(N)
346
377
  # Amortized time Complexity: O(log(N))
347
- # @raise [ArgumentError] if the heap is empty
378
+ # @return [Object] The top element
379
+ # @return [nil] If the heap is empty
348
380
  def pop
349
- raise ArgumentError, "Cannot remove from an empty heap" if @root.nil?
381
+ return nil if @root.nil?
350
382
  @size -= 1
351
383
 
352
384
  elem = @root.elem
@@ -357,27 +389,42 @@ module PairingHeap
357
389
  end
358
390
  alias_method :dequeue, :pop
359
391
 
392
+ # @see #pop
360
393
  # @return [Object]
394
+ # @return [nil] If the heap is empty
361
395
  def pop_priority
362
396
  node = @root
363
397
  pop
364
- node.priority
398
+ node&.priority
365
399
  end
366
400
 
401
+ # @see #pop
367
402
  # @return [Array(Object, Object)]
403
+ # @return [Array(nil, nil)] If the heap is empty
368
404
  def pop_with_priority
369
405
  node = @root
370
406
  pop
371
- [node.elem, node.priority]
407
+ [node&.elem, node&.priority]
372
408
  end
373
409
 
374
- # Returns enumerator of elements. No order guarantees are provided.
375
- # @return [Enumerator]
410
+ # Returns enumerator of elements.
411
+ # @note There are no order guarantees.
412
+ # @yieldparam [Object] element element in the heap
413
+ # @return [Enumerator<Object>] if no block given
376
414
  def each
377
415
  return to_enum(__method__) { size } unless block_given?
378
416
  NodeVisitor.visit_node(@root) { |x| yield x.elem }
379
417
  end
380
418
 
419
+ # @return [Enumerator<Array(Object, Object)>] if no block given
420
+ # @yieldparam [Array(Object, Object)] element Element in the heap with its priority
421
+ # Returns enumerator of elements.
422
+ # @note There are no order guarantees.
423
+ def each_with_priority
424
+ return to_enum(__method__) { size } unless block_given?
425
+ NodeVisitor.visit_node(@root) { |x| yield [x.elem, x.priority] }
426
+ end
427
+
381
428
  private
382
429
 
383
430
  include MergePairs
@@ -424,7 +471,7 @@ module PairingHeap
424
471
  # Time Complexity: O(N)
425
472
  # Amortized Time Complexity: O(log(N))
426
473
  # @raise [ArgumentError] if the element is not in the heap
427
- # @return [PairingHeap]
474
+ # @return [self]
428
475
  def change_priority(elem, priority)
429
476
  raise ArgumentError, "Provided element is not in heap" unless @nodes.key?(elem)
430
477
  if !@order[priority, @nodes[elem].priority]
data/pairing_heap.gemspec CHANGED
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  spec.add_development_dependency "minitest", "~> 5.0"
34
34
  spec.add_development_dependency "rake", "~> 13.0"
35
- spec.add_development_dependency "codecov", "0.6.0"
35
+ spec.add_development_dependency "simplecov", "~> 0.22.0"
36
36
  spec.add_development_dependency "standard", "~> 1.20"
37
37
 
38
38
  # For more information and examples about making a new gem, checkout our
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pairing_heap
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Henryk Bartkowiak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-28 00:00:00.000000000 Z
11
+ date: 2023-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: codecov
42
+ name: simplecov
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.6.0
47
+ version: 0.22.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.6.0
54
+ version: 0.22.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: standard
57
57
  requirement: !ruby/object:Gem::Requirement