Skip to content

Commit

Permalink
A colon can be mixed with indices and ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
emmt committed Nov 8, 2019
1 parent 8fd6031 commit c03810a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ A[…] == A[:,:,:,:]
A[,3] == A[:,:,:,3]
A[2,] == A[2,:,:,:]
A[,2:4,5] == A[:,:,2:4,5]
A[:,2:3,] == A[:,2:3,:,:]
A[2:3,,1,2:4] == A[2:3,:,1,2:4]
```

As you can see the advantage of the *rubber index* `` is that it automatically
expands as the number of colons needed to have the correct number of indices.
As you can see, the advantage of the *rubber index* `` is that it
automatically expands as the number of colons needed to have the correct number
of indices. The expressions are also more readable. The idea comes from the
[`Yorick`](http://yorick.github.com/) language by Dave Munro.

The rubber index `` may also be used for setting values.
The rubber index may also be used for setting values. For instance:

```julia
A[] .= 1 # to fill A with ones
A[,3] = A[,2] # to copy A[:,:,:,2] in A[:,:,:,3]
A[,3] .= A[,2] # idem but faster
A[2,] = A[3,] # to copy A[3,:,:,:] in A[2,:,:,:]
A[,2:4,5] .= 7 # to set all elements in A[:,:,2:4,5] to 7
```

Leading/trailing indices may be specified as Cartesian indices (of type
`CartesianIndex`).
Expand Down
17 changes: 11 additions & 6 deletions src/rubberindex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,27 @@ A[…,2:4,5] == A[:,:,2:4,5]
A[2:3,…,1,2:4] == A[2:3,:,1,2:4]
```
As you can see the advantage of the rubber index `…` is that it automatically
As you can see, the advantage of the rubber index `…` is that it automatically
expands as the number of colons needed to have the correct number of indices.
The expressions are also more readable.
The rubber index may also be used for setting values. For instance:
```julia
A[…] .= 1 # to fill A with ones
A[…,3] = A[…,2] # to copy A[:,:,:,2] in A[:,:,:,3]
A[2,…] = A[3,…] # to copy A[3,:,:,:] in A[2,:,:,:]
A[…,2:4,5] .= 7 # to set all elements in A[:,:,2:4,5] to 7
A[…] .= 1 # to fill A with ones
A[…,3] = A[…,2] # to copy A[:,:,:,2] in A[:,:,:,3]
A[…,3] .= A[…,2] # idem but faster
A[2,…] = A[3,…] # to copy A[3,:,:,:] in A[2,:,:,:]
A[…,2:4,5] .= 7 # to set all elements in A[:,:,2:4,5] to 7
```
Leading/trailing indices may be specified as Cartesian indices (of type
`CartesianIndex`).
!!! warn
There is one known limitation: the `end` reserved word can be used in
intervals specified *before* the rubber index but not *after*.
See also: [`rubberindex`](@ref).
"""
Expand All @@ -103,7 +108,7 @@ const … = RubberIndex()
integers and integer valued ranges.
"""
const Indices = Union{Integer,AbstractRange{<:Integer}}
const Indices = Union{Integer,AbstractRange{<:Integer},Colon}

#numberofindices(::Integer) = 1
#numberofindices(::CartesianIndex{N}) where {N} = N
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,25 @@ end
@test A[2,] == A[2,:,:,:]
@test A[,2:4,5] == A[:,:,2:4,5]
@test A[2:3,,1,2:4] == A[2:3,:,1,2:4]
@test A[:,2,,2:4] == A[:,2,:,2:4]
@test A[:,2:3,] == A[:,2:3,:,:]
@test A[,2:3,:] == A[:,:,2:3,:]
@test A[I1,] == A[I1,:,:,:]
@test A[I2,] == A[I2,:,:]
@test A[,I1] == A[:,:,:,I1]
@test A[,I2] == A[:,:,I2]
@test A[I1,,I2] == A[I1,:,I2]
@test A[I2,,I1] == A[I2,:,I1]
@test A[I1,,I3] == A[I1,I3]
@test A[1,2:end,] == A[1,2:end,:,:]
@test A[1,2:end-1,] == A[1,2:end-1,:,:]
@test_broken A[2,,3:end] == A[2,:,:,3:end]
A[1,] .= 3
@test all(isequal(3), A[1,:,:,:])
A[1,] = A[2,]
@test A[1,:,:,:] == A[2,:,:,:]
A[1,,2:3] .= A[2,,3:2:5]
@test A[1,:,:,2:3] == A[2,:,:,3:2:5]
end

@testset "Storage" begin
Expand Down

0 comments on commit c03810a

Please sign in to comment.