rbtree-ruby 0.1.5 → 0.1.6
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 +7 -0
- data/lib/rbtree/version.rb +1 -1
- data/lib/rbtree.rb +14 -5
- 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: bf6eb0a695ef93f89f9af8ceea5928c549785ea0110992f061b00ecdc76e3d64
|
|
4
|
+
data.tar.gz: 458cbee1ded3e8e0686ec495392bffbaf881ac0bc798d68790240abb0a3011b7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 48ee9755aad89802b6cb1b6af19a7919151e14531f2c6e2f2dfbb07062c5e1060490e8d5244d0fd960f44d4eb8b00e84ee9f24e5b3be6319c808f212dde848c2
|
|
7
|
+
data.tar.gz: aeadf12c689069742c8cc05c569a84bbb6a87b3736f2f7e6351b68fb2aae5dbc24eca80e65a97455b50dae82b47c3ade7b8b70a171d86f879a179865eab9182d
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@ 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.1.6] - 2026-01-13
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- **Performance**: Standardized on Boolean colors (`true`/`false`) instead of Symbols (`:red`/`:black`) for faster checks.
|
|
12
|
+
- **Optimization**: `insert` operations now check the internal hash index first, allowing O(1) updates for existing keys (RBTree) and O(1) appends (MultiRBTree).
|
|
13
|
+
|
|
8
14
|
## [0.1.5] - 2026-01-13
|
|
9
15
|
|
|
10
16
|
### Changed
|
|
@@ -77,6 +83,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
77
83
|
- ASCII diagrams for tree rotation operations
|
|
78
84
|
- MIT License (Copyright © 2026 Masahito Suzuki)
|
|
79
85
|
|
|
86
|
+
[0.1.6]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.1.6
|
|
80
87
|
[0.1.5]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.1.5
|
|
81
88
|
[0.1.4]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.1.4
|
|
82
89
|
[0.1.3]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.1.3
|
data/lib/rbtree/version.rb
CHANGED
data/lib/rbtree.rb
CHANGED
|
@@ -174,6 +174,11 @@ class RBTree
|
|
|
174
174
|
# tree.insert(1, 'uno', overwrite: false) # => nil (no change)
|
|
175
175
|
# tree[2] = 'two' # using alias
|
|
176
176
|
def insert(key, value, overwrite: true)
|
|
177
|
+
if (node = @hash_index[key])
|
|
178
|
+
return nil unless overwrite
|
|
179
|
+
node.value = value
|
|
180
|
+
return true
|
|
181
|
+
end
|
|
177
182
|
y = @nil_node
|
|
178
183
|
x = @root
|
|
179
184
|
while x != @nil_node
|
|
@@ -1052,6 +1057,10 @@ class MultiRBTree < RBTree
|
|
|
1052
1057
|
# tree.insert(1, 'first')
|
|
1053
1058
|
# tree.insert(1, 'second') # adds another value for key 1
|
|
1054
1059
|
def insert(key, value)
|
|
1060
|
+
if (node = @hash_index[key])
|
|
1061
|
+
node.value << value
|
|
1062
|
+
return true
|
|
1063
|
+
end
|
|
1055
1064
|
y = @nil_node
|
|
1056
1065
|
x = @root
|
|
1057
1066
|
while x != @nil_node
|
|
@@ -1288,16 +1297,16 @@ end
|
|
|
1288
1297
|
class RBTree::Node
|
|
1289
1298
|
attr_accessor :key, :value, :color, :left, :right, :parent
|
|
1290
1299
|
|
|
1291
|
-
# Red color constant
|
|
1292
|
-
RED =
|
|
1293
|
-
# Black color constant
|
|
1294
|
-
BLACK =
|
|
1300
|
+
# Red color constant (true)
|
|
1301
|
+
RED = true
|
|
1302
|
+
# Black color constant (false)
|
|
1303
|
+
BLACK = false
|
|
1295
1304
|
|
|
1296
1305
|
# Creates a new Node.
|
|
1297
1306
|
#
|
|
1298
1307
|
# @param key [Object] the key
|
|
1299
1308
|
# @param value [Object] the value
|
|
1300
|
-
# @param color [
|
|
1309
|
+
# @param color [Boolean] the color (true=red, false=black)
|
|
1301
1310
|
# @param left [Node] the left child
|
|
1302
1311
|
# @param right [Node] the right child
|
|
1303
1312
|
# @param parent [Node] the parent node
|