@@ -57,15 +57,16 @@ struct VariableOrderAccumulator{Eltype<:Integer,VNType<:VarName} <: AbstractAccu
57
57
" the number of observations"
58
58
num_produce:: Eltype
59
59
" mapping of variable names to their order in the model"
60
- order:: OrderedDict{VNType, Eltype}
60
+ order:: OrderedDict{VNType,Eltype}
61
61
end
62
62
63
63
"""
64
64
VariableOrderAccumulator{T<:Integer}(n=zero(T))
65
65
66
66
Create a new `VariableOrderAccumulator` accumulator with the number of observations set to n
67
67
"""
68
- VariableOrderAccumulator {T} (n= zero (T)) where {T<: Integer } = VariableOrderAccumulator (convert (T, n), OrderedDict {VarName, T} ())
68
+ VariableOrderAccumulator {T} (n= zero (T)) where {T<: Integer } =
69
+ VariableOrderAccumulator (convert (T, n), OrderedDict {VarName,T} ())
69
70
VariableOrderAccumulator (n) = VariableOrderAccumulator {typeof(n)} (n)
70
71
VariableOrderAccumulator () = VariableOrderAccumulator {Int} ()
71
72
@@ -76,7 +77,38 @@ function Base.show(io::IO, acc::LogLikelihoodAccumulator)
76
77
return print (io, " LogLikelihoodAccumulator($(repr (acc. logp)) )" )
77
78
end
78
79
function Base. show (io:: IO , acc:: VariableOrderAccumulator )
79
- return print (io, " VariableOrderAccumulator($(repr (acc. num_produce)) , $(repr (acc. order)) )" )
80
+ return print (
81
+ io, " VariableOrderAccumulator($(repr (acc. num_produce)) , $(repr (acc. order)) )"
82
+ )
83
+ end
84
+
85
+ # Note that == and isequal are different, and equality under the latter should imply
86
+ # equality of hashes. Both of the below implementations are also different from the default
87
+ # implementation for structs.
88
+ Base.:(== )(acc1:: LogPriorAccumulator , acc2:: LogPriorAccumulator ) = acc1. logp == acc2. logp
89
+ function Base.:(== )(acc1:: LogLikelihoodAccumulator , acc2:: LogLikelihoodAccumulator )
90
+ return acc1. logp == acc2. logp
91
+ end
92
+ function Base.:(== )(acc1:: VariableOrderAccumulator , acc2:: VariableOrderAccumulator )
93
+ return acc1. num_produce == acc2. num_produce && acc1. order == acc2. order
94
+ end
95
+
96
+ function Base. isequal (acc1:: LogPriorAccumulator , acc2:: LogPriorAccumulator )
97
+ return isequal (acc1. logp, acc2. logp)
98
+ end
99
+ function Base. isequal (acc1:: LogLikelihoodAccumulator , acc2:: LogLikelihoodAccumulator )
100
+ return isequal (acc1. logp, acc2. logp)
101
+ end
102
+ function Base. isequal (acc1:: VariableOrderAccumulator , acc2:: VariableOrderAccumulator )
103
+ return isequal (acc1. num_produce, acc2. num_produce) && isequal (acc1. order, acc2. order)
104
+ end
105
+
106
+ Base. hash (acc:: LogPriorAccumulator , h:: UInt ) = hash ((LogPriorAccumulator, acc. logp), h)
107
+ function Base. hash (acc:: LogLikelihoodAccumulator , h:: UInt )
108
+ return hash ((LogLikelihoodAccumulator, acc. logp), h)
109
+ end
110
+ function Base. hash (acc:: VariableOrderAccumulator , h:: UInt )
111
+ return hash ((VariableOrderAccumulator, acc. num_produce, acc. order), h)
80
112
end
81
113
82
114
accumulator_name (:: Type{<:LogPriorAccumulator} ) = :LogPrior
96
128
function combine (acc:: VariableOrderAccumulator , acc2:: VariableOrderAccumulator )
97
129
# Note that assumptions are not allowed within in parallelised blocks, and thus the
98
130
# dictionaries should be identical.
99
- return VariableOrderAccumulator (max (acc. num_produce, acc2. num_produce), merge (acc. order, acc2. order))
131
+ return VariableOrderAccumulator (
132
+ max (acc. num_produce, acc2. num_produce), merge (acc. order, acc2. order)
133
+ )
100
134
end
101
135
102
136
function Base.:+ (acc1:: LogPriorAccumulator , acc2:: LogPriorAccumulator )
105
139
function Base.:+ (acc1:: LogLikelihoodAccumulator , acc2:: LogLikelihoodAccumulator )
106
140
return LogLikelihoodAccumulator (acc1. logp + acc2. logp)
107
141
end
108
- increment (acc:: VariableOrderAccumulator ) = VariableOrderAccumulator (acc. num_produce + oneunit (acc. num_produce), acc. order)
142
+ function increment (acc:: VariableOrderAccumulator )
143
+ return VariableOrderAccumulator (acc. num_produce + oneunit (acc. num_produce), acc. order)
144
+ end
109
145
110
146
Base. zero (acc:: LogPriorAccumulator ) = LogPriorAccumulator (zero (acc. logp))
111
147
Base. zero (acc:: LogLikelihoodAccumulator ) = LogLikelihoodAccumulator (zero (acc. logp))
@@ -138,9 +174,9 @@ function Base.convert(
138
174
return LogLikelihoodAccumulator (convert (T, acc. logp))
139
175
end
140
176
function Base. convert (
141
- :: Type{VariableOrderAccumulator{ElType, VnType}} , acc:: VariableOrderAccumulator
142
- ) where {ElType, VnType}
143
- order = OrderedDict {VnType, ElType} ()
177
+ :: Type{VariableOrderAccumulator{ElType,VnType}} , acc:: VariableOrderAccumulator
178
+ ) where {ElType,VnType}
179
+ order = OrderedDict {VnType,ElType} ()
144
180
for (k, v) in acc. order
145
181
order[convert (VnType, k)] = convert (ElType, v)
146
182
end
0 commit comments