Skip to content

Commit abf38ef

Browse files
authored
Fix potential type instability in unblock, add intersect for BlockRange (#125)
* Fix potential type instability in unblock * intersect for BlockRange * Update views.jl
1 parent 86572bb commit abf38ef

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "0.12.9"
3+
version = "0.12.10"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/BlockArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Base: @propagate_inbounds, Array, to_indices, to_index,
2828
RangeIndex, Int, Integer, Number,
2929
+, -, *, /, \, min, max, isless, in, copy, copyto!, axes, @deprecate,
3030
BroadcastStyle, checkbounds, throw_boundserror,
31-
ones, zeros
31+
ones, zeros, intersect
3232
using Base: ReshapedArray, dataids
3333

3434

src/blockindices.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,6 @@ last(iter::BlockRange) = Block(map(last, iter.indices))
362362
end
363363
_in(b, ::Tuple{}, ::Tuple{}, ::Tuple{}) = b
364364
@inline _in(b, i, start, stop) = _in(b & (start[1] <= i[1] <= stop[1]), tail(i), tail(start), tail(stop))
365+
366+
# We sometimes need intersection of BlockRange to return a BlockRange
367+
intersect(a::BlockRange{1}, b::BlockRange{1}) = BlockRange(intersect(a.indices[1], b.indices[1]))

src/views.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ Returns the indices associated with a block as a `BlockSlice`.
77
"""
88
function unblock(A, inds, I)
99
B = first(I)
10-
if length(inds) == 0
11-
# Allow `ones(2)[Block(1)[1:1], Block(1)[1:1]]` which is
12-
# similar to `ones(2)[1:1, 1:1]`.
13-
BlockSlice(B,Base.OneTo(1))
14-
else
15-
BlockSlice(B,inds[1][B])
16-
end
10+
BlockSlice(B,inds[1][B])
1711
end
12+
# Allow `ones(2)[Block(1)[1:1], Block(1)[1:1]]` which is
13+
# similar to `ones(2)[1:1, 1:1]`.
14+
unblock(A, ::Tuple{}, I) = BlockSlice(first(I),Base.OneTo(1))
1815

1916
to_index(::Block) = throw(ArgumentError("Block must be converted by to_indices(...)"))
2017
to_index(::BlockIndex) = throw(ArgumentError("BlockIndex must be converted by to_indices(...)"))

test/test_blockindices.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
8888
@test Base.OneTo.(1:5) isa Vector{Base.OneTo{Int}} #98
8989
@test Base.OneTo(5)[Block.(1:1)] === Base.OneTo(5)
9090
@test_throws BlockBoundsError Base.OneTo(5)[Block.(1:3)]
91+
92+
@test intersect(Block.(2:5), Block.(3:6)) Block.(3:5)
9193
end
9294
end
9395

0 commit comments

Comments
 (0)