Dataset Viewer
Auto-converted to Parquet Duplicate
prompt
stringlengths
5.33k
21.1k
metadata
dict
context_start_lineno
int64
1
913
line_no
int64
16
984
repo
stringclasses
5 values
id
int64
0
416
target_function_prompt
stringlengths
201
13.6k
function_signature
stringlengths
7
453
solution_position
listlengths
2
2
raw_solution
stringlengths
201
13.6k
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/int_set.jl ##CHUNK 1 Base.copy(s1::IntSet) = copy!(IntSet(), s1) function Base.copy!(to::IntSet, from::IntSet) resize!(to.bits, length(from.bits)) copyto!(to.bits, from.bits) to.inverse = from.inverse return to end Base.eltype(::Type{IntSet}) = Int Base.sizehint!(s::IntSet, n::Integer) = (_resize0!(s.bits, n+1); s) # An internal function for setting the inclusion bit for a given integer n >= 0 @inline function _setint!(s::IntSet, n::Integer, b::Bool) idx = n+1 if idx > length(s.bits) !b && return s # setting a bit to zero outside the set's bits is a no-op newlen = idx + idx>>1 # This operation may overflow; we want saturation _resize0!(s.bits, ifelse(newlen<0, typemax(Int), newlen)) end unsafe_setindex!(s.bits, b, idx) # Use @inbounds once available return s ##CHUNK 2 # An internal function for setting the inclusion bit for a given integer n >= 0 @inline function _setint!(s::IntSet, n::Integer, b::Bool) idx = n+1 if idx > length(s.bits) !b && return s # setting a bit to zero outside the set's bits is a no-op newlen = idx + idx>>1 # This operation may overflow; we want saturation _resize0!(s.bits, ifelse(newlen<0, typemax(Int), newlen)) end unsafe_setindex!(s.bits, b, idx) # Use @inbounds once available return s end # An internal function to resize a bitarray and ensure the newly allocated # elements are zeroed (will become unnecessary if this behavior changes) @inline function _resize0!(b::BitVector, newlen::Integer) len = length(b) resize!(b, newlen) len < newlen && @inbounds(b[len+1:newlen] .= false) # resize! gives dirty memory return b end #FILE: DataStructures.jl/src/heaps/arrays_as_heaps.jl ##CHUNK 1 # Binary min-heap percolate up. function percolate_up!(xs::AbstractArray, i::Integer, x=xs[i], o::Ordering=Forward) @inbounds while (j = heapparent(i)) >= 1 lt(o, x, xs[j]) || break xs[i] = xs[j] i = j end xs[i] = x end @inline percolate_up!(xs::AbstractArray, i::Integer, o::Ordering) = percolate_up!(xs, i, xs[i], o) """ heappop!(v, [ord]) Given a binary heap-ordered array, remove and return the lowest ordered element. For efficiency, this function does not check that the array is indeed heap-ordered. """ function heappop!(xs::AbstractArray, o::Ordering=Forward) ##CHUNK 2 j = r > len || lt(o, xs[l], xs[r]) ? l : r lt(o, xs[j], x) || break xs[i] = xs[j] i = j end xs[i] = x end percolate_down!(xs::AbstractArray, i::Integer, o::Ordering, len::Integer=length(xs)) = percolate_down!(xs, i, xs[i], o, len) # Binary min-heap percolate up. function percolate_up!(xs::AbstractArray, i::Integer, x=xs[i], o::Ordering=Forward) @inbounds while (j = heapparent(i)) >= 1 lt(o, x, xs[j]) || break xs[i] = xs[j] i = j end xs[i] = x end #FILE: DataStructures.jl/src/dict_support.jl ##CHUNK 1 # support functions function not_iterator_of_pairs(kv::T) where T # if the object is not iterable, return true, else check the eltype of the iteration Base.isiterable(T) || return true # else, check if we can check `eltype`: if Base.IteratorEltype(kv) isa Base.HasEltype typ = eltype(kv) if !(typ == Any) return !(typ <: Union{<: Tuple, <: Pair}) end end # we can't check eltype, or eltype is not useful, # so brute force it. return any(x->!isa(x, Union{Tuple,Pair}), kv) end #FILE: DataStructures.jl/src/robin_dict.jl ##CHUNK 1 ``` """ function Base.getkey(h::RobinDict{K,V}, key, default) where {K, V} index = rh_search(h, key) @inbounds return (index < 0) ? default : h.keys[index]::K end # backward shift deletion by not keeping any tombstones function rh_delete!(h::RobinDict{K, V}, index) where {K, V} @assert index > 0 # this assumes that there is a key/value present in the dictionary at index index0 = index sz = length(h.keys) @inbounds while true index0 = (index0 & (sz - 1)) + 1 if isslotempty(h, index0) || calculate_distance(h, index0) == 0 break end end #CURRENT FILE: DataStructures.jl/src/accumulator.jl ##CHUNK 1 end Base.issubset(a::Accumulator, b::Accumulator) = all(b[k] >= v for (k, v) in a) Base.union(a::Accumulator, b::Accumulator, c::Accumulator...) = union(union(a,b), c...) Base.union(a::Accumulator, b::Accumulator) = union!(copy(a), b) function Base.union!(a::Accumulator, b::Accumulator) for (kb, vb) in b va = a[kb] vb >= 0 || throw(MultiplicityException(kb, vb)) va >= 0 || throw(MultiplicityException(kb, va)) a[kb] = max(va, vb) end return a end Base.intersect(a::Accumulator, b::Accumulator, c::Accumulator...) = intersect(intersect(a,b), c...) Base.intersect(a::Accumulator, b::Accumulator) = intersect!(copy(a), b) function Base.show(io::IO, acc::Accumulator{T,V}) where {T,V} ##CHUNK 2 function Base.setdiff(a::Accumulator, b::Accumulator) ret = copy(a) for (k, v) in b v > 0 || throw(MultiplicityException(k, v)) dec!(ret, k, v) drop_nonpositive!(ret, k) end return ret end Base.issubset(a::Accumulator, b::Accumulator) = all(b[k] >= v for (k, v) in a) Base.union(a::Accumulator, b::Accumulator, c::Accumulator...) = union(union(a,b), c...) Base.union(a::Accumulator, b::Accumulator) = union!(copy(a), b) function Base.union!(a::Accumulator, b::Accumulator) for (kb, vb) in b va = a[kb] vb >= 0 || throw(MultiplicityException(kb, vb)) ##CHUNK 3 va >= 0 || throw(MultiplicityException(kb, va)) a[kb] = max(va, vb) end return a end Base.intersect(a::Accumulator, b::Accumulator, c::Accumulator...) = intersect(intersect(a,b), c...) Base.intersect(a::Accumulator, b::Accumulator) = intersect!(copy(a), b) function Base.show(io::IO, acc::Accumulator{T,V}) where {T,V} l = length(acc) if l>0 print(io, "Accumulator(") else print(io,"Accumulator{$T,$V}(") end for (count, (k, v)) in enumerate(acc) print(io, k, " => ", v) if count < l print(io, ", ") ##CHUNK 4 k::K v::V end function Base.showerror(io::IO, err::MultiplicityException) print(io, "When using an `Accumulator` as a multiset, all elements must have positive multiplicity") print(io, " element `$(err.k)` has multiplicity $(err.v)") end drop_nonpositive!(a::Accumulator, k) = (a[k] > 0 || delete!(a.map, k)) function Base.setdiff(a::Accumulator, b::Accumulator) ret = copy(a) for (k, v) in b v > 0 || throw(MultiplicityException(k, v)) dec!(ret, k, v) drop_nonpositive!(ret, k) end return ret Based on the information above, please generate test code for the following function: function Base.intersect!(a::Accumulator, b::Accumulator) for k in union(keys(a), keys(b)) # union not intersection as we want to check both multiplicities va = a[k] vb = b[k] va >= 0 || throw(MultiplicityException(k, va)) vb >= 0 || throw(MultiplicityException(k, vb)) a[k] = min(va, vb) drop_nonpositive!(a, k) # Drop any that ended up zero end return a end
{ "fpath_tuple": [ "DataStructures.jl", "src", "accumulator.jl" ], "ground_truth": "function Base.intersect!(a::Accumulator, b::Accumulator)\n for k in union(keys(a), keys(b)) # union not intersection as we want to check both multiplicities\n va = a[k]\n vb = b[k]\n va >= 0 || throw(MultiplicityException(k, va))\n vb >= 0 || throw(MultiplicityException(k, vb))\n\n a[k] = min(va, vb)\n drop_nonpositive!(a, k) # Drop any that ended up zero\n end\n return a\nend", "task_id": "DataStructures/0" }
230
241
DataStructures.jl
0
function Base.intersect!(a::Accumulator, b::Accumulator) for k in union(keys(a), keys(b)) # union not intersection as we want to check both multiplicities va = a[k] vb = b[k] va >= 0 || throw(MultiplicityException(k, va)) vb >= 0 || throw(MultiplicityException(k, vb)) a[k] = min(va, vb) drop_nonpositive!(a, k) # Drop any that ended up zero end return a end
Base.intersect!(a::Accumulator, b::Accumulator)
[ 230, 241 ]
function Base.intersect!(a::Accumulator, b::Accumulator) for k in union(keys(a), keys(b)) # union not intersection as we want to check both multiplicities va = a[k] vb = b[k] va >= 0 || throw(MultiplicityException(k, va)) vb >= 0 || throw(MultiplicityException(k, vb)) a[k] = min(va, vb) drop_nonpositive!(a, k) # Drop any that ended up zero end return a end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/red_black_tree.jl ##CHUNK 1 x = y.rightChild if y.parent == z x.parent = y else rb_transplant(tree, y, y.rightChild) y.rightChild = z.rightChild y.rightChild.parent = y end rb_transplant(tree, z, y) y.leftChild = z.leftChild y.leftChild.parent = y y.color = z.color end !y_original_color && delete_fix(tree, x) tree.count -= 1 return tree end ##CHUNK 2 x = RBTreeNode{K}() if z.leftChild === tree.nil x = z.rightChild rb_transplant(tree, z, z.rightChild) elseif z.rightChild === tree.nil x = z.leftChild rb_transplant(tree, z, z.leftChild) else y = minimum_node(tree, z.rightChild) y_original_color = y.color x = y.rightChild if y.parent == z x.parent = y else rb_transplant(tree, y, y.rightChild) y.rightChild = z.rightChild y.rightChild.parent = y end ##CHUNK 3 node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color x = RBTreeNode{K}() if z.leftChild === tree.nil x = z.rightChild rb_transplant(tree, z, z.rightChild) elseif z.rightChild === tree.nil x = z.leftChild rb_transplant(tree, z, z.leftChild) else y = minimum_node(tree, z.rightChild) y_original_color = y.color ##CHUNK 4 function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color ##CHUNK 5 rb_transplant(tree, z, y) y.leftChild = z.leftChild y.leftChild.parent = y y.color = z.color end !y_original_color && delete_fix(tree, x) tree.count -= 1 return tree end Base.in(key, tree::RBTree) = haskey(tree, key) """ getindex(tree, ind) Gets the key present at index `ind` of the tree. Indexing is done in increasing order of key. """ function Base.getindex(tree::RBTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(ArgumentError("$ind should be in between 1 and $(tree.count)")) #FILE: DataStructures.jl/src/splay_tree.jl ##CHUNK 1 node = SplayTreeNode{K}(d) y = nothing x = tree.root while x !== nothing y = x if node.data > x.data x = x.rightChild else x = x.leftChild end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node ##CHUNK 2 end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node end splay!(tree, node) tree.count += 1 return tree end function Base.getindex(tree::SplayTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)")) function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing}) if (node != nothing) #FILE: DataStructures.jl/benchmark/bench_sparse_int_set.jl ##CHUNK 1 end function iterate_two_exclude_one_bench(x,y,z) t = 0 for (ix, iy) in zip(x, y, exclude=(z,)) t += ix + iy end return t end x_y_z_setup = ( Random.seed!(1234); x = SparseIntSet(rand(1:30000, 1000)); y = SparseIntSet(rand(1:30000, 1000)); z = SparseIntSet(rand(1:30000, 1000)); ) suite["iterate one"] = @benchmarkable iterate_one_bench(x) setup=x_y_z_setup suite["iterate two"] = #CURRENT FILE: DataStructures.jl/src/avl_tree.jl ##CHUNK 1 α = y.rightChild y.rightChild = z z.leftChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end """ minimum_node(tree::AVLTree, node::AVLTreeNode) Returns the AVLTreeNode with minimum value in subtree of `node`. """ function minimum_node(node::Union{AVLTreeNode, Nothing}) while node != nothing && node.leftChild != nothing node = node.leftChild end return node ##CHUNK 2 Performs a left-rotation on `node_x`, updates height of the nodes, and returns the rotated node. """ """ right_rotate(node_x::AVLTreeNode) Performs a right-rotation on `node_x`, updates height of the nodes, and returns the rotated node. """ function right_rotate(z::AVLTreeNode) y = z.leftChild α = y.rightChild y.rightChild = z z.leftChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end Based on the information above, please generate test code for the following function: function left_rotate(z::AVLTreeNode) y = z.rightChild α = y.leftChild y.leftChild = z z.rightChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end
{ "fpath_tuple": [ "DataStructures.jl", "src", "avl_tree.jl" ], "ground_truth": "function left_rotate(z::AVLTreeNode)\n y = z.rightChild\n α = y.leftChild\n y.leftChild = z\n z.rightChild = α\n z.height = compute_height(z)\n y.height = compute_height(y)\n z.subsize = compute_subtree_size(z)\n y.subsize = compute_subtree_size(y)\n return y\nend", "task_id": "DataStructures/1" }
83
93
DataStructures.jl
1
function left_rotate(z::AVLTreeNode) y = z.rightChild α = y.leftChild y.leftChild = z z.rightChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end
left_rotate(z::AVLTreeNode)
[ 83, 93 ]
function left_rotate(z::AVLTreeNode) y = z.rightChild α = y.leftChild y.leftChild = z z.rightChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/red_black_tree.jl ##CHUNK 1 x = RBTreeNode{K}() if z.leftChild === tree.nil x = z.rightChild rb_transplant(tree, z, z.rightChild) elseif z.rightChild === tree.nil x = z.leftChild rb_transplant(tree, z, z.leftChild) else y = minimum_node(tree, z.rightChild) y_original_color = y.color x = y.rightChild if y.parent == z x.parent = y else rb_transplant(tree, y, y.rightChild) y.rightChild = z.rightChild y.rightChild.parent = y end ##CHUNK 2 x = y.rightChild if y.parent == z x.parent = y else rb_transplant(tree, y, y.rightChild) y.rightChild = z.rightChild y.rightChild.parent = y end rb_transplant(tree, z, y) y.leftChild = z.leftChild y.leftChild.parent = y y.color = z.color end !y_original_color && delete_fix(tree, x) tree.count -= 1 return tree end ##CHUNK 3 node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color x = RBTreeNode{K}() if z.leftChild === tree.nil x = z.rightChild rb_transplant(tree, z, z.rightChild) elseif z.rightChild === tree.nil x = z.leftChild rb_transplant(tree, z, z.leftChild) else y = minimum_node(tree, z.rightChild) y_original_color = y.color ##CHUNK 4 function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color #FILE: DataStructures.jl/src/splay_tree.jl ##CHUNK 1 node = SplayTreeNode{K}(d) y = nothing x = tree.root while x !== nothing y = x if node.data > x.data x = x.rightChild else x = x.leftChild end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node ##CHUNK 2 end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node end splay!(tree, node) tree.count += 1 return tree end function Base.getindex(tree::SplayTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)")) function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing}) if (node != nothing) #FILE: DataStructures.jl/benchmark/bench_sparse_int_set.jl ##CHUNK 1 end function iterate_two_exclude_one_bench(x,y,z) t = 0 for (ix, iy) in zip(x, y, exclude=(z,)) t += ix + iy end return t end x_y_z_setup = ( Random.seed!(1234); x = SparseIntSet(rand(1:30000, 1000)); y = SparseIntSet(rand(1:30000, 1000)); z = SparseIntSet(rand(1:30000, 1000)); ) suite["iterate one"] = @benchmarkable iterate_one_bench(x) setup=x_y_z_setup suite["iterate two"] = #CURRENT FILE: DataStructures.jl/src/avl_tree.jl ##CHUNK 1 Performs a left-rotation on `node_x`, updates height of the nodes, and returns the rotated node. """ function left_rotate(z::AVLTreeNode) y = z.rightChild α = y.leftChild y.leftChild = z z.rightChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end """ right_rotate(node_x::AVLTreeNode) Performs a right-rotation on `node_x`, updates height of the nodes, and returns the rotated node. """ ##CHUNK 2 else L = get_subsize(node.leftChild) R = get_subsize(node.rightChild) return (L + R + Int32(1)) end end """ left_rotate(node_x::AVLTreeNode) Performs a left-rotation on `node_x`, updates height of the nodes, and returns the rotated node. """ function left_rotate(z::AVLTreeNode) y = z.rightChild α = y.leftChild y.leftChild = z z.rightChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) ##CHUNK 3 y.subsize = compute_subtree_size(y) return y end """ right_rotate(node_x::AVLTreeNode) Performs a right-rotation on `node_x`, updates height of the nodes, and returns the rotated node. """ """ minimum_node(tree::AVLTree, node::AVLTreeNode) Returns the AVLTreeNode with minimum value in subtree of `node`. """ function minimum_node(node::Union{AVLTreeNode, Nothing}) while node != nothing && node.leftChild != nothing node = node.leftChild end return node Based on the information above, please generate test code for the following function: function right_rotate(z::AVLTreeNode) y = z.leftChild α = y.rightChild y.rightChild = z z.leftChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end
{ "fpath_tuple": [ "DataStructures.jl", "src", "avl_tree.jl" ], "ground_truth": "function right_rotate(z::AVLTreeNode)\n y = z.leftChild\n α = y.rightChild\n y.rightChild = z\n z.leftChild = α\n z.height = compute_height(z)\n y.height = compute_height(y)\n z.subsize = compute_subtree_size(z)\n y.subsize = compute_subtree_size(y)\n return y\nend", "task_id": "DataStructures/2" }
100
110
DataStructures.jl
2
function right_rotate(z::AVLTreeNode) y = z.leftChild α = y.rightChild y.rightChild = z z.leftChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end
right_rotate(z::AVLTreeNode)
[ 100, 110 ]
function right_rotate(z::AVLTreeNode) y = z.leftChild α = y.rightChild y.rightChild = z z.leftChild = α z.height = compute_height(z) y.height = compute_height(y) z.subsize = compute_subtree_size(z) y.subsize = compute_subtree_size(y) return y end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/splay_tree.jl ##CHUNK 1 prev = nothing while node != nothing && node.data != d prev = node if node.data < d node = node.rightChild else node = node.leftChild end end return (node == nothing) ? prev : node end function Base.haskey(tree::SplayTree{K}, d::K) where K node = tree.root if node === nothing return false else node = search_node(tree, d) (node === nothing) && return false is_found = (node.data == d) ##CHUNK 2 x = maximum_node(s) splay!(tree, x) x.rightChild = t t.parent = x return x end end function search_node(tree::SplayTree{K}, d::K) where K node = tree.root prev = nothing while node != nothing && node.data != d prev = node if node.data < d node = node.rightChild else node = node.leftChild end end return (node == nothing) ? prev : node ##CHUNK 3 node = SplayTreeNode{K}(d) y = nothing x = tree.root while x !== nothing y = x if node.data > x.data x = x.rightChild else x = x.leftChild end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node ##CHUNK 4 end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node end splay!(tree, node) tree.count += 1 return tree end function Base.getindex(tree::SplayTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)")) function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing}) if (node != nothing) #FILE: DataStructures.jl/src/red_black_tree.jl ##CHUNK 1 """ search_node(tree, key) function search_node(tree::RBTree{K}, d::K) where K node = tree.root while node !== tree.nil && d != node.data if d < node.data node = node.leftChild else node = node.rightChild end end return node end """ haskey(tree, key) Returns true if `key` is present in the `tree`, else returns false. """ ##CHUNK 2 function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color ##CHUNK 3 end RBTree() = RBTree{Any}() Base.length(tree::RBTree) = tree.count """ search_node(tree, key) Returns the last visited node, while traversing through in binary-search-tree fashion looking for `key`. """ search_node(tree, key) function search_node(tree::RBTree{K}, d::K) where K node = tree.root while node !== tree.nil && d != node.data if d < node.data node = node.leftChild else node = node.rightChild ##CHUNK 4 node = node.leftChild end return node end """ delete!(tree::RBTree, key) Deletes `key` from `tree`, if present, else returns the unmodified tree. """ function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data ##CHUNK 5 function insert_node!(tree::RBTree, node::RBTreeNode) node_y = nothing node_x = tree.root while node_x !== tree.nil node_y = node_x if node.data < node_x.data node_x = node_x.leftChild else node_x = node_x.rightChild end end node.parent = node_y if node_y == nothing tree.root = node elseif node.data < node_y.data node_y.leftChild = node else node_y.rightChild = node #CURRENT FILE: DataStructures.jl/src/avl_tree.jl ##CHUNK 1 julia> sorted_rank(tree, 17) 9 ``` """ function sorted_rank(tree::AVLTree{K}, key::K) where K !haskey(tree, key) && throw(KeyError(key)) node = tree.root rank = 0 while node.data != key if (node.data < key) rank += (1 + get_subsize(node.leftChild)) node = node.rightChild else node = node.leftChild end end rank += (1 + get_subsize(node.leftChild)) return rank end Based on the information above, please generate test code for the following function: function search_node(tree::AVLTree{K}, d::K) where K prev = nothing node = tree.root while node != nothing && node.data != nothing && node.data != d prev = node if d < node.data node = node.leftChild else node = node.rightChild end end return (node == nothing) ? prev : node end
{ "fpath_tuple": [ "DataStructures.jl", "src", "avl_tree.jl" ], "ground_truth": "function search_node(tree::AVLTree{K}, d::K) where K\n prev = nothing\n node = tree.root\n while node != nothing && node.data != nothing && node.data != d\n\n prev = node\n if d < node.data\n node = node.leftChild\n else\n node = node.rightChild\n end\n end\n\n return (node == nothing) ? prev : node\nend", "task_id": "DataStructures/3" }
124
138
DataStructures.jl
3
function search_node(tree::AVLTree{K}, d::K) where K prev = nothing node = tree.root while node != nothing && node.data != nothing && node.data != d prev = node if d < node.data node = node.leftChild else node = node.rightChild end end return (node == nothing) ? prev : node end
search_node(tree::AVLTree{K}, d::K) where K
[ 124, 138 ]
function search_node(tree::AVLTree{K}, d::K) where K prev = nothing node = tree.root while node != nothing && node.data != nothing && node.data != d prev = node if d < node.data node = node.leftChild else node = node.rightChild end end return (node == nothing) ? prev : node end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/red_black_tree.jl ##CHUNK 1 """ search_node(tree, key) function search_node(tree::RBTree{K}, d::K) where K node = tree.root while node !== tree.nil && d != node.data if d < node.data node = node.leftChild else node = node.rightChild end end return node end """ haskey(tree, key) Returns true if `key` is present in the `tree`, else returns false. """ ##CHUNK 2 function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color #CURRENT FILE: DataStructures.jl/src/avl_tree.jl ##CHUNK 1 node.height = compute_height(node) balance = get_balance(node) if balance > 1 if get_balance(node.leftChild) >= 0 return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if get_balance(node.rightChild) <= 0 return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end ##CHUNK 2 result = node.leftChild return result else result = minimum_node(node.rightChild) node.data = result.data node.rightChild = delete_node!(node.rightChild, result.data) end end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if get_balance(node.leftChild) >= 0 return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end ##CHUNK 3 function delete_node!(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = delete_node!(node.leftChild, key) elseif key > node.data node.rightChild = delete_node!(node.rightChild, key) else if node.leftChild == nothing result = node.rightChild return result elseif node.rightChild == nothing result = node.leftChild return result else result = minimum_node(node.rightChild) node.data = result.data node.rightChild = delete_node!(node.rightChild, result.data) end end node.subsize = compute_subtree_size(node) ##CHUNK 4 end if balance < -1 if get_balance(node.rightChild) <= 0 return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end """ delete!(tree::AVLTree{K}, k::K) where K Delete key `k` from `tree` AVL tree. """ function Base.delete!(tree::AVLTree{K}, k::K) where K ##CHUNK 5 """ push!(tree::AVLTree{K}, key) where K Insert `key` in AVL tree `tree`. """ function Base.push!(tree::AVLTree{K}, key) where K key0 = convert(K, key) insert!(tree, key0) end function delete_node!(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = delete_node!(node.leftChild, key) elseif key > node.data node.rightChild = delete_node!(node.rightChild, key) else if node.leftChild == nothing result = node.rightChild return result elseif node.rightChild == nothing ##CHUNK 6 julia> for k in 1:2:20 push!(tree, k) end julia> sorted_rank(tree, 17) 9 ``` """ function sorted_rank(tree::AVLTree{K}, key::K) where K !haskey(tree, key) && throw(KeyError(key)) node = tree.root rank = 0 while node.data != key if (node.data < key) rank += (1 + get_subsize(node.leftChild)) node = node.rightChild else node = node.leftChild end end ##CHUNK 7 return node end function search_node(tree::AVLTree{K}, d::K) where K prev = nothing node = tree.root while node != nothing && node.data != nothing && node.data != d prev = node if d < node.data node = node.leftChild else node = node.rightChild end end return (node == nothing) ? prev : node end """ ##CHUNK 8 node = tree.root rank = 0 while node.data != key if (node.data < key) rank += (1 + get_subsize(node.leftChild)) node = node.rightChild else node = node.leftChild end end rank += (1 + get_subsize(node.leftChild)) return rank end """ getindex(tree::AVLTree{K}, ind::Integer) where K Considering the elements of `tree` sorted, returns the `ind`-th element in `tree`. Search operation is performed in \$O(\\log n)\$ time complexity. Based on the information above, please generate test code for the following function: function insert_node(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = insert_node(node.leftChild, key) else node.rightChild = insert_node(node.rightChild, key) end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if key < node.leftChild.data return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if key > node.rightChild.data return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end
{ "fpath_tuple": [ "DataStructures.jl", "src", "avl_tree.jl" ], "ground_truth": "function insert_node(node::AVLTreeNode{K}, key::K) where K\n if key < node.data\n node.leftChild = insert_node(node.leftChild, key)\n else\n node.rightChild = insert_node(node.rightChild, key)\n end\n\n node.subsize = compute_subtree_size(node)\n node.height = compute_height(node)\n balance = get_balance(node)\n\n if balance > 1\n if key < node.leftChild.data\n return right_rotate(node)\n else\n node.leftChild = left_rotate(node.leftChild)\n return right_rotate(node)\n end\n end\n\n if balance < -1\n if key > node.rightChild.data\n return left_rotate(node)\n else\n node.rightChild = right_rotate(node.rightChild)\n return left_rotate(node)\n end\n end\n\n return node\nend", "task_id": "DataStructures/4" }
162
192
DataStructures.jl
4
function insert_node(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = insert_node(node.leftChild, key) else node.rightChild = insert_node(node.rightChild, key) end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if key < node.leftChild.data return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if key > node.rightChild.data return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end
insert_node(node::AVLTreeNode{K}, key::K) where K
[ 162, 192 ]
function insert_node(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = insert_node(node.leftChild, key) else node.rightChild = insert_node(node.rightChild, key) end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if key < node.leftChild.data return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if key > node.rightChild.data return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/splay_tree.jl ##CHUNK 1 x = maximum_node(s) splay!(tree, x) x.rightChild = t t.parent = x return x end end function search_node(tree::SplayTree{K}, d::K) where K node = tree.root prev = nothing while node != nothing && node.data != d prev = node if node.data < d node = node.rightChild else node = node.leftChild end end return (node == nothing) ? prev : node ##CHUNK 2 prev = nothing while node != nothing && node.data != d prev = node if node.data < d node = node.rightChild else node = node.leftChild end end return (node == nothing) ? prev : node end function Base.haskey(tree::SplayTree{K}, d::K) where K node = tree.root if node === nothing return false else node = search_node(tree, d) (node === nothing) && return false is_found = (node.data == d) #FILE: DataStructures.jl/src/red_black_tree.jl ##CHUNK 1 """ search_node(tree, key) function search_node(tree::RBTree{K}, d::K) where K node = tree.root while node !== tree.nil && d != node.data if d < node.data node = node.leftChild else node = node.rightChild end end return node end """ haskey(tree, key) Returns true if `key` is present in the `tree`, else returns false. """ ##CHUNK 2 function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color #CURRENT FILE: DataStructures.jl/src/avl_tree.jl ##CHUNK 1 function insert_node(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = insert_node(node.leftChild, key) else node.rightChild = insert_node(node.rightChild, key) end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if key < node.leftChild.data return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end ##CHUNK 2 balance = get_balance(node) if balance > 1 if key < node.leftChild.data return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if key > node.rightChild.data return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end ##CHUNK 3 """ in(key, tree::AVLTree) `In` infix operator for `key` and `tree` types. Analogous to [`haskey(tree::AVLTree{K}, k::K) where K`](@ref). """ Base.in(key, tree::AVLTree) = haskey(tree, key) function insert_node(node::Nothing, key::K) where K return AVLTreeNode{K}(key) end function insert_node(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = insert_node(node.leftChild, key) else node.rightChild = insert_node(node.rightChild, key) end node.subsize = compute_subtree_size(node) node.height = compute_height(node) ##CHUNK 4 if balance < -1 if key > node.rightChild.data return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end function Base.insert!(tree::AVLTree{K}, d::K) where K haskey(tree, d) && return tree tree.root = insert_node(tree.root, d) tree.count += 1 return tree end ##CHUNK 5 return node end function search_node(tree::AVLTree{K}, d::K) where K prev = nothing node = tree.root while node != nothing && node.data != nothing && node.data != d prev = node if d < node.data node = node.leftChild else node = node.rightChild end end return (node == nothing) ? prev : node end """ ##CHUNK 6 end julia> sorted_rank(tree, 17) 9 ``` """ function sorted_rank(tree::AVLTree{K}, key::K) where K !haskey(tree, key) && throw(KeyError(key)) node = tree.root rank = 0 while node.data != key if (node.data < key) rank += (1 + get_subsize(node.leftChild)) node = node.rightChild else node = node.leftChild end end rank += (1 + get_subsize(node.leftChild)) return rank Based on the information above, please generate test code for the following function: function delete_node!(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = delete_node!(node.leftChild, key) elseif key > node.data node.rightChild = delete_node!(node.rightChild, key) else if node.leftChild == nothing result = node.rightChild return result elseif node.rightChild == nothing result = node.leftChild return result else result = minimum_node(node.rightChild) node.data = result.data node.rightChild = delete_node!(node.rightChild, result.data) end end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if get_balance(node.leftChild) >= 0 return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if get_balance(node.rightChild) <= 0 return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end
{ "fpath_tuple": [ "DataStructures.jl", "src", "avl_tree.jl" ], "ground_truth": "function delete_node!(node::AVLTreeNode{K}, key::K) where K\n if key < node.data\n node.leftChild = delete_node!(node.leftChild, key)\n elseif key > node.data\n node.rightChild = delete_node!(node.rightChild, key)\n else\n if node.leftChild == nothing\n result = node.rightChild\n return result\n elseif node.rightChild == nothing\n result = node.leftChild\n return result\n else\n result = minimum_node(node.rightChild)\n node.data = result.data\n node.rightChild = delete_node!(node.rightChild, result.data)\n end\n end\n\n node.subsize = compute_subtree_size(node)\n node.height = compute_height(node)\n balance = get_balance(node)\n\n if balance > 1\n if get_balance(node.leftChild) >= 0\n return right_rotate(node)\n else\n node.leftChild = left_rotate(node.leftChild)\n return right_rotate(node)\n end\n end\n\n if balance < -1\n if get_balance(node.rightChild) <= 0\n return left_rotate(node)\n else\n node.rightChild = right_rotate(node.rightChild)\n return left_rotate(node)\n end\n end\n\n return node\nend", "task_id": "DataStructures/5" }
212
254
DataStructures.jl
5
function delete_node!(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = delete_node!(node.leftChild, key) elseif key > node.data node.rightChild = delete_node!(node.rightChild, key) else if node.leftChild == nothing result = node.rightChild return result elseif node.rightChild == nothing result = node.leftChild return result else result = minimum_node(node.rightChild) node.data = result.data node.rightChild = delete_node!(node.rightChild, result.data) end end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if get_balance(node.leftChild) >= 0 return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if get_balance(node.rightChild) <= 0 return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end
delete_node!(node::AVLTreeNode{K}, key::K) where K
[ 212, 254 ]
function delete_node!(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = delete_node!(node.leftChild, key) elseif key > node.data node.rightChild = delete_node!(node.rightChild, key) else if node.leftChild == nothing result = node.rightChild return result elseif node.rightChild == nothing result = node.leftChild return result else result = minimum_node(node.rightChild) node.data = result.data node.rightChild = delete_node!(node.rightChild, result.data) end end node.subsize = compute_subtree_size(node) node.height = compute_height(node) balance = get_balance(node) if balance > 1 if get_balance(node.leftChild) >= 0 return right_rotate(node) else node.leftChild = left_rotate(node.leftChild) return right_rotate(node) end end if balance < -1 if get_balance(node.rightChild) <= 0 return left_rotate(node) else node.rightChild = right_rotate(node.rightChild) return left_rotate(node) end end return node end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/red_black_tree.jl ##CHUNK 1 """ search_node(tree, key) function search_node(tree::RBTree{K}, d::K) where K node = tree.root while node !== tree.nil && d != node.data if d < node.data node = node.leftChild else node = node.rightChild end end return node end """ haskey(tree, key) Returns true if `key` is present in the `tree`, else returns false. """ ##CHUNK 2 end RBTree() = RBTree{Any}() Base.length(tree::RBTree) = tree.count """ search_node(tree, key) Returns the last visited node, while traversing through in binary-search-tree fashion looking for `key`. """ search_node(tree, key) function search_node(tree::RBTree{K}, d::K) where K node = tree.root while node !== tree.nil && d != node.data if d < node.data node = node.leftChild else node = node.rightChild ##CHUNK 3 function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data node = node.leftChild else node = node.rightChild end end (z === tree.nil) && return tree y = z y_original_color = y.color ##CHUNK 4 node = node.leftChild end return node end """ delete!(tree::RBTree, key) Deletes `key` from `tree`, if present, else returns the unmodified tree. """ function Base.delete!(tree::RBTree{K}, d::K) where K z = tree.nil node = tree.root while node !== tree.nil if node.data == d z = node end if d < node.data #FILE: DataStructures.jl/src/splay_tree.jl ##CHUNK 1 prev = nothing while node != nothing && node.data != d prev = node if node.data < d node = node.rightChild else node = node.leftChild end end return (node == nothing) ? prev : node end function Base.haskey(tree::SplayTree{K}, d::K) where K node = tree.root if node === nothing return false else node = search_node(tree, d) (node === nothing) && return false is_found = (node.data == d) ##CHUNK 2 x = maximum_node(s) splay!(tree, x) x.rightChild = t t.parent = x return x end end function search_node(tree::SplayTree{K}, d::K) where K node = tree.root prev = nothing while node != nothing && node.data != d prev = node if node.data < d node = node.rightChild else node = node.leftChild end end return (node == nothing) ? prev : node ##CHUNK 3 end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node end splay!(tree, node) tree.count += 1 return tree end function Base.getindex(tree::SplayTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)")) function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing}) if (node != nothing) #CURRENT FILE: DataStructures.jl/src/avl_tree.jl ##CHUNK 1 return node end function search_node(tree::AVLTree{K}, d::K) where K prev = nothing node = tree.root while node != nothing && node.data != nothing && node.data != d prev = node if d < node.data node = node.leftChild else node = node.rightChild end end return (node == nothing) ? prev : node end """ ##CHUNK 2 function delete_node!(node::AVLTreeNode{K}, key::K) where K if key < node.data node.leftChild = delete_node!(node.leftChild, key) elseif key > node.data node.rightChild = delete_node!(node.rightChild, key) else if node.leftChild == nothing result = node.rightChild return result elseif node.rightChild == nothing result = node.leftChild return result else result = minimum_node(node.rightChild) node.data = result.data node.rightChild = delete_node!(node.rightChild, result.data) end end ##CHUNK 3 """ minimum_node(tree::AVLTree, node::AVLTreeNode) Returns the AVLTreeNode with minimum value in subtree of `node`. """ function minimum_node(node::Union{AVLTreeNode, Nothing}) while node != nothing && node.leftChild != nothing node = node.leftChild end return node end function search_node(tree::AVLTree{K}, d::K) where K prev = nothing node = tree.root while node != nothing && node.data != nothing && node.data != d prev = node if d < node.data Based on the information above, please generate test code for the following function: function sorted_rank(tree::AVLTree{K}, key::K) where K !haskey(tree, key) && throw(KeyError(key)) node = tree.root rank = 0 while node.data != key if (node.data < key) rank += (1 + get_subsize(node.leftChild)) node = node.rightChild else node = node.leftChild end end rank += (1 + get_subsize(node.leftChild)) return rank end
{ "fpath_tuple": [ "DataStructures.jl", "src", "avl_tree.jl" ], "ground_truth": "function sorted_rank(tree::AVLTree{K}, key::K) where K\n !haskey(tree, key) && throw(KeyError(key))\n node = tree.root\n rank = 0\n while node.data != key\n if (node.data < key)\n rank += (1 + get_subsize(node.leftChild))\n node = node.rightChild\n else\n node = node.leftChild\n end\n end\n rank += (1 + get_subsize(node.leftChild))\n return rank\nend", "task_id": "DataStructures/6" }
290
304
DataStructures.jl
6
function sorted_rank(tree::AVLTree{K}, key::K) where K !haskey(tree, key) && throw(KeyError(key)) node = tree.root rank = 0 while node.data != key if (node.data < key) rank += (1 + get_subsize(node.leftChild)) node = node.rightChild else node = node.leftChild end end rank += (1 + get_subsize(node.leftChild)) return rank end
sorted_rank(tree::AVLTree{K}, key::K) where K
[ 290, 304 ]
function sorted_rank(tree::AVLTree{K}, key::K) where K !haskey(tree, key) && throw(KeyError(key)) node = tree.root rank = 0 while node.data != key if (node.data < key) rank += (1 + get_subsize(node.leftChild)) node = node.rightChild else node = node.leftChild end end rank += (1 + get_subsize(node.leftChild)) return rank end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/src/splay_tree.jl ##CHUNK 1 end end node.parent = y if y === nothing tree.root = node elseif node.data < y.data y.leftChild = node else y.rightChild = node end splay!(tree, node) tree.count += 1 return tree end function Base.getindex(tree::SplayTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)")) function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing}) if (node != nothing) ##CHUNK 2 end splay!(tree, node) tree.count += 1 return tree end function Base.getindex(tree::SplayTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)")) function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing}) if (node != nothing) left = traverse_tree_inorder(node.leftChild) right = traverse_tree_inorder(node.rightChild) append!(push!(left, node.data), right) else return K[] end end arr = traverse_tree_inorder(tree.root) return @inbounds arr[ind] end #FILE: DataStructures.jl/src/red_black_tree.jl ##CHUNK 1 Base.in(key, tree::RBTree) = haskey(tree, key) """ getindex(tree, ind) Gets the key present at index `ind` of the tree. Indexing is done in increasing order of key. """ function Base.getindex(tree::RBTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(ArgumentError("$ind should be in between 1 and $(tree.count)")) function traverse_tree_inorder(node::RBTreeNode{K}) where K if (node !== tree.nil) left = traverse_tree_inorder(node.leftChild) right = traverse_tree_inorder(node.rightChild) append!(push!(left, node.data), right) else return K[] end end arr = traverse_tree_inorder(tree.root) ##CHUNK 2 rb_transplant(tree, z, y) y.leftChild = z.leftChild y.leftChild.parent = y y.color = z.color end !y_original_color && delete_fix(tree, x) tree.count -= 1 return tree end Base.in(key, tree::RBTree) = haskey(tree, key) """ getindex(tree, ind) Gets the key present at index `ind` of the tree. Indexing is done in increasing order of key. """ function Base.getindex(tree::RBTree{K}, ind) where K @boundscheck (1 <= ind <= tree.count) || throw(ArgumentError("$ind should be in between 1 and $(tree.count)")) #FILE: DataStructures.jl/src/fenwick.jl ##CHUNK 1 5 ``` """ function prefixsum(ft::FenwickTree{T}, ind::Integer) where T sum = zero(T) ind < 1 && return sum i = ind n = ft.n @boundscheck 1 <= i <= n || throw(ArgumentError("$i should be in between 1 and $n")) @inbounds while i > 0 sum += ft.bi_tree[i] i -= i&(-i) end sum end Base.getindex(ft::FenwickTree{T}, ind::Integer) where T = prefixsum(ft, ind) #FILE: DataStructures.jl/src/robin_dict.jl ##CHUNK 1 function Base.delete!(h::RobinDict{K, V}, key0) where {K, V} key = convert(K, key0) index = rh_search(h, key) if index > 0 rh_delete!(h, index) end return h end function get_next_filled(h::RobinDict, i) L = length(h.keys) (1 <= i <= L) || return 0 for j = i:L @inbounds if isslotfilled(h, j) return j end end return 0 end ##CHUNK 2 L = length(h.keys) (1 <= i <= L) || return 0 for j = i:L @inbounds if isslotfilled(h, j) return j end end return 0 end Base.@propagate_inbounds _iterate(t::RobinDict{K,V}, i) where {K,V} = i == 0 ? nothing : (Pair{K,V}(t.keys[i],t.vals[i]), i == typemax(Int) ? 0 : get_next_filled(t, i+1)) Base.@propagate_inbounds function Base.iterate(t::RobinDict) _iterate(t, t.idxfloor) end Base.@propagate_inbounds Base.iterate(t::RobinDict, i) = _iterate(t, get_next_filled(t, i)) function _merge_kvtypes(d, others...) K, V = keytype(d), valtype(d) for other in others K = promote_type(K, keytype(other)) #FILE: DataStructures.jl/src/circular_buffer.jl ##CHUNK 1 cb.length = 0 return cb end Base.@propagate_inbounds function _buffer_index_checked(cb::CircularBuffer, i::Int) @boundscheck if i < 1 || i > cb.length throw(BoundsError(cb, i)) end _buffer_index(cb, i) end @inline function _buffer_index(cb::CircularBuffer, i::Int) n = cb.capacity idx = cb.first + i - 1 return ifelse(idx > n, idx - n, idx) end """ cb[i] #FILE: DataStructures.jl/src/int_set.jl ##CHUNK 1 end function Base.symdiff!(s1::IntSet, s2::IntSet) e = _matchlength!(s1.bits, length(s2.bits)) map!(⊻, s1.bits, s1.bits, s2.bits) s2.inverse && (s1.inverse = !s1.inverse) append!(s1.bits, e) return s1 end function Base.in(n::Integer, s::IntSet) idx = n+1 if 1 <= idx <= length(s.bits) unsafe_getindex(s.bits, idx) != s.inverse else ifelse((idx <= 0) | (idx > typemax(Int)), false, s.inverse) end end function findnextidx(s::IntSet, i::Int, invert=false) if s.inverse ⊻ invert #CURRENT FILE: DataStructures.jl/src/avl_tree.jl ##CHUNK 1 # computes the height of the subtree, which basically is # one added the maximum of the height of the left subtree and right subtree compute_height(node::AVLTreeNode) = Int8(1) + max(get_height(node.leftChild), get_height(node.rightChild)) get_subsize(node::AVLTreeNode_or_null) = (node == nothing) ? Int32(0) : node.subsize # compute the subtree size function compute_subtree_size(node::AVLTreeNode_or_null) if node == nothing return Int32(0) else L = get_subsize(node.leftChild) R = get_subsize(node.rightChild) return (L + R + Int32(1)) end end """ left_rotate(node_x::AVLTreeNode) Based on the information above, please generate test code for the following function: function Base.getindex(tree::AVLTree{K}, ind::Integer) where K @boundscheck (1 <= ind <= tree.count) || throw(BoundsError("$ind should be in between 1 and $(tree.count)")) function traverse_tree(node::AVLTreeNode_or_null, idx) if (node != nothing) L = get_subsize(node.leftChild) if idx <= L return traverse_tree(node.leftChild, idx) elseif idx == L + 1 return node.data else return traverse_tree(node.rightChild, idx - L - 1) end end end value = traverse_tree(tree.root, ind) return value end
{ "fpath_tuple": [ "DataStructures.jl", "src", "avl_tree.jl" ], "ground_truth": "function Base.getindex(tree::AVLTree{K}, ind::Integer) where K\n @boundscheck (1 <= ind <= tree.count) || throw(BoundsError(\"$ind should be in between 1 and $(tree.count)\"))\n function traverse_tree(node::AVLTreeNode_or_null, idx)\n if (node != nothing)\n L = get_subsize(node.leftChild)\n if idx <= L\n return traverse_tree(node.leftChild, idx)\n elseif idx == L + 1\n return node.data\n else\n return traverse_tree(node.rightChild, idx - L - 1)\n end\n end\n end\n value = traverse_tree(tree.root, ind)\n return value\nend", "task_id": "DataStructures/7" }
329
345
DataStructures.jl
7
function Base.getindex(tree::AVLTree{K}, ind::Integer) where K @boundscheck (1 <= ind <= tree.count) || throw(BoundsError("$ind should be in between 1 and $(tree.count)")) function traverse_tree(node::AVLTreeNode_or_null, idx) if (node != nothing) L = get_subsize(node.leftChild) if idx <= L return traverse_tree(node.leftChild, idx) elseif idx == L + 1 return node.data else return traverse_tree(node.rightChild, idx - L - 1) end end end value = traverse_tree(tree.root, ind) return value end
Base.getindex(tree::AVLTree{K}, ind::Integer) where K
[ 329, 345 ]
function Base.getindex(tree::AVLTree{K}, ind::Integer) where K @boundscheck (1 <= ind <= tree.count) || throw(BoundsError("$ind should be in between 1 and $(tree.count)")) function traverse_tree(node::AVLTreeNode_or_null, idx) if (node != nothing) L = get_subsize(node.leftChild) if idx <= L return traverse_tree(node.leftChild, idx) elseif idx == L + 1 return node.data else return traverse_tree(node.rightChild, idx - L - 1) end end end value = traverse_tree(tree.root, ind) return value end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/test/test_sorted_containers.jl ##CHUNK 1 end remove_spaces(s::String) = replace(s, r"\s+"=>"") ## Function checkcorrectness checks a balanced tree for correctness. function checkcorrectness(t::DataStructures.BalancedTree23{K,D,Ord}, allowdups=false) where {K,D,Ord <: Ordering} dsz = size(t.data, 1) tsz = size(t.tree, 1) r = t.rootloc bfstreenodes = Vector{Int}() tdpth = t.depth intree = BitSet() levstart = Vector{Int}(undef, tdpth) push!(bfstreenodes, r) levstart[1] = 1 ##CHUNK 2 allowdups=false) where {K,D,Ord <: Ordering} dsz = size(t.data, 1) tsz = size(t.tree, 1) r = t.rootloc bfstreenodes = Vector{Int}() tdpth = t.depth intree = BitSet() levstart = Vector{Int}(undef, tdpth) push!(bfstreenodes, r) levstart[1] = 1 push!(intree, r) for curdepth = 2 : tdpth levstart[curdepth] = size(bfstreenodes, 1) + 1 for l = levstart[curdepth - 1] : levstart[curdepth] - 1 anc = bfstreenodes[l] c1 = t.tree[anc].child1 if in(c1, intree) println("anc = $anc c1 = $c1") throw(ErrorException("Tree contains loops 1")) end #FILE: DataStructures.jl/src/splay_tree.jl ##CHUNK 1 (x == nothing) && return tree t = nothing s = nothing splay!(tree, x) if x.rightChild !== nothing t = x.rightChild t.parent = nothing end s = x s.rightChild = nothing if s.leftChild !== nothing s.leftChild.parent = nothing end tree.root = _join!(tree, s.leftChild, t) tree.count -= 1 #CURRENT FILE: DataStructures.jl/src/balanced_tree.jl ##CHUNK 1 t.deletionchild[2] = leftsib t.deletionchild[3] = p t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionleftkey[3] = sk2 end p = pparent deletionleftkey1_valid = false end curdepth -= 1 end if mustdeleteroot @invariant !deletionleftkey1_valid @invariant p == t.rootloc t.rootloc = t.deletionchild[1] t.depth -= 1 push!(t.freetreeinds, p) end ## If deletionleftkey1_valid, this means that the new ## min key of the deleted node and its right neighbors ##CHUNK 2 # If the root has been split, then we need to add a level # to the tree that is the parent of the old root and the new node. if splitroot @invariant existingchild == t.rootloc newroot = TreeNode{K}(existingchild, newchild, 0, 0, minkeynewchild, minkeynewchild) newrootloc = push_or_reuse!(t.tree, t.freetreeinds, newroot) replaceparent!(t.tree, existingchild, newrootloc) replaceparent!(t.tree, newchild, newrootloc) t.rootloc = newrootloc t.depth += 1 end return true, newind end ## nextloc0: returns the next item in the tree according to the ##CHUNK 3 ## by inserting a dummy tree node with two children, the before-start ## marker whose index is 1 and the after-end marker whose index is 2. ## These two markers live in dummy data nodes. function initializeTree!(tree::Vector{TreeNode{K}}) where K resize!(tree,1) tree[1] = TreeNode{K}(K, 1, 2, 0, 0) return nothing end function initializeData!(data::Vector{KDRec{K,D}}) where {K,D} resize!(data, 2) data[1] = KDRec{K,D}(1) data[2] = KDRec{K,D}(1) return nothing end ## Type BalancedTree23{K,D,Ord} is 'base class' for ## SortedDict, SortedMultiDict and SortedSet. ##CHUNK 4 depthp = t.depth @inbounds while true if depthp < t.depth p = t.tree[ii].parent end if t.tree[p].child1 == ii nextchild = t.tree[p].child2 break end if t.tree[p].child2 == ii && t.tree[p].child3 > 0 nextchild = t.tree[p].child3 break end ii = p depthp -= 1 end @inbounds while true if depthp == t.depth return nextchild end ##CHUNK 5 @inbounds while true if depthp < t.depth p = t.tree[ii].parent end if t.tree[p].child3 == ii prevchild = t.tree[p].child2 break end if t.tree[p].child2 == ii prevchild = t.tree[p].child1 break end ii = p depthp -= 1 end @inbounds while true if depthp == t.depth return prevchild end p = prevchild ##CHUNK 6 @inbounds return curnode, (curnode > 2 && eq(t.ord, t.data[curnode].k, k)) end ## The findkeyless function finds the index of a (key,data) pair in the tree that ## with the greatest key that is less than the given key. If there is no ## key less than the given key, then it returns 1 (the before-start node). function findkeyless(t::BalancedTree23, k) curnode = t.rootloc for depthcount = 1 : t.depth - 1 @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2le_nonleaf(t.ord, thisnode, k) : cmp3le_nonleaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 end @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? ##CHUNK 7 if exactfound && !allowdups t.data[leafind] = KDRec{K,D}(parent, k,d) return false, leafind end # We get here if k was not already found in the tree or # if duplicates are allowed. # In this case we insert a new node. depth = t.depth ord = t.ord ## Store the new data item in the tree's data array. Later ## go back and fix the parent. newind = push_or_reuse!(t.data, t.freedatainds, KDRec{K,D}(0,k,d)) push!(t.useddatacells, newind) p1 = parent newchild = newind minkeynewchild = k splitroot = false Based on the information above, please generate test code for the following function: function Base.empty!(t::BalancedTree23) resize!(t.data,2) initializeData!(t.data) resize!(t.tree,1) initializeTree!(t.tree) t.depth = 1 t.rootloc = 1 empty!(t.freetreeinds) empty!(t.freedatainds) empty!(t.useddatacells) push!(t.useddatacells, 1, 2) return nothing end
{ "fpath_tuple": [ "DataStructures.jl", "src", "balanced_tree.jl" ], "ground_truth": "function Base.empty!(t::BalancedTree23)\n resize!(t.data,2)\n initializeData!(t.data)\n resize!(t.tree,1)\n initializeTree!(t.tree)\n t.depth = 1\n t.rootloc = 1\n empty!(t.freetreeinds)\n empty!(t.freedatainds)\n empty!(t.useddatacells)\n push!(t.useddatacells, 1, 2)\n return nothing\nend", "task_id": "DataStructures/8" }
238
250
DataStructures.jl
8
function Base.empty!(t::BalancedTree23) resize!(t.data,2) initializeData!(t.data) resize!(t.tree,1) initializeTree!(t.tree) t.depth = 1 t.rootloc = 1 empty!(t.freetreeinds) empty!(t.freedatainds) empty!(t.useddatacells) push!(t.useddatacells, 1, 2) return nothing end
Base.empty!(t::BalancedTree23)
[ 238, 250 ]
function Base.empty!(t::BalancedTree23) resize!(t.data,2) initializeData!(t.data) resize!(t.tree,1) initializeTree!(t.tree) t.depth = 1 t.rootloc = 1 empty!(t.freetreeinds) empty!(t.freedatainds) empty!(t.useddatacells) push!(t.useddatacells, 1, 2) return nothing end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #FILE: DataStructures.jl/test/test_robin_dict.jl ##CHUNK 1 pos_diff = i - des_ind else pos_diff = sz - des_ind + i end dist = calculate_distance(h, i) @assert dist == pos_diff max_disp = max(max_disp, dist) distlast = (i != 1) ? isslotfilled(h, i-1) ? calculate_distance(h, i-1) : 0 : isslotfilled(h, sz) ? calculate_distance(h, sz) : 0 @assert dist <= distlast + 1 end @assert h.idxfloor == min_idx @assert cnt == length(h) end h = RobinDict() for i = 1:10000 h[i] = i+1 end check_invariants(h) #FILE: DataStructures.jl/test/test_sorted_containers.jl ##CHUNK 1 lt(o::ForBack, a, b) = o.flag ? isless(a,b) : isless(b,a) @noinline my_assert(stmt) = stmt ? nothing : throw(AssertionError("assertion failed")) function my_primes(N) w = Vector{Bool}(undef, N) fill!(w,true) for k = 2 : N if w[k] w[2 * k : k : N] .= false end end p = Int[] for k = 2 : N if w[k] push!(p,k) end end p ##CHUNK 2 merge!(m1, m2) my_assert(isequal(m1, m3)) m7 = SortedMultiDict{Int,Int}() n1 = 10000 for k = 1 : n1 push_return_semitoken!(m7, k=>k+1) end for k = 1 : n1 push_return_semitoken!(m7, k=>k+2) end for k = 1 : n1 i1, i2 = searchequalrange(m7, k) count = 0 for (key,v) in inclusive(m7, i1, i2) count += 1 my_assert(key == k) my_assert(v == k + count) end my_assert(count == 2) count = 0 #FILE: DataStructures.jl/src/sorted_container_iteration.jl ##CHUNK 1 """ status(token::Token) status((m, st)) Determine the status of a token. Return values are: - 0 = invalid token - 1 = valid and points to live data - 2 = before-start token - 3 = past-end token The second form creates the token in-place as a tuple of a sorted container `m` and a semitoken `st`. Time: O(1) """ status(ii::Token) = !(ii[2].address in ii[1].bt.useddatacells) ? 0 : ii[2].address == 1 ? 2 : ii[2].address == 2 ? 3 : 1 """ compare(m::SortedContainer, s::IntSemiToken, t::IntSemiToken) #CURRENT FILE: DataStructures.jl/src/balanced_tree.jl ##CHUNK 1 @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2_nonleaf(t.ord, thisnode, k) : cmp3_nonleaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 end @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2_leaf(t.ord, thisnode, k) : cmp3_leaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 @inbounds return curnode, (curnode > 2 && eq(t.ord, t.data[curnode].k, k)) end ## The findkeyless function finds the index of a (key,data) pair in the tree that ## with the greatest key that is less than the given key. If there is no ## key less than the given key, then it returns 1 (the before-start node). ##CHUNK 2 ## where the given key lives (if it is present), or ## if the key is not present, to the lower bound for the key, ## i.e., the data item that comes immediately before it. ## If there are multiple equal keys, then it finds the last one. ## It returns the index of the key found and a boolean indicating ## whether the exact key was found or not. function findkey(t::BalancedTree23, k) curnode = t.rootloc for depthcount = 1 : t.depth - 1 @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2_nonleaf(t.ord, thisnode, k) : cmp3_nonleaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 end @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2_leaf(t.ord, thisnode, k) : ##CHUNK 3 cmp3_leaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 @inbounds return curnode, (curnode > 2 && eq(t.ord, t.data[curnode].k, k)) end ## The findkeyless function finds the index of a (key,data) pair in the tree that ## with the greatest key that is less than the given key. If there is no ## key less than the given key, then it returns 1 (the before-start node). ## The following are helper routines for the insert! and delete! functions. ## They replace the 'parent' field of either an internal tree node or ## a data node at the bottom tree level. function replaceparent!(data::Vector{KDRec{K,D}}, whichind::Int, newparent::Int) where {K,D} data[whichind] = KDRec{K,D}(newparent, data[whichind].k, data[whichind].d) return nothing ##CHUNK 4 treenode::TreeNode, k) !lt(o,treenode.splitkey1, k) ? 1 : !lt(o,treenode.splitkey2, k) ? 2 : 3 end @inline function cmp3le_leaf(o::Ordering, treenode::TreeNode, k) !lt(o,treenode.splitkey1,k) ? 1 : (treenode.child3 == 2 || !lt(o,treenode.splitkey2, k)) ? 2 : 3 end ## The empty! function deletes all data in the balanced tree. ## Therefore, it invalidates all indices. function Base.empty!(t::BalancedTree23) resize!(t.data,2) initializeData!(t.data) ##CHUNK 5 if height == 0 child_belowaddress[whichc] = (i1 == 1) ? 1 : ((i1 == length(m.data)) ? 2 : i1 + 1) child_keyaddress[whichc] = child_belowaddress[whichc] else child_belowaddress[whichc] = levbelowbaseind + i1 child_keyaddress[whichc] = keysbelow[i1] end end if numchildren == 2 child_belowaddress[3] = 0 child_keyaddress[3] = child_keyaddress[2] end push!(newkeysbelow, child_keyaddress[1]) push!(m.tree, TreeNode{K}(child_belowaddress[1], child_belowaddress[2], child_belowaddress[3], 0, m.data[child_keyaddress[2]].k, m.data[child_keyaddress[3]].k)) myaddress = length(m.tree) ##CHUNK 6 resize!(newkeysbelow, 0) # Loop over the nodes of the level below, stepping by 2's # to form the tree nodes on the new level. One tree node (the # last one) may need to have three children if the # number of nodes on the level below is odd. for i = 1 : div(belowlevlength, 2) cbase = i * 2 - 2 numchildren = (cbase == belowlevlength - 3) ? 3 : 2 for whichc = 1 : numchildren i1 = cbase + whichc if height == 0 child_belowaddress[whichc] = (i1 == 1) ? 1 : ((i1 == length(m.data)) ? 2 : i1 + 1) child_keyaddress[whichc] = child_belowaddress[whichc] else child_belowaddress[whichc] = levbelowbaseind + i1 child_keyaddress[whichc] = keysbelow[i1] end end if numchildren == 2 Based on the information above, please generate test code for the following function: function findkeyless(t::BalancedTree23, k) curnode = t.rootloc for depthcount = 1 : t.depth - 1 @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2le_nonleaf(t.ord, thisnode, k) : cmp3le_nonleaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 end @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2le_leaf(t.ord, thisnode, k) : cmp3le_leaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 return curnode end
{ "fpath_tuple": [ "DataStructures.jl", "src", "balanced_tree.jl" ], "ground_truth": "function findkeyless(t::BalancedTree23, k)\n curnode = t.rootloc\n for depthcount = 1 : t.depth - 1\n @inbounds thisnode = t.tree[curnode]\n cmp = thisnode.child3 == 0 ?\n cmp2le_nonleaf(t.ord, thisnode, k) :\n cmp3le_nonleaf(t.ord, thisnode, k)\n curnode = cmp == 1 ? thisnode.child1 :\n cmp == 2 ? thisnode.child2 : thisnode.child3\n end\n @inbounds thisnode = t.tree[curnode]\n cmp = thisnode.child3 == 0 ?\n cmp2le_leaf(t.ord, thisnode, k) :\n cmp3le_leaf(t.ord, thisnode, k)\n curnode = cmp == 1 ? thisnode.child1 :\n cmp == 2 ? thisnode.child2 : thisnode.child3\n return curnode\nend", "task_id": "DataStructures/9" }
292
309
DataStructures.jl
9
function findkeyless(t::BalancedTree23, k) curnode = t.rootloc for depthcount = 1 : t.depth - 1 @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2le_nonleaf(t.ord, thisnode, k) : cmp3le_nonleaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 end @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2le_leaf(t.ord, thisnode, k) : cmp3le_leaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 return curnode end
findkeyless(t::BalancedTree23, k)
[ 292, 309 ]
function findkeyless(t::BalancedTree23, k) curnode = t.rootloc for depthcount = 1 : t.depth - 1 @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2le_nonleaf(t.ord, thisnode, k) : cmp3le_nonleaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 end @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2le_leaf(t.ord, thisnode, k) : cmp3le_leaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 return curnode end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #CURRENT FILE: DataStructures.jl/src/balanced_tree.jl ##CHUNK 1 if newchildcount == 3 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], t.deletionchild[3], pparent, t.deletionleftkey[2], t.deletionleftkey[3]) break end @invariant newchildcount == 1 ## For the rest of this loop, we cover the case ## that p has one child. ## If newchildcount == 1 and curdepth==1, this means that ## the root of the tree has only one child. In this case, we can ## delete the root and make its one child the new root (see below). if curdepth == 1 mustdeleteroot = true break end ## We now branch on three cases depending on whether p is child1, ##CHUNK 2 ## If newchildcount == 1 and curdepth==1, this means that ## the root of the tree has only one child. In this case, we can ## delete the root and make its one child the new root (see below). if curdepth == 1 mustdeleteroot = true break end ## We now branch on three cases depending on whether p is child1, ## child2 or child3 of its parent. if t.tree[pparent].child1 == p rightsib = t.tree[pparent].child2 ## Here p is child1 and rightsib is child2. ## If rightsib has 2 children, then p and ## rightsib are merged into a single node ## that has three children. ## If rightsib has 3 children, then p and ##CHUNK 3 if mustdeleteroot @invariant !deletionleftkey1_valid @invariant p == t.rootloc t.rootloc = t.deletionchild[1] t.depth -= 1 push!(t.freetreeinds, p) end ## If deletionleftkey1_valid, this means that the new ## min key of the deleted node and its right neighbors ## has never been stored in the tree. It must be stored ## as splitkey1 or splitkey2 of some ancestor of the ## deleted node, so we continue ascending the tree ## until we find a node which has p (and therefore the ## deleted node) as its descendent through its second ## or third child. ## It cannot be the case that the deleted node is ## is a descendent of the root always through ## first children, since this would mean the deleted ## node is the leftmost placeholder, which ##CHUNK 4 mustdeleteroot = false pparent = -1 ## The following loop ascends the tree and contracts nodes (reduces their ## number of children) as ## needed. If newchildcount == 2 or 3, then the ascent is terminated ## and a node is created with 2 or 3 children. ## If newchildcount == 1, then the ascent must continue since a tree ## node cannot have one child. while true pparent = t.tree[p].parent ## Simple cases when the new child count is 2 or 3 if newchildcount == 2 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], 0, pparent, t.deletionleftkey[2], defaultKey) break end ##CHUNK 5 0, pparent, t.tree[rightsib].splitkey2, defaultKey) if curdepth == t.depth replaceparent!(t.data, rc1, p) else replaceparent!(t.tree, rc1, p) end newchildcount = 2 t.deletionchild[1] = p t.deletionchild[2] = rightsib t.deletionleftkey[2] = sk1 end ## If pparent had a third child (besides p and rightsib) ## then we add this to t.deletionchild c3 = t.tree[pparent].child3 if c3 > 0 ##CHUNK 6 if c3 != it && c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.data[c3].k end @invariant newchildcount == 1 || newchildcount == 2 push!(t.freedatainds, it) pop!(t.useddatacells,it) defaultKey = t.tree[1].splitkey1 curdepth = t.depth mustdeleteroot = false pparent = -1 ## The following loop ascends the tree and contracts nodes (reduces their ## number of children) as ## needed. If newchildcount == 2 or 3, then the ascent is terminated ## and a node is created with 2 or 3 children. ## If newchildcount == 1, then the ascent must continue since a tree ## node cannot have one child. ##CHUNK 7 pparent, lk, defaultKey) sk2 = t.tree[leftsib].splitkey2 t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1, t.tree[leftsib].child2, 0, pparent, t.tree[leftsib].splitkey1, defaultKey) if curdepth == t.depth replaceparent!(t.data, lc3, p) else replaceparent!(t.tree, lc3, p) end newchildcount = 2 t.deletionchild[1] = leftsib t.deletionchild[2] = p t.deletionleftkey[2] = sk2 end ## If pparent had a third child (besides p and leftsib) ## then we add this to t.deletionchild ##CHUNK 8 t.deletionchild[2] = leftsib t.deletionchild[3] = p t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionleftkey[3] = sk2 end p = pparent deletionleftkey1_valid = false end curdepth -= 1 end if mustdeleteroot @invariant !deletionleftkey1_valid @invariant p == t.rootloc t.rootloc = t.deletionchild[1] t.depth -= 1 push!(t.freetreeinds, p) end ## If deletionleftkey1_valid, this means that the new ## min key of the deleted node and its right neighbors ##CHUNK 9 ## has never been stored in the tree. It must be stored ## as splitkey1 or splitkey2 of some ancestor of the ## deleted node, so we continue ascending the tree ## until we find a node which has p (and therefore the ## deleted node) as its descendent through its second ## or third child. ## It cannot be the case that the deleted node is ## is a descendent of the root always through ## first children, since this would mean the deleted ## node is the leftmost placeholder, which ## cannot be deleted. if deletionleftkey1_valid while true pparentnode = t.tree[pparent] if pparentnode.child2 == p t.tree[pparent] = TreeNode{K}(pparentnode.child1, pparentnode.child2, pparentnode.child3, pparentnode.parent, ##CHUNK 10 macro invariant_support_statement(expr) end struct KDRec{K,D} parent::Int k::K d::D KDRec{K,D}(p::Int, k1::K, d1::D) where {K,D} = new{K,D}(p,k1,d1) KDRec{K,D}(p::Int) where {K,D} = new{K,D}(p) end ## TreeNode is an internal node of the tree. ## child1,child2,child3: ## These are the three children node numbers. ## If the node is a 2-node (rather than 3), then child3 == 0. ## If this is a leaf then child1,child2,child3 are subscripts ## of data nodes, else they are subscripts of other tree nodes. ## splitkey1: ## the minimum key of the subtree at child2. If this is a leaf Based on the information above, please generate test code for the following function: function Base.insert!(t::BalancedTree23{K,D,Ord}, k, d, allowdups::Bool) where {K,D,Ord <: Ordering} ## First we find the greatest data node that is <= k. leafind, exactfound = findkey(t, k) parent = t.data[leafind].parent ## The following code is necessary because in the case of a ## brand new tree, the initial tree and data entries were incompletely ## initialized by the constructor. In this case, the call to insert! ## underway carries ## valid K and D values, so these valid values may now be ## stored in the dummy placeholder nodes so that they no ## longer hold undefined references. if size(t.data,1) == 2 @invariant t.rootloc == 1 && t.depth == 1 t.tree[1] = TreeNode{K}(t.tree[1].child1, t.tree[1].child2, t.tree[1].child3, t.tree[1].parent, k, k) t.data[1] = KDRec{K,D}(t.data[1].parent, k, d) t.data[2] = KDRec{K,D}(t.data[2].parent, k, d) end ## If we have found exactly k in the tree, then we ## replace the data associated with k and return. if exactfound && !allowdups t.data[leafind] = KDRec{K,D}(parent, k,d) return false, leafind end # We get here if k was not already found in the tree or # if duplicates are allowed. # In this case we insert a new node. depth = t.depth ord = t.ord ## Store the new data item in the tree's data array. Later ## go back and fix the parent. newind = push_or_reuse!(t.data, t.freedatainds, KDRec{K,D}(0,k,d)) push!(t.useddatacells, newind) p1 = parent newchild = newind minkeynewchild = k splitroot = false curdepth = depth existingchild = leafind ## This loop ascends the tree (i.e., follows the path from a leaf to the root) ## starting from the parent p1 of ## where the new key k will go. ## Variables updated by the loop: ## p1: parent of where the new node goes ## newchild: index of the child to be inserted ## minkeynewchild: the minimum key in the subtree rooted at newchild ## existingchild: a child of p1; the newchild must ## be inserted in the slot to the right of existingchild ## curdepth: depth of newchild ## For each 3-node we encounter ## during the ascent, we add a new child, which requires splitting ## the 3-node into two 2-nodes. Then we keep going until we hit the root. ## If we encounter a 2-node, then the ascent can stop; we can ## change the 2-node to a 3-node with the new child. while true # Let newchild1,...newchild4 be the new children of # the parent node # Initially, take the three children of the existing parent # node and set newchild4 to 0. newchild1 = t.tree[p1].child1 newchild2 = t.tree[p1].child2 minkeychild2 = t.tree[p1].splitkey1 newchild3 = t.tree[p1].child3 minkeychild3 = t.tree[p1].splitkey2 p1parent = t.tree[p1].parent newchild4 = 0 # Now figure out which of the 4 children is the new node # and insert it into newchild1 ... newchild4 if newchild1 == existingchild newchild4 = newchild3 minkeychild4 = minkeychild3 newchild3 = newchild2 minkeychild3 = minkeychild2 newchild2 = newchild minkeychild2 = minkeynewchild elseif newchild2 == existingchild newchild4 = newchild3 minkeychild4 = minkeychild3 newchild3 = newchild minkeychild3 = minkeynewchild elseif newchild3 == existingchild newchild4 = newchild minkeychild4 = minkeynewchild else throw(AssertionError("Tree structure is corrupted 1")) end # Two cases: either we need to split the tree node # if newchild4>0 else we convert a 2-node to a 3-node # if newchild4==0 if newchild4 == 0 # Change the parent from a 2-node to a 3-node t.tree[p1] = TreeNode{K}(newchild1, newchild2, newchild3, p1parent, minkeychild2, minkeychild3) if curdepth == depth replaceparent!(t.data, newchild, p1) else replaceparent!(t.tree, newchild, p1) end break end # Split the parent t.tree[p1] = TreeNode{K}(newchild1, newchild2, 0, p1parent, minkeychild2, minkeychild2) newtreenode = TreeNode{K}(newchild3, newchild4, 0, p1parent, minkeychild4, minkeychild2) newparentnum = push_or_reuse!(t.tree, t.freetreeinds, newtreenode) if curdepth == depth replaceparent!(t.data, newchild2, p1) replaceparent!(t.data, newchild3, newparentnum) replaceparent!(t.data, newchild4, newparentnum) else replaceparent!(t.tree, newchild2, p1) replaceparent!(t.tree, newchild3, newparentnum) replaceparent!(t.tree, newchild4, newparentnum) end # Update the loop variables for the next level of the # ascension existingchild = p1 newchild = newparentnum p1 = p1parent minkeynewchild = minkeychild3 curdepth -= 1 if curdepth == 0 splitroot = true break end end # If the root has been split, then we need to add a level # to the tree that is the parent of the old root and the new node. if splitroot @invariant existingchild == t.rootloc newroot = TreeNode{K}(existingchild, newchild, 0, 0, minkeynewchild, minkeynewchild) newrootloc = push_or_reuse!(t.tree, t.freetreeinds, newroot) replaceparent!(t.tree, existingchild, newrootloc) replaceparent!(t.tree, newchild, newrootloc) t.rootloc = newrootloc t.depth += 1 end return true, newind end
{ "fpath_tuple": [ "DataStructures.jl", "src", "balanced_tree.jl" ], "ground_truth": "function Base.insert!(t::BalancedTree23{K,D,Ord}, k, d, allowdups::Bool) where {K,D,Ord <: Ordering}\n\n ## First we find the greatest data node that is <= k.\n leafind, exactfound = findkey(t, k)\n parent = t.data[leafind].parent\n\n ## The following code is necessary because in the case of a\n ## brand new tree, the initial tree and data entries were incompletely\n ## initialized by the constructor. In this case, the call to insert!\n ## underway carries\n ## valid K and D values, so these valid values may now be\n ## stored in the dummy placeholder nodes so that they no\n ## longer hold undefined references.\n\n if size(t.data,1) == 2\n @invariant t.rootloc == 1 && t.depth == 1\n t.tree[1] = TreeNode{K}(t.tree[1].child1, t.tree[1].child2,\n t.tree[1].child3, t.tree[1].parent,\n k, k)\n t.data[1] = KDRec{K,D}(t.data[1].parent, k, d)\n t.data[2] = KDRec{K,D}(t.data[2].parent, k, d)\n end\n\n ## If we have found exactly k in the tree, then we\n ## replace the data associated with k and return.\n\n if exactfound && !allowdups\n t.data[leafind] = KDRec{K,D}(parent, k,d)\n return false, leafind\n end\n\n # We get here if k was not already found in the tree or\n # if duplicates are allowed.\n # In this case we insert a new node.\n depth = t.depth\n ord = t.ord\n\n ## Store the new data item in the tree's data array. Later\n ## go back and fix the parent.\n\n newind = push_or_reuse!(t.data, t.freedatainds, KDRec{K,D}(0,k,d))\n push!(t.useddatacells, newind)\n p1 = parent\n newchild = newind\n minkeynewchild = k\n splitroot = false\n curdepth = depth\n existingchild = leafind\n\n \n ## This loop ascends the tree (i.e., follows the path from a leaf to the root)\n ## starting from the parent p1 of\n ## where the new key k will go.\n ## Variables updated by the loop:\n ## p1: parent of where the new node goes\n ## newchild: index of the child to be inserted\n ## minkeynewchild: the minimum key in the subtree rooted at newchild\n ## existingchild: a child of p1; the newchild must\n ## be inserted in the slot to the right of existingchild\n ## curdepth: depth of newchild\n ## For each 3-node we encounter\n ## during the ascent, we add a new child, which requires splitting\n ## the 3-node into two 2-nodes. Then we keep going until we hit the root.\n ## If we encounter a 2-node, then the ascent can stop; we can\n ## change the 2-node to a 3-node with the new child.\n\n while true\n\n\n # Let newchild1,...newchild4 be the new children of\n # the parent node\n # Initially, take the three children of the existing parent\n # node and set newchild4 to 0.\n\n newchild1 = t.tree[p1].child1\n newchild2 = t.tree[p1].child2\n minkeychild2 = t.tree[p1].splitkey1\n newchild3 = t.tree[p1].child3\n minkeychild3 = t.tree[p1].splitkey2\n p1parent = t.tree[p1].parent\n newchild4 = 0\n\n # Now figure out which of the 4 children is the new node\n # and insert it into newchild1 ... newchild4\n\n if newchild1 == existingchild\n newchild4 = newchild3\n minkeychild4 = minkeychild3\n newchild3 = newchild2\n minkeychild3 = minkeychild2\n newchild2 = newchild\n minkeychild2 = minkeynewchild\n elseif newchild2 == existingchild\n newchild4 = newchild3\n minkeychild4 = minkeychild3\n newchild3 = newchild\n minkeychild3 = minkeynewchild\n elseif newchild3 == existingchild\n newchild4 = newchild\n minkeychild4 = minkeynewchild\n else\n throw(AssertionError(\"Tree structure is corrupted 1\"))\n end\n\n # Two cases: either we need to split the tree node\n # if newchild4>0 else we convert a 2-node to a 3-node\n # if newchild4==0\n\n if newchild4 == 0\n # Change the parent from a 2-node to a 3-node\n t.tree[p1] = TreeNode{K}(newchild1, newchild2, newchild3,\n p1parent, minkeychild2, minkeychild3)\n if curdepth == depth\n replaceparent!(t.data, newchild, p1)\n else\n replaceparent!(t.tree, newchild, p1)\n end\n break\n end\n # Split the parent\n t.tree[p1] = TreeNode{K}(newchild1, newchild2, 0,\n p1parent, minkeychild2, minkeychild2)\n newtreenode = TreeNode{K}(newchild3, newchild4, 0,\n p1parent, minkeychild4, minkeychild2)\n newparentnum = push_or_reuse!(t.tree, t.freetreeinds, newtreenode)\n if curdepth == depth\n replaceparent!(t.data, newchild2, p1)\n replaceparent!(t.data, newchild3, newparentnum)\n replaceparent!(t.data, newchild4, newparentnum)\n else\n replaceparent!(t.tree, newchild2, p1)\n replaceparent!(t.tree, newchild3, newparentnum)\n replaceparent!(t.tree, newchild4, newparentnum)\n end\n # Update the loop variables for the next level of the\n # ascension\n existingchild = p1\n newchild = newparentnum\n p1 = p1parent\n minkeynewchild = minkeychild3\n curdepth -= 1\n if curdepth == 0\n splitroot = true\n break\n end\n end\n\n # If the root has been split, then we need to add a level\n # to the tree that is the parent of the old root and the new node.\n\n if splitroot\n @invariant existingchild == t.rootloc\n newroot = TreeNode{K}(existingchild, newchild, 0,\n 0, minkeynewchild, minkeynewchild)\n \n newrootloc = push_or_reuse!(t.tree, t.freetreeinds, newroot)\n replaceparent!(t.tree, existingchild, newrootloc)\n replaceparent!(t.tree, newchild, newrootloc)\n t.rootloc = newrootloc\n t.depth += 1\n end\n return true, newind\nend", "task_id": "DataStructures/10" }
358
520
DataStructures.jl
10
function Base.insert!(t::BalancedTree23{K,D,Ord}, k, d, allowdups::Bool) where {K,D,Ord <: Ordering} ## First we find the greatest data node that is <= k. leafind, exactfound = findkey(t, k) parent = t.data[leafind].parent ## The following code is necessary because in the case of a ## brand new tree, the initial tree and data entries were incompletely ## initialized by the constructor. In this case, the call to insert! ## underway carries ## valid K and D values, so these valid values may now be ## stored in the dummy placeholder nodes so that they no ## longer hold undefined references. if size(t.data,1) == 2 @invariant t.rootloc == 1 && t.depth == 1 t.tree[1] = TreeNode{K}(t.tree[1].child1, t.tree[1].child2, t.tree[1].child3, t.tree[1].parent, k, k) t.data[1] = KDRec{K,D}(t.data[1].parent, k, d) t.data[2] = KDRec{K,D}(t.data[2].parent, k, d) end ## If we have found exactly k in the tree, then we ## replace the data associated with k and return. if exactfound && !allowdups t.data[leafind] = KDRec{K,D}(parent, k,d) return false, leafind end # We get here if k was not already found in the tree or # if duplicates are allowed. # In this case we insert a new node. depth = t.depth ord = t.ord ## Store the new data item in the tree's data array. Later ## go back and fix the parent. newind = push_or_reuse!(t.data, t.freedatainds, KDRec{K,D}(0,k,d)) push!(t.useddatacells, newind) p1 = parent newchild = newind minkeynewchild = k splitroot = false curdepth = depth existingchild = leafind ## This loop ascends the tree (i.e., follows the path from a leaf to the root) ## starting from the parent p1 of ## where the new key k will go. ## Variables updated by the loop: ## p1: parent of where the new node goes ## newchild: index of the child to be inserted ## minkeynewchild: the minimum key in the subtree rooted at newchild ## existingchild: a child of p1; the newchild must ## be inserted in the slot to the right of existingchild ## curdepth: depth of newchild ## For each 3-node we encounter ## during the ascent, we add a new child, which requires splitting ## the 3-node into two 2-nodes. Then we keep going until we hit the root. ## If we encounter a 2-node, then the ascent can stop; we can ## change the 2-node to a 3-node with the new child. while true # Let newchild1,...newchild4 be the new children of # the parent node # Initially, take the three children of the existing parent # node and set newchild4 to 0. newchild1 = t.tree[p1].child1 newchild2 = t.tree[p1].child2 minkeychild2 = t.tree[p1].splitkey1 newchild3 = t.tree[p1].child3 minkeychild3 = t.tree[p1].splitkey2 p1parent = t.tree[p1].parent newchild4 = 0 # Now figure out which of the 4 children is the new node # and insert it into newchild1 ... newchild4 if newchild1 == existingchild newchild4 = newchild3 minkeychild4 = minkeychild3 newchild3 = newchild2 minkeychild3 = minkeychild2 newchild2 = newchild minkeychild2 = minkeynewchild elseif newchild2 == existingchild newchild4 = newchild3 minkeychild4 = minkeychild3 newchild3 = newchild minkeychild3 = minkeynewchild elseif newchild3 == existingchild newchild4 = newchild minkeychild4 = minkeynewchild else throw(AssertionError("Tree structure is corrupted 1")) end # Two cases: either we need to split the tree node # if newchild4>0 else we convert a 2-node to a 3-node # if newchild4==0 if newchild4 == 0 # Change the parent from a 2-node to a 3-node t.tree[p1] = TreeNode{K}(newchild1, newchild2, newchild3, p1parent, minkeychild2, minkeychild3) if curdepth == depth replaceparent!(t.data, newchild, p1) else replaceparent!(t.tree, newchild, p1) end break end # Split the parent t.tree[p1] = TreeNode{K}(newchild1, newchild2, 0, p1parent, minkeychild2, minkeychild2) newtreenode = TreeNode{K}(newchild3, newchild4, 0, p1parent, minkeychild4, minkeychild2) newparentnum = push_or_reuse!(t.tree, t.freetreeinds, newtreenode) if curdepth == depth replaceparent!(t.data, newchild2, p1) replaceparent!(t.data, newchild3, newparentnum) replaceparent!(t.data, newchild4, newparentnum) else replaceparent!(t.tree, newchild2, p1) replaceparent!(t.tree, newchild3, newparentnum) replaceparent!(t.tree, newchild4, newparentnum) end # Update the loop variables for the next level of the # ascension existingchild = p1 newchild = newparentnum p1 = p1parent minkeynewchild = minkeychild3 curdepth -= 1 if curdepth == 0 splitroot = true break end end # If the root has been split, then we need to add a level # to the tree that is the parent of the old root and the new node. if splitroot @invariant existingchild == t.rootloc newroot = TreeNode{K}(existingchild, newchild, 0, 0, minkeynewchild, minkeynewchild) newrootloc = push_or_reuse!(t.tree, t.freetreeinds, newroot) replaceparent!(t.tree, existingchild, newrootloc) replaceparent!(t.tree, newchild, newrootloc) t.rootloc = newrootloc t.depth += 1 end return true, newind end
Base.insert!(t::BalancedTree23{K,D,Ord}, k, d, allowdups::Bool) where {K,D,Ord <: Ordering}
[ 358, 520 ]
function Base.insert!(t::BalancedTree23{K,D,Ord}, k, d, allowdups::Bool) where {K,D,Ord <: Ordering} ## First we find the greatest data node that is <= k. leafind, exactfound = findkey(t, k) parent = t.data[leafind].parent ## The following code is necessary because in the case of a ## brand new tree, the initial tree and data entries were incompletely ## initialized by the constructor. In this case, the call to insert! ## underway carries ## valid K and D values, so these valid values may now be ## stored in the dummy placeholder nodes so that they no ## longer hold undefined references. if size(t.data,1) == 2 @invariant t.rootloc == 1 && t.depth == 1 t.tree[1] = TreeNode{K}(t.tree[1].child1, t.tree[1].child2, t.tree[1].child3, t.tree[1].parent, k, k) t.data[1] = KDRec{K,D}(t.data[1].parent, k, d) t.data[2] = KDRec{K,D}(t.data[2].parent, k, d) end ## If we have found exactly k in the tree, then we ## replace the data associated with k and return. if exactfound && !allowdups t.data[leafind] = KDRec{K,D}(parent, k,d) return false, leafind end # We get here if k was not already found in the tree or # if duplicates are allowed. # In this case we insert a new node. depth = t.depth ord = t.ord ## Store the new data item in the tree's data array. Later ## go back and fix the parent. newind = push_or_reuse!(t.data, t.freedatainds, KDRec{K,D}(0,k,d)) push!(t.useddatacells, newind) p1 = parent newchild = newind minkeynewchild = k splitroot = false curdepth = depth existingchild = leafind ## This loop ascends the tree (i.e., follows the path from a leaf to the root) ## starting from the parent p1 of ## where the new key k will go. ## Variables updated by the loop: ## p1: parent of where the new node goes ## newchild: index of the child to be inserted ## minkeynewchild: the minimum key in the subtree rooted at newchild ## existingchild: a child of p1; the newchild must ## be inserted in the slot to the right of existingchild ## curdepth: depth of newchild ## For each 3-node we encounter ## during the ascent, we add a new child, which requires splitting ## the 3-node into two 2-nodes. Then we keep going until we hit the root. ## If we encounter a 2-node, then the ascent can stop; we can ## change the 2-node to a 3-node with the new child. while true # Let newchild1,...newchild4 be the new children of # the parent node # Initially, take the three children of the existing parent # node and set newchild4 to 0. newchild1 = t.tree[p1].child1 newchild2 = t.tree[p1].child2 minkeychild2 = t.tree[p1].splitkey1 newchild3 = t.tree[p1].child3 minkeychild3 = t.tree[p1].splitkey2 p1parent = t.tree[p1].parent newchild4 = 0 # Now figure out which of the 4 children is the new node # and insert it into newchild1 ... newchild4 if newchild1 == existingchild newchild4 = newchild3 minkeychild4 = minkeychild3 newchild3 = newchild2 minkeychild3 = minkeychild2 newchild2 = newchild minkeychild2 = minkeynewchild elseif newchild2 == existingchild newchild4 = newchild3 minkeychild4 = minkeychild3 newchild3 = newchild minkeychild3 = minkeynewchild elseif newchild3 == existingchild newchild4 = newchild minkeychild4 = minkeynewchild else throw(AssertionError("Tree structure is corrupted 1")) end # Two cases: either we need to split the tree node # if newchild4>0 else we convert a 2-node to a 3-node # if newchild4==0 if newchild4 == 0 # Change the parent from a 2-node to a 3-node t.tree[p1] = TreeNode{K}(newchild1, newchild2, newchild3, p1parent, minkeychild2, minkeychild3) if curdepth == depth replaceparent!(t.data, newchild, p1) else replaceparent!(t.tree, newchild, p1) end break end # Split the parent t.tree[p1] = TreeNode{K}(newchild1, newchild2, 0, p1parent, minkeychild2, minkeychild2) newtreenode = TreeNode{K}(newchild3, newchild4, 0, p1parent, minkeychild4, minkeychild2) newparentnum = push_or_reuse!(t.tree, t.freetreeinds, newtreenode) if curdepth == depth replaceparent!(t.data, newchild2, p1) replaceparent!(t.data, newchild3, newparentnum) replaceparent!(t.data, newchild4, newparentnum) else replaceparent!(t.tree, newchild2, p1) replaceparent!(t.tree, newchild3, newparentnum) replaceparent!(t.tree, newchild4, newparentnum) end # Update the loop variables for the next level of the # ascension existingchild = p1 newchild = newparentnum p1 = p1parent minkeynewchild = minkeychild3 curdepth -= 1 if curdepth == 0 splitroot = true break end end # If the root has been split, then we need to add a level # to the tree that is the parent of the old root and the new node. if splitroot @invariant existingchild == t.rootloc newroot = TreeNode{K}(existingchild, newchild, 0, 0, minkeynewchild, minkeynewchild) newrootloc = push_or_reuse!(t.tree, t.freetreeinds, newroot) replaceparent!(t.tree, existingchild, newrootloc) replaceparent!(t.tree, newchild, newrootloc) t.rootloc = newrootloc t.depth += 1 end return true, newind end
Your task is to generate only the test code for the given focal function in Julia programming language. Instructions: - Use the Test module with idiomatic @testset and @test macros. - (IMPORTANT) Always refer to the focal function using the module_name.function_name format — even if it is exported. - Do NOT rewrite or include the function definition. - Make sure to test different input types, edge cases, and assertions. - Only generate the test code block. Generate the test code using this format: ```julia using Test using ModuleName @testset "Test function_name" begin @test ModuleName.function_name(args...) == expected # Add more test cases end ``` Here is all the context you may find useful to generate the test: #CURRENT FILE: DataStructures.jl/src/balanced_tree.jl ##CHUNK 1 ## prevloc0: returns the previous item in the tree according to the ## sort order, given an index i (subscript of t.data) of a current ## item. ## The routine returns 1 if there is no previous item (i.e., we started ## from the first one in the sorted order). function prevloc0(t::BalancedTree23, i::Int) @invariant i != 1 && i in t.useddatacells ii = i @inbounds p = t.data[i].parent prevchild = 0 depthp = t.depth @inbounds while true if depthp < t.depth p = t.tree[ii].parent end if t.tree[p].child3 == ii prevchild = t.tree[p].child2 break ##CHUNK 2 ## nextloc0: returns the next item in the tree according to the ## sort order, given an index i (subscript of t.data) of a current ## item. ## The routine returns 2 if there is no next item (i.e., we started ## from the last one in the sorted order). function nextloc0(t, i::Int) ii = i @invariant i != 2 && i in t.useddatacells @inbounds p = t.data[i].parent nextchild = 0 depthp = t.depth @inbounds while true if depthp < t.depth p = t.tree[ii].parent end if t.tree[p].child1 == ii nextchild = t.tree[p].child2 ##CHUNK 3 end if t.tree[p].child2 == ii prevchild = t.tree[p].child1 break end ii = p depthp -= 1 end @inbounds while true if depthp == t.depth return prevchild end p = prevchild c3 = t.tree[p].child3 prevchild = c3 > 0 ? c3 : t.tree[p].child2 depthp += 1 end end ## This function takes two indices into t.data and checks which ##CHUNK 4 macro invariant_support_statement(expr) end struct KDRec{K,D} parent::Int k::K d::D KDRec{K,D}(p::Int, k1::K, d1::D) where {K,D} = new{K,D}(p,k1,d1) KDRec{K,D}(p::Int) where {K,D} = new{K,D}(p) end ## TreeNode is an internal node of the tree. ## child1,child2,child3: ## These are the three children node numbers. ## If the node is a 2-node (rather than 3), then child3 == 0. ## If this is a leaf then child1,child2,child3 are subscripts ## of data nodes, else they are subscripts of other tree nodes. ## splitkey1: ## the minimum key of the subtree at child2. If this is a leaf ##CHUNK 5 return prevchild end p = prevchild c3 = t.tree[p].child3 prevchild = c3 > 0 ? c3 : t.tree[p].child2 depthp += 1 end end ## This function takes two indices into t.data and checks which ## one comes first in the sorted order by chasing them both ## up the tree until a common ancestor is found. ## The return value is -1 if i1 precedes i2, 0 if i1 == i2 ##, 1 if i2 precedes i1. function compareInd(t::BalancedTree23, i1::Int, i2::Int) @invariant(i1 in t.useddatacells && i2 in t.useddatacells) if i1 == i2 return 0 end ##CHUNK 6 if depthp == t.depth return nextchild end p = nextchild nextchild = t.tree[p].child1 depthp += 1 end end ## prevloc0: returns the previous item in the tree according to the ## sort order, given an index i (subscript of t.data) of a current ## item. ## The routine returns 1 if there is no previous item (i.e., we started ## from the first one in the sorted order). function prevloc0(t::BalancedTree23, i::Int) @invariant i != 1 && i in t.useddatacells ii = i ##CHUNK 7 leafind, exactfound = findkey(t, k) parent = t.data[leafind].parent ## The following code is necessary because in the case of a ## brand new tree, the initial tree and data entries were incompletely ## initialized by the constructor. In this case, the call to insert! ## underway carries ## valid K and D values, so these valid values may now be ## stored in the dummy placeholder nodes so that they no ## longer hold undefined references. if size(t.data,1) == 2 @invariant t.rootloc == 1 && t.depth == 1 t.tree[1] = TreeNode{K}(t.tree[1].child1, t.tree[1].child2, t.tree[1].child3, t.tree[1].parent, k, k) t.data[1] = KDRec{K,D}(t.data[1].parent, k, d) t.data[2] = KDRec{K,D}(t.data[2].parent, k, d) end ##CHUNK 8 ## then it is the key of child2. ## splitkey2: ## if child3 > 0 then splitkey2 is the minimum key of the subtree at child3. ## If this is a leaf, then it is the key of child3. ## Again, there are two constructors for the same reason mentioned above. struct TreeNode{K} child1::Int child2::Int child3::Int parent::Int splitkey1::K splitkey2::K TreeNode{K}(::Type{K}, c1::Int, c2::Int, c3::Int, p::Int) where {K} = new{K}(c1, c2, c3, p) TreeNode{K}(c1::Int, c2::Int, c3::Int, p::Int, sk1::K, sk2::K) where {K} = new{K}(c1, c2, c3, p, sk1, sk2) end ## The next two functions are called to initialize the tree ##CHUNK 9 0, minkeynewchild, minkeynewchild) newrootloc = push_or_reuse!(t.tree, t.freetreeinds, newroot) replaceparent!(t.tree, existingchild, newrootloc) replaceparent!(t.tree, newchild, newrootloc) t.rootloc = newrootloc t.depth += 1 end return true, newind end ## nextloc0: returns the next item in the tree according to the ## sort order, given an index i (subscript of t.data) of a current ## item. ## The routine returns 2 if there is no next item (i.e., we started ## from the last one in the sorted order). function nextloc0(t, i::Int) ii = i ##CHUNK 10 ## where the given key lives (if it is present), or ## if the key is not present, to the lower bound for the key, ## i.e., the data item that comes immediately before it. ## If there are multiple equal keys, then it finds the last one. ## It returns the index of the key found and a boolean indicating ## whether the exact key was found or not. function findkey(t::BalancedTree23, k) curnode = t.rootloc for depthcount = 1 : t.depth - 1 @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2_nonleaf(t.ord, thisnode, k) : cmp3_nonleaf(t.ord, thisnode, k) curnode = cmp == 1 ? thisnode.child1 : cmp == 2 ? thisnode.child2 : thisnode.child3 end @inbounds thisnode = t.tree[curnode] cmp = thisnode.child3 == 0 ? cmp2_leaf(t.ord, thisnode, k) : Based on the information above, please generate test code for the following function: function Base.delete!(t::BalancedTree23{K,D,Ord}, it::Int) where {K,D,Ord<:Ordering} ## Put the cell indexed by 'it' into the deletion list. ## ## Create the following data items maintained in the ## upcoming loop. ## ## p is a tree-node ancestor of the deleted node ## The children of p are stored in ## t.deletionchild[..] ## The number of these children is newchildcount, which is 1, 2 or 3. ## The keys that lower bound the children ## are stored in t.deletionleftkey[..] ## There is a special case for t.deletionleftkey[1]; the ## flag deletionleftkey1_valid indicates that the left key ## for the immediate right neighbor of the ## deleted node has not yet been been stored in the tree. ## Once it is stored, t.deletionleftkey[1] is no longer needed ## or used. ## The flag mustdeleteroot means that the tree has contracted ## enough that it loses a level. p = t.data[it].parent newchildcount = 0 c1 = t.tree[p].child1 deletionleftkey1_valid = true if c1 != it deletionleftkey1_valid = false newchildcount += 1 t.deletionchild[newchildcount] = c1 t.deletionleftkey[newchildcount] = t.data[c1].k end c2 = t.tree[p].child2 if c2 != it newchildcount += 1 t.deletionchild[newchildcount] = c2 t.deletionleftkey[newchildcount] = t.data[c2].k end c3 = t.tree[p].child3 if c3 != it && c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.data[c3].k end @invariant newchildcount == 1 || newchildcount == 2 push!(t.freedatainds, it) pop!(t.useddatacells,it) defaultKey = t.tree[1].splitkey1 curdepth = t.depth mustdeleteroot = false pparent = -1 ## The following loop ascends the tree and contracts nodes (reduces their ## number of children) as ## needed. If newchildcount == 2 or 3, then the ascent is terminated ## and a node is created with 2 or 3 children. ## If newchildcount == 1, then the ascent must continue since a tree ## node cannot have one child. while true pparent = t.tree[p].parent ## Simple cases when the new child count is 2 or 3 if newchildcount == 2 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], 0, pparent, t.deletionleftkey[2], defaultKey) break end if newchildcount == 3 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], t.deletionchild[3], pparent, t.deletionleftkey[2], t.deletionleftkey[3]) break end @invariant newchildcount == 1 ## For the rest of this loop, we cover the case ## that p has one child. ## If newchildcount == 1 and curdepth==1, this means that ## the root of the tree has only one child. In this case, we can ## delete the root and make its one child the new root (see below). if curdepth == 1 mustdeleteroot = true break end ## We now branch on three cases depending on whether p is child1, ## child2 or child3 of its parent. if t.tree[pparent].child1 == p rightsib = t.tree[pparent].child2 ## Here p is child1 and rightsib is child2. ## If rightsib has 2 children, then p and ## rightsib are merged into a single node ## that has three children. ## If rightsib has 3 children, then p and ## rightsib are reformed so that each has ## two children. if t.tree[rightsib].child3 == 0 rc1 = t.tree[rightsib].child1 rc2 = t.tree[rightsib].child2 t.tree[p] = TreeNode{K}(t.deletionchild[1], rc1, rc2, pparent, t.tree[pparent].splitkey1, t.tree[rightsib].splitkey1) if curdepth == t.depth replaceparent!(t.data, rc1, p) replaceparent!(t.data, rc2, p) else replaceparent!(t.tree, rc1, p) replaceparent!(t.tree, rc2, p) end push!(t.freetreeinds, rightsib) newchildcount = 1 t.deletionchild[1] = p else rc1 = t.tree[rightsib].child1 t.tree[p] = TreeNode{K}(t.deletionchild[1], rc1, 0, pparent, t.tree[pparent].splitkey1, defaultKey) sk1 = t.tree[rightsib].splitkey1 t.tree[rightsib] = TreeNode{K}(t.tree[rightsib].child2, t.tree[rightsib].child3, 0, pparent, t.tree[rightsib].splitkey2, defaultKey) if curdepth == t.depth replaceparent!(t.data, rc1, p) else replaceparent!(t.tree, rc1, p) end newchildcount = 2 t.deletionchild[1] = p t.deletionchild[2] = rightsib t.deletionleftkey[2] = sk1 end ## If pparent had a third child (besides p and rightsib) ## then we add this to t.deletionchild c3 = t.tree[pparent].child3 if c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2 end p = pparent elseif t.tree[pparent].child2 == p ## Here p is child2 and leftsib is child1. ## If leftsib has 2 children, then p and ## leftsib are merged into a single node ## that has three children. ## If leftsib has 3 children, then p and ## leftsib are reformed so that each has ## two children. leftsib = t.tree[pparent].child1 lk = deletionleftkey1_valid ? t.deletionleftkey[1] : t.tree[pparent].splitkey1 if t.tree[leftsib].child3 == 0 lc1 = t.tree[leftsib].child1 lc2 = t.tree[leftsib].child2 t.tree[p] = TreeNode{K}(lc1, lc2, t.deletionchild[1], pparent, t.tree[leftsib].splitkey1, lk) if curdepth == t.depth replaceparent!(t.data, lc1, p) replaceparent!(t.data, lc2, p) else replaceparent!(t.tree, lc1, p) replaceparent!(t.tree, lc2, p) end push!(t.freetreeinds, leftsib) newchildcount = 1 t.deletionchild[1] = p else lc3 = t.tree[leftsib].child3 t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0, pparent, lk, defaultKey) sk2 = t.tree[leftsib].splitkey2 t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1, t.tree[leftsib].child2, 0, pparent, t.tree[leftsib].splitkey1, defaultKey) if curdepth == t.depth replaceparent!(t.data, lc3, p) else replaceparent!(t.tree, lc3, p) end newchildcount = 2 t.deletionchild[1] = leftsib t.deletionchild[2] = p t.deletionleftkey[2] = sk2 end ## If pparent had a third child (besides p and leftsib) ## then we add this to t.deletionchild c3 = t.tree[pparent].child3 if c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2 end p = pparent deletionleftkey1_valid = false else ## Here p is child3 and leftsib is child2. ## If leftsib has 2 children, then p and ## leftsib are merged into a single node ## that has three children. ## If leftsib has 3 children, then p and ## leftsib are reformed so that each has ## two children. @invariant t.tree[pparent].child3 == p leftsib = t.tree[pparent].child2 lk = deletionleftkey1_valid ? t.deletionleftkey[1] : t.tree[pparent].splitkey2 if t.tree[leftsib].child3 == 0 lc1 = t.tree[leftsib].child1 lc2 = t.tree[leftsib].child2 t.tree[p] = TreeNode{K}(lc1, lc2, t.deletionchild[1], pparent, t.tree[leftsib].splitkey1, lk) if curdepth == t.depth replaceparent!(t.data, lc1, p) replaceparent!(t.data, lc2, p) else replaceparent!(t.tree, lc1, p) replaceparent!(t.tree, lc2, p) end push!(t.freetreeinds, leftsib) newchildcount = 2 t.deletionchild[1] = t.tree[pparent].child1 t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionchild[2] = p else lc3 = t.tree[leftsib].child3 t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0, pparent, lk, defaultKey) sk2 = t.tree[leftsib].splitkey2 t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1, t.tree[leftsib].child2, 0, pparent, t.tree[leftsib].splitkey1, defaultKey) if curdepth == t.depth replaceparent!(t.data, lc3, p) else replaceparent!(t.tree, lc3, p) end newchildcount = 3 t.deletionchild[1] = t.tree[pparent].child1 t.deletionchild[2] = leftsib t.deletionchild[3] = p t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionleftkey[3] = sk2 end p = pparent deletionleftkey1_valid = false end curdepth -= 1 end if mustdeleteroot @invariant !deletionleftkey1_valid @invariant p == t.rootloc t.rootloc = t.deletionchild[1] t.depth -= 1 push!(t.freetreeinds, p) end ## If deletionleftkey1_valid, this means that the new ## min key of the deleted node and its right neighbors ## has never been stored in the tree. It must be stored ## as splitkey1 or splitkey2 of some ancestor of the ## deleted node, so we continue ascending the tree ## until we find a node which has p (and therefore the ## deleted node) as its descendent through its second ## or third child. ## It cannot be the case that the deleted node is ## is a descendent of the root always through ## first children, since this would mean the deleted ## node is the leftmost placeholder, which ## cannot be deleted. if deletionleftkey1_valid while true pparentnode = t.tree[pparent] if pparentnode.child2 == p t.tree[pparent] = TreeNode{K}(pparentnode.child1, pparentnode.child2, pparentnode.child3, pparentnode.parent, t.deletionleftkey[1], pparentnode.splitkey2) break elseif pparentnode.child3 == p t.tree[pparent] = TreeNode{K}(pparentnode.child1, pparentnode.child2, pparentnode.child3, pparentnode.parent, pparentnode.splitkey1, t.deletionleftkey[1]) break else p = pparent pparent = pparentnode.parent curdepth -= 1 @invariant curdepth > 0 end end end return nothing end
{ "fpath_tuple": [ "DataStructures.jl", "src", "balanced_tree.jl" ], "ground_truth": "function Base.delete!(t::BalancedTree23{K,D,Ord}, it::Int) where {K,D,Ord<:Ordering}\n\n ## Put the cell indexed by 'it' into the deletion list.\n ##\n ## Create the following data items maintained in the\n ## upcoming loop.\n ##\n ## p is a tree-node ancestor of the deleted node\n ## The children of p are stored in\n ## t.deletionchild[..]\n ## The number of these children is newchildcount, which is 1, 2 or 3.\n ## The keys that lower bound the children\n ## are stored in t.deletionleftkey[..]\n ## There is a special case for t.deletionleftkey[1]; the\n ## flag deletionleftkey1_valid indicates that the left key\n ## for the immediate right neighbor of the\n ## deleted node has not yet been been stored in the tree.\n ## Once it is stored, t.deletionleftkey[1] is no longer needed\n ## or used.\n ## The flag mustdeleteroot means that the tree has contracted\n ## enough that it loses a level.\n\n p = t.data[it].parent\n newchildcount = 0\n c1 = t.tree[p].child1\n deletionleftkey1_valid = true\n if c1 != it\n deletionleftkey1_valid = false\n newchildcount += 1\n t.deletionchild[newchildcount] = c1\n t.deletionleftkey[newchildcount] = t.data[c1].k\n end\n c2 = t.tree[p].child2\n if c2 != it\n newchildcount += 1\n t.deletionchild[newchildcount] = c2\n t.deletionleftkey[newchildcount] = t.data[c2].k\n end\n c3 = t.tree[p].child3\n if c3 != it && c3 > 0\n newchildcount += 1\n t.deletionchild[newchildcount] = c3\n t.deletionleftkey[newchildcount] = t.data[c3].k\n end\n @invariant newchildcount == 1 || newchildcount == 2\n push!(t.freedatainds, it)\n pop!(t.useddatacells,it)\n defaultKey = t.tree[1].splitkey1\n curdepth = t.depth\n mustdeleteroot = false\n pparent = -1\n\n ## The following loop ascends the tree and contracts nodes (reduces their\n ## number of children) as\n ## needed. If newchildcount == 2 or 3, then the ascent is terminated\n ## and a node is created with 2 or 3 children.\n ## If newchildcount == 1, then the ascent must continue since a tree\n ## node cannot have one child.\n\n while true\n pparent = t.tree[p].parent\n ## Simple cases when the new child count is 2 or 3\n if newchildcount == 2\n t.tree[p] = TreeNode{K}(t.deletionchild[1],\n t.deletionchild[2], 0, pparent,\n t.deletionleftkey[2], defaultKey)\n\n break\n end\n if newchildcount == 3\n t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2],\n t.deletionchild[3], pparent,\n t.deletionleftkey[2], t.deletionleftkey[3])\n break\n end\n @invariant newchildcount == 1\n ## For the rest of this loop, we cover the case\n ## that p has one child.\n\n ## If newchildcount == 1 and curdepth==1, this means that\n ## the root of the tree has only one child. In this case, we can\n ## delete the root and make its one child the new root (see below).\n\n if curdepth == 1\n mustdeleteroot = true\n break\n end\n\n ## We now branch on three cases depending on whether p is child1,\n ## child2 or child3 of its parent.\n\n if t.tree[pparent].child1 == p\n rightsib = t.tree[pparent].child2\n\n ## Here p is child1 and rightsib is child2.\n ## If rightsib has 2 children, then p and\n ## rightsib are merged into a single node\n ## that has three children.\n ## If rightsib has 3 children, then p and\n ## rightsib are reformed so that each has\n ## two children.\n\n if t.tree[rightsib].child3 == 0\n rc1 = t.tree[rightsib].child1\n rc2 = t.tree[rightsib].child2\n t.tree[p] = TreeNode{K}(t.deletionchild[1],\n rc1, rc2,\n pparent,\n t.tree[pparent].splitkey1,\n t.tree[rightsib].splitkey1)\n if curdepth == t.depth\n replaceparent!(t.data, rc1, p)\n replaceparent!(t.data, rc2, p)\n else\n replaceparent!(t.tree, rc1, p)\n replaceparent!(t.tree, rc2, p)\n end\n push!(t.freetreeinds, rightsib)\n newchildcount = 1\n t.deletionchild[1] = p\n else\n rc1 = t.tree[rightsib].child1\n t.tree[p] = TreeNode{K}(t.deletionchild[1], rc1, 0,\n pparent,\n t.tree[pparent].splitkey1,\n defaultKey)\n sk1 = t.tree[rightsib].splitkey1\n t.tree[rightsib] = TreeNode{K}(t.tree[rightsib].child2,\n t.tree[rightsib].child3,\n 0,\n pparent,\n t.tree[rightsib].splitkey2,\n defaultKey)\n if curdepth == t.depth\n replaceparent!(t.data, rc1, p)\n else\n replaceparent!(t.tree, rc1, p)\n end\n newchildcount = 2\n t.deletionchild[1] = p\n t.deletionchild[2] = rightsib\n t.deletionleftkey[2] = sk1\n end\n\n ## If pparent had a third child (besides p and rightsib)\n ## then we add this to t.deletionchild\n\n c3 = t.tree[pparent].child3\n if c3 > 0\n newchildcount += 1\n t.deletionchild[newchildcount] = c3\n t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2\n end\n p = pparent\n elseif t.tree[pparent].child2 == p\n\n ## Here p is child2 and leftsib is child1.\n ## If leftsib has 2 children, then p and\n ## leftsib are merged into a single node\n ## that has three children.\n ## If leftsib has 3 children, then p and\n ## leftsib are reformed so that each has\n ## two children.\n\n leftsib = t.tree[pparent].child1\n lk = deletionleftkey1_valid ?\n t.deletionleftkey[1] :\n t.tree[pparent].splitkey1\n if t.tree[leftsib].child3 == 0\n lc1 = t.tree[leftsib].child1\n lc2 = t.tree[leftsib].child2\n t.tree[p] = TreeNode{K}(lc1, lc2,\n t.deletionchild[1],\n pparent,\n t.tree[leftsib].splitkey1,\n lk)\n if curdepth == t.depth\n replaceparent!(t.data, lc1, p)\n replaceparent!(t.data, lc2, p)\n else\n replaceparent!(t.tree, lc1, p)\n replaceparent!(t.tree, lc2, p)\n end\n push!(t.freetreeinds, leftsib)\n newchildcount = 1\n t.deletionchild[1] = p\n else\n lc3 = t.tree[leftsib].child3\n t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0,\n pparent, lk, defaultKey)\n sk2 = t.tree[leftsib].splitkey2\n t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1,\n t.tree[leftsib].child2,\n 0, pparent,\n t.tree[leftsib].splitkey1,\n defaultKey)\n if curdepth == t.depth\n replaceparent!(t.data, lc3, p)\n else\n replaceparent!(t.tree, lc3, p)\n end\n newchildcount = 2\n t.deletionchild[1] = leftsib\n t.deletionchild[2] = p\n t.deletionleftkey[2] = sk2\n end\n\n ## If pparent had a third child (besides p and leftsib)\n ## then we add this to t.deletionchild\n\n c3 = t.tree[pparent].child3\n if c3 > 0\n newchildcount += 1\n t.deletionchild[newchildcount] = c3\n t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2\n end\n p = pparent\n deletionleftkey1_valid = false\n else\n ## Here p is child3 and leftsib is child2.\n ## If leftsib has 2 children, then p and\n ## leftsib are merged into a single node\n ## that has three children.\n ## If leftsib has 3 children, then p and\n ## leftsib are reformed so that each has\n ## two children.\n\n @invariant t.tree[pparent].child3 == p\n leftsib = t.tree[pparent].child2\n lk = deletionleftkey1_valid ?\n t.deletionleftkey[1] :\n t.tree[pparent].splitkey2\n if t.tree[leftsib].child3 == 0\n lc1 = t.tree[leftsib].child1\n lc2 = t.tree[leftsib].child2\n t.tree[p] = TreeNode{K}(lc1, lc2,\n t.deletionchild[1],\n pparent,\n t.tree[leftsib].splitkey1,\n lk)\n if curdepth == t.depth\n replaceparent!(t.data, lc1, p)\n replaceparent!(t.data, lc2, p)\n else\n replaceparent!(t.tree, lc1, p)\n replaceparent!(t.tree, lc2, p)\n end\n push!(t.freetreeinds, leftsib)\n newchildcount = 2\n t.deletionchild[1] = t.tree[pparent].child1\n t.deletionleftkey[2] = t.tree[pparent].splitkey1\n t.deletionchild[2] = p\n else\n lc3 = t.tree[leftsib].child3\n t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0,\n pparent, lk, defaultKey)\n sk2 = t.tree[leftsib].splitkey2\n t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1,\n t.tree[leftsib].child2,\n 0, pparent,\n t.tree[leftsib].splitkey1,\n defaultKey)\n if curdepth == t.depth\n replaceparent!(t.data, lc3, p)\n else\n replaceparent!(t.tree, lc3, p)\n end\n newchildcount = 3\n t.deletionchild[1] = t.tree[pparent].child1\n t.deletionchild[2] = leftsib\n t.deletionchild[3] = p\n t.deletionleftkey[2] = t.tree[pparent].splitkey1\n t.deletionleftkey[3] = sk2\n end\n p = pparent\n deletionleftkey1_valid = false\n end\n curdepth -= 1\n end\n if mustdeleteroot\n @invariant !deletionleftkey1_valid\n @invariant p == t.rootloc\n t.rootloc = t.deletionchild[1]\n t.depth -= 1\n push!(t.freetreeinds, p)\n end\n\n ## If deletionleftkey1_valid, this means that the new\n ## min key of the deleted node and its right neighbors\n ## has never been stored in the tree. It must be stored\n ## as splitkey1 or splitkey2 of some ancestor of the\n ## deleted node, so we continue ascending the tree\n ## until we find a node which has p (and therefore the\n ## deleted node) as its descendent through its second\n ## or third child.\n ## It cannot be the case that the deleted node is\n ## is a descendent of the root always through\n ## first children, since this would mean the deleted\n ## node is the leftmost placeholder, which\n ## cannot be deleted.\n\n if deletionleftkey1_valid\n while true\n pparentnode = t.tree[pparent]\n if pparentnode.child2 == p\n t.tree[pparent] = TreeNode{K}(pparentnode.child1,\n pparentnode.child2,\n pparentnode.child3,\n pparentnode.parent,\n t.deletionleftkey[1],\n pparentnode.splitkey2)\n break\n elseif pparentnode.child3 == p\n t.tree[pparent] = TreeNode{K}(pparentnode.child1,\n pparentnode.child2,\n pparentnode.child3,\n pparentnode.parent,\n pparentnode.splitkey1,\n t.deletionleftkey[1])\n break\n else\n p = pparent\n pparent = pparentnode.parent\n curdepth -= 1\n @invariant curdepth > 0\n end\n end\n end\n return nothing\nend", "task_id": "DataStructures/11" }
655
984
DataStructures.jl
11
function Base.delete!(t::BalancedTree23{K,D,Ord}, it::Int) where {K,D,Ord<:Ordering} ## Put the cell indexed by 'it' into the deletion list. ## ## Create the following data items maintained in the ## upcoming loop. ## ## p is a tree-node ancestor of the deleted node ## The children of p are stored in ## t.deletionchild[..] ## The number of these children is newchildcount, which is 1, 2 or 3. ## The keys that lower bound the children ## are stored in t.deletionleftkey[..] ## There is a special case for t.deletionleftkey[1]; the ## flag deletionleftkey1_valid indicates that the left key ## for the immediate right neighbor of the ## deleted node has not yet been been stored in the tree. ## Once it is stored, t.deletionleftkey[1] is no longer needed ## or used. ## The flag mustdeleteroot means that the tree has contracted ## enough that it loses a level. p = t.data[it].parent newchildcount = 0 c1 = t.tree[p].child1 deletionleftkey1_valid = true if c1 != it deletionleftkey1_valid = false newchildcount += 1 t.deletionchild[newchildcount] = c1 t.deletionleftkey[newchildcount] = t.data[c1].k end c2 = t.tree[p].child2 if c2 != it newchildcount += 1 t.deletionchild[newchildcount] = c2 t.deletionleftkey[newchildcount] = t.data[c2].k end c3 = t.tree[p].child3 if c3 != it && c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.data[c3].k end @invariant newchildcount == 1 || newchildcount == 2 push!(t.freedatainds, it) pop!(t.useddatacells,it) defaultKey = t.tree[1].splitkey1 curdepth = t.depth mustdeleteroot = false pparent = -1 ## The following loop ascends the tree and contracts nodes (reduces their ## number of children) as ## needed. If newchildcount == 2 or 3, then the ascent is terminated ## and a node is created with 2 or 3 children. ## If newchildcount == 1, then the ascent must continue since a tree ## node cannot have one child. while true pparent = t.tree[p].parent ## Simple cases when the new child count is 2 or 3 if newchildcount == 2 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], 0, pparent, t.deletionleftkey[2], defaultKey) break end if newchildcount == 3 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], t.deletionchild[3], pparent, t.deletionleftkey[2], t.deletionleftkey[3]) break end @invariant newchildcount == 1 ## For the rest of this loop, we cover the case ## that p has one child. ## If newchildcount == 1 and curdepth==1, this means that ## the root of the tree has only one child. In this case, we can ## delete the root and make its one child the new root (see below). if curdepth == 1 mustdeleteroot = true break end ## We now branch on three cases depending on whether p is child1, ## child2 or child3 of its parent. if t.tree[pparent].child1 == p rightsib = t.tree[pparent].child2 ## Here p is child1 and rightsib is child2. ## If rightsib has 2 children, then p and ## rightsib are merged into a single node ## that has three children. ## If rightsib has 3 children, then p and ## rightsib are reformed so that each has ## two children. if t.tree[rightsib].child3 == 0 rc1 = t.tree[rightsib].child1 rc2 = t.tree[rightsib].child2 t.tree[p] = TreeNode{K}(t.deletionchild[1], rc1, rc2, pparent, t.tree[pparent].splitkey1, t.tree[rightsib].splitkey1) if curdepth == t.depth replaceparent!(t.data, rc1, p) replaceparent!(t.data, rc2, p) else replaceparent!(t.tree, rc1, p) replaceparent!(t.tree, rc2, p) end push!(t.freetreeinds, rightsib) newchildcount = 1 t.deletionchild[1] = p else rc1 = t.tree[rightsib].child1 t.tree[p] = TreeNode{K}(t.deletionchild[1], rc1, 0, pparent, t.tree[pparent].splitkey1, defaultKey) sk1 = t.tree[rightsib].splitkey1 t.tree[rightsib] = TreeNode{K}(t.tree[rightsib].child2, t.tree[rightsib].child3, 0, pparent, t.tree[rightsib].splitkey2, defaultKey) if curdepth == t.depth replaceparent!(t.data, rc1, p) else replaceparent!(t.tree, rc1, p) end newchildcount = 2 t.deletionchild[1] = p t.deletionchild[2] = rightsib t.deletionleftkey[2] = sk1 end ## If pparent had a third child (besides p and rightsib) ## then we add this to t.deletionchild c3 = t.tree[pparent].child3 if c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2 end p = pparent elseif t.tree[pparent].child2 == p ## Here p is child2 and leftsib is child1. ## If leftsib has 2 children, then p and ## leftsib are merged into a single node ## that has three children. ## If leftsib has 3 children, then p and ## leftsib are reformed so that each has ## two children. leftsib = t.tree[pparent].child1 lk = deletionleftkey1_valid ? t.deletionleftkey[1] : t.tree[pparent].splitkey1 if t.tree[leftsib].child3 == 0 lc1 = t.tree[leftsib].child1 lc2 = t.tree[leftsib].child2 t.tree[p] = TreeNode{K}(lc1, lc2, t.deletionchild[1], pparent, t.tree[leftsib].splitkey1, lk) if curdepth == t.depth replaceparent!(t.data, lc1, p) replaceparent!(t.data, lc2, p) else replaceparent!(t.tree, lc1, p) replaceparent!(t.tree, lc2, p) end push!(t.freetreeinds, leftsib) newchildcount = 1 t.deletionchild[1] = p else lc3 = t.tree[leftsib].child3 t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0, pparent, lk, defaultKey) sk2 = t.tree[leftsib].splitkey2 t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1, t.tree[leftsib].child2, 0, pparent, t.tree[leftsib].splitkey1, defaultKey) if curdepth == t.depth replaceparent!(t.data, lc3, p) else replaceparent!(t.tree, lc3, p) end newchildcount = 2 t.deletionchild[1] = leftsib t.deletionchild[2] = p t.deletionleftkey[2] = sk2 end ## If pparent had a third child (besides p and leftsib) ## then we add this to t.deletionchild c3 = t.tree[pparent].child3 if c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2 end p = pparent deletionleftkey1_valid = false else ## Here p is child3 and leftsib is child2. ## If leftsib has 2 children, then p and ## leftsib are merged into a single node ## that has three children. ## If leftsib has 3 children, then p and ## leftsib are reformed so that each has ## two children. @invariant t.tree[pparent].child3 == p leftsib = t.tree[pparent].child2 lk = deletionleftkey1_valid ? t.deletionleftkey[1] : t.tree[pparent].splitkey2 if t.tree[leftsib].child3 == 0 lc1 = t.tree[leftsib].child1 lc2 = t.tree[leftsib].child2 t.tree[p] = TreeNode{K}(lc1, lc2, t.deletionchild[1], pparent, t.tree[leftsib].splitkey1, lk) if curdepth == t.depth replaceparent!(t.data, lc1, p) replaceparent!(t.data, lc2, p) else replaceparent!(t.tree, lc1, p) replaceparent!(t.tree, lc2, p) end push!(t.freetreeinds, leftsib) newchildcount = 2 t.deletionchild[1] = t.tree[pparent].child1 t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionchild[2] = p else lc3 = t.tree[leftsib].child3 t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0, pparent, lk, defaultKey) sk2 = t.tree[leftsib].splitkey2 t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1, t.tree[leftsib].child2, 0, pparent, t.tree[leftsib].splitkey1, defaultKey) if curdepth == t.depth replaceparent!(t.data, lc3, p) else replaceparent!(t.tree, lc3, p) end newchildcount = 3 t.deletionchild[1] = t.tree[pparent].child1 t.deletionchild[2] = leftsib t.deletionchild[3] = p t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionleftkey[3] = sk2 end p = pparent deletionleftkey1_valid = false end curdepth -= 1 end if mustdeleteroot @invariant !deletionleftkey1_valid @invariant p == t.rootloc t.rootloc = t.deletionchild[1] t.depth -= 1 push!(t.freetreeinds, p) end ## If deletionleftkey1_valid, this means that the new ## min key of the deleted node and its right neighbors ## has never been stored in the tree. It must be stored ## as splitkey1 or splitkey2 of some ancestor of the ## deleted node, so we continue ascending the tree ## until we find a node which has p (and therefore the ## deleted node) as its descendent through its second ## or third child. ## It cannot be the case that the deleted node is ## is a descendent of the root always through ## first children, since this would mean the deleted ## node is the leftmost placeholder, which ## cannot be deleted. if deletionleftkey1_valid while true pparentnode = t.tree[pparent] if pparentnode.child2 == p t.tree[pparent] = TreeNode{K}(pparentnode.child1, pparentnode.child2, pparentnode.child3, pparentnode.parent, t.deletionleftkey[1], pparentnode.splitkey2) break elseif pparentnode.child3 == p t.tree[pparent] = TreeNode{K}(pparentnode.child1, pparentnode.child2, pparentnode.child3, pparentnode.parent, pparentnode.splitkey1, t.deletionleftkey[1]) break else p = pparent pparent = pparentnode.parent curdepth -= 1 @invariant curdepth > 0 end end end return nothing end
Base.delete!(t::BalancedTree23{K,D,Ord}, it::Int) where {K,D,Ord<:Ordering}
[ 655, 984 ]
function Base.delete!(t::BalancedTree23{K,D,Ord}, it::Int) where {K,D,Ord<:Ordering} ## Put the cell indexed by 'it' into the deletion list. ## ## Create the following data items maintained in the ## upcoming loop. ## ## p is a tree-node ancestor of the deleted node ## The children of p are stored in ## t.deletionchild[..] ## The number of these children is newchildcount, which is 1, 2 or 3. ## The keys that lower bound the children ## are stored in t.deletionleftkey[..] ## There is a special case for t.deletionleftkey[1]; the ## flag deletionleftkey1_valid indicates that the left key ## for the immediate right neighbor of the ## deleted node has not yet been been stored in the tree. ## Once it is stored, t.deletionleftkey[1] is no longer needed ## or used. ## The flag mustdeleteroot means that the tree has contracted ## enough that it loses a level. p = t.data[it].parent newchildcount = 0 c1 = t.tree[p].child1 deletionleftkey1_valid = true if c1 != it deletionleftkey1_valid = false newchildcount += 1 t.deletionchild[newchildcount] = c1 t.deletionleftkey[newchildcount] = t.data[c1].k end c2 = t.tree[p].child2 if c2 != it newchildcount += 1 t.deletionchild[newchildcount] = c2 t.deletionleftkey[newchildcount] = t.data[c2].k end c3 = t.tree[p].child3 if c3 != it && c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.data[c3].k end @invariant newchildcount == 1 || newchildcount == 2 push!(t.freedatainds, it) pop!(t.useddatacells,it) defaultKey = t.tree[1].splitkey1 curdepth = t.depth mustdeleteroot = false pparent = -1 ## The following loop ascends the tree and contracts nodes (reduces their ## number of children) as ## needed. If newchildcount == 2 or 3, then the ascent is terminated ## and a node is created with 2 or 3 children. ## If newchildcount == 1, then the ascent must continue since a tree ## node cannot have one child. while true pparent = t.tree[p].parent ## Simple cases when the new child count is 2 or 3 if newchildcount == 2 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], 0, pparent, t.deletionleftkey[2], defaultKey) break end if newchildcount == 3 t.tree[p] = TreeNode{K}(t.deletionchild[1], t.deletionchild[2], t.deletionchild[3], pparent, t.deletionleftkey[2], t.deletionleftkey[3]) break end @invariant newchildcount == 1 ## For the rest of this loop, we cover the case ## that p has one child. ## If newchildcount == 1 and curdepth==1, this means that ## the root of the tree has only one child. In this case, we can ## delete the root and make its one child the new root (see below). if curdepth == 1 mustdeleteroot = true break end ## We now branch on three cases depending on whether p is child1, ## child2 or child3 of its parent. if t.tree[pparent].child1 == p rightsib = t.tree[pparent].child2 ## Here p is child1 and rightsib is child2. ## If rightsib has 2 children, then p and ## rightsib are merged into a single node ## that has three children. ## If rightsib has 3 children, then p and ## rightsib are reformed so that each has ## two children. if t.tree[rightsib].child3 == 0 rc1 = t.tree[rightsib].child1 rc2 = t.tree[rightsib].child2 t.tree[p] = TreeNode{K}(t.deletionchild[1], rc1, rc2, pparent, t.tree[pparent].splitkey1, t.tree[rightsib].splitkey1) if curdepth == t.depth replaceparent!(t.data, rc1, p) replaceparent!(t.data, rc2, p) else replaceparent!(t.tree, rc1, p) replaceparent!(t.tree, rc2, p) end push!(t.freetreeinds, rightsib) newchildcount = 1 t.deletionchild[1] = p else rc1 = t.tree[rightsib].child1 t.tree[p] = TreeNode{K}(t.deletionchild[1], rc1, 0, pparent, t.tree[pparent].splitkey1, defaultKey) sk1 = t.tree[rightsib].splitkey1 t.tree[rightsib] = TreeNode{K}(t.tree[rightsib].child2, t.tree[rightsib].child3, 0, pparent, t.tree[rightsib].splitkey2, defaultKey) if curdepth == t.depth replaceparent!(t.data, rc1, p) else replaceparent!(t.tree, rc1, p) end newchildcount = 2 t.deletionchild[1] = p t.deletionchild[2] = rightsib t.deletionleftkey[2] = sk1 end ## If pparent had a third child (besides p and rightsib) ## then we add this to t.deletionchild c3 = t.tree[pparent].child3 if c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2 end p = pparent elseif t.tree[pparent].child2 == p ## Here p is child2 and leftsib is child1. ## If leftsib has 2 children, then p and ## leftsib are merged into a single node ## that has three children. ## If leftsib has 3 children, then p and ## leftsib are reformed so that each has ## two children. leftsib = t.tree[pparent].child1 lk = deletionleftkey1_valid ? t.deletionleftkey[1] : t.tree[pparent].splitkey1 if t.tree[leftsib].child3 == 0 lc1 = t.tree[leftsib].child1 lc2 = t.tree[leftsib].child2 t.tree[p] = TreeNode{K}(lc1, lc2, t.deletionchild[1], pparent, t.tree[leftsib].splitkey1, lk) if curdepth == t.depth replaceparent!(t.data, lc1, p) replaceparent!(t.data, lc2, p) else replaceparent!(t.tree, lc1, p) replaceparent!(t.tree, lc2, p) end push!(t.freetreeinds, leftsib) newchildcount = 1 t.deletionchild[1] = p else lc3 = t.tree[leftsib].child3 t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0, pparent, lk, defaultKey) sk2 = t.tree[leftsib].splitkey2 t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1, t.tree[leftsib].child2, 0, pparent, t.tree[leftsib].splitkey1, defaultKey) if curdepth == t.depth replaceparent!(t.data, lc3, p) else replaceparent!(t.tree, lc3, p) end newchildcount = 2 t.deletionchild[1] = leftsib t.deletionchild[2] = p t.deletionleftkey[2] = sk2 end ## If pparent had a third child (besides p and leftsib) ## then we add this to t.deletionchild c3 = t.tree[pparent].child3 if c3 > 0 newchildcount += 1 t.deletionchild[newchildcount] = c3 t.deletionleftkey[newchildcount] = t.tree[pparent].splitkey2 end p = pparent deletionleftkey1_valid = false else ## Here p is child3 and leftsib is child2. ## If leftsib has 2 children, then p and ## leftsib are merged into a single node ## that has three children. ## If leftsib has 3 children, then p and ## leftsib are reformed so that each has ## two children. @invariant t.tree[pparent].child3 == p leftsib = t.tree[pparent].child2 lk = deletionleftkey1_valid ? t.deletionleftkey[1] : t.tree[pparent].splitkey2 if t.tree[leftsib].child3 == 0 lc1 = t.tree[leftsib].child1 lc2 = t.tree[leftsib].child2 t.tree[p] = TreeNode{K}(lc1, lc2, t.deletionchild[1], pparent, t.tree[leftsib].splitkey1, lk) if curdepth == t.depth replaceparent!(t.data, lc1, p) replaceparent!(t.data, lc2, p) else replaceparent!(t.tree, lc1, p) replaceparent!(t.tree, lc2, p) end push!(t.freetreeinds, leftsib) newchildcount = 2 t.deletionchild[1] = t.tree[pparent].child1 t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionchild[2] = p else lc3 = t.tree[leftsib].child3 t.tree[p] = TreeNode{K}(lc3, t.deletionchild[1], 0, pparent, lk, defaultKey) sk2 = t.tree[leftsib].splitkey2 t.tree[leftsib] = TreeNode{K}(t.tree[leftsib].child1, t.tree[leftsib].child2, 0, pparent, t.tree[leftsib].splitkey1, defaultKey) if curdepth == t.depth replaceparent!(t.data, lc3, p) else replaceparent!(t.tree, lc3, p) end newchildcount = 3 t.deletionchild[1] = t.tree[pparent].child1 t.deletionchild[2] = leftsib t.deletionchild[3] = p t.deletionleftkey[2] = t.tree[pparent].splitkey1 t.deletionleftkey[3] = sk2 end p = pparent deletionleftkey1_valid = false end curdepth -= 1 end if mustdeleteroot @invariant !deletionleftkey1_valid @invariant p == t.rootloc t.rootloc = t.deletionchild[1] t.depth -= 1 push!(t.freetreeinds, p) end ## If deletionleftkey1_valid, this means that the new ## min key of the deleted node and its right neighbors ## has never been stored in the tree. It must be stored ## as splitkey1 or splitkey2 of some ancestor of the ## deleted node, so we continue ascending the tree ## until we find a node which has p (and therefore the ## deleted node) as its descendent through its second ## or third child. ## It cannot be the case that the deleted node is ## is a descendent of the root always through ## first children, since this would mean the deleted ## node is the leftmost placeholder, which ## cannot be deleted. if deletionleftkey1_valid while true pparentnode = t.tree[pparent] if pparentnode.child2 == p t.tree[pparent] = TreeNode{K}(pparentnode.child1, pparentnode.child2, pparentnode.child3, pparentnode.parent, t.deletionleftkey[1], pparentnode.splitkey2) break elseif pparentnode.child3 == p t.tree[pparent] = TreeNode{K}(pparentnode.child1, pparentnode.child2, pparentnode.child3, pparentnode.parent, pparentnode.splitkey1, t.deletionleftkey[1]) break else p = pparent pparent = pparentnode.parent curdepth -= 1 @invariant curdepth > 0 end end end return nothing end
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
9