Skip to content

Commit ee83c65

Browse files
authored
Define IndexStyle for AbstractWeights (#823)
Since indexing into a weight vector in turn indexes into its vector of values, the optimal index style for a weight vector is the same as that of its values. Currently it uses the default `IndexCartesian` since no more specific `IndexStyle` method is defined. Before this commit: ``` julia> eachindex(1:3, weights(1:3)) 3-element CartesianIndices{1, Tuple{Base.OneTo{Int64}}}: CartesianIndex(1,) CartesianIndex(2,) CartesianIndex(3,) ``` After: ``` julia> eachindex(1:3, weights(1:3)) Base.OneTo(3) ```
1 parent c19bff4 commit ee83c65

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

src/weights.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ isempty(wv::AbstractWeights) = isempty(wv.values)
2323
size(wv::AbstractWeights) = size(wv.values)
2424
Base.axes(wv::AbstractWeights) = Base.axes(wv.values)
2525

26+
Base.IndexStyle(::Type{<:AbstractWeights{S,T,V}}) where {S,T,V} = IndexStyle(V)
27+
2628
Base.dataids(wv::AbstractWeights) = Base.dataids(wv.values)
2729

2830
Base.convert(::Type{Vector}, wv::AbstractWeights) = convert(Vector, wv.values)

test/weights.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ weight_funcs = (weights, aweights, fweights, pweights)
1414
@test isempty(f(Float64[]))
1515
@test size(f([1, 2, 3])) == (3,)
1616
@test axes(f([1, 2, 3])) == (Base.OneTo(3),)
17+
@test IndexStyle(f([1, 2, 3])) == IndexLinear()
1718

1819
w = [1., 2., 3.]
1920
wv = f(w)

0 commit comments

Comments
 (0)