fix(b+tree): lower_bound past the last key returns end(), and end() is a valid insert hint - #154
Merged
Conversation
psiha
force-pushed
the
bt/2-lower-bound
branch
2 times, most recently
from
September 9, 2026 10:56
89b0981 to
ecbe4dc
Compare
…s a valid insert hint lower_bound expressed 'one past the end of a leaf' as the first value of the FOLLOWING leaf. Past the LAST leaf there is no following leaf, so it built an iterator from a null node slot - while end() is the last leaf at its num_vals offset. On a non-empty tree, for a key greater than every key present: tree.lower_bound( k ) == tree.end() was false, and tree.insert( tree.lower_bound( k ), k ) access-violated. So the ordinary sorted-container idiom failed for every append. end() is accepted as the append hint: it already works structurally - it is the last leaf at its num_vals offset, which insert() appends to - only the debug hint assertions dereferenced the hint unconditionally. The returned iterator now goes through make_iter( insert_pos_t ), which steps back from the next-insert position and so stays correct across a split that moved the value into the new node.
psiha
force-pushed
the
bt/2-lower-bound
branch
from
September 9, 2026 12:01
ecbe4dc to
4d6d91d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
lower_boundexpressed "one past the end of a leaf" as the first value of the following leaf. Past the last leaf there is no following leaf, so it built an iterator from a null node slot — whileend()is the last leaf at itsnum_valsoffset.On a non-empty tree, for a key greater than every key present:
So the ordinary sorted-container maintenance idiom failed for every append.
end()is now accepted as the append hint. It already worked structurally — it is the last leaf at itsnum_valsoffset, whichinsert()appends to; only the debug hint assertions dereferenced the hint unconditionally. The returned iterator now goes throughmake_iter( insert_pos_t ), which steps back from the next-insert position and so stays correct across a split that moved the value into the new node.Witness:
TEST( bp_tree, hinted_insert_boundaries ).Stacked on #153.