@@ -65,22 +65,23 @@ module IteratorsMD
65
65
one {N} (:: Type{CartesianIndex{N}} ) = CartesianIndex (ntuple (x -> 1 , Val{N}))
66
66
67
67
# arithmetic, min/max
68
- (- ){N}(index:: CartesianIndex{N} ) = CartesianIndex {N} (map (- , index. I))
69
- (+ ){N}(index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
68
+ @inline (- ){N}(index:: CartesianIndex{N} ) =
69
+ CartesianIndex {N} (map (- , index. I))
70
+ @inline (+ ){N}(index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
70
71
CartesianIndex {N} (map (+ , index1. I, index2. I))
71
- (- ){N}(index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
72
+ @inline (- ){N}(index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
72
73
CartesianIndex {N} (map (- , index1. I, index2. I))
73
- min {N} (index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
74
+ @inline min {N} (index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
74
75
CartesianIndex {N} (map (min, index1. I, index2. I))
75
- max {N} (index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
76
+ @inline max {N} (index1:: CartesianIndex{N} , index2:: CartesianIndex{N} ) =
76
77
CartesianIndex {N} (map (max, index1. I, index2. I))
77
78
78
- (+ )(i:: Integer , index:: CartesianIndex ) = index+ i
79
- (+ ){N}(index:: CartesianIndex{N} , i:: Integer ) = CartesianIndex {N} (map (x-> x+ i, index. I))
80
- (- ){N}(index:: CartesianIndex{N} , i:: Integer ) = CartesianIndex {N} (map (x-> x- i, index. I))
81
- (- ){N}(i:: Integer , index:: CartesianIndex{N} ) = CartesianIndex {N} (map (x-> i- x, index. I))
82
- (* ){N}(a:: Integer , index:: CartesianIndex{N} ) = CartesianIndex {N} (map (x-> a* x, index. I))
83
- (* )(index:: CartesianIndex ,a:: Integer )= * (a,index)
79
+ @inline (+ )(i:: Integer , index:: CartesianIndex ) = index+ i
80
+ @inline (+ ){N}(index:: CartesianIndex{N} , i:: Integer ) = CartesianIndex {N} (map (x-> x+ i, index. I))
81
+ @inline (- ){N}(index:: CartesianIndex{N} , i:: Integer ) = CartesianIndex {N} (map (x-> x- i, index. I))
82
+ @inline (- ){N}(i:: Integer , index:: CartesianIndex{N} ) = CartesianIndex {N} (map (x-> i- x, index. I))
83
+ @inline (* ){N}(a:: Integer , index:: CartesianIndex{N} ) = CartesianIndex {N} (map (x-> a* x, index. I))
84
+ @inline (* )(index:: CartesianIndex ,a:: Integer )= * (a,index)
84
85
85
86
# comparison
86
87
@inline isless {N} (I1:: CartesianIndex{N} , I2:: CartesianIndex{N} ) = _isless (0 , I1. I, I2. I)
@@ -209,6 +210,16 @@ module IteratorsMD
209
210
@inline _split {N} (tN:: NTuple{N,Any} , :: Tuple{} , :: Type{Val{N}} ) = tN, () # ambig.
210
211
@inline _split {N} (tN, :: Tuple{} , :: Type{Val{N}} ) = tN, ()
211
212
@inline _split {N} (tN:: NTuple{N,Any} , trest, :: Type{Val{N}} ) = tN, trest
213
+
214
+ @inline function split (I:: CartesianIndex , V:: Type{<:Val} )
215
+ i, j = split (I. I, V)
216
+ CartesianIndex (i), CartesianIndex (j)
217
+ end
218
+ function split (R:: CartesianRange , V:: Type{<:Val} )
219
+ istart, jstart = split (first (R), V)
220
+ istop, jstop = split (last (R), V)
221
+ CartesianRange (istart, istop), CartesianRange (jstart, jstop)
222
+ end
212
223
end # IteratorsMD
213
224
214
225
@@ -781,6 +792,13 @@ function fill!{T}(A::AbstractArray{T}, x)
781
792
A
782
793
end
783
794
795
+ """
796
+ copy!(dest, src) -> dest
797
+
798
+ Copy all elements from collection `src` to array `dest`.
799
+ """
800
+ copy! (dest, src)
801
+
784
802
function copy! {T,N} (dest:: AbstractArray{T,N} , src:: AbstractArray{T,N} )
785
803
@boundscheck checkbounds (dest, indices (src)... )
786
804
for I in eachindex (IndexStyle (src,dest), src)
@@ -789,22 +807,38 @@ function copy!{T,N}(dest::AbstractArray{T,N}, src::AbstractArray{T,N})
789
807
dest
790
808
end
791
809
792
- function copy! (dest:: AbstractArray , Rdest:: CartesianRange , src:: AbstractArray , Rsrc:: CartesianRange )
793
- isempty (Rdest) && return dest
794
- if size (Rdest) != size (Rsrc)
795
- throw (ArgumentError (" source and destination must have same size (got $(size (Rsrc)) and $(size (Rdest)) )" ))
796
- end
797
- @boundscheck checkbounds (dest, Rdest. start)
798
- @boundscheck checkbounds (dest, Rdest. stop)
799
- @boundscheck checkbounds (src, Rsrc. start)
800
- @boundscheck checkbounds (src, Rsrc. stop)
801
- deltaI = Rdest. start - Rsrc. start
802
- for I in Rsrc
803
- @inbounds dest[I+ deltaI] = src[I]
810
+ @generated function copy! {T1,T2,N} (dest:: AbstractArray{T1,N} ,
811
+ Rdest:: CartesianRange{CartesianIndex{N}} ,
812
+ src:: AbstractArray{T2,N} ,
813
+ Rsrc:: CartesianRange{CartesianIndex{N}} )
814
+ quote
815
+ isempty (Rdest) && return dest
816
+ if size (Rdest) != size (Rsrc)
817
+ throw (ArgumentError (" source and destination must have same size (got $(size (Rsrc)) and $(size (Rdest)) )" ))
818
+ end
819
+ @boundscheck checkbounds (dest, Rdest. start)
820
+ @boundscheck checkbounds (dest, Rdest. stop)
821
+ @boundscheck checkbounds (src, Rsrc. start)
822
+ @boundscheck checkbounds (src, Rsrc. stop)
823
+ ΔI = Rdest. start - Rsrc. start
824
+ # TODO : restore when #9080 is fixed
825
+ # for I in Rsrc
826
+ # @inbounds dest[I+ΔI] = src[I]
827
+ @nloops $ N i (n-> Rsrc. start[n]: Rsrc. stop[n]) begin
828
+ @inbounds @nref ($ N,dest,n-> i_n+ ΔI[n]) = @nref ($ N,src,i)
829
+ end
830
+ dest
804
831
end
805
- dest
806
832
end
807
833
834
+ """
835
+ copy!(dest, Rdest::CartesianRange, src, Rsrc::CartesianRange) -> dest
836
+
837
+ Copy the block of `src` in the range of `Rsrc` to the block of `dest`
838
+ in the range of `Rdest`. The sizes of the two regions must match.
839
+ """
840
+ copy! (:: AbstractArray , :: CartesianRange , :: AbstractArray , :: CartesianRange )
841
+
808
842
# circshift!
809
843
circshift! (dest:: AbstractArray , src, :: Tuple{} ) = copy! (dest, src)
810
844
"""
0 commit comments