Skip to content

Commit a6c7bac

Browse files
committed
Simplify setproperty specializations
1 parent e521d18 commit a6c7bac

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

src/avl_tree.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ end
1414

1515
AVLTreeNode(d) = AVLTreeNode{Any}(d)
1616

17-
_setproperty!(x::AVLTreeNode{T}, f, v) where {T} =
18-
setfield!(x, f, v)
1917
Base.setproperty!(x::AVLTreeNode{T}, f::Symbol, v) where {T} =
20-
_setproperty!(x, f, v)
18+
setfield!(x, f, v)
2119

2220
AVLTreeNode_or_null{T} = Union{AVLTreeNode{T}, Nothing}
2321

@@ -45,7 +43,7 @@ end
4543

4644
# computes the height of the subtree, which basically is
4745
# one added the maximum of the height of the left subtree and right subtree
48-
compute_height(node::AVLTreeNode) = Int8(1 + max(get_height(node.leftChild), get_height(node.rightChild)))
46+
compute_height(node::AVLTreeNode) = Int8(1) + max(get_height(node.leftChild), get_height(node.rightChild))
4947

5048
get_subsize(node::AVLTreeNode_or_null) = (node == nothing) ? Int32(0) : node.subsize
5149

src/red_black_tree.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ end
1717
RBTreeNode() = RBTreeNode{Any}()
1818
RBTreeNode(d) = RBTreeNode{Any}(d)
1919

20-
_setproperty!(x::RBTreeNode{K}, f, v) where {K} =
21-
setfield!(x, f, v)
2220
Base.setproperty!(x::RBTreeNode{K}, f::Symbol, v) where {K} =
23-
_setproperty!(x, f, v)
21+
setfield!(x, f, v)
2422

2523
function create_null_node(K::Type)
2624
node = RBTreeNode{K}()

src/splay_tree.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ end
1111
SplayTreeNode(d) = SplayTreeNode{Any}(d)
1212
SplayTreeNode() = SplayTreeNode{Any}()
1313

14+
Base.setproperty!(x::SplayTreeNode{K}, f::Symbol, v) where {K} =
15+
setfield!(x, f, v)
16+
1417
mutable struct SplayTree{K}
1518
root::Union{SplayTreeNode{K}, Nothing}
1619
count::Int
1720

1821
SplayTree{K}() where K = new{K}(nothing, 0)
19-
end
22+
end
2023

2124
Base.length(tree::SplayTree) = tree.count
2225

@@ -41,7 +44,7 @@ function left_rotate!(tree::SplayTree, node_x::SplayTreeNode)
4144
node_y.leftChild = node_x
4245
end
4346
node_x.parent = node_y
44-
end
47+
end
4548

4649
function right_rotate!(tree::SplayTree, node_x::SplayTreeNode)
4750
node_y = node_x.leftChild
@@ -59,7 +62,7 @@ function right_rotate!(tree::SplayTree, node_x::SplayTreeNode)
5962
end
6063
node_y.rightChild = node_x
6164
node_x.parent = node_y
62-
end
65+
end
6366

6467
# The splaying operation moves node_x to the root of the tree using the series of rotations.
6568
function splay!(tree::SplayTree, node_x::SplayTreeNode)
@@ -71,7 +74,7 @@ function splay!(tree::SplayTree, node_x::SplayTreeNode)
7174
if node_x == parent.leftChild
7275
# zig rotation
7376
right_rotate!(tree, node_x.parent)
74-
else
77+
else
7578
# zag rotation
7679
left_rotate!(tree, node_x.parent)
7780
end
@@ -104,7 +107,7 @@ function maximum_node(node::Union{SplayTreeNode, Nothing})
104107
return node
105108
end
106109

107-
# Join operations joins two trees S and T
110+
# Join operations joins two trees S and T
108111
# All the items in S are smaller than the items in T.
109112
# This is a two-step process.
110113
# In the first step, splay the largest node in S. This moves the largest node to the root node.
@@ -157,10 +160,10 @@ function Base.delete!(tree::SplayTree{K}, d::K) where K
157160
x = search_node(tree, d)
158161
(x == nothing) && return tree
159162
t = nothing
160-
s = nothing
161-
163+
s = nothing
164+
162165
splay!(tree, x)
163-
166+
164167
if x.rightChild !== nothing
165168
t = x.rightChild
166169
t.parent = nothing
@@ -211,7 +214,7 @@ function Base.push!(tree::SplayTree{K}, d0) where K
211214
return tree
212215
end
213216

214-
function Base.getindex(tree::SplayTree{K}, ind) where K
217+
function Base.getindex(tree::SplayTree{K}, ind) where K
215218
@boundscheck (1 <= ind <= tree.count) || throw(KeyError("$ind should be in between 1 and $(tree.count)"))
216219
function traverse_tree_inorder(node::Union{SplayTreeNode, Nothing})
217220
if (node != nothing)
@@ -222,6 +225,6 @@ function Base.getindex(tree::SplayTree{K}, ind) where K
222225
return K[]
223226
end
224227
end
225-
arr = traverse_tree_inorder(tree.root)
228+
arr = traverse_tree_inorder(tree.root)
226229
return @inbounds arr[ind]
227230
end

test/runtests.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ using Serialization
55

66
import DataStructures: IntSet
77

8-
# These fail due to setproperty! specializations for AVL and RB tree.
9-
# @test [] == detect_ambiguities(Core, DataStructures)
10-
# @test [] == detect_ambiguities(Base, DataStructures)
8+
@test [] == detect_ambiguities(Core, DataStructures)
9+
@test [] == detect_ambiguities(Base, DataStructures)
1110

1211
tests = ["deprecations",
1312
"int_set",

0 commit comments

Comments
 (0)