@@ -11,28 +11,28 @@ ReshapedArray{T,N}(parent::AbstractArray{T}, dims::NTuple{N,Int}, mi) = Reshaped
1111typealias ReshapedArrayLF{T,N,P<: AbstractArray } ReshapedArray{T,N,P,Tuple{}}
1212
1313# Fast iteration on ReshapedArrays: use the parent iterator
14- immutable ReshapedRange {I,M}
14+ immutable ReshapedArrayIterator {I,M}
1515 iter:: I
1616 mi:: NTuple{M,SignedMultiplicativeInverse{Int}}
1717end
18- ReshapedRange (A:: ReshapedArray ) = reshapedrange (parent (A), A. mi)
19- function reshapedrange {M} (P, mi:: NTuple{M} )
18+ ReshapedArrayIterator (A:: ReshapedArray ) = _rs_iterator (parent (A), A. mi)
19+ function _rs_iterator {M} (P, mi:: NTuple{M} )
2020 iter = eachindex (P)
21- ReshapedRange {typeof(iter),M} (iter, mi)
21+ ReshapedArrayIterator {typeof(iter),M} (iter, mi)
2222end
2323
2424immutable ReshapedIndex{T}
2525 parentindex:: T
2626end
2727
28- # eachindex(A::ReshapedArray) = ReshapedRange (A) # TODO : uncomment this line
29- start (R:: ReshapedRange ) = start (R. iter)
30- @inline done (R:: ReshapedRange , i) = done (R. iter, i)
31- @inline function next (R:: ReshapedRange , i)
28+ # eachindex(A::ReshapedArray) = ReshapedArrayIterator (A) # TODO : uncomment this line
29+ start (R:: ReshapedArrayIterator ) = start (R. iter)
30+ @inline done (R:: ReshapedArrayIterator , i) = done (R. iter, i)
31+ @inline function next (R:: ReshapedArrayIterator , i)
3232 item, inext = next (R. iter, i)
3333 ReshapedIndex (item), inext
3434end
35- length (R:: ReshapedRange ) = length (R. iter)
35+ length (R:: ReshapedArrayIterator ) = length (R. iter)
3636
3737function reshape (parent:: AbstractArray , dims:: Dims )
3838 prod (dims) == length (parent) || throw (DimensionMismatch (" parent has $(length (parent)) elements, which is incompatible with size $dims " ))
@@ -84,19 +84,61 @@ reinterpret{T}(::Type{T}, A::ReshapedArray, dims::Dims) = reinterpret(T, parent(
8484 ind2sub_rs ((d+ 1 , out... ), tail (strds), r)
8585end
8686
87- @inline getindex (A:: ReshapedArrayLF , index:: Int ) = (@boundscheck checkbounds (A, index); @inbounds ret = parent (A)[index]; ret)
88- @inline getindex (A:: ReshapedArray , indexes:: Int... ) = (@boundscheck checkbounds (A, indexes... ); _unsafe_getindex (A, indexes... ))
89- @inline getindex (A:: ReshapedArray , index:: ReshapedIndex ) = (@boundscheck checkbounds (parent (A), index. parentindex); @inbounds ret = parent (A)[index. parentindex]; ret)
87+ @inline function getindex (A:: ReshapedArrayLF , index:: Int )
88+ @boundscheck checkbounds (A, index)
89+ @inbounds ret = parent (A)[index]
90+ ret
91+ end
92+ @inline function getindex (A:: ReshapedArray , indexes:: Int... )
93+ @boundscheck checkbounds (A, indexes... )
94+ _unsafe_getindex (A, indexes... )
95+ end
96+ @inline function getindex (A:: ReshapedArray , index:: ReshapedIndex )
97+ @boundscheck checkbounds (parent (A), index. parentindex)
98+ @inbounds ret = parent (A)[index. parentindex]
99+ ret
100+ end
101+
102+ @inline function _unsafe_getindex (A:: ReshapedArray , indexes:: Int... )
103+ @inbounds ret = parent (A)[ind2sub_rs (A. mi, sub2ind (size (A), indexes... ))... ]
104+ ret
105+ end
106+ @inline function _unsafe_getindex (A:: ReshapedArrayLF , indexes:: Int... )
107+ @inbounds ret = parent (A)[sub2ind (size (A), indexes... )]
108+ ret
109+ end
90110
91- @inline _unsafe_getindex (A:: ReshapedArray , indexes:: Int... ) = (@inbounds ret = parent (A)[ind2sub_rs (A. mi, sub2ind (size (A), indexes... ))... ]; ret)
92- @inline _unsafe_getindex (A:: ReshapedArrayLF , indexes:: Int... ) = (@inbounds ret = parent (A)[sub2ind (size (A), indexes... )]; ret)
111+ @inline function setindex! (A:: ReshapedArrayLF , val, index:: Int )
112+ @boundscheck checkbounds (A, index)
113+ @inbounds parent (A)[index] = val
114+ val
115+ end
116+ @inline function setindex! (A:: ReshapedArray , val, indexes:: Int... )
117+ @boundscheck checkbounds (A, indexes... )
118+ _unsafe_setindex! (A, val, indexes... )
119+ end
120+ @inline function setindex! (A:: ReshapedArray , val, index:: ReshapedIndex )
121+ @boundscheck checkbounds (parent (A), index. parentindex)
122+ @inbounds parent (A)[index. parentindex] = val
123+ val
124+ end
125+
126+ @inline function _unsafe_setindex! (A:: ReshapedArray , val, indexes:: Int... )
127+ @inbounds parent (A)[ind2sub_rs (A. mi, sub2ind (size (A), indexes... ))... ] = val
128+ val
129+ end
130+ @inline function _unsafe_setindex! (A:: ReshapedArrayLF , val, indexes:: Int... )
131+ @inbounds parent (A)[sub2ind (size (A), indexes... )] = val
132+ val
133+ end
93134
94- @inline setindex! (A:: ReshapedArrayLF , val, index:: Int ) = (@boundscheck checkbounds (A, index); @inbounds parent (A)[index] = val; val)
95- @inline setindex! (A:: ReshapedArray , val, indexes:: Int... ) = (@boundscheck checkbounds (A, indexes... ); _unsafe_setindex! (A, val, indexes... ))
96- @inline setindex! (A:: ReshapedArray , val, index:: ReshapedIndex ) = (@boundscheck checkbounds (parent (A), index. parentindex); @inbounds parent (A)[index. parentindex] = val; val)
135+ # helpful error message for a common failure case
136+ typealias ReshapedRange{T,N,A<: Range } ReshapedArray{T,N,A,Tuple{}}
137+ setindex! (A:: ReshapedRange , val, index:: Int ) = _rs_setindex!_err ()
138+ setindex! (A:: ReshapedRange , val, indexes:: Int... ) = _rs_setindex!_err ()
139+ setindex! (A:: ReshapedRange , val, index:: ReshapedIndex ) = _rs_setindex!_err ()
97140
98- @inline _unsafe_setindex! (A:: ReshapedArray , val, indexes:: Int... ) = (@inbounds parent (A)[ind2sub_rs (A. mi, sub2ind (size (A), indexes... ))... ] = val; val)
99- @inline _unsafe_setindex! (A:: ReshapedArrayLF , val, indexes:: Int... ) = (@inbounds parent (A)[sub2ind (size (A), indexes... )] = val; val)
141+ _rs_setindex!_err () = error (" indexed assignment fails for a reshaped range; consider calling collect" )
100142
101143typealias ArrayT{N, T} Array{T,N}
102144convert {T,S,N} (:: Type{Array{T,N}} , V:: ReshapedArray{S,N} ) = copy! (Array (T, size (V)), V)
0 commit comments