Skip to content

Commit 6d18933

Browse files
authored
Merge pull request #100 from Tokazama/clean-format
Clean up formatting
2 parents 57b03b3 + 81b2e6b commit 6d18933

File tree

8 files changed

+1077
-782
lines changed

8 files changed

+1077
-782
lines changed

src/ArrayInterface.jl

Lines changed: 568 additions & 476 deletions
Large diffs are not rendered by default.

src/cuarrays.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
fast_scalar_indexing(::Type{<:CuArrays.CuArray}) = false
2-
@inline allowed_getindex(x::CuArrays.CuArray,i...) = CuArrays.@allowscalar(x[i...])
3-
@inline allowed_setindex!(x::CuArrays.CuArray,v,i...) = (CuArrays.@allowscalar(x[i...] = v))
2+
@inline allowed_getindex(x::CuArrays.CuArray, i...) = CuArrays.@allowscalar(x[i...])
3+
@inline function allowed_setindex!(x::CuArrays.CuArray, v, i...)
4+
return (CuArrays.@allowscalar(x[i...] = v))
5+
end
46

5-
function Base.setindex(x::CuArrays.CuArray,v,i::Int)
6-
_x = copy(x)
7-
allowed_setindex!(_x,v,i)
8-
_x
7+
function Base.setindex(x::CuArrays.CuArray, v, i::Int)
8+
_x = copy(x)
9+
allowed_setindex!(_x, v, i)
10+
return _x
911
end
1012

11-
function restructure(x::CuArrays.CuArray,y)
12-
reshape(Adapt.adapt(parameterless_type(x),y), Base.size(x)...)
13+
function restructure(x::CuArrays.CuArray, y)
14+
return reshape(Adapt.adapt(parameterless_type(x), y), Base.size(x)...)
1315
end
1416

1517
Device(::Type{<:CuArrays.CuArray}) = GPU()

src/cuarrays2.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
fast_scalar_indexing(::Type{<:CUDA.CuArray}) = false
2-
@inline allowed_getindex(x::CUDA.CuArray,i...) = CUDA.@allowscalar(x[i...])
3-
@inline allowed_setindex!(x::CUDA.CuArray,v,i...) = (CUDA.@allowscalar(x[i...] = v))
2+
@inline allowed_getindex(x::CUDA.CuArray, i...) = CUDA.@allowscalar(x[i...])
3+
@inline allowed_setindex!(x::CUDA.CuArray, v, i...) = (CUDA.@allowscalar(x[i...] = v))
44

5-
function Base.setindex(x::CUDA.CuArray,v,i::Int)
6-
_x = copy(x)
7-
allowed_setindex!(_x,v,i)
8-
_x
5+
function Base.setindex(x::CUDA.CuArray, v, i::Int)
6+
_x = copy(x)
7+
allowed_setindex!(_x, v, i)
8+
return _x
99
end
1010

11-
function restructure(x::CUDA.CuArray,y)
12-
reshape(Adapt.adapt(parameterless_type(x),y), Base.size(x)...)
11+
function restructure(x::CUDA.CuArray, y)
12+
return reshape(Adapt.adapt(parameterless_type(x), y), Base.size(x)...)
1313
end
1414

1515
Device(::Type{<:CUDA.CuArray}) = GPU()

src/dimensions.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ This helps it get optimised out of existance. It is less of an abuse of `@pure`
9797
most of the stuff for making `NamedTuples` work.
9898
"""
9999
Base.@pure function tuple_issubset(
100-
lhs::Tuple{Vararg{Symbol,N}}, rhs::Tuple{Vararg{Symbol,M}},
100+
lhs::Tuple{Vararg{Symbol,N}}, rhs::Tuple{Vararg{Symbol,M}}
101101
) where {N,M}
102102
N <= M || return false
103103
for a in lhs
@@ -155,8 +155,12 @@ julia> ArrayInterface.size(A)
155155
@inline size(A) = Base.size(A)
156156
@inline size(A, d::Integer) = size(A)[Int(d)]
157157
@inline size(A, d) = Base.size(A, to_dims(A, d))
158-
@inline size(x::LinearAlgebra.Adjoint{T,V}) where {T, V <: AbstractVector{T}} = (One(), static_length(x))
159-
@inline size(x::LinearAlgebra.Transpose{T,V}) where {T, V <: AbstractVector{T}} = (One(), static_length(x))
158+
@inline function size(x::LinearAlgebra.Adjoint{T,V}) where {T,V<:AbstractVector{T}}
159+
return (One(), static_length(x))
160+
end
161+
@inline function size(x::LinearAlgebra.Transpose{T,V}) where {T,V<:AbstractVector{T}}
162+
return (One(), static_length(x))
163+
end
160164

161165
"""
162166
axes(A, d)

src/indexing.jl

Lines changed: 85 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ argdims(::ArrayStyle, ::Type{T}) where {T<:AbstractArray} = ndims(T)
2828
argdims(::ArrayStyle, ::Type{T}) where {N,T<:CartesianIndex{N}} = N
2929
argdims(::ArrayStyle, ::Type{T}) where {N,T<:AbstractArray{CartesianIndex{N}}} = N
3030
argdims(::ArrayStyle, ::Type{T}) where {N,T<:AbstractArray{<:Any,N}} = N
31-
argdims(::ArrayStyle, ::Type{T}) where {N,T<:LogicalIndex{<:Any,<:AbstractArray{Bool,N}}} = N
31+
argdims(::ArrayStyle, ::Type{T}) where {N,T<:LogicalIndex{<:Any,<:AbstractArray{Bool,N}}} =
32+
N
3233
@generated function argdims(s::ArrayStyle, ::Type{T}) where {N,T<:Tuple{Vararg{Any,N}}}
3334
e = Expr(:tuple)
3435
for p in T.parameters
@@ -116,20 +117,35 @@ flatten_new_indexer(A::Array2, args) = ...
116117
@inline function flatten_args(A, args::Tuple{Arg,Vararg{Any}}) where {Arg}
117118
return (first(args), flatten_args(A, tail(args))...)
118119
end
119-
@inline function flatten_args(A, args::Tuple{Arg,Vararg{Any}}) where {N,Arg<:CartesianIndex{N}}
120+
@inline function flatten_args(
121+
A,
122+
args::Tuple{Arg,Vararg{Any}},
123+
) where {N,Arg<:CartesianIndex{N}}
120124
return (first(args).I..., flatten_args(A, tail(args))...)
121125
end
122-
@inline function flatten_args(A, args::Tuple{Arg,Vararg{Any}}) where {N,Arg<:LinearIndices{N}}
126+
@inline function flatten_args(
127+
A,
128+
args::Tuple{Arg,Vararg{Any}},
129+
) where {N,Arg<:LinearIndices{N}}
123130
return (eachindex(first(args)), flatten_args(A, tail(args))...)
124131
end
125-
@inline function flatten_args(A, args::Tuple{Arg,Vararg{Any}}) where {N,Arg<:CartesianIndices{N}}
132+
@inline function flatten_args(
133+
A,
134+
args::Tuple{Arg,Vararg{Any}},
135+
) where {N,Arg<:CartesianIndices{N}}
126136
return (first(args).indices..., flatten_args(A, tail(args))...)
127137
end
128138
# we preserve CartesianIndices{0} for dropping dimensions
129-
@inline function flatten_args(A, args::Tuple{Arg,Vararg{Any}}) where {Arg<:CartesianIndices{0}}
139+
@inline function flatten_args(
140+
A,
141+
args::Tuple{Arg,Vararg{Any}},
142+
) where {Arg<:CartesianIndices{0}}
130143
return (first(args), flatten_args(A, tail(args))...)
131144
end
132-
@inline function flatten_args(A, args::Tuple{Arg,Vararg{Any}}) where {N,Arg<:AbstractArray{Bool,N}}
145+
@inline function flatten_args(
146+
A,
147+
args::Tuple{Arg,Vararg{Any}},
148+
) where {N,Arg<:AbstractArray{Bool,N}}
133149
if length(args) === 1
134150
if IndexStyle(A) isa IndexLinear
135151
return (LogicalIndex{Int}(first(args)),)
@@ -167,7 +183,7 @@ ArrayInterface.can_flatten(::Type{A}, ::Type{T}) where {A<:ForbiddenArray,T<:New
167183
can_flatten(A, x) = can_flatten(typeof(A), typeof(x))
168184
can_flatten(::Type{A}, ::Type{T}) where {A,T} = false
169185
can_flatten(::Type{A}, ::Type{T}) where {A,I<:CartesianIndex,T<:AbstractArray{I}} = false
170-
can_flatten(::Type{A}, ::Type{T}) where {A,T<: CartesianIndices} = true
186+
can_flatten(::Type{A}, ::Type{T}) where {A,T<:CartesianIndices} = true
171187
can_flatten(::Type{A}, ::Type{T}) where {A,N,T<:AbstractArray{Bool,N}} = N > 1
172188
can_flatten(::Type{A}, ::Type{T}) where {A,N,T<:CartesianIndex{N}} = true
173189
@generated function can_flatten(::Type{A}, ::Type{T}) where {A,T<:Tuple}
@@ -196,11 +212,18 @@ be accomplished using `to_index(axis, arg)`.
196212
end
197213
end
198214
@propagate_inbounds to_indices(A, args::Tuple{}) = to_indices(A, axes(A), ())
199-
@propagate_inbounds function to_indices(A, axs::Tuple, args::Tuple{Arg,Vararg{Any}}) where {Arg}
215+
@propagate_inbounds function to_indices(
216+
A,
217+
axs::Tuple,
218+
args::Tuple{Arg,Vararg{Any}},
219+
) where {Arg}
200220
N = argdims(A, Arg)
201221
if N > 1
202222
axes_front, axes_tail = Base.IteratorsMD.split(axs, Val(N))
203-
return (to_multi_index(axes_front, first(args)), to_indices(A, axes_tail, tail(args))...)
223+
return (
224+
to_multi_index(axes_front, first(args)),
225+
to_indices(A, axes_tail, tail(args))...,
226+
)
204227
else
205228
return (to_index(first(axs), first(args)), to_indices(A, tail(axs), tail(args))...)
206229
end
@@ -211,7 +234,11 @@ end
211234
end
212235
return to_indices(A, tail(axs), args)
213236
end
214-
@propagate_inbounds function to_indices(A, ::Tuple{}, args::Tuple{Arg,Vararg{Any}}) where {Arg}
237+
@propagate_inbounds function to_indices(
238+
A,
239+
::Tuple{},
240+
args::Tuple{Arg,Vararg{Any}},
241+
) where {Arg}
215242
return (to_index(OneTo(1), first(args)), to_indices(A, (), tail(args))...)
216243
end
217244
to_indices(A, axs::Tuple{}, args::Tuple{}) = ()
@@ -265,13 +292,21 @@ end
265292
@boundscheck checkbounds(axis, arg)
266293
return @inbounds(axis[arg])
267294
end
268-
@propagate_inbounds function to_index(::IndexStyle, axis, arg::AbstractArray{I}) where {I<:Integer}
295+
@propagate_inbounds function to_index(
296+
::IndexStyle,
297+
axis,
298+
arg::AbstractArray{I},
299+
) where {I<:Integer}
269300
@boundscheck if !checkindex(Bool, axis, arg)
270301
throw(BoundsError(axis, arg))
271302
end
272303
return arg
273304
end
274-
@propagate_inbounds function to_index(::IndexStyle, axis, arg::AbstractRange{I}) where {I<:Integer}
305+
@propagate_inbounds function to_index(
306+
::IndexStyle,
307+
axis,
308+
arg::AbstractRange{I},
309+
) where {I<:Integer}
275310
@boundscheck if !checkindex(Bool, axis, arg)
276311
throw(BoundsError(axis, arg))
277312
end
@@ -291,7 +326,8 @@ function unsafe_reconstruct(A::OneTo, data; kwargs...)
291326
if can_change_size(A)
292327
return typeof(A)(data)
293328
else
294-
if data isa Slice || !(known_length(A) === nothing || known_length(A) !== known_length(data))
329+
if data isa Slice ||
330+
!(known_length(A) === nothing || known_length(A) !== known_length(data))
295331
return A
296332
else
297333
return OneTo(data)
@@ -302,7 +338,8 @@ function unsafe_reconstruct(A::UnitRange, data; kwargs...)
302338
if can_change_size(A)
303339
return typeof(A)(data)
304340
else
305-
if data isa Slice || !(known_length(A) === nothing || known_length(A) !== known_length(data))
341+
if data isa Slice ||
342+
!(known_length(A) === nothing || known_length(A) !== known_length(data))
306343
return A
307344
else
308345
return UnitRange(data)
@@ -313,7 +350,8 @@ function unsafe_reconstruct(A::OptionallyStaticUnitRange, data; kwargs...)
313350
if can_change_size(A)
314351
return typeof(A)(data)
315352
else
316-
if data isa Slice || !(known_length(A) === nothing || known_length(A) !== known_length(data))
353+
if data isa Slice ||
354+
!(known_length(A) === nothing || known_length(A) !== known_length(data))
317355
return A
318356
else
319357
return OptionallyStaticUnitRange(data)
@@ -344,7 +382,11 @@ pair of axes and indices calling [`to_axis`](@ref).
344382
end
345383
to_axes(A, ::Tuple{Ax,Vararg{Any}}, ::Tuple{}) where {Ax} = ()
346384
to_axes(A, ::Tuple{}, ::Tuple{}) = ()
347-
@propagate_inbounds function to_axes(A, axs::Tuple{Ax,Vararg{Any}}, inds::Tuple{I,Vararg{Any}}) where {Ax,I}
385+
@propagate_inbounds function to_axes(
386+
A,
387+
axs::Tuple{Ax,Vararg{Any}},
388+
inds::Tuple{I,Vararg{Any}},
389+
) where {Ax,I}
348390
N = argdims(A, I)
349391
if N === 0
350392
# drop this dimension
@@ -355,7 +397,10 @@ to_axes(A, ::Tuple{}, ::Tuple{}) = ()
355397
# Only multidimensional AbstractArray{Bool} and AbstractVector{CartesianIndex{N}}
356398
# make it to this point. They collapse several dimensions into one.
357399
axes_front, axes_tail = Base.IteratorsMD.split(axs, Val(N))
358-
return (to_multi_axis(IndexStyle(A), axes_front, first(inds)), to_axes(A, axes_tail, tail(inds))...)
400+
return (
401+
to_multi_axis(IndexStyle(A), axes_front, first(inds)),
402+
to_axes(A, axes_tail, tail(inds))...,
403+
)
359404
end
360405
end
361406

@@ -368,7 +413,8 @@ previously executed `to_index(old_axis, arg) -> index`. `to_axis` assumes that
368413
`new_axis` begins at one and extends the length of `index` (i.e., one-based indexing).
369414
"""
370415
@inline function to_axis(axis, inds)
371-
if !can_change_size(axis) && (known_length(inds) !== nothing && known_length(axis) === known_length(inds))
416+
if !can_change_size(axis) &&
417+
(known_length(inds) !== nothing && known_length(axis) === known_length(inds))
372418
return axis
373419
else
374420
return to_axis(IndexStyle(axis), axis, inds)
@@ -497,8 +543,7 @@ Store the given values at the given key or index within a collection.
497543
if can_setindex(A)
498544
return unsafe_setindex!(A, val, to_indices(A, args))
499545
else
500-
error("Instance of type $(typeof(A)) are not mutable and cannot change " *
501-
"elements after construction.")
546+
error("Instance of type $(typeof(A)) are not mutable and cannot change elements after construction.")
502547
end
503548
end
504549
@propagate_inbounds function setindex!(A, val; kwargs...)
@@ -533,7 +578,7 @@ have been checked for being in bounds. Any new array type using `ArrayInterface.
533578
must define `unsafe_set_element!(::NewArrayType, val, inds)`.
534579
"""
535580
function unsafe_set_element!(A, val, inds; kwargs...)
536-
throw(MethodError(unsafe_set_element!, (A, val, inds)))
581+
return throw(MethodError(unsafe_set_element!, (A, val, inds)))
537582
end
538583
function unsafe_set_element!(A::Array{T}, val, inds::Tuple) where {T}
539584
if length(inds) === 0
@@ -555,7 +600,6 @@ Sets `inds` of `A` to `val`. `inds` is assumed to have been bounds-checked.
555600
return _unsafe_setindex!(IndexStyle(A), A, val, inds...; kwargs...)
556601
end
557602

558-
559603
# these let us use `@ncall` on getindex/setindex! that have kwargs
560604
function _setindex_kwargs!(x, val, kwargs, args...)
561605
@inbounds setindex!(x, val, args...; kwargs...)
@@ -569,7 +613,7 @@ function _generate_unsafe_getindex!_body(N::Int)
569613
Base.@_inline_meta
570614
D = eachindex(dest)
571615
Dy = iterate(D)
572-
@inbounds Base.Cartesian.@nloops $N j d->I[d] begin
616+
@inbounds Base.Cartesian.@nloops $N j d -> I[d] begin
573617
# This condition is never hit, but at the moment
574618
# the optimizer is not clever enough to split the union without it
575619
Dy === nothing && return dest
@@ -584,11 +628,11 @@ end
584628
function _generate_unsafe_setindex!_body(N::Int)
585629
quote
586630
x′ = Base.unalias(A, x)
587-
Base.Cartesian.@nexprs $N d->(I_d = Base.unalias(A, I[d]))
631+
Base.Cartesian.@nexprs $N d -> (I_d = Base.unalias(A, I[d]))
588632
idxlens = Base.Cartesian.@ncall $N Base.index_lengths I
589-
Base.Cartesian.@ncall $N Base.setindex_shape_check x′ (d->idxlens[d])
633+
Base.Cartesian.@ncall $N Base.setindex_shape_check x′ (d -> idxlens[d])
590634
Xy = iterate(x′)
591-
@inbounds Base.Cartesian.@nloops $N i d->I_d begin
635+
@inbounds Base.Cartesian.@nloops $N i d -> I_d begin
592636
# This is never reached, but serves as an assumption for
593637
# the optimizer that it does not need to emit error paths
594638
Xy === nothing && break
@@ -600,11 +644,21 @@ function _generate_unsafe_setindex!_body(N::Int)
600644
end
601645
end
602646

603-
@generated function _unsafe_getindex!(dest::AbstractArray, src::AbstractArray, I::Vararg{Union{Real, AbstractArray}, N}; kwargs...) where N
604-
_generate_unsafe_getindex!_body(N)
647+
@generated function _unsafe_getindex!(
648+
dest::AbstractArray,
649+
src::AbstractArray,
650+
I::Vararg{Union{Real,AbstractArray},N};
651+
kwargs...,
652+
) where {N}
653+
return _generate_unsafe_getindex!_body(N)
605654
end
606655

607-
@generated function _unsafe_setindex!(::IndexStyle, A::AbstractArray, x, I::Vararg{Union{Real,AbstractArray}, N}; kwargs...) where N
608-
_generate_unsafe_setindex!_body(N)
656+
@generated function _unsafe_setindex!(
657+
::IndexStyle,
658+
A::AbstractArray,
659+
x,
660+
I::Vararg{Union{Real,AbstractArray},N};
661+
kwargs...,
662+
) where {N}
663+
return _generate_unsafe_setindex!_body(N)
609664
end
610-

0 commit comments

Comments
 (0)