Skip to content

Commit 1ef376a

Browse files
committed
Document and enforce unsupported << for range trees
Add README and TREE_TYPES notes clarifying << support matrix across tree types.\n\nExplicitly document that FenwickTree and SegmentTree do not support << and require indexed update semantics.\n\nAdd unit and RSpec coverage asserting that << is unavailable and raises NoMethodError for these classes.
1 parent d52e739 commit 1ef376a

7 files changed

Lines changed: 58 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Changes section to scan for breaking or behavioral changes.
1212
instead of generic child-node attachment semantics.
1313
* Add `<<` insertion shorthand to `Tree::AATree` and `Tree::BTree` for
1414
`[key, value]`, `{ key:, value: }`, and `Entry` inputs.
15+
* Clarify in docs that `Tree::FenwickTree` and `Tree::SegmentTree` do not
16+
support `<<`, and add tests asserting that unsupported behavior.
1517

1618
### 3.0.0pre / 2026-02-09
1719

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ Available tree types include:
5252
* [Order-Statistic Tree][] under `Tree::OrderStatisticTreeNode` (require
5353
`tree/orderstatistictree`).
5454

55+
Insertion operator (`<<`) support:
56+
57+
* Supported:
58+
`Tree::TreeNode` and TreeNode-derived node types,
59+
`Tree::AATree`, `Tree::BTree`.
60+
* Not supported:
61+
`Tree::FenwickTree`, `Tree::SegmentTree`.
62+
These classes use explicit indexed updates (`update`, `[]=`) rather than
63+
append-style insertion semantics.
64+
5565
See [TREE_TYPES](./TREE_TYPES.md) for detailed descriptions, ASCII diagrams,
5666
and use cases for each tree type.
5767

TREE_TYPES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ conventions, while array-backed trees (Fenwick, Segment) and multi-entry trees
129129
These non-TreeNode types provide a TreeNode-like subset where it fits their
130130
semantics (Enumerable iteration, Comparable, and serialization).
131131

132+
`<<` insertion support:
133+
134+
- Supported:
135+
`Tree::TreeNode` and TreeNode-derived node types, `Tree::AATree`,
136+
`Tree::BTree`.
137+
- Not supported:
138+
`Tree::FenwickTree`, `Tree::SegmentTree` (use `update` / `[]=`).
139+
132140
## Quick Examples
133141

134142
```ruby

spec/tree/fenwick_tree_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,16 @@ def build_tree(values)
100100
expect(left < right).to be(true)
101101
end
102102
end
103+
104+
describe '<<' do
105+
it 'is not supported' do
106+
tree = build_tree([1, 2, 3])
107+
expect(tree.respond_to?(:<<)).to be(false)
108+
end
109+
110+
it 'raises when called' do
111+
tree = build_tree([1, 2, 3])
112+
expect { tree << 4 }.to raise_error(NoMethodError)
113+
end
114+
end
103115
end

spec/tree/segment_tree_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,16 @@ def build_tree(values)
9393
expect(left < right).to be(true)
9494
end
9595
end
96+
97+
describe '<<' do
98+
it 'is not supported' do
99+
tree = build_tree([1, 2, 3])
100+
expect(tree.respond_to?(:<<)).to be(false)
101+
end
102+
103+
it 'raises when called' do
104+
tree = build_tree([1, 2, 3])
105+
expect { tree << 4 }.to raise_error(NoMethodError)
106+
end
107+
end
96108
end

test/test_fenwicktree.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,12 @@ def test_comparable
120120

121121
assert_equal(-1, left <=> right)
122122
end
123+
124+
def test_shovel_operator_not_supported
125+
tree = build_tree([1, 2, 3])
126+
127+
assert_equal(false, tree.respond_to?(:<<))
128+
assert_raise(NoMethodError) { tree << 4 }
129+
end
123130
end
124131
end

test/test_segmenttree.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,12 @@ def test_comparable
114114

115115
assert_equal(-1, left <=> right)
116116
end
117+
118+
def test_shovel_operator_not_supported
119+
tree = build_tree([1, 2, 3])
120+
121+
assert_equal(false, tree.respond_to?(:<<))
122+
assert_raise(NoMethodError) { tree << 4 }
123+
end
117124
end
118125
end

0 commit comments

Comments
 (0)