rbtree-ruby 0.3.3 → 0.3.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2145d91a96cc64b7e65c65460716b678301f74d6decdffa2dbd077a914273c21
4
- data.tar.gz: 66503c542ce07672a493830afbfcb66ebfea14d8563e67b1b2dd1d5ecd1d9c48
3
+ metadata.gz: 911e435d0a205afc1a62791f857ac66d6d6300d2aa332566aa916e65e16148d2
4
+ data.tar.gz: d1a2b2aec3404901985cfec8a54c5b3df87d12bd4c8872833ba111dd3641033b
5
5
  SHA512:
6
- metadata.gz: 7befca8277d78b75242a443c2ec155bded73f899222b32330c6416e6ffeaad8ec6bca01e11ce23c6047208871cf71cfb8c6a742732b4ae0b531f54e51a8e424e
7
- data.tar.gz: eb77127897b535277717575afe27c47cd087ba15685f59b0df67516c55d393a8d7412cf08d667cea5494978746967c098294bb73e8e9771c2aee363762afcca9
6
+ metadata.gz: 6b1a96900494faa73b223e5905897299799c9a5432aa6cbdd95c6881c48c8666fe82f1653907231918fd027413eed3036da64a21087bfded6307a434c3de63b2
7
+ data.tar.gz: 6456acef3636bf7f857611a22407c4f4a053c632a973b2dfc05913d99b55a338c3a4dbce41d03074c7589354cc4bad2765c5e4be683d2a080248ea30b6a22b04
data/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.3.4] - 2026-01-25
9
+
10
+ ### Added
11
+ - **Range-based Lookup**: Support for passing `Range` objects to the bracket operator `[]`.
12
+ - `tree[2..4]` -> `between(2, 4)`
13
+ - `tree[2...4]` -> `between(2, 4, include_max: false)`
14
+ - `tree[5..]` -> `gte(5)`
15
+ - `tree[..10]` -> `lte(10)`
16
+ - `tree[...10]` -> `lt(10)`
17
+ - Returns an `Enumerator` of `[key, value]` pairs.
18
+ - *Note: `gt` (greater than exclusive) is not covered by standard Ruby Range syntax and remains available via the `gt` method.*
19
+
8
20
  ## [0.3.3] - 2026-01-23
9
21
 
10
22
  ### Optimized
@@ -207,6 +219,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
207
219
  - ASCII diagrams for tree rotation operations
208
220
  - MIT License (Copyright © 2026 Masahito Suzuki)
209
221
 
222
+ [0.3.4]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.3.4
223
+ [0.3.3]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.3.3
210
224
  [0.3.2]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.3.2
211
225
  [0.3.1]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.3.1
212
226
  [0.3.0]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.3.0
data/README.ja.md CHANGED
@@ -80,9 +80,16 @@ tree.max # => [20, "twenty"]
80
80
 
81
81
  # 範囲クエリ(Enumeratorを返す、配列には.to_aを使用)
82
82
  tree.lt(10).to_a # => [[1, "one"], [2, "two"], [3, "three"]]
83
- tree.gte(10).to_a # => [[10, "ten"], [20, "twenty"]]
83
+ tree.gte(10).each { |k, v| puts k } # ブロック渡しでのループ
84
84
  tree.between(2, 10).to_a # => [[2, "two"], [3, "three"], [10, "ten"]]
85
85
 
86
+ # []でのRangeオブジェクトの利用 (v0.3.4+)
87
+ tree[..10].to_a # lte(10)相当
88
+ tree[2..10].each { |k, v| ... } # Rangeでのループ
89
+ tree[2...10].to_a # between(2, 10, include_max: false)相当
90
+ tree[10..].to_a # gte(10)相当
91
+ tree[2..10, reverse: true].to_a # オプション指定可能
92
+
86
93
  # shiftとpop
87
94
  tree.shift # => [1, "one"] (最小値を削除)
88
95
  tree.pop # => [20, "twenty"] (最大値を削除)
data/README.md CHANGED
@@ -80,9 +80,16 @@ tree.max # => [20, "twenty"]
80
80
 
81
81
  # Range queries (return Enumerator, use .to_a for Array)
82
82
  tree.lt(10).to_a # => [[1, "one"], [2, "two"], [3, "three"]]
83
- tree.gte(10).to_a # => [[10, "ten"], [20, "twenty"]]
83
+ tree.gte(10).each { |k, v| puts k } # Block iteration
84
84
  tree.between(2, 10).to_a # => [[2, "two"], [3, "three"], [10, "ten"]]
85
85
 
86
+ # Range objects in [] (v0.3.4+)
87
+ tree[..10].to_a # lte(10)
88
+ tree[2..10].each { |k, v| ... } # Block iteration on Range
89
+ tree[2...10].to_a # between(2, 10, include_max: false)
90
+ tree[10..].to_a # gte(10)
91
+ tree[2..10, reverse: true].to_a # with options
92
+
86
93
  # Shift and pop
87
94
  tree.shift # => [1, "one"] (removes minimum)
88
95
  tree.pop # => [20, "twenty"] (removes maximum)
@@ -2,5 +2,5 @@
2
2
 
3
3
  class RBTree
4
4
  # The version of the rbtree-ruby gem
5
- VERSION = "0.3.3"
5
+ VERSION = "0.3.4"
6
6
  end
data/lib/rbtree.rb CHANGED
@@ -191,11 +191,35 @@ class RBTree
191
191
  # @example
192
192
  # tree = RBTree.new({1 => 'one', 2 => 'two'})
193
193
  # tree.get(1) # => "one"
194
- # tree[2] # => "two"
195
- # tree[3] # => nil
196
194
  def value(key) = @hash_index[key]&.value
197
195
  alias :get :value
198
- alias :[] :value
196
+
197
+ # Retrieves a value associated with the given key, or a range of entries if a Range is provided.
198
+ #
199
+ # @param key_or_range [Object, Range] the key to look up or a Range for query
200
+ # @param ... [Hash] additional options to pass to the respective lookup method
201
+ # @return [Object, Enumerator, nil]
202
+ # - If a key is provided: the associated value, or nil if not found
203
+ # - If a Range is provided: an Enumerator yielding [key, value] pairs
204
+ # @example Single key lookup
205
+ # tree[2] # => "two"
206
+ # @example Range lookup
207
+ # tree[2..4].to_a # => [[2, "two"], [3, "three"], [4, "four"]]
208
+ # tree[...3].to_a # => [[1, "one"], [2, "two"]]
209
+ def [](key_or_range, **)
210
+ return value(key_or_range, **) if !key_or_range.is_a?(Range)
211
+
212
+ r = key_or_range
213
+ r.begin ? (
214
+ r.end ?
215
+ between(r.begin, r.end, include_max: !r.exclude_end?, **) :
216
+ gte(r.begin, **)
217
+ ) : (
218
+ r.end ?
219
+ (r.exclude_end? ? lt(r.end, **) : lte(r.end, **)) :
220
+ each(**)
221
+ )
222
+ end
199
223
 
200
224
  # Returns the key with the key closest to the given key.
201
225
  #
@@ -1499,7 +1523,6 @@ class MultiRBTree < RBTree
1499
1523
  # tree.get(1, last: true) # => "second"
1500
1524
  def value(key, last: false) = @hash_index[key]&.value&.send(last ? :last : :first)
1501
1525
  alias :get :value
1502
- alias :[] :value
1503
1526
 
1504
1527
  # Retrieves the first value associated with the given key.
1505
1528
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbtree-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masahito Suzuki