Skip to content

Commit

Permalink
Get rid of union! (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle authored May 3, 2024
1 parent 83cb0b0 commit ae7199d
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/tracers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const SET_TYPE_MESSAGE = """
The provided index set type `S` has to satisfy the following conditions:
- it is an iterable with `<:Integer` element type
- it implements methods `union`, `union!` and `push!`
- it implements `union`
Subtypes of `AbstractSet{<:Integer}` are a natural choice, like `BitSet` or `Set{UInt64}`.
"""
Expand Down Expand Up @@ -152,9 +152,8 @@ HessianTracer(t::HessianTracer) = t
# Turn first-order interactions into second-order interactions
function promote_order(t::HessianTracer)
d = deepcopy(t.inputs)
ks = keys(d)
for v in values(d)
union!(v, ks)
for (k, v) in pairs(d)
d[k] = reduce(union, keys(d); init=v)
end
return HessianTracer(d)
end
Expand All @@ -168,15 +167,14 @@ end
function distributive_merge(a::HessianTracer, b::HessianTracer)
da = deepcopy(a.inputs)
db = deepcopy(b.inputs)
for ka in keys(da)
for kb in keys(db)
# add second-order interaction term
union!(da[ka], kb)
union!(db[kb], ka)
end
# add second-order interaction term
for (ka, va) in pairs(da)
da[ka] = reduce(union, keys(db); init=va)
end
merge!(da, db)
return HessianTracer(da)
for (kb, vb) in pairs(db)
db[kb] = reduce(union, keys(da); init=vb)
end
return HessianTracer(merge(da, db))
end

#===========#
Expand Down

0 comments on commit ae7199d

Please sign in to comment.