red-black-tree 0.1.4 → 0.1.5

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: 3e772062ad3ed9928eff044fa4e40bbd80dd0ab121a0c9cb2982969159bcf1ef
4
- data.tar.gz: ad53b47275b4c5ad04a80faf5e05a06e5e7576cb33f7d232e502c30b1a4455c8
3
+ metadata.gz: b90594d4e04c47851ed0eaa18f0f517c37081a9c52c21ae5d4af66b142e2fdb1
4
+ data.tar.gz: de26391d21001786ee4f39859ef248985bbb5ca9750187f069219b3fdd72813e
5
5
  SHA512:
6
- metadata.gz: 856164178d9887beec334f8a2fea1a51f60cf3f822a427a2a232847720a9c016250404e2af244962fe4033dbfaecfb447c3e746c3ebbe6861c77dab3ed6b3216
7
- data.tar.gz: c1a13b5313f3b31c9a0aab24e708e556ac9b707ea53ed539f7aaaa7fa6264eb17ca2205be6efdc1690277acb3d8ea09536590d1b5864e133fa9d649740d1fc51
6
+ metadata.gz: df049ff727c8918d99e255e44977281e378269620d04dbb16e3d6d1990e72416d85c1c42c7815de67d0c2f1366c68db463587d4cdfe56213cc6f45e4476f6648
7
+ data.tar.gz: dea0e150ee92555bff5af6e81cf81e4df9f75326a3c7e8bfa907c24b17dfa42f13548f113d9ec0b3448127a92794a0059701136eda88cd7c6fe0c44f5a75bfe9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.5] - 2024-10-28
4
+
5
+ - Alias `RedBlackTree#search` as `RedBlackTree#find`
6
+ - Add `RedBlackTree#select`, aliased as `RedBlackTree#filter` and `RedBlackTree#find_all`
7
+
3
8
  ## [0.1.4] - 2024-10-27
4
9
 
5
10
  - Fix incorrect `super` fallback in `RedBlackTree::DataDelegation#respond_to_missing?`
@@ -224,7 +224,7 @@ class RedBlackTree
224
224
  # @return [RedBlackTree::Node, nil] the matching node
225
225
  def search data = nil, &block
226
226
  if block_given?
227
- raise ArgumentError, "provide either data or block, not both" if data
227
+ raise ArgumentError, "provide either data or block, not both for search" if data
228
228
 
229
229
  _search_by_block block, @root
230
230
  else
@@ -233,6 +233,15 @@ class RedBlackTree
233
233
  _search_by_data data, @root
234
234
  end
235
235
  end
236
+ alias_method :find, :search
237
+
238
+ def select &block
239
+ raise ArgumentError, "block must be provided for select" unless block
240
+
241
+ _select_by_block block, @root
242
+ end
243
+ alias_method :filter, :select
244
+ alias_method :find_all, :select
236
245
 
237
246
  # Returns true if there is a node which matches the given data/value, and false if there is not.
238
247
  #
@@ -327,14 +336,6 @@ class RedBlackTree
327
336
  opp_direction_child
328
337
  end
329
338
 
330
- def _search_by_block block, node
331
- traverse node do |current|
332
- next if current.leaf?
333
-
334
- return current if block.call current
335
- end
336
- end
337
-
338
339
  def _search_by_data data, node
339
340
  return if node.nil? || node.leaf?
340
341
  return node if data == node.data
@@ -347,6 +348,24 @@ class RedBlackTree
347
348
  end
348
349
  end
349
350
 
351
+ def _search_by_block block, node
352
+ traverse node do |current|
353
+ next if current.leaf?
354
+
355
+ return current if block.call current
356
+ end
357
+ end
358
+
359
+ def _select_by_block block, node
360
+ [].tap do |result|
361
+ traverse node do |current|
362
+ next if current.leaf?
363
+
364
+ result << current if block.call current
365
+ end
366
+ end
367
+ end
368
+
350
369
  def is_root? node
351
370
  node && @root && node.object_id == @root.object_id
352
371
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class RedBlackTree
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red-black-tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Young