Skip to content

Commit 49462ad

Browse files
committed
more SubArray functionality (addressing #93):
- works on integer inputs - better handling of calling sub on a SubArray
1 parent f644d77 commit 49462ad

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

j/tensor.j

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,18 +1038,25 @@ end
10381038

10391039
## subarrays ##
10401040

1041-
type SubArray{T,N,A<:AbstractArray,I<:(Indices...)} <: AbstractArray{T,N}
1041+
type SubArray{T,N,A<:AbstractArray,I<:(AbstractVector...)} <: AbstractArray{T,N}
10421042
parent::A
10431043
indexes::I
10441044
dims::Dims
10451045

10461046
SubArray(p::A, i::I) = new(p, i, map(length, i))
10471047
end
10481048

1049-
sub{T,N}(A::AbstractArray{T,N}, i::NTuple{N,Indices}) =
1049+
sub{T,N}(A::AbstractArray{T,N}, i::NTuple{N,AbstractVector}) =
10501050
SubArray{T,N,typeof(A),typeof(i)}(A, i)
10511051

1052-
sub(A::AbstractArray, i::Indices...) = sub(A, i)
1052+
#change integer indexes into Range1 objects
1053+
sub(A::AbstractArray, i::Indices...) =
1054+
sub(A, ntuple(length(i), j -> isa(i[j],AbstractVector) ? i[j] :
1055+
(i[j]:i[j])))
1056+
1057+
sub(A::SubArray, i::Indices...) =
1058+
sub(A.parent, ntuple(length(i), j -> isa(i[j],AbstractVector) ? A.indexes[j][i[j]] :
1059+
(A.indexes[j][i[j]]):(A.indexes[j][i[j]])))
10531060

10541061
size(s::SubArray) = s.dims
10551062
ndims{T,N}(s::SubArray{T,N}) = N

0 commit comments

Comments
 (0)