rbtree-ruby 0.1.0 → 0.1.1

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: 6cdac6cf721ca88b6fa05ccc11997093f9dee8d64585303d153f13bc4c1bf678
4
- data.tar.gz: dae5536d6035f67546136f12ffe1fc52d602bdffc8dd5eeba120fec2313c4cd7
3
+ metadata.gz: 047f125ed11edef5fa998d90ff50d80f90879a065d6407b02184d21089f0b1a8
4
+ data.tar.gz: 1bb83c26b7aca89d6cce328298f51d9ba9c3e1e5b7fc1ea2adf3b59c88bf2c40
5
5
  SHA512:
6
- metadata.gz: 48533967b1513900e0008f5046b138049fd60dd24fd0153ba69278c0f21e2e7183d4ef54380ae9f875f3f9a5853c09d4f57280c5c91a456c4aef560d5a0e9677
7
- data.tar.gz: 8cf8d948c812075b068479e25737a0d43999831f2dea87a018d6d62fe8c1049d96bb27076f88173ed91d4bd4ae1939669d768402ee9e8ce8e5c215c14014d909
6
+ metadata.gz: '0847bf075101301c924d093b9561c6fe014efbd9bfbc94020427c0382ab38608c275f5e50e0df7221c21b21afc2282de920273bc22dbbb11db2822a367cabc8f'
7
+ data.tar.gz: 5b0bc3e6f05ffa239b0316c8927380eddfb322b5e207001930354f5482748f0b148bfd2a42b8796ae83fd19f0b7dd97d9f5b863fcc1500094406b7731bbf3c78
data/CHANGELOG.md ADDED
@@ -0,0 +1,38 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2026-01-13
9
+
10
+ ### Added
11
+ - Initial release of rbtree-ruby gem
12
+ - `RBTree` class: Pure Ruby Red-Black Tree implementation
13
+ - **Core operations**: `insert`, `delete`, `get` (aliased as `[]`/`[]=`) - O(log n)
14
+ - **Query methods**: `has_key?`, `empty?`, `size`, `min`, `max`
15
+ - **Iteration**: `each`, `reverse_each` with Enumerable support (`first`, `last`, `to_a`, etc.)
16
+ - **Stack operations**: `shift` (remove min), `pop` (remove max)
17
+ - **Range queries**: `lt`, `lte`, `gt`, `gte`, `between` - all support block iteration
18
+ - **Nearest search**: `nearest(key)` for finding closest numeric key
19
+ - **Utility**: `clear`, `inspect`, `valid?` (tree property validation)
20
+ - `MultiRBTree` class: Red-Black Tree with multi-value support
21
+ - **Extends RBTree**: Inherits all range query and iteration methods
22
+ - **Multi-value storage**: Each key maps to a `RBTree::LinkedList` of values
23
+ - **Value retrieval**: `get` (first value), `get_all` (all values as LinkedList)
24
+ - **Flexible deletion**: `delete_one` (removes first value), `delete` (removes all values)
25
+ - **Order preservation**: Values maintain insertion order per key
26
+ - **Accurate sizing**: `size` reflects total key-value pairs, not unique keys
27
+ - `RBTree::LinkedList` class: Internal doubly-linked list for MultiRBTree
28
+ - Supports `<<`, `shift`, `pop`, `first`, `last`, `empty?`, `each`, `reverse_each`
29
+ - Maintains insertion order with O(1) operations at both ends
30
+ - Comprehensive RDoc documentation
31
+ - All 4 classes fully documented (RBTree, MultiRBTree, Node, LinkedList)
32
+ - 80%+ documentation coverage with type annotations
33
+ - Method signatures with `@param`, `@return`, `@yield` tags
34
+ - Practical usage examples for all public methods
35
+ - ASCII diagrams for tree rotation operations
36
+ - MIT License (Copyright © 2026 Masahito Suzuki)
37
+
38
+ [0.1.0]: https://github.com/firelzrd/rbtree-ruby/releases/tag/v0.1.0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Masahito Suzuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # RBTree
2
+
3
+ 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.
4
+
5
+ ## Features
6
+
7
+ - **Self-Balancing Binary Search Tree**: Maintains optimal performance through red-black tree properties
8
+ - **Ordered Operations**: Efficient range queries, min/max retrieval, and sorted iteration
9
+ - **Multi-Value Support**: `MultiRBTree` class for storing multiple values per key
10
+ - **Pure Ruby**: No C extensions required, works on any Ruby implementation
11
+ - **Well-Documented**: Comprehensive RDoc documentation with examples
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'rbtree-ruby'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```bash
24
+ bundle install
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```bash
30
+ gem install rbtree-ruby
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### Basic RBTree
36
+
37
+ ```ruby
38
+ require 'rbtree'
39
+
40
+ # Create an empty tree
41
+ tree = RBTree.new
42
+
43
+ # Or initialize with data
44
+ tree = RBTree.new({3 => 'three', 1 => 'one', 2 => 'two'})
45
+ tree = RBTree[[5, 'five'], [4, 'four']]
46
+
47
+ # Insert and retrieve values
48
+ tree.insert(10, 'ten')
49
+ tree[20] = 'twenty'
50
+ puts tree[10] # => "ten"
51
+
52
+ # Iterate in sorted order
53
+ tree.each { |key, value| puts "#{key}: #{value}" }
54
+ # Output:
55
+ # 1: one
56
+ # 2: two
57
+ # 3: three
58
+ # 10: ten
59
+ # 20: twenty
60
+
61
+ # Min and max
62
+ tree.min # => [1, "one"]
63
+ tree.max # => [20, "twenty"]
64
+
65
+ # Range queries
66
+ tree.lt(10) # => [[1, "one"], [2, "two"], [3, "three"]]
67
+ tree.gte(10) # => [[10, "ten"], [20, "twenty"]]
68
+ tree.between(2, 10) # => [[2, "two"], [3, "three"], [10, "ten"]]
69
+
70
+ # Shift and pop
71
+ tree.shift # => [1, "one"] (removes minimum)
72
+ tree.pop # => [20, "twenty"] (removes maximum)
73
+
74
+ # Delete
75
+ tree.delete(3) # => "three"
76
+
77
+ # Check membership
78
+ tree.has_key?(2) # => true
79
+ tree.size # => 2
80
+ ```
81
+
82
+ ### MultiRBTree with Duplicate Keys
83
+
84
+ ```ruby
85
+ require 'rbtree'
86
+
87
+ tree = MultiRBTree.new
88
+
89
+ # Insert multiple values for the same key
90
+ tree.insert(1, 'first one')
91
+ tree.insert(1, 'second one')
92
+ tree.insert(1, 'third one')
93
+ tree.insert(2, 'two')
94
+
95
+ tree.size # => 4 (total number of key-value pairs)
96
+
97
+ # Get first value
98
+ tree.get(1) # => "first one"
99
+ tree[1] # => "first one"
100
+
101
+ # Get all values for a key
102
+ tree.get_all(1) # => LinkedList with ["first one", "second one", "third one"]
103
+
104
+ # Iterate over all key-value pairs
105
+ tree.each { |k, v| puts "#{k}: #{v}" }
106
+ # Output:
107
+ # 1: first one
108
+ # 1: second one
109
+ # 1: third one
110
+ # 2: two
111
+
112
+ # Delete only first value
113
+ tree.delete_one(1) # => "first one"
114
+ tree.get(1) # => "second one"
115
+
116
+ # Delete all values for a key
117
+ tree.delete(1) # removes all remaining values
118
+ ```
119
+
120
+ ### Nearest Key Search
121
+
122
+ ```ruby
123
+ tree = RBTree.new({1 => 'one', 5 => 'five', 10 => 'ten'})
124
+
125
+ tree.nearest(4) # => [5, "five"] (closest key to 4)
126
+ tree.nearest(7) # => [5, "five"] (same distance, returns smaller key)
127
+ tree.nearest(8) # => [10, "ten"]
128
+ ```
129
+
130
+ ## Performance
131
+
132
+ All major operations run in **O(log n)** time:
133
+
134
+ - `insert(key, value)` - O(log n)
135
+ - `delete(key)` - O(log n)
136
+ - `get(key)` / `[]` - O(log n)
137
+ - `min` / `max` - O(1) for min, O(log n) for max
138
+ - `shift` / `pop` - O(log n)
139
+
140
+ Iteration over all elements takes O(n) time.
141
+
142
+ ## API Documentation
143
+
144
+ Full RDoc documentation is available. Generate it locally with:
145
+
146
+ ```bash
147
+ rdoc lib/rbtree.rb
148
+ ```
149
+
150
+ Then open `doc/index.html` in your browser.
151
+
152
+ ## Development
153
+
154
+ After checking out the repo, run `bundle install` to install dependencies.can then run:
155
+
156
+ ```bash
157
+ # Generate RDoc documentation
158
+ rake rdoc
159
+
160
+ # Build the gem
161
+ rake build
162
+
163
+ # Install locally
164
+ rake install
165
+ ```
166
+
167
+ ## Contributing
168
+
169
+ Bug reports and pull requests are welcome on GitHub at https://github.com/firelzrd/rbtree-ruby.
170
+
171
+ ## License
172
+
173
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
174
+
175
+ ## Author
176
+
177
+ Masahito Suzuki (firelzrd@gmail.com)
178
+
179
+ Copyright © 2026 Masahito Suzuki
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rdoc/task"
5
+
6
+ RDoc::Task.new(:rdoc) do |rdoc|
7
+ rdoc.rdoc_dir = "doc"
8
+ rdoc.title = "RBTree Documentation"
9
+ rdoc.options << "--line-numbers"
10
+ rdoc.rdoc_files.include("lib/**/*.rb")
11
+ rdoc.rdoc_files.include("README.md")
12
+ rdoc.rdoc_files.include("LICENSE")
13
+ end
14
+
15
+ desc "Build the gem"
16
+ task :build do
17
+ sh "gem build rbtree-ruby.gemspec"
18
+ end
19
+
20
+ desc "Install the gem locally"
21
+ task :install => :build do
22
+ sh "gem install ./rbtree-ruby-#{RBTree::VERSION}.gem"
23
+ end
24
+
25
+ desc "Clean build artifacts"
26
+ task :clean do
27
+ sh "rm -f *.gem"
28
+ sh "rm -rf doc/"
29
+ end
30
+
31
+ task :default => :rdoc
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RBTree
4
+ # The version of the rbtree-ruby gem
5
+ VERSION = "0.1.1"
6
+ end