Skip to content

Commit 15a051a

Browse files
committed
Changed symbolic indexing to slice. Closes #90
1 parent 5856123 commit 15a051a

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
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.9.11"
4+
version = "0.10.0"
55

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

src/array_interface.jl

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@ Base.to_indices(x::ComponentArray, i::Tuple{Vararg{Union{Integer, CartesianIndex
8484
Base.to_indices(x::ComponentArray, i::Tuple{Vararg{Int64}}) where N = i
8585
Base.to_index(x::ComponentArray, i) = i
8686

87-
# Get AbstractAxis index
88-
@inline Base.getindex(::AbstractAxis, idx::FlatIdx) = ComponentIndex(idx)
89-
@inline Base.getindex(ax::AbstractAxis, ::Colon) = ComponentIndex(:, ax)
90-
@inline Base.getindex(::AbstractAxis{IdxMap}, s::Symbol) where IdxMap =
91-
ComponentIndex(getproperty(IdxMap, s))
92-
9387
# Get ComponentArray index
9488
Base.@propagate_inbounds Base.getindex(x::ComponentArray, idx::CartesianIndex) = getdata(x)[idx]
9589
Base.@propagate_inbounds Base.getindex(x::ComponentArray, idx::FlatIdx...) = getdata(x)[idx...]
@@ -101,7 +95,7 @@ end
10195
Base.@propagate_inbounds Base.getindex(x::ComponentArray, ::Colon) = getdata(x)[:]
10296
Base.@propagate_inbounds Base.getindex(x::ComponentArray, ::Colon...) = x
10397
@inline Base.getindex(x::ComponentArray, idx...) = getindex(x, toval.(idx)...)
104-
@inline Base.getindex(x::ComponentArray, idx::Val...) = _getindex(x, idx...)
98+
@inline Base.getindex(x::ComponentArray, idx::Val...) = _getindex(getindex, x, idx...)
10599

106100
# Set ComponentArray index
107101
Base.@propagate_inbounds Base.setindex!(x::ComponentArray, v, idx::FlatOrColonIdx...) = setindex!(getdata(x), v, idx...)
@@ -111,22 +105,23 @@ Base.@propagate_inbounds Base.setindex!(x::ComponentArray, v, ::Colon) = setinde
111105

112106
# Explicitly view
113107
Base.@propagate_inbounds Base.view(x::ComponentArray, idx::ComponentArrays.FlatIdx...) = view(getdata(x), idx...)
114-
Base.@propagate_inbounds Base.view(x::ComponentArray, idx...) = _getindex(x, toval.(idx)...)
108+
Base.@propagate_inbounds Base.view(x::ComponentArray, idx...) = _getindex(view, x, toval.(idx)...)
109+
110+
Base.@propagate_inbounds Base.maybeview(x::ComponentArray, idx::ComponentArrays.FlatIdx...) = Base.maybeview(getdata(x), idx...)
111+
Base.@propagate_inbounds Base.maybeview(x::ComponentArray, idx...) = _getindex(Base.maybeview, x, toval.(idx)...)
115112

116113
# Generated get and set index methods to do all of the heavy lifting in the type domain
117-
@generated function _getindex(x::ComponentArray, idx...)
114+
@generated function _getindex(index_fun, x::ComponentArray, idx...)
118115
ci = getindex.(getaxes(x), getval.(idx))
119116
inds = map(i -> i.idx, ci)
120117
axs = map(i -> i.ax, ci)
121118
axs = remove_nulls(axs...)
122-
# the index must be valid after computing `ci`
123-
:(Base.@_inline_meta; ComponentArray(Base.maybeview(getdata(x), $inds...), $axs...))
119+
return :(Base.@_inline_meta; ComponentArray(index_fun(getdata(x), $inds...), $axs...))
124120
end
125121

126122
@generated function _setindex!(x::ComponentArray, v, idx...)
127123
ci = getindex.(getaxes(x), getval.(idx))
128124
inds = map(i -> i.idx, ci)
129-
# the index must be valid after computing `ci`
130125
return :(Base.@_inline_meta; setindex!(getdata(x), v, $inds...))
131126
end
132127

src/axis.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,9 @@ reindex(i, offset) = i .+ offset
148148
reindex(ax::FlatAxis, _) = ax
149149
reindex(ax::Axis, offset) = Axis(map(x->reindex(x, offset), indexmap(ax)))
150150
reindex(ax::ViewAxis, offset) = ViewAxis(viewindex(ax) .+ offset, indexmap(ax))
151+
152+
# Get AbstractAxis index
153+
@inline Base.getindex(::AbstractAxis, idx::FlatIdx) = ComponentIndex(idx)
154+
@inline Base.getindex(ax::AbstractAxis, ::Colon) = ComponentIndex(:, ax)
155+
@inline Base.getindex(::AbstractAxis{IdxMap}, s::Symbol) where IdxMap =
156+
ComponentIndex(getproperty(IdxMap, s))

src/componentarray.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,10 @@ getaxes(x) = ()
289289

290290
"""
291291
valkeys(x::ComponentVector)
292+
valkeys(x::AbstractAxis)
292293
293-
Returns `Val`-wrapped keys of `ComponentVector` for fast iteration over component keys.
294+
Returns `Val`-wrapped keys of `ComponentVector` for fast iteration over component keys. Also works
295+
directly on an `AbstractAxis`.
294296
295297
# Examples
296298
@@ -310,8 +312,9 @@ julia> sum(prod(ca[k]) for k in valkeys(ca))
310312
11
311313
```
312314
"""
313-
@generated function valkeys(ca::ComponentVector)
314-
idxmap = ComponentArrays.indexmap(getaxes(ca)[1])
315+
@generated function valkeys(ax::AbstractAxis)
316+
idxmap = indexmap(ax)
315317
k = Val.(keys(idxmap))
316318
return :($k)
317-
end
319+
end
320+
valkeys(ca::ComponentVector) = valkeys(getaxes(ca)[1])

src/namedtuple_interface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Base.haskey(x::ComponentVector, s::Symbol) = haskey(indexmap(getaxes(x)[1]), s)
1111
Base.propertynames(x::ComponentVector) = propertynames(indexmap(getaxes(x)[1]))
1212

1313
# Property access for ComponentVectors goes through _get/_setindex
14-
@inline Base.getproperty(x::ComponentVector, s::Symbol) = _getindex(x, Val(s))
15-
@inline Base.getproperty(x::ComponentVector, s::Val) = _getindex(x, s)
14+
@inline Base.getproperty(x::ComponentVector, s::Symbol) = _getindex(Base.maybeview, x, Val(s))
15+
@inline Base.getproperty(x::ComponentVector, s::Val) = _getindex(Base.maybeview, x, s)
1616

1717
@inline Base.setproperty!(x::ComponentVector, s::Symbol, v) = _setindex!(x, v, Val(s))
1818
@inline Base.setproperty!(x::ComponentVector, s::Val, v) = _setindex!(x, v, s)

test/runtests.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ end
175175
@test size(caa.b) == size(sq_mat)
176176
@test caa.b[1:2, 3] == sq_mat[1:2, 3]
177177

178-
@test view(ca, :a) == ca.a
179-
@test cmat[:c, :a] == view(cmat, :c, :a)
178+
@test Base.maybeview(ca, :a) == ca.a
179+
@test cmat[:c, :a] == getindex(cmat, :c, :a)
180+
@test @view(cmat[:c, :a]) == view(cmat, :c, :a)
180181

181182
@test ca[CartesianIndex(1)] == ca[1]
182183
@test cmat[CartesianIndex(1, 2)] == cmat[1, 2]
@@ -230,8 +231,8 @@ end
230231

231232
temp.c.a .= 1000
232233

233-
tempmat[:b,:b][1,1][:a,:a][:a,:a] = 100000
234-
tempmat[:b,:a][2].b = 1000
234+
view(view(tempmat,:b,:b)[1,1],:a,:a)[:a,:a] = 100000
235+
@view(tempmat[:b,:a])[2].b = 1000
235236

236237
@test temp.c.a.a == 1000
237238

0 commit comments

Comments
 (0)