Skip to content

Commit

Permalink
Merge pull request #41063 from JuliaLang/jb/statstructmethods
Browse files Browse the repository at this point in the history
more efficient `==` and `hash` for `StatStruct`
  • Loading branch information
JeffBezanson committed Jun 3, 2021
2 parents e2d647e + 81dcc81 commit 8535b8c
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ struct StatStruct
mtime :: Float64
ctime :: Float64
end
const StatFieldTypes = Union{UInt,Int,Int64,Float64}

function Base.:(==)(x::StatStruct, y::StatStruct) # do not include `desc` in equality or hash
for i = 2:nfields(x)
xi = getfield(x, i)::StatFieldTypes
xi === getfield(y, i) || return false
end
return true
@eval function Base.:(==)(x::StatStruct, y::StatStruct) # do not include `desc` in equality or hash
$(let ex = true
for fld in fieldnames(StatStruct)[2:end]
ex = :(getfield(x, $(QuoteNode(fld))) === getfield(y, $(QuoteNode(fld))) && $ex)
end
Expr(:return, ex)
end)
end
function Base.hash(obj::StatStruct, h::UInt)
for i = 2:nfields(obj)
h = hash(getfield(obj, i)::StatFieldTypes, h)
end
return h
@eval function Base.hash(obj::StatStruct, h::UInt)
$(quote
$(Any[:(h = hash(getfield(obj, $(QuoteNode(fld))), h)) for fld in fieldnames(StatStruct)[2:end]]...)
return h
end)
end

StatStruct() = StatStruct("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Expand Down

0 comments on commit 8535b8c

Please sign in to comment.