diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 74ed2b7c1e07b..2b1127d70d4eb 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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, isless, eachindex, ndims, iteratorsize 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 @@ -73,6 +73,15 @@ end end *(index::CartesianIndex,a::Integer)=*(a,index) +# comparison +@inline isless{N}(I1::CartesianIndex{N}, I2::CartesianIndex{N}) = _isless(0, I1.I, I2.I) +@inline function _isless{N}(ret, I1::NTuple{N,Int}, I2::NTuple{N,Int}) + newret = ifelse(ret==0, icmp(I1[N], I2[N]), ret) + _isless(newret, Base.front(I1), Base.front(I2)) +end +_isless(ret, ::Tuple{}, ::Tuple{}) = ifelse(ret==1, true, false) +icmp(a, b) = ifelse(isless(a,b), 1, ifelse(a==b, 0, -1)) + # Iteration immutable CartesianRange{I<:CartesianIndex} start::I diff --git a/test/arrayops.jl b/test/arrayops.jl index cc08f66a2cc50..f548085c15bb9 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1197,6 +1197,11 @@ I2 = CartesianIndex((-1,5,2)) @test length(I1) == 3 +@test isless(CartesianIndex((1,1)), CartesianIndex((2,1))) +@test isless(CartesianIndex((1,1)), CartesianIndex((1,2))) +@test isless(CartesianIndex((2,1)), CartesianIndex((1,2))) +@test !isless(CartesianIndex((1,2)), CartesianIndex((2,1))) + a = spzeros(2,3) @test CartesianRange(size(a)) == eachindex(a) a[CartesianIndex{2}(2,3)] = 5