Are x and y offsets with respect to the "parent" range cr?
If so, you can achieve this with a 1-liner,
CartesianRange(cr.start+x-1, cr.start+y-1)
although you need https://github.com/JuliaLang/julia/pull/15030 to support the
"- 1". On julia 0.4, that would have to be
@generated function Base.(:-){N}(I::CartesianIndex{N}, i::Integer)
args = [:(I[$d]-i) for d = 1:N]
:(CartesianIndex(tuple($(args...))))
end
As a homework exercise, one could add bounds-checking if desired. But:
> I note that constructing CartesianRange(x,y) is valid code, but won't work
> because, we need to somehow embed start, stop into the sub-range.
> Also, it is possible for some x[i] > y[i], and I think the logic for
> CartesianRanges requires x[i] <= y[i] for all i.
Already ahead of you there :-).
julia> I1 = CartesianIndex((3,3,3))
CartesianIndex{3}((3,3,3))
julia> I2 = CartesianIndex((5,1,7))
CartesianIndex{3}((5,1,7))
julia> R = CartesianRange(I1, I2)
CartesianRange{CartesianIndex{3}}(CartesianIndex{3}((3,3,3)),CartesianIndex{3}
((5,1,7)))
julia> s = start(R)
CartesianIndex{3}((3,3,8))
julia> done(R, s)
true
meaning that it is an empty iterator (the body of a for loop would never
execute).
Best,
--Tim