rbtree-ruby 0.2.1 → 0.2.3
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 +4 -4
- data/CHANGELOG.md +23 -0
- data/README.ja.md +12 -6
- data/README.md +8 -4
- data/lib/rbtree/version.rb +1 -1
- data/lib/rbtree.rb +411 -664
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f3c97bfa59aaaa7bbc03f272fdc5e383b258f529134f1d2a79fd3758d1d53b02
|
|
4
|
+
data.tar.gz: fa9c8df14da95bf4224164d720c90981c1c68ef96e5cb544007327b9a1564b11
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e856b4d10d326c3d3261d21044db760b41447dd01e1eb22bff4dfe1ac07e71f709792b75a9ee48ddc3195f06993fd86e8434511b3f2f0eaafbcf0827da05aa1e
|
|
7
|
+
data.tar.gz: 10e8c1af8787b201c73a64557ba86304867b601daae13ad816771b87e52a81e30bc94dbeda5accc1e224a55d397ae28e0325918d61d8d4db40ee09cdd83876fa
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,29 @@ 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.2.3] - 2026-01-14
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **Bulk Insert**: `insert` now supports bulk insertion from Hash, Array of pairs, Enumerator, or Block.
|
|
12
|
+
- `tree.insert({1 => 'one', 2 => 'two'})`
|
|
13
|
+
- `tree.insert([[1, 'one'], [2, 'two']])`
|
|
14
|
+
- `tree.insert { source_data }`
|
|
15
|
+
- **Flexible Initialization**: `RBTree.new` and `MultiRBTree.new` now accept bulk data and an `overwrite:` option.
|
|
16
|
+
- `RBTree.new([[1, 'a'], [1, 'b']], overwrite: false)`
|
|
17
|
+
|
|
18
|
+
## [0.2.2] - 2026-01-14
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
- **Iterative Traversal (Complete)**: All traversal methods (`lt`, `lte`, `gt`, `gte`, `between` and their `_desc` variants) now use an iterative stack-based approach instead of recursion.
|
|
22
|
+
- Ensures deep trees can be traversed without `SystemStackError`.
|
|
23
|
+
- Applies to both `RBTree` and `MultiRBTree`.
|
|
24
|
+
- **Code Deduplication**: Unified traversal logic between `RBTree` and `MultiRBTree` for better maintainability.
|
|
25
|
+
- **Documentation**: Minor typo fixes.
|
|
26
|
+
|
|
27
|
+
### Fixed
|
|
28
|
+
- **MultiRBTree#prev / #succ**: Fixed incorrect use of bare `super` which forwarded keyword arguments to parent methods that don't accept them. Also fixed incorrect access to parent return value (was treating `[key, value]` pair as a node object).
|
|
29
|
+
- **MultiRBTree#shift / #pop hash index leak**: When `shift` or `pop` removed the last value for a key, the key remained in the internal hash index (`@hash_index`). Now correctly uses `delete_node(key)` instead of `remove_node(node)` to ensure hash index consistency.
|
|
30
|
+
|
|
8
31
|
## [0.2.1] - 2026-01-14
|
|
9
32
|
|
|
10
33
|
### Added
|
data/README.ja.md
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
🌍 *[English](README.md) | [日本語](README.ja.md)*
|
|
4
4
|
|
|
5
|
-
Red-Black Tree(赤黒木)データ構造のピュアRuby実装です。挿入、削除、検索操作がO(log n)
|
|
6
|
-
効率的な順序付きキーバリューストレージを提供します。
|
|
5
|
+
Red-Black Tree(赤黒木)データ構造のピュアRuby実装です。挿入、削除、検索操作がO(log n)の時間計算量で実行できる、効率的な順序付きキーバリューストレージを提供します。
|
|
7
6
|
|
|
8
7
|
## 特徴
|
|
9
8
|
|
|
@@ -43,13 +42,18 @@ require 'rbtree'
|
|
|
43
42
|
# 空のツリーを作成
|
|
44
43
|
tree = RBTree.new
|
|
45
44
|
|
|
46
|
-
#
|
|
45
|
+
# データで初期化(バルク挿入)
|
|
47
46
|
tree = RBTree.new({3 => 'three', 1 => 'one', 2 => 'two'})
|
|
48
47
|
tree = RBTree[[5, 'five'], [4, 'four']]
|
|
48
|
+
tree = RBTree.new do # ブロック初期化
|
|
49
|
+
data_source.each { |data| [data.time, data.content] }
|
|
50
|
+
end
|
|
49
51
|
|
|
50
52
|
# 値の挿入と取得
|
|
51
53
|
tree.insert(10, 'ten')
|
|
52
54
|
tree[20] = 'twenty'
|
|
55
|
+
# バルク挿入
|
|
56
|
+
tree.insert({30 => 'thirty', 40 => 'forty'})
|
|
53
57
|
puts tree[10] # => "ten"
|
|
54
58
|
|
|
55
59
|
# ソート順でイテレーション
|
|
@@ -138,6 +142,8 @@ tree.nearest(8) # => [10, "ten"]
|
|
|
138
142
|
|
|
139
143
|
### 前後キー検索
|
|
140
144
|
|
|
145
|
+
ツリーの中で次のキーまたは前のキーを検索します。
|
|
146
|
+
|
|
141
147
|
```ruby
|
|
142
148
|
tree = RBTree.new({1 => 'one', 3 => 'three', 5 => 'five', 7 => 'seven'})
|
|
143
149
|
|
|
@@ -202,8 +208,8 @@ tree.max(last: true) # => [2, "b"] (最大キーの最後の値)
|
|
|
202
208
|
|
|
203
209
|
- `insert(key, value)` - O(log n)
|
|
204
210
|
- `delete(key)` - O(log n)
|
|
205
|
-
- `get(key)` / `[]` - **O(1)** (
|
|
206
|
-
- `has_key?` - **O(1)** (
|
|
211
|
+
- `get(key)` / `[]` - **O(1)** (内部ハッシュインデックスによる超高速アクセス)
|
|
212
|
+
- `has_key?` - **O(1)** (内部ハッシュインデックスによる超高速チェック)
|
|
207
213
|
- `min` - **O(1)**
|
|
208
214
|
- `max` - O(log n)
|
|
209
215
|
- `shift` / `pop` - O(log n)
|
|
@@ -227,7 +233,7 @@ RBTreeは内部的な**メモリプール**を使用してノードオブジェ
|
|
|
227
233
|
| **範囲クエリ** | **O(log n + k)** | O(n) フィルター | **〜540倍高速** | 部分木へ直接ジャンプ vs 全件スキャン |
|
|
228
234
|
| **最小値抽出** | **O(log n)** | O(n) 検索 | **〜160倍高速** | 連続的なリバランス vs 全件スキャン |
|
|
229
235
|
| **ソート済みイテレーション** | **O(n)** | O(n log n) | **無料** | 常にソート済み vs 明示的な`sort` |
|
|
230
|
-
| **キー検索** | **O(1)** | O(1) | **同等** |
|
|
236
|
+
| **キー検索** | **O(1)** | O(1) | **同等** | **ハイブリッドハッシュインデックスにより、Hashと同等のO(1)検索速度を実現** |
|
|
231
237
|
|
|
232
238
|
### RBTreeを使うべき場面
|
|
233
239
|
|
data/README.md
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
🌍 *[English](README.md) | [日本語](README.ja.md)*
|
|
4
4
|
|
|
5
|
-
A pure Ruby implementation of the Red-Black Tree data structure, providing efficient ordered
|
|
6
|
-
key-value storage with O(log n) time complexity for insertion, deletion, and lookup operations.
|
|
5
|
+
A pure Ruby implementation of the Red-Black Tree data structure, providing efficient ordered key-value storage with O(log n) time complexity for insertion, deletion, and lookup operations.
|
|
7
6
|
|
|
8
7
|
## Features
|
|
9
8
|
|
|
@@ -43,13 +42,18 @@ require 'rbtree'
|
|
|
43
42
|
# Create an empty tree
|
|
44
43
|
tree = RBTree.new
|
|
45
44
|
|
|
46
|
-
# Or initialize with data
|
|
45
|
+
# Or initialize with data (Bulk Insert)
|
|
47
46
|
tree = RBTree.new({3 => 'three', 1 => 'one', 2 => 'two'})
|
|
48
47
|
tree = RBTree[[5, 'five'], [4, 'four']]
|
|
48
|
+
tree = RBTree.new do # Block initialization
|
|
49
|
+
data_source.each { |data| [data.time, data.content] }
|
|
50
|
+
end
|
|
49
51
|
|
|
50
52
|
# Insert and retrieve values
|
|
51
53
|
tree.insert(10, 'ten')
|
|
52
54
|
tree[20] = 'twenty'
|
|
55
|
+
# Bulk insert
|
|
56
|
+
tree.insert({30 => 'thirty', 40 => 'forty'})
|
|
53
57
|
puts tree[10] # => "ten"
|
|
54
58
|
|
|
55
59
|
# Iterate in sorted order
|
|
@@ -229,7 +233,7 @@ For ordered and spatial operations, RBTree is not just faster—it is in a compl
|
|
|
229
233
|
| **Range Queries** | **O(log n + k)** | O(n) filter | **~540x faster** | Direct subtree jump vs full scan |
|
|
230
234
|
| **Min Extraction** | **O(log n)** | O(n) search | **~160x faster** | Continuous rebalancing vs full scan |
|
|
231
235
|
| **Sorted Iteration** | **O(n)** | O(n log n) | **FREE** | Always sorted vs explicit `sort` |
|
|
232
|
-
| **Key Lookup** | **O(1)** | O(1) | **Equal** |
|
|
236
|
+
| **Key Lookup** | **O(1)** | O(1) | **Equal** | **Hybrid Hash Index provides O(1) access like standard Hash** |
|
|
233
237
|
|
|
234
238
|
### When to Use RBTree
|
|
235
239
|
|
data/lib/rbtree/version.rb
CHANGED