Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hash field to all BasicSymbolic subtypes #590

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,32 @@ sdict(kv...) = Dict{Any, Any}(kv...)

using Base: RefValue
const EMPTY_ARGS = []
const EMPTY_HASH = RefValue(UInt(0))
const EMPTY_HASH = UInt(0) # zero indicating that hash has not been computed
const NOT_SORTED = RefValue(false)
const EMPTY_DICT = sdict()
const EMPTY_DICT_T = typeof(EMPTY_DICT)

@compactify show_methods=false begin
@abstract struct BasicSymbolic{T} <: Symbolic{T}
metadata::Metadata = NO_METADATA
hash::RefValue{UInt} = Ref(EMPTY_HASH) # create a RefValue object whenever creating a new BasicSymbolic
end
struct Sym{T} <: BasicSymbolic{T}
name::Symbol = :OOF
end
struct Term{T} <: BasicSymbolic{T}
f::Any = identity # base/num if Pow; issorted if Add/Dict
arguments::Vector{Any} = EMPTY_ARGS
hash::RefValue{UInt} = EMPTY_HASH
end
struct Mul{T} <: BasicSymbolic{T}
coeff::Any = 0 # exp/den if Pow
dict::EMPTY_DICT_T = EMPTY_DICT
hash::RefValue{UInt} = EMPTY_HASH
arguments::Vector{Any} = EMPTY_ARGS
issorted::RefValue{Bool} = NOT_SORTED
end
struct Add{T} <: BasicSymbolic{T}
coeff::Any = 0 # exp/den if Pow
dict::EMPTY_DICT_T = EMPTY_DICT
hash::RefValue{UInt} = EMPTY_HASH
arguments::Vector{Any} = EMPTY_ARGS
issorted::RefValue{Bool} = NOT_SORTED
end
Expand Down Expand Up @@ -256,25 +254,42 @@ const SUB_SALT = 0xaaaaaaaaaaaaaaaa % UInt
const DIV_SALT = 0x334b218e73bbba53 % UInt
const POW_SALT = 0x2b55b97a6efb080c % UInt
function Base.hash(s::BasicSymbolic, salt::UInt)::UInt
zero_salt = iszero(salt)
h = s.hash[]
# for any type, if hash has been computed and stored, then just return it
if zero_salt && h != EMPTY_HASH
return h
end
E = exprtype(s)
if E === SYM
if zero_salt
h_new = hash(nameof(s), SYM_SALT)
s.hash[] = h_new
return h_new
end
hash(nameof(s), salt ⊻ SYM_SALT)
elseif E === ADD || E === MUL
!iszero(salt) && return hash(hash(s, zero(UInt)), salt)
h = s.hash[]
!iszero(h) && return h
!zero_salt && return hash(hash(s, zero(UInt)), salt)
hashoffset = isadd(s) ? ADD_SALT : SUB_SALT
h′ = hash(hashoffset, hash(s.coeff, hash(s.dict, salt)))
s.hash[] = h′
return h′
elseif E === DIV
if zero_salt
h_new = hash(s.num, hash(s.den, DIV_SALT))
s.hash[] = h_new
return h_new
end
return hash(s.num, hash(s.den, salt ⊻ DIV_SALT))
elseif E === POW
if zero_salt
h_new = hash(s.exp, hash(s.base, POW_SALT))
s.hash[] = h_new
return h_new
end
hash(s.exp, hash(s.base, salt ⊻ POW_SALT))
elseif E === TERM
!iszero(salt) && return hash(hash(s, zero(UInt)), salt)
h = s.hash[]
!iszero(h) && return h
!zero_salt && return hash(hash(s, zero(UInt)), salt)
op = operation(s)
oph = op isa Function ? nameof(op) : op
h′ = hashvec(arguments(s), hash(oph, salt))
Expand All @@ -298,7 +313,7 @@ function Term{T}(f, args; kw...) where T
args = convert(Vector{Any}, args)
end

Term{T}(;f=f, arguments=args, hash=Ref(UInt(0)), kw...)
Term{T}(;f=f, arguments=args, kw...)
end

function Term(f, args; metadata=NO_METADATA)
Expand All @@ -318,7 +333,7 @@ function Add(::Type{T}, coeff, dict; metadata=NO_METADATA, kw...) where T
end
end

Add{T}(; coeff, dict, hash=Ref(UInt(0)), metadata, arguments=[], issorted=RefValue(false), kw...)
Add{T}(; coeff, dict, metadata, arguments=[], issorted=RefValue(false), kw...)
end

function Mul(T, a, b; metadata=NO_METADATA, kw...)
Expand All @@ -333,7 +348,7 @@ function Mul(T, a, b; metadata=NO_METADATA, kw...)
else
coeff = a
dict = b
Mul{T}(; coeff, dict, hash=Ref(UInt(0)), metadata, arguments=[], issorted=RefValue(false), kw...)
Mul{T}(; coeff, dict, metadata, arguments=[], issorted=RefValue(false), kw...)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/rulesets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ end
_f(x) = x === a
@testset "where" begin

@syms a b
@syms b
r = @rule ~x => ~x where {_f(~x)}
@eqtest r(a) == a
@test isnothing(r(b))
Expand Down
Loading