Skip to content

Commit

Permalink
Add sub2ind for CartesianIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Apr 3, 2016
1 parent 8c17d60 commit ccaca30
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,8 @@ sub2ind(dims::Tuple{Vararg{Integer}}, I::Integer...) = _sub2ind(dims,I)
end
Expr(:block, meta,:($ex + 1))
end
sub2ind(A::AbstractArray, I::Integer...) = _sub2ind(size(A), I)
_sub2ind(A::AbstractArray, I) = _sub2ind(size(A), I)

@generated function ind2sub{N}(dims::NTuple{N,Integer}, ind::Integer)
meta = Expr(:meta,:inline)
Expand Down
1 change: 0 additions & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ end
@deprecate flipud(A::AbstractArray) flipdim(A, 1)
@deprecate fliplr(A::AbstractArray) flipdim(A, 2)

@deprecate sub2ind{T<:Integer}(dims::Array{T}, sub::Array{T}) sub2ind(tuple(dims...), sub...)
@deprecate ind2sub!{T<:Integer}(sub::Array{T}, dims::Array{T}, ind::T) ind2sub!(sub, tuple(dims...), ind)

@deprecate strftime Libc.strftime
Expand Down
6 changes: 5 additions & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Multidimensional iterators
module IteratorsMD

import Base: eltype, length, size, start, done, next, last, getindex, setindex!, linearindexing, min, max, eachindex, ndims, iteratorsize
import Base: eltype, length, size, start, done, next, last, getindex, setindex!, linearindexing, min, max, eachindex, ndims, iteratorsize, sub2ind, _sub2ind
importall ..Base.Operators
import Base: simd_outer_range, simd_inner_length, simd_index, @generated
import Base: @nref, @ncall, @nif, @nexprs, LinearFast, LinearSlow, to_index, AbstractCartesianIndex
Expand Down Expand Up @@ -50,6 +50,10 @@ length{N}(::Type{CartesianIndex{N}})=N
# indexing
getindex(index::CartesianIndex, i::Integer) = index.I[i]

# conversion to linear index
sub2ind(A::AbstractArray, I::CartesianIndex) = _sub2ind(A, I.I)
_sub2ind(A::AbstractArray, I::CartesianIndex) = _sub2ind(A, I.I)

# arithmetic, min/max
for op in (:+, :-, :min, :max)
@eval begin
Expand Down

0 comments on commit ccaca30

Please sign in to comment.