Skip to content

Commit afb7b24

Browse files
committed
static slicing
1 parent 5523233 commit afb7b24

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/static.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ or call `static_slice(A, sizes, false)` to omit the reshape.
1515
IN = length(tup)
1616
for d in 1:IN
1717
size(A,d) == tup[d] || throw(DimensionMismatch("cannot slice array of size $(size(A)) using Size$tup"))
18+
first(axes(A,d)) == 1 || throw(ArgumentError("cannot creat static slices with offset indices"))
1819
end
1920
IT = SArray{Tuple{tup...}, T, IN, prod(tup)}
20-
if N-IN>1 && finalshape
21+
@assert finalshape
22+
# if N-IN>1 && finalshape
2123
finalaxes = axes(A)[1+IN:end]
2224
reshape(reinterpret(IT, vec(A)), finalaxes)
23-
else
24-
reinterpret(IT, vec(A)) # always a vector
25-
end
25+
# else
26+
# reinterpret(IT, vec(A)) # always a vector
27+
# end
2628
end
2729

2830
function static_slice(A::AbstractArray, code::Tuple, finalshape::Bool=true)

test/four.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ end
9393

9494
# reduction
9595
@reduce C[_,j] := sum(i) A[i,j]
96-
@test axes(C) == (1:1, 7:15) # but perhaps the wrong answer!
96+
@test axes(C) == (1:1, 7:15)
97+
@test extrema(C) == (141, 301) # was reading out of bounds, TransmuteDims bug
9798

9899
# slicing
99100
@test axes(@cast _[j] := A[:,j]) == (7:15,)
@@ -102,10 +103,12 @@ end
102103
@test axes(first(@cast _[j][i] := A[i,j])) == (0:1,)
103104

104105
using StaticArrays
105-
@test_broken axes(@cast _[j] := A{:,j}) == (7:15,) # drops offset from container?
106-
@test first(@cast _[j] := A{:,j}) === SVector(70,71) # ignores offset, to make static slice?
107-
@test first(@cast _[j] := A{:2,j}) === SVector(70,71)
108-
# note that reinterpret(reshape, SVector{2,Int}, A) gives an error, but it only insists on 1st axis
106+
@test_throws Exception @cast _[j] := A{:,j} # similar error to reinterpret(reshape, SVector{2,Int}, A)
107+
@cast D[i,j] := i+10j (i in 1:3, j in 7:15)
108+
@test axes(@cast _[j] := D{:,j}) == (7:15,)
109+
@test first(@cast _[j] := D{:,j}) === SVector(71, 72, 73)
110+
@test first(@cast _[j] := D{:3,j}) === SVector(71, 72, 73)
111+
@test_throws Exception @cast _[j] := D{:2,j} # wrong size
109112
end
110113

111114
@testset "tuples" begin

0 commit comments

Comments
 (0)