Skip to content

Commit f27f4b8

Browse files
authored
Add size(::LinearIndices) method (#25541)
Fixes indexing LinearIndices with an array. This is useful in particular to convert cartesian indices that find() now returns to linear indices.
1 parent 5b345ff commit f27f4b8

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ This section lists changes that do not have deprecation warnings.
371371
`AbstractDict`, `AbstractString`, `Tuple` and `NamedTuple` objects ([#24774]).
372372
In particular, this means that it returns `CartesianIndex` objects for matrices
373373
and higher-dimensional arrays instead of linear indices as was previously the case.
374-
Use `Int[LinearIndices(size(a))[i] for i in find(f, a)]` to compute linear indices.
374+
Use `LinearIndices(a)[find(f, a)]` to compute linear indices.
375375

376376
* `AbstractSet` objects are now considered equal by `==` and `isequal` if all of their
377377
elements are equal ([#25368]). This has required changing the hashing algorithm

base/multidimensional.jl

+1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ module IteratorsMD
418418
# AbstractArray implementation
419419
Base.IndexStyle(::Type{LinearIndices{N,R}}) where {N,R} = IndexCartesian()
420420
Base.axes(iter::LinearIndices{N,R}) where {N,R} = iter.indices
421+
Base.size(iter::LinearIndices{N,R}) where {N,R} = length.(iter.indices)
421422
@inline function Base.getindex(iter::LinearIndices{N,R}, I::Vararg{Int, N}) where {N,R}
422423
dims = length.(iter.indices)
423424
#without the inbounds, this is slower than Base._sub2ind(iter.indices, I...)

test/abstractarray.jl

+23-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ end
119119
@test LinearIndices()[1] == 1
120120
@test_throws BoundsError LinearIndices()[2]
121121
@test LinearIndices()[1,1] == 1
122+
@test LinearIndices()[] == 1
123+
@test size(LinearIndices()) == ()
122124
@test CartesianIndices()[1] == CartesianIndex()
123125
@test_throws BoundsError CartesianIndices()[2]
124126
end
@@ -129,6 +131,8 @@ end
129131
@test CartesianIndices((3,))[i] == CartesianIndex(i,)
130132
end
131133
@test LinearIndices((3,))[2,1] == 2
134+
@test LinearIndices((3,))[[1]] == [1]
135+
@test size(LinearIndices((3,))) == (3,)
132136
@test_throws BoundsError CartesianIndices((3,))[2,2]
133137
# ambiguity btw cartesian indexing and linear indexing in 1d when
134138
# indices may be nontraditional
@@ -140,22 +144,39 @@ end
140144
k = 0
141145
cartesian = CartesianIndices((4,3))
142146
linear = LinearIndices(cartesian)
147+
@test size(cartesian) == size(linear) == (4, 3)
143148
for j = 1:3, i = 1:4
144-
@test linear[i,j] == (k+=1)
149+
k += 1
150+
@test linear[i,j] == linear[k] == k
145151
@test cartesian[k] == CartesianIndex(i,j)
146152
@test LinearIndices(0:3,3:5)[i-1,j+2] == k
147153
@test CartesianIndices(0:3,3:5)[k] == CartesianIndex(i-1,j+2)
148154
end
155+
@test linear[linear] == linear
156+
@test linear[vec(linear)] == vec(linear)
157+
@test linear[cartesian] == linear
158+
@test linear[vec(cartesian)] == vec(linear)
159+
@test cartesian[linear] == cartesian
160+
@test cartesian[vec(linear)] == vec(cartesian)
161+
@test cartesian[cartesian] == cartesian
162+
@test cartesian[vec(cartesian)] == vec(cartesian)
149163
end
150164

151165
@testset "3-dimensional" begin
152166
l = 0
153167
for k = 1:2, j = 1:3, i = 1:4
154-
@test LinearIndices((4,3,2))[i,j,k] == (l+=1)
168+
l += 1
169+
@test LinearIndices((4,3,2))[i,j,k] == l
170+
@test LinearIndices((4,3,2))[l] == l
171+
@test CartesianIndices((4,3,2))[i,j,k] == CartesianIndex(i,j,k)
155172
@test CartesianIndices((4,3,2))[l] == CartesianIndex(i,j,k)
156173
@test LinearIndices(1:4,1:3,1:2)[i,j,k] == l
174+
@test LinearIndices(1:4,1:3,1:2)[l] == l
175+
@test CartesianIndices(1:4,1:3,1:2)[i,j,k] == CartesianIndex(i,j,k)
157176
@test CartesianIndices(1:4,1:3,1:2)[l] == CartesianIndex(i,j,k)
158177
@test LinearIndices(0:3,3:5,-101:-100)[i-1,j+2,k-102] == l
178+
@test LinearIndices(0:3,3:5,-101:-100)[l] == l
179+
@test CartesianIndices(0:3,3:5,-101:-100)[i,j,k] == CartesianIndex(i-1, j+2, k-102)
159180
@test CartesianIndices(0:3,3:5,-101:-100)[l] == CartesianIndex(i-1, j+2, k-102)
160181
end
161182

0 commit comments

Comments
 (0)