-
Notifications
You must be signed in to change notification settings - Fork 194
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
Implement Base.allequal
and Base.allunique
for weight vectors
#894
Conversation
src/weights.jl
Outdated
@@ -313,6 +319,12 @@ length(wv::UnitWeights) = wv.len | |||
size(wv::UnitWeights) = tuple(length(wv)) | |||
Base.axes(wv::UnitWeights) = tuple(Base.OneTo(length(wv))) | |||
|
|||
# https://github.com/JuliaLang/julia/pull/43354 | |||
if VERSION >= v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal at all but it might be nice to combine the two conditionals so that there's only one place that needs editing once we require 1.8 or later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f2421a4.
src/weights.jl
Outdated
if VERSION >= v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d | ||
Base.allequal(wv::AbstractWeights) = allequal(wv.values) | ||
end | ||
Base.allunique(wv::AbstractWeights) = allunique(wv.values) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that allequal
would be a good addition but I'm a bit ambivalent about allunique
. Doesn't seem bad to have but I can't really think of a situation where it'd be useful. Do you have one in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't have a use-case in mind, I think it won't be used a lot. I mainly added it since it complements allequal
(and while the fallback would be sufficient, as for allequal
, one could potentially benefit if an optimied method exists for weights.values
).
In this PR I propose to define
allequal
andallunique
for weight vectors by falling back onallequal(weights.values)
andallunique(weights.values)
(this means optimizations for types ofweights.values
will be exploited instead of falling back to the generic iterative algorithm) and implement the optimizations forUnitWeights
.In a project I wanted to check
allequal(weights)
to avoid an expensive computation for this special case. However, to make this performant forUnitWeights
currently I have to writeweights isa UnitWeights || allequal(weights)
.