@@ -28,7 +28,8 @@ argdims(::ArrayStyle, ::Type{T}) where {T<:AbstractArray} = ndims(T)
28
28
argdims (:: ArrayStyle , :: Type{T} ) where {N,T<: CartesianIndex{N} } = N
29
29
argdims (:: ArrayStyle , :: Type{T} ) where {N,T<: AbstractArray{CartesianIndex{N}} } = N
30
30
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
32
33
@generated function argdims (s:: ArrayStyle , :: Type{T} ) where {N,T<: Tuple{Vararg{Any,N}} }
33
34
e = Expr (:tuple )
34
35
for p in T. parameters
@@ -116,20 +117,35 @@ flatten_new_indexer(A::Array2, args) = ...
116
117
@inline function flatten_args (A, args:: Tuple{Arg,Vararg{Any}} ) where {Arg}
117
118
return (first (args), flatten_args (A, tail (args))... )
118
119
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} }
120
124
return (first (args). I... , flatten_args (A, tail (args))... )
121
125
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} }
123
130
return (eachindex (first (args)), flatten_args (A, tail (args))... )
124
131
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} }
126
136
return (first (args). indices... , flatten_args (A, tail (args))... )
127
137
end
128
138
# 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} }
130
143
return (first (args), flatten_args (A, tail (args))... )
131
144
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} }
133
149
if length (args) === 1
134
150
if IndexStyle (A) isa IndexLinear
135
151
return (LogicalIndex {Int} (first (args)),)
@@ -167,7 +183,7 @@ ArrayInterface.can_flatten(::Type{A}, ::Type{T}) where {A<:ForbiddenArray,T<:New
167
183
can_flatten (A, x) = can_flatten (typeof (A), typeof (x))
168
184
can_flatten (:: Type{A} , :: Type{T} ) where {A,T} = false
169
185
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
171
187
can_flatten (:: Type{A} , :: Type{T} ) where {A,N,T<: AbstractArray{Bool,N} } = N > 1
172
188
can_flatten (:: Type{A} , :: Type{T} ) where {A,N,T<: CartesianIndex{N} } = true
173
189
@generated function can_flatten (:: Type{A} , :: Type{T} ) where {A,T<: Tuple }
@@ -196,11 +212,18 @@ be accomplished using `to_index(axis, arg)`.
196
212
end
197
213
end
198
214
@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}
200
220
N = argdims (A, Arg)
201
221
if N > 1
202
222
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
+ )
204
227
else
205
228
return (to_index (first (axs), first (args)), to_indices (A, tail (axs), tail (args))... )
206
229
end
211
234
end
212
235
return to_indices (A, tail (axs), args)
213
236
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}
215
242
return (to_index (OneTo (1 ), first (args)), to_indices (A, (), tail (args))... )
216
243
end
217
244
to_indices (A, axs:: Tuple{} , args:: Tuple{} ) = ()
@@ -265,13 +292,21 @@ end
265
292
@boundscheck checkbounds (axis, arg)
266
293
return @inbounds (axis[arg])
267
294
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 }
269
300
@boundscheck if ! checkindex (Bool, axis, arg)
270
301
throw (BoundsError (axis, arg))
271
302
end
272
303
return arg
273
304
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 }
275
310
@boundscheck if ! checkindex (Bool, axis, arg)
276
311
throw (BoundsError (axis, arg))
277
312
end
@@ -291,7 +326,8 @@ function unsafe_reconstruct(A::OneTo, data; kwargs...)
291
326
if can_change_size (A)
292
327
return typeof (A)(data)
293
328
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))
295
331
return A
296
332
else
297
333
return OneTo (data)
@@ -302,7 +338,8 @@ function unsafe_reconstruct(A::UnitRange, data; kwargs...)
302
338
if can_change_size (A)
303
339
return typeof (A)(data)
304
340
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))
306
343
return A
307
344
else
308
345
return UnitRange (data)
@@ -313,7 +350,8 @@ function unsafe_reconstruct(A::OptionallyStaticUnitRange, data; kwargs...)
313
350
if can_change_size (A)
314
351
return typeof (A)(data)
315
352
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))
317
355
return A
318
356
else
319
357
return OptionallyStaticUnitRange (data)
@@ -344,7 +382,11 @@ pair of axes and indices calling [`to_axis`](@ref).
344
382
end
345
383
to_axes (A, :: Tuple{Ax,Vararg{Any}} , :: Tuple{} ) where {Ax} = ()
346
384
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}
348
390
N = argdims (A, I)
349
391
if N === 0
350
392
# drop this dimension
@@ -355,7 +397,10 @@ to_axes(A, ::Tuple{}, ::Tuple{}) = ()
355
397
# Only multidimensional AbstractArray{Bool} and AbstractVector{CartesianIndex{N}}
356
398
# make it to this point. They collapse several dimensions into one.
357
399
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
+ )
359
404
end
360
405
end
361
406
@@ -368,7 +413,8 @@ previously executed `to_index(old_axis, arg) -> index`. `to_axis` assumes that
368
413
`new_axis` begins at one and extends the length of `index` (i.e., one-based indexing).
369
414
"""
370
415
@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))
372
418
return axis
373
419
else
374
420
return to_axis (IndexStyle (axis), axis, inds)
@@ -497,8 +543,7 @@ Store the given values at the given key or index within a collection.
497
543
if can_setindex (A)
498
544
return unsafe_setindex! (A, val, to_indices (A, args))
499
545
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." )
502
547
end
503
548
end
504
549
@propagate_inbounds function setindex! (A, val; kwargs... )
@@ -533,7 +578,7 @@ have been checked for being in bounds. Any new array type using `ArrayInterface.
533
578
must define `unsafe_set_element!(::NewArrayType, val, inds)`.
534
579
"""
535
580
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)))
537
582
end
538
583
function unsafe_set_element! (A:: Array{T} , val, inds:: Tuple ) where {T}
539
584
if length (inds) === 0
@@ -555,7 +600,6 @@ Sets `inds` of `A` to `val`. `inds` is assumed to have been bounds-checked.
555
600
return _unsafe_setindex! (IndexStyle (A), A, val, inds... ; kwargs... )
556
601
end
557
602
558
-
559
603
# these let us use `@ncall` on getindex/setindex! that have kwargs
560
604
function _setindex_kwargs! (x, val, kwargs, args... )
561
605
@inbounds setindex! (x, val, args... ; kwargs... )
@@ -569,7 +613,7 @@ function _generate_unsafe_getindex!_body(N::Int)
569
613
Base. @_inline_meta
570
614
D = eachindex (dest)
571
615
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
573
617
# This condition is never hit, but at the moment
574
618
# the optimizer is not clever enough to split the union without it
575
619
Dy === nothing && return dest
@@ -584,11 +628,11 @@ end
584
628
function _generate_unsafe_setindex!_body (N:: Int )
585
629
quote
586
630
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]))
588
632
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])
590
634
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
592
636
# This is never reached, but serves as an assumption for
593
637
# the optimizer that it does not need to emit error paths
594
638
Xy === nothing && break
@@ -600,11 +644,21 @@ function _generate_unsafe_setindex!_body(N::Int)
600
644
end
601
645
end
602
646
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)
605
654
end
606
655
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)
609
664
end
610
-
0 commit comments