Skip to content
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

Merged
merged 2 commits into from
Sep 25, 2023

Conversation

devmotion
Copy link
Member

In this PR I propose to define allequal and allunique for weight vectors by falling back on allequal(weights.values) and allunique(weights.values) (this means optimizations for types of weights.values will be exploited instead of falling back to the generic iterative algorithm) and implement the optimizations for UnitWeights.

In a project I wanted to check allequal(weights) to avoid an expensive computation for this special case. However, to make this performant for UnitWeights currently I have to write weights isa UnitWeights || allequal(weights).

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
Copy link
Member

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.

Copy link
Member Author

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)
Copy link
Member

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?

Copy link
Member Author

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).

@devmotion devmotion merged commit e794383 into master Sep 25, 2023
@devmotion devmotion deleted the dw/allequal_allunique branch September 25, 2023 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants