Skip to content

Commit d1279b3

Browse files
committed
Added to_index and to_indices methods. Fixes #58
1 parent bdc7b81 commit d1279b3

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ComponentArrays"
22
uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
33
authors = ["Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com>"]
4-
version = "0.8.10"
4+
version = "0.8.11"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/set_get.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
getaxes(x) = ()
66

77

8+
# Since we aren't really using the standard approach to indexing, this will forward things to
9+
# the correct methods
10+
Base.to_indices(x::ComponentArray, i::Tuple) = i
11+
Base.to_indices(x::ComponentArray, i::Tuple{Vararg{Union{Integer, CartesianIndex}, N}}) where N = i
12+
Base.to_index(x::ComponentArray, i) = i
13+
814
# Get AbstractAxis index
915
@inline Base.getindex(::AbstractAxis, idx::FlatIdx) = ComponentIndex(idx)
1016
@inline Base.getindex(ax::AbstractAxis, ::Colon) = ComponentIndex(:, ax)
@@ -15,8 +21,7 @@ getaxes(x) = ()
1521
Base.@propagate_inbounds Base.getindex(x::ComponentArray, idx::CartesianIndex) = getdata(x)[idx]
1622
Base.@propagate_inbounds Base.getindex(x::ComponentArray, idx::FlatIdx...) = getdata(x)[idx...]
1723
Base.@propagate_inbounds function Base.getindex(x::ComponentArray, idx::FlatOrColonIdx...)
18-
ci = getindex.(getaxes(x), idx)
19-
axs = map(i -> i.ax, ci)
24+
axs = map((ax, i) -> getindex(ax, i).ax, getaxes(x), idx)
2025
axs = remove_nulls(axs...)
2126
return ComponentArray(getdata(x)[idx...], axs...)
2227
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ end
165165
@test A_mat isa ComponentMatrix
166166
@test getdata(A_vec) isa Vector
167167
@test getdata(A_mat) isa Matrix
168+
169+
@test Base.to_indices(ca, (:a, :b)) == (:a, :b)
170+
@test Base.to_indices(ca, (1, 2)) == (1, 2)
171+
@test Base.to_index(ca, :a) == :a
168172
end
169173

170174
@testset "Set" begin

0 commit comments

Comments
 (0)