@@ -28,6 +28,43 @@ using SparseArraysBase:
28
28
setunstoredindex!,
29
29
storedlength
30
30
31
+ function view! (a:: AbstractArray{<:Any,N} , index:: Block{N} ) where {N}
32
+ return view! (a, Tuple (index)... )
33
+ end
34
+ function view! (a:: AbstractArray{<:Any,N} , index:: Vararg{Block{1},N} ) where {N}
35
+ blocks (a)[Int .(index)... ] = blocks (a)[Int .(index)... ]
36
+ return blocks (a)[Int .(index)... ]
37
+ end
38
+ # Fix ambiguity error.
39
+ function view! (a:: AbstractArray{<:Any,0} )
40
+ blocks (a)[] = blocks (a)[]
41
+ return blocks (a)[]
42
+ end
43
+
44
+ function view! (a:: AbstractArray{<:Any,N} , index:: BlockIndexRange{N} ) where {N}
45
+ # TODO : Is there a better code pattern for this?
46
+ indices = ntuple (N) do dim
47
+ return Tuple (Block (index))[dim][index. indices[dim]]
48
+ end
49
+ return view! (a, indices... )
50
+ end
51
+ function view! (a:: AbstractArray{<:Any,N} , index:: Vararg{BlockIndexRange{1},N} ) where {N}
52
+ b = view! (a, Block .(index)... )
53
+ r = map (index -> only (index. indices), index)
54
+ return @view b[r... ]
55
+ end
56
+
57
+ using MacroTools: @capture
58
+ is_getindex_expr (expr:: Expr ) = (expr. head === :ref )
59
+ is_getindex_expr (x) = false
60
+ macro view! (expr)
61
+ if ! is_getindex_expr (expr)
62
+ error (" @view must be used with getindex syntax (as `@view! a[i,j,...]`)" )
63
+ end
64
+ @capture (expr, array_[indices__])
65
+ return :(view! ($ (esc (array)), $ (esc .(indices)... )))
66
+ end
67
+
31
68
# A return type for `blocks(array)` when `array` isn't blocked.
32
69
# Represents a vector with just that single block.
33
70
struct SingleBlockView{N,Array<: AbstractArray{<:Any,N} } <: AbstractArray{Array,N}
@@ -568,12 +605,34 @@ function Base.getindex(a::BlockView{<:Any,N}, index::Vararg{Int,N}) where {N}
568
605
return blocks (parent (a))[Int .(a. block)... ][index... ]
569
606
end
570
607
function Base. setindex! (a:: BlockView{<:Any,N} , value, index:: Vararg{Int,N} ) where {N}
571
- I = Int .(a. block)
572
- if ! isstored (blocks (parent (a)), I... )
573
- unstored_value = getunstoredindex (blocks (parent (a)), I... )
574
- setunstoredindex! (blocks (parent (a)), unstored_value, I... )
575
- end
576
- blocks (parent (a))[I... ][index... ] = value
608
+ b = @view! parent (a)[a. block... ]
609
+ b[index... ] = value
610
+ return a
611
+ end
612
+ function Base. fill! (a:: BlockView , value)
613
+ b = @view! parent (a)[a. block... ]
614
+ fill! (b, value)
615
+ end
616
+ using Base. Broadcast: AbstractArrayStyle, Broadcasted, broadcasted
617
+ materialize_blockviews (x) = x
618
+ materialize_blockviews (a:: BlockView ) = blocks (parent (a))[Int .(a. block)... ]
619
+ function materialize_blockviews (bc:: Broadcasted )
620
+ return broadcasted (bc. f, map (materialize_blockviews, bc. args)... )
621
+ end
622
+ function Base. copyto! (a:: BlockView , bc:: Broadcasted )
623
+ b = @view! parent (a)[a. block... ]
624
+ bc′ = materialize_blockviews (bc)
625
+ copyto! (b, bc′)
626
+ return a
627
+ end
628
+ function Base. copyto! (a:: BlockView , bc:: Broadcasted{<:AbstractArrayStyle{0}} )
629
+ b = @view! parent (a)[a. block... ]
630
+ copyto! (b, bc)
631
+ return a
632
+ end
633
+ function Base. copyto! (a:: BlockView , src:: AbstractArray )
634
+ b = @view! parent (a)[a. block... ]
635
+ copyto! (b, src)
577
636
return a
578
637
end
579
638
@@ -602,43 +661,6 @@ function ArrayLayouts.sub_materialize(a::BlockView)
602
661
return blocks (parent (a))[Int .(a. block)... ]
603
662
end
604
663
605
- function view! (a:: AbstractArray{<:Any,N} , index:: Block{N} ) where {N}
606
- return view! (a, Tuple (index)... )
607
- end
608
- function view! (a:: AbstractArray{<:Any,N} , index:: Vararg{Block{1},N} ) where {N}
609
- blocks (a)[Int .(index)... ] = blocks (a)[Int .(index)... ]
610
- return blocks (a)[Int .(index)... ]
611
- end
612
- # Fix ambiguity error.
613
- function view! (a:: AbstractArray{<:Any,0} )
614
- blocks (a)[] = blocks (a)[]
615
- return blocks (a)[]
616
- end
617
-
618
- function view! (a:: AbstractArray{<:Any,N} , index:: BlockIndexRange{N} ) where {N}
619
- # TODO : Is there a better code pattern for this?
620
- indices = ntuple (N) do dim
621
- return Tuple (Block (index))[dim][index. indices[dim]]
622
- end
623
- return view! (a, indices... )
624
- end
625
- function view! (a:: AbstractArray{<:Any,N} , index:: Vararg{BlockIndexRange{1},N} ) where {N}
626
- b = view! (a, Block .(index)... )
627
- r = map (index -> only (index. indices), index)
628
- return @view b[r... ]
629
- end
630
-
631
- using MacroTools: @capture
632
- is_getindex_expr (expr:: Expr ) = (expr. head === :ref )
633
- is_getindex_expr (x) = false
634
- macro view! (expr)
635
- if ! is_getindex_expr (expr)
636
- error (" @view must be used with getindex syntax (as `@view! a[i,j,...]`)" )
637
- end
638
- @capture (expr, array_[indices__])
639
- return :(view! ($ (esc (array)), $ (esc .(indices)... )))
640
- end
641
-
642
664
# SVD additions
643
665
# -------------
644
666
using LinearAlgebra: Algorithm
0 commit comments