Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional tools for type-stable multidimensional coding #15030

Merged
merged 2 commits into from
Mar 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ for op in (:+, :-, :min, :max)
end
end

(+){N}(index::CartesianIndex{N}, i::Integer) = CartesianIndex{N}(map(x->x+i, index.I))
(+){N}(i::Integer, index::CartesianIndex{N}) = index+i
(-){N}(index::CartesianIndex{N}, i::Integer) = CartesianIndex{N}(map(x->x-i, index.I))
(-){N}(i::Integer, index::CartesianIndex{N}) = CartesianIndex{N}(map(x->i-x, index.I))

@generated function *{N}(a::Integer, index::CartesianIndex{N})
I = index
args = [:(a*index[$d]) for d = 1:N]
Expand All @@ -78,7 +83,9 @@ end
startargs = fill(1, N)
:(CartesianRange($I($(startargs...)), I))
end
CartesianRange(::Tuple{}) = CartesianRange{CartesianIndex{0}}(CartesianIndex{0}(()),CartesianIndex{0}(()))
CartesianRange{N}(sz::NTuple{N,Int}) = CartesianRange(CartesianIndex(sz))
CartesianRange{N}(rngs::NTuple{N,Union{Int,UnitRange{Int}}}) = CartesianRange(CartesianIndex(map(r->first(r), rngs)), CartesianIndex(map(r->last(r), rngs)))

ndims(R::CartesianRange) = length(R.start)
ndims{I<:CartesianIndex}(::Type{CartesianRange{I}}) = length(I)
Expand Down
16 changes: 16 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ tails(t::Tuple, ts::Tuple...) = (tail(t), tails(ts...)...)
map(f, ::Tuple{}, ts::Tuple...) = ()
map(f, ts::Tuple...) = (f(heads(ts...)...), map(f, tails(ts...)...)...)

# type-stable padding
fill_to_length{N}(t::Tuple, val, ::Type{Val{N}}) = _ftl((), val, Val{N}, t...)
_ftl{N}(out::NTuple{N}, val, ::Type{Val{N}}) = out
function _ftl{N}(out::NTuple{N}, val, ::Type{Val{N}}, t...)
@_inline_meta
error("input tuple of length $(N+length(t)), requested $N")
end
function _ftl{N}(out, val, ::Type{Val{N}}, t1, t...)
@_inline_meta
_ftl((out..., t1), val, Val{N}, t...)
end
function _ftl{N}(out, val, ::Type{Val{N}})
@_inline_meta
_ftl((out..., val), val, Val{N})
end

## comparison ##

function isequal(t1::Tuple, t2::Tuple)
Expand Down
5 changes: 5 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,8 @@ I2 = CartesianIndex((-1,5,2))
@test I2 + I1 == CartesianIndex((1,8,2))
@test I1 - I2 == CartesianIndex((3,-2,-2))
@test I2 - I1 == CartesianIndex((-3,2,2))
@test I1 + 1 == CartesianIndex((3,4,1))
@test I1 - 2 == CartesianIndex((0,1,-2))

@test min(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((2,2))
@test max(CartesianIndex((2,3)), CartesianIndex((5,2))) == CartesianIndex((5,3))
Expand Down Expand Up @@ -1147,6 +1149,9 @@ indexes = collect(R)
@test length(R) == 12
@test ndims(R) == 2

@test CartesianRange((3:5,-7:7)) == CartesianRange(CartesianIndex{2}(3,-7),CartesianIndex{2}(5,7))
@test CartesianRange((3,-7:7)) == CartesianRange(CartesianIndex{2}(3,-7),CartesianIndex{2}(3,7))

r = 2:3
itr = eachindex(r)
state = start(itr)
Expand Down
4 changes: 4 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

@test getindex((5,6,7,8), []) === ()

## filling to specified length
@test @inferred(Base.fill_to_length((1,2,3), -1, Val{5})) == (1,2,3,-1,-1)
@test_throws ErrorException Base.fill_to_length((1,2,3), -1, Val{2})

## iterating ##
@test start((1,2,3)) === 1

Expand Down