Skip to content

Commit

Permalink
Add hash field to all BasicSymbolic subtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenszhu committed May 1, 2024
1 parent e9a96bd commit 4aacc38
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 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 @@ -298,7 +296,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 +316,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 +331,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

0 comments on commit 4aacc38

Please sign in to comment.