Skip to content

Commit 1587df9

Browse files
authored
Dl/arraylayouts02 (#30)
* Support ArrayLayouts 0.2 * Update Project.toml * Update Project.toml * remove unused _getindex * Fix SubQuasiArray indexing * Update multidimensional.jl * Update multidimensional.jl * remove more unused, test for fill!
1 parent 378e4bb commit 1587df9

9 files changed

+38
-93
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
name = "QuasiArrays"
22
uuid = "c4ea9172-b204-11e9-377d-29865faadc5c"
33
authors = ["Sheehan Olver <[email protected]>"]
4-
version = "0.2"
4+
version = "0.2.0"
55

66
[deps]
77
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
88
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010

1111
[compat]
12-
ArrayLayouts = "0.1.5"
13-
LazyArrays = "0.15"
12+
ArrayLayouts = "0.2.4"
13+
LazyArrays = "0.16.3"
1414
julia = "1.3"
1515

1616
[extras]

src/QuasiArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import LazyArrays: MemoryLayout, UnknownLayout, Mul, ApplyLayout, BroadcastLayou
4040
rowsupport, colsupport, tuple_type_memorylayouts, applylayout, broadcastlayout,
4141
LdivApplyStyle, most, InvLayout, PInvLayout, sub_materialize,
4242
_mul, rowsupport, DiagonalLayout, adjointlayout, transposelayout, conjlayout,
43-
sublayout, call, combine_mul_styles, result_mul_style, LazyArrayStyle, lazy_getindex
43+
sublayout, call, combine_mul_styles, result_mul_style, LazyArrayStyle, layout_getindex
4444

4545
import Base.IteratorsMD
4646

src/abstractquasiarray.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ function unsafe_getindex(A::AbstractQuasiArray, I...)
385385
end
386386

387387
## Internal definitions
388-
_getindex(_, ::IndexStyle, A::AbstractQuasiArray, I) = lazy_getindex(A, I...)
388+
_getindex(_, ::IndexStyle, A::AbstractQuasiArray, I) =
389+
layout_getindex(A, I...)
389390

390391

391392
## IndexCartesian Scalar indexing: Canonical method is full dimensionality of indices
@@ -426,16 +427,12 @@ _to_subscript_indices(A::AbstractQuasiArray{T,N}, I::Vararg{Any,N}) where {T,N}
426427
## Setindex! is defined similarly. We first dispatch to an internal _setindex!
427428
# function that allows dispatch on array storage
428429

430+
setindex!(A::AbstractQuasiArray, v, I...) = _setindex!(indextype(A), A, v, I)
429431

430-
function setindex!(A::AbstractQuasiArray, v, I...)
432+
function _setindex!(::Type{IND}, A::AbstractQuasiArray, v, I) where IND
431433
@_propagate_inbounds_meta
432434
error_if_canonical_setindex(IndexStyle(A), A, I)
433-
_setindex!(indextype(A), IndexStyle(A), A, v, to_indices(A, I))
434-
end
435-
function unsafe_setindex!(A::AbstractQuasiArray, v, I...)
436-
@_inline_meta
437-
@inbounds r = setindex!(A, v, I...)
438-
r
435+
_setindex!(IND, IndexStyle(A), A, v, to_indices(A, I))
439436
end
440437

441438
error_if_canonical_setindex(::IndexCartesian, A::AbstractQuasiArray{T,N}, I::Tuple) where {T,N} =

src/multidimensional.jl

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -423,17 +423,7 @@ to_indices(A::AbstractQuasiArray, I::Tuple{Any}) = (@_inline_meta; to_indices(A,
423423
(to_index(A, I[1]), to_indices(A, indstail, tail(I))...)
424424
end
425425

426-
# Colons get converted to slices by `uncolon`
427-
const CI0 = Union{QuasiCartesianIndex{0}, AbstractArray{QuasiCartesianIndex{0}}}
428-
429-
### From abstractarray.jl: Internal multidimensional indexing definitions ###
430-
getindex(x::Number, i::QuasiCartesianIndex{0}) = x
431-
getindex(t::Tuple, i::QuasiCartesianIndex{1}) = getindex(t, i.I[1])
432-
433-
@inline function _getindex(l::IndexStyle, A::AbstractQuasiArray, I::Union{Any, AbstractArray}...)
434-
@boundscheck checkbounds(A, I...)
435-
return _unsafe_getindex(l, _maybe_reshape(l, A, I...), I...)
436-
end
426+
### From abstractarray.jl: Internal multidimensional indexing definitions ###
437427

438428
@inline index_dimsum(::AbstractQuasiArray{Bool}, I...) = (true, index_dimsum(I...)...)
439429
@inline function index_dimsum(::AbstractQuasiArray{<:Any,N}, I...) where N
@@ -442,35 +432,6 @@ end
442432

443433
Slice(d::AbstractQuasiVector) = Inclusion(d)
444434

445-
446-
_maybe_reshape(::IndexLinear, A::AbstractQuasiArray, I...) = A
447-
_maybe_reshape(::IndexCartesian, A::AbstractQuasiVector, I...) = A
448-
@inline _maybe_reshape(::IndexCartesian, A::AbstractQuasiArray, I...) = __maybe_reshape(A, index_ndims(I...))
449-
@inline __maybe_reshape(A::AbstractQuasiArray{T,N}, ::NTuple{N,Any}) where {T,N} = A
450-
@inline __maybe_reshape(A::AbstractQuasiArray, ::NTuple{N,Any}) where {N} = reshape(A, Val(N))
451-
452-
_unsafe_getindex(::IndexStyle, A::AbstractQuasiArray, I::Vararg{Union{Any, AbstractArray}, N}) where N =
453-
lazy_getindex(A, I...)
454-
455-
# Always index with the exactly indices provided.
456-
@generated function _unsafe_getindex!(dest::Union{AbstractArray,AbstractQuasiArray}, src::AbstractQuasiArray, I::Vararg{Union{Any, AbstractArray}, N}) where N
457-
quote
458-
@_inline_meta
459-
D = eachindex(dest)
460-
Dy = iterate(D)
461-
@inbounds @nloops $N j d->I[d] begin
462-
# This condition is never hit, but at the moment
463-
# the optimizer is not clever enough to split the union without it
464-
Dy === nothing && return dest
465-
(idx, state) = Dy
466-
dest[idx] = @ncall $N getindex src j
467-
Dy = iterate(D, state)
468-
end
469-
return dest
470-
end
471-
end
472-
473-
474435
function fill!(A::AbstractQuasiArray{T}, x) where T
475436
xT = convert(T, x)
476437
for I in eachindex(A)

src/quasiarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ _inclusion(d) = Inclusion(d)
4949
axes(A::QuasiArray) = _inclusion.(A.axes)
5050
parent(A::QuasiArray) = A.parent
5151

52-
@propagate_inbounds @inline function _getindex(::Type{IND}, A::QuasiArray{<:Any,N}, I::IND) where {N,IND}
52+
@propagate_inbounds @inline function _getindex(::Type{IND}, A::QuasiArray, I::IND) where IND
5353
@boundscheck checkbounds(A, I...)
5454
A.parent[findfirst.(isequal.(I), A.axes)...]
5555
end
5656

57-
@propagate_inbounds @inline function setindex!(A::QuasiArray{<:Any,N}, v, I::Vararg{Number,N}) where N
57+
@propagate_inbounds @inline function _setindex!(::Type{IND}, A::QuasiArray, v, I::IND) where IND
5858
@boundscheck checkbounds(A, I...)
5959
@inbounds A.parent[findfirst.(isequal.(I), A.axes)...] = v
6060
A

src/subquasiarray.jl

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -220,50 +220,20 @@ end
220220

221221
# In general, we simply re-index the parent indices by the provided ones
222222
SlowSubQuasiArray{T,N,P,I} = SubQuasiArray{T,N,P,I,false}
223-
function getindex(V::SlowSubQuasiArray{T,N}, I::Vararg{Number,N}) where {T,N}
223+
function _getindex(::Type{IND}, V::SlowSubQuasiArray, I::IND) where IND
224224
@_inline_meta
225225
@boundscheck checkbounds(V, I...)
226226
@inbounds r = V.parent[reindex(V.indices, I)...]
227227
r
228228
end
229229

230-
FastSubQuasiArray{T,N,P,I} = SubQuasiArray{T,N,P,I,true}
231-
function getindex(V::FastSubQuasiArray, i::Number)
232-
@_inline_meta
233-
@boundscheck checkbounds(V, i)
234-
@inbounds r = V.parent[V.offset1 + V.stride1*i]
235-
r
236-
end
237-
# We can avoid a multiplication if the first parent index is a Colon or AbstractUnitRange
238-
FastContiguousSubQuasiArray{T,N,P,I<:Tuple{Union{Slice, AbstractUnitRange}, Vararg{Any}}} = SubQuasiArray{T,N,P,I,true}
239-
function getindex(V::FastContiguousSubQuasiArray, i::Number)
240-
@_inline_meta
241-
@boundscheck checkbounds(V, i)
242-
@inbounds r = V.parent[V.offset1 + i]
243-
r
244-
end
245-
246-
function setindex!(V::SlowSubQuasiArray{T,N}, x, I::Vararg{Number,N}) where {T,N}
230+
function _setindex!(::Type{IND}, V::SlowSubQuasiArray, x, I::IND) where IND
247231
@_inline_meta
248232
@boundscheck checkbounds(V, I...)
249233
@inbounds V.parent[reindex(V.indices, I)...] = x
250234
V
251235
end
252236

253-
function setindex!(V::FastSubQuasiArray, x, i::Number)
254-
@_inline_meta
255-
@boundscheck checkbounds(V, i)
256-
@inbounds V.parent[V.offset1 + V.stride1*i] = x
257-
V
258-
end
259-
function setindex!(V::FastContiguousSubQuasiArray, x, i::Number)
260-
@_inline_meta
261-
@boundscheck checkbounds(V, i)
262-
@inbounds V.parent[V.offset1 + i] = x
263-
V
264-
end
265-
266-
IndexStyle(::Type{<:FastSubQuasiArray}) = IndexLinear()
267237
IndexStyle(::Type{<:SubQuasiArray}) = IndexCartesian()
268238

269239
# Strides are the distance in memory between adjacent elements in a given dimension
@@ -278,11 +248,6 @@ compute_stride1(parent::AbstractQuasiArray, I::NTuple{N,Any}) where {N} =
278248

279249
elsize(::Type{<:SubQuasiArray{<:Any,<:Any,P}}) where {P} = elsize(P)
280250

281-
iscontiguous(A::SubQuasiArray) = iscontiguous(typeof(A))
282-
iscontiguous(::Type{<:SubQuasiArray}) = false
283-
iscontiguous(::Type{<:FastContiguousSubQuasiArray}) = true
284-
285-
first_index(V::FastSubQuasiArray) = V.offset1 + V.stride1 # cached for fast linear SubQuasiArrays
286251
function first_index(V::SubQuasiArray)
287252
P, I = parent(V), V.indices
288253
s1 = compute_stride1(P, I)
@@ -325,8 +290,6 @@ find_extended_inds() = ()
325290
unsafe_convert(::Type{Ptr{T}}, V::SubQuasiArray{T,N,P,<:Tuple{Vararg{RangeIndex}}}) where {T,N,P} =
326291
unsafe_convert(Ptr{T}, V.parent) + (first_index(V)-1)*sizeof(T)
327292

328-
pointer(V::FastSubQuasiArray, i::Int) = pointer(V.parent, V.offset1 + V.stride1*i)
329-
pointer(V::FastContiguousSubQuasiArray, i::Int) = pointer(V.parent, V.offset1 + i)
330293
pointer(V::SubQuasiArray, i::Int) = _pointer(V, i)
331294
_pointer(V::SubQuasiArray{<:Any,1}, i::Int) = pointer(V, (i,))
332295
_pointer(V::SubQuasiArray, i::Int) = pointer(V, Base._ind2sub(axes(V), i))

test/test_abstractquasiarray.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ import QuasiArrays: QuasiCartesianIndex
102102
v = QuasiArray([1, 2, 3],(0:0.5:1,))
103103
@test axes(v) == axes(v[:]) == axes(v[Inclusion(0:0.5:1)]) == (Inclusion(0:0.5:1),)
104104
@test v[0.5] == v[:][0.5] == v[Inclusion(0:0.5:1)][0.5] == 2
105+
106+
A = QuasiArray(rand(3,2), (0:0.5:1,[1,3]))
107+
@test A[1,[1,3]] == A[:,[1,3]][1,:]
105108
end
106109

107110
@testset "Vec indexing" begin

test/test_arrayops.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,9 @@ using QuasiArrays, Test
8888
@test X[Y[end],0.0] == 5
8989
@test X[end,Y[end]] == 11
9090
end
91+
92+
@testset "fill" begin
93+
a = QuasiArray(fill(1.,2,2),(0:0.5:0.5,1:0.5:1.5))
94+
@test fill!(similar(a), 1.0) == a
95+
end
9196
end

test/test_quasisubarray.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ using QuasiArrays, Test
3333
@test axes(sA) === (Base.OneTo(2), Base.OneTo(2), Base.OneTo(2))
3434
end
3535

36+
@testset "FastSubQuasiArray" begin
37+
a = QuasiArray(randn(5), (1:0.5:3,))
38+
v = view(a, Inclusion(1.5:0.5:2))
39+
@test axes(v) == (Inclusion(1.5:0.5:2),)
40+
@test v[1.5] == a[1.5]
41+
@test_throws BoundsError v[1]
42+
v[1.5] = 3
43+
@test a[1.5] === 3.0
44+
45+
a = QuasiArray(randn(2), ([[1,2],[3,4]],))
46+
v = view(a, Inclusion([[1,2],[3,4]]))
47+
@test v[[1,2]] == a[[1,2]]
48+
v[[1,2]] = 3
49+
@test a[[1,2]] == 3.0
50+
end
51+
3652
# @testset "logical indexing #4763" begin
3753
# A = view(QuasiVector(1:5,range(0,2;length=5)), 0.5:0.5:1.5)
3854
# A[Inclusion(0.5:0.5:1.0)]

0 commit comments

Comments
 (0)