Skip to content

Commit 99dfa91

Browse files
committed
refactor: prevent need for custom hash and ==
1 parent 3f30a23 commit 99dfa91

File tree

3 files changed

+5
-26
lines changed

3 files changed

+5
-26
lines changed

src/Expression.jl

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using ..NodeModule: AbstractExpressionNode, Node
66
using ..OperatorEnumModule: AbstractOperatorEnum, OperatorEnum
77
using ..UtilsModule: Undefined
88

9+
import ..NodeModule: copy_node, set_node!, count_nodes, tree_mapreduce, constructorof
910
import ..NodeUtilsModule:
1011
preserve_sharing,
1112
count_constants,
@@ -128,12 +129,6 @@ end
128129
function Base.copy(ex::AbstractExpression; break_sharing::Val=Val(false))
129130
return error("`copy` function must be implemented for $(typeof(ex)) types.")
130131
end
131-
function Base.hash(ex::AbstractExpression, h::UInt)
132-
return error("`hash` function must be implemented for $(typeof(ex)) types.")
133-
end
134-
function Base.:(==)(x::AbstractExpression, y::AbstractExpression)
135-
return error("`==` function must be implemented for $(typeof(x)) types.")
136-
end
137132
function get_constants(ex::AbstractExpression)
138133
return error("`get_constants` function must be implemented for $(typeof(ex)) types.")
139134
end
@@ -199,25 +194,17 @@ end
199194
function Base.copy(ex::Expression; break_sharing::Val=Val(false))
200195
return Expression(copy(ex.tree; break_sharing), copy(ex.metadata))
201196
end
202-
function Base.hash(ex::Expression, h::UInt)
203-
return hash(ex.tree, hash(ex.metadata, h))
197+
function Base.hash(ex::AbstractExpression, h::UInt)
198+
return hash(get_contents(ex), hash(get_metadata(ex), h))
204199
end
205-
206-
"""
207-
Base.:(==)(x::Expression, y::Expression)
208-
209-
Check equality of two expressions `x` and `y` by comparing their trees and metadata.
210-
"""
211-
function Base.:(==)(x::Expression, y::Expression)
212-
return x.tree == y.tree && x.metadata == y.metadata
200+
function Base.:(==)(x::AbstractExpression, y::AbstractExpression)
201+
return get_contents(x) == get_contents(y) && get_metadata(x) == get_metadata(y)
213202
end
214203

215204
# Overload all methods on AbstractExpressionNode that return an aggregation, or can
216205
# return an entire tree. Methods that only return the nodes are *not* overloaded, so
217206
# that the user must use the low-level interface.
218207

219-
import ..NodeModule: copy_node, set_node!, count_nodes, tree_mapreduce, constructorof
220-
221208
#! format: off
222209
@unstable constructorof(::Type{E}) where {E<:AbstractExpression} = Base.typename(E).wrapper
223210
@unstable constructorof(::Type{<:Expression}) = Expression

src/ParametricExpression.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ function Base.copy(ex::ParametricExpression; break_sharing::Val=Val(false))
153153
parameter_names=_copy_with_nothing(ex.metadata.parameter_names),
154154
)
155155
end
156-
function Base.hash(ex::ParametricExpression, h::UInt)
157-
return hash(ex.tree, hash(ex.metadata, h))
158-
end
159-
function Base.:(==)(x::ParametricExpression, y::ParametricExpression)
160-
return x.tree == y.tree && x.metadata == y.metadata
161-
end
162156
###############################################################################
163157

164158
###############################################################################

test/test_multi_expression.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161
)
6262
@test_throws "`get_tree` function must be implemented for" DE.get_tree(multi_ex)
6363
@test_throws "`copy` function must be implemented for" copy(multi_ex)
64-
@test_throws "`hash` function must be implemented for" hash(multi_ex, UInt(0))
65-
@test_throws "`==` function must be implemented for" multi_ex == multi_ex
6664
@test_throws "`get_constants` function must be implemented for" get_constants(
6765
multi_ex
6866
)

0 commit comments

Comments
 (0)