Skip to content

Commit 03ceae5

Browse files
nickrobinson251dlfivefifty
authored andcommitted
Loosen some type constraints Int -> Integer (#83)
1 parent fe14605 commit 03ceae5

File tree

6 files changed

+41
-48
lines changed

6 files changed

+41
-48
lines changed

src/abstractblockarray.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const AbstractBlockVecOrMat{T} = Union{AbstractBlockMatrix{T}, AbstractBlockVect
2424
block2string(b, s) = string(join(map(string,b), '×'), "-blocked ", Base.dims2string(s))
2525
Base.summary(a::AbstractBlockArray) = string(block2string(nblocks(a), size(a)), " ", typeof(a))
2626
_show_typeof(io, a) = show(io, typeof(a))
27-
function Base.summary(io::IO, a::AbstractBlockArray)
27+
function Base.summary(io::IO, a::AbstractBlockArray)
2828
print(io, block2string(nblocks(a), size(a)))
2929
print(io, ' ')
3030
_show_typeof(io, a)
@@ -51,13 +51,12 @@ julia> nblocks(A, 3, 2)
5151
(4, 3)
5252
```
5353
"""
54-
nblocks(block_array::AbstractArray, i::Int) = nblocks(block_array)[i]
54+
nblocks(block_array::AbstractArray, i::Integer) = nblocks(block_array)[i]
5555

56-
nblocks(block_array::AbstractArray, i::Vararg{Int, N}) where {N} =
56+
nblocks(block_array::AbstractArray, i::Vararg{Integer, N}) where {N} =
5757
nblocks(blocksizes(block_array), i...)
5858

5959

60-
6160
"""
6261
Block(inds...)
6362
@@ -154,12 +153,11 @@ julia> A[Block(1, 2)]
154153
5
155154
```
156155
"""
157-
function getblock(A::AbstractBlockArray{T,N}, ::Vararg{Int, N}) where {T,N}
156+
function getblock(A::AbstractBlockArray{T,N}, ::Vararg{Integer, N}) where {T,N}
158157
throw(error("getblock for ", typeof(A), " is not implemented"))
159158
end
160159

161160

162-
163161
"""
164162
getblock!(X, A, inds...)
165163
@@ -182,7 +180,7 @@ julia> x
182180
1.0 1.0
183181
```
184182
"""
185-
getblock!(X, A::AbstractBlockArray{T,N}, ::Vararg{Int, N}) where {T,N} = throw(error("getblock! for ", typeof(A), " is not implemented"))
183+
getblock!(X, A::AbstractBlockArray{T,N}, ::Vararg{Integer, N}) where {T,N} = throw(error("getblock! for ", typeof(A), " is not implemented"))
186184

187185
@inline getblock!(X, A::AbstractBlockArray{T,N}, block::Block{N}) where {T,N} = getblock!(X, A, block.n...)
188186
@inline getblock!(X, A::AbstractBlockVector, block::Block{1}) = getblock!(X, A, block.n[1])
@@ -208,7 +206,7 @@ julia> A
208206
3.0 4.0 │ 0.0
209207
```
210208
"""
211-
setblock!(A::AbstractBlockArray{T,N}, v, ::Vararg{Int, N}) where {T,N} = throw(error("setblock! for ", typeof(A), " is not implemented"))
209+
setblock!(A::AbstractBlockArray{T,N}, v, ::Vararg{Integer, N}) where {T,N} = throw(error("setblock! for ", typeof(A), " is not implemented"))
212210

213211
@inline setblock!(A::AbstractBlockArray{T, N}, v, block::Block{N}) where {T,N} = setblock!(A, v, block.n...)
214212
@inline setblock!(A::AbstractBlockVector, v, block::Block{1}) = setblock!(A, v, block.n[1])
@@ -256,15 +254,15 @@ ERROR: BlockBoundsError: attempt to access 2×2-blocked 2×3 BlockArray{Float64,
256254
[...]
257255
```
258256
"""
259-
@inline function blockcheckbounds(A::AbstractBlockArray{T, N}, i::Vararg{Int, N}) where {T,N}
257+
@inline function blockcheckbounds(A::AbstractBlockArray{T, N}, i::Vararg{Integer, N}) where {T,N}
260258
if blockcheckbounds(Bool, A, i...)
261259
return
262260
else
263261
throw(BlockBoundsError(A, i))
264262
end
265263
end
266264

267-
@inline function blockcheckbounds(::Type{Bool}, A::AbstractBlockArray{T, N}, i::Vararg{Int, N}) where {T,N}
265+
@inline function blockcheckbounds(::Type{Bool}, A::AbstractBlockArray{T, N}, i::Vararg{Integer, N}) where {T,N}
268266
n = nblocks(A)
269267
k = 0
270268
for idx in 1:N # using enumerate here will allocate

src/blockarray.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ end
129129
end
130130

131131

132-
133132
@generated function initialized_blocks_BlockArray(::Type{R}, block_sizes::BlockSizes{N}) where R<:AbstractArray{V,N} where {T,N,V<:AbstractArray{T,N}}
134133
return quote
135134
block_arr = _BlockArray(R, block_sizes)
@@ -208,7 +207,7 @@ julia> blocks = permutedims(reshape([
208207
3ones(2, 3), 4ones(2, 2),
209208
], (2, 2)))
210209
2×2 Array{Array{Float64,2},2}:
211-
[1.0 1.0 1.0] [2.0 2.0]
210+
[1.0 1.0 1.0] [2.0 2.0]
212211
[3.0 3.0 3.0; 3.0 3.0 3.0] [4.0 4.0; 4.0 4.0]
213212
214213
julia> mortar(blocks)
@@ -303,7 +302,7 @@ copy(A::BlockArray) = _BlockArray(copy.(A.blocks), copy(A.block_sizes))
303302
################################
304303
@inline blocksizes(block_array::BlockArray) = block_array.block_sizes
305304

306-
@inline function getblock(block_arr::BlockArray{T,N}, block::Vararg{Int, N}) where {T,N}
305+
@inline function getblock(block_arr::BlockArray{T,N}, block::Vararg{Integer, N}) where {T,N}
307306
@boundscheck blockcheckbounds(block_arr, block...)
308307
block_arr.blocks[block...]
309308
end
@@ -325,13 +324,13 @@ end
325324
_BlockArray(similar(block_array.blocks, Array{T2, N}), copy(blocksizes(block_array)))
326325
end
327326

328-
@inline function Base.getindex(block_arr::BlockArray{T, N}, i::Vararg{Int, N}) where {T,N}
327+
@inline function Base.getindex(block_arr::BlockArray{T, N}, i::Vararg{Integer, N}) where {T,N}
329328
@boundscheck checkbounds(block_arr, i...)
330329
@inbounds v = block_arr[global2blockindex(blocksizes(block_arr), i)]
331330
return v
332331
end
333332

334-
@inline function Base.setindex!(block_arr::BlockArray{T, N}, v, i::Vararg{Int, N}) where {T,N}
333+
@inline function Base.setindex!(block_arr::BlockArray{T, N}, v, i::Vararg{Integer, N}) where {T,N}
335334
@boundscheck checkbounds(block_arr, i...)
336335
@inbounds block_arr[global2blockindex(blocksizes(block_arr), i)] = v
337336
return block_arr
@@ -341,7 +340,7 @@ end
341340
# Indexing #
342341
############
343342

344-
function _check_setblock!(block_arr::BlockArray{T, N}, v, block::NTuple{N, Int}) where {T,N}
343+
function _check_setblock!(block_arr::BlockArray{T, N}, v, block::NTuple{N, Integer}) where {T,N}
345344
for i in 1:N
346345
if size(v, i) != blocksize(block_arr, i, block[i])
347346
throw(DimensionMismatch(string("tried to assign $(size(v)) array to ", blocksize(block_arr, block), " block")))
@@ -350,7 +349,7 @@ function _check_setblock!(block_arr::BlockArray{T, N}, v, block::NTuple{N, Int})
350349
end
351350

352351

353-
@inline function setblock!(block_arr::BlockArray{T, N}, v, block::Vararg{Int, N}) where {T,N}
352+
@inline function setblock!(block_arr::BlockArray{T, N}, v, block::Vararg{Integer, N}) where {T,N}
354353
@boundscheck blockcheckbounds(block_arr, block...)
355354
@boundscheck _check_setblock!(block_arr, v, block)
356355
@inbounds block_arr.blocks[block...] = v

src/blockrange.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ broadcasted(::DefaultArrayStyle{1}, ::typeof(Int), block_range::BlockRange{1}) =
3333
# AbstractArray implementation
3434
axes(iter::BlockRange{N,R}) where {N,R} = map(axes1, iter.indices)
3535
Base.IndexStyle(::Type{BlockRange{N,R}}) where {N,R} = IndexCartesian()
36-
@inline function Base.getindex(iter::BlockRange{N,<:NTuple{N,Base.OneTo}}, I::Vararg{Int, N}) where {N}
36+
@inline function Base.getindex(iter::BlockRange{N,<:NTuple{N,Base.OneTo}}, I::Vararg{Integer, N}) where {N}
3737
@boundscheck checkbounds(iter, I...)
3838
Block(I)
3939
end
40-
@inline function Base.getindex(iter::BlockRange{N,R}, I::Vararg{Int, N}) where {N,R}
40+
@inline function Base.getindex(iter::BlockRange{N,R}, I::Vararg{Integer, N}) where {N,R}
4141
@boundscheck checkbounds(iter, I...)
4242
Block(I .- first.(axes1.(iter.indices)) .+ first.(iter.indices))
4343
end
@@ -58,7 +58,7 @@ end
5858

5959
# increment & carry
6060
@inline inc(::Tuple{}, ::Tuple{}, ::Tuple{}) = ()
61-
@inline inc(state::Tuple{Int}, start::Tuple{Int}, stop::Tuple{Int}) = (state[1]+1,)
61+
@inline inc(state::Tuple{Integer}, start::Tuple{Integer}, stop::Tuple{Integer}) = (state[1]+1,)
6262
@inline function inc(state, start, stop)
6363
if state[1] < stop[1]
6464
return (state[1]+1,tail(state)...)

src/blocksizes.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ end
4949
cumulsizes(block_sizes, i, j+1) - cumulsizes(block_sizes, i, j)
5050

5151
# ntuple with Val was slow here. @generated it is!
52-
@generated function blocksize(block_sizes::AbstractBlockSizes{N}, i::NTuple{N, Int}) where {N}
52+
@generated function blocksize(block_sizes::AbstractBlockSizes{N}, i::NTuple{N, Integer}) where {N}
5353
exp = Expr(:tuple, [:(blocksize(block_sizes, $k, i[$k])) for k in 1:N]...)
5454
return exp
5555
end
@@ -91,7 +91,7 @@ end
9191

9292
_find_block(bs::AbstractVector, i::Integer) = length(bs) > 10 ? last(searchsorted(bs, i)) : searchlinear(bs, i)
9393

94-
@inline function _find_block(block_sizes::AbstractBlockSizes, dim::Int, i::Int)
94+
@inline function _find_block(block_sizes::AbstractBlockSizes, dim::Integer, i::Integer)
9595
bs = cumulsizes(block_sizes, dim)
9696
block = _find_block(bs, i)
9797
@inbounds cum_size = cumulsizes(block_sizes, dim, block) - 1
@@ -105,10 +105,10 @@ end
105105
end
106106
end
107107

108-
@inline @propagate_inbounds nblocks(block_sizes::AbstractBlockSizes, i::Int) =
108+
@inline @propagate_inbounds nblocks(block_sizes::AbstractBlockSizes, i::Integer) =
109109
length(cumulsizes(block_sizes,i)) - 1
110110

111-
function nblocks(block_sizes::AbstractBlockSizes, i::Vararg{Int, N}) where {N}
111+
function nblocks(block_sizes::AbstractBlockSizes, i::Vararg{Integer, N}) where {N}
112112
b = nblocks(block_sizes)
113113
return ntuple(k-> b[i[k]], Val(N))
114114
end
@@ -123,7 +123,7 @@ end
123123
end
124124

125125
# Computes the global range of an Array that corresponds to a given block_index
126-
@generated function globalrange(block_sizes::AbstractBlockSizes{N}, block_index::NTuple{N, Int}) where {N}
126+
@generated function globalrange(block_sizes::AbstractBlockSizes{N}, block_index::NTuple{N, Integer}) where {N}
127127
indices_ex = Expr(:tuple, [:(cumulsizes(block_sizes, $i, block_index[$i]):cumulsizes(block_sizes, $i, block_index[$i] + 1) - 1) for i = 1:N]...)
128128
return quote
129129
$(Expr(:meta, :inline))
@@ -133,26 +133,25 @@ end
133133
end
134134

135135
# I hate having these function definitions but the generated function above sometimes(!) generates bad code and starts to allocate
136-
@inline function globalrange(block_sizes::AbstractBlockSizes{1}, block_index::NTuple{1, Int})
136+
@inline function globalrange(block_sizes::AbstractBlockSizes{1}, block_index::NTuple{1, Integer})
137137
@inbounds v = (cumulsizes(block_sizes, 1, block_index[1]):cumulsizes(block_sizes, 1, block_index[1] + 1) - 1,)
138138
return v
139139
end
140140

141-
@inline function globalrange(block_sizes::AbstractBlockSizes{2}, block_index::NTuple{2, Int})
141+
@inline function globalrange(block_sizes::AbstractBlockSizes{2}, block_index::NTuple{2, Integer})
142142
@inbounds v = (cumulsizes(block_sizes, 1, block_index[1]):cumulsizes(block_sizes, 1, block_index[1] + 1) - 1,
143143
cumulsizes(block_sizes, 2, block_index[2]):cumulsizes(block_sizes, 2, block_index[2] + 1) - 1)
144144
return v
145145
end
146146

147-
@inline function globalrange(block_sizes::AbstractBlockSizes{3}, block_index::NTuple{3, Int})
147+
@inline function globalrange(block_sizes::AbstractBlockSizes{3}, block_index::NTuple{3, Integer})
148148
@inbounds v = (cumulsizes(block_sizes, 1, block_index[1]):cumulsizes(block_sizes, 1, block_index[1] + 1) - 1,
149149
cumulsizes(block_sizes, 2, block_index[2]):cumulsizes(block_sizes, 2, block_index[2] + 1) - 1,
150150
cumulsizes(block_sizes, 3, block_index[3]):cumulsizes(block_sizes, 3, block_index[3] + 1) - 1)
151151
return v
152152
end
153153

154154

155-
156155
"""
157156
blocksizes(A)
158157
@@ -178,10 +177,10 @@ julia> blocksize(A, (2, 1, 3))
178177
(4, 1, 2)
179178
```
180179
"""
181-
@inline blocksize(block_array::AbstractArray, i::Int...) =
180+
@inline blocksize(block_array::AbstractArray, i::Integer...) =
182181
blocksize(blocksizes(block_array), i...)
183182

184-
@inline blocksize(block_array::AbstractArray{T,N}, i::NTuple{N, Int}) where {T, N} =
183+
@inline blocksize(block_array::AbstractArray{T,N}, i::NTuple{N, Integer}) where {T, N} =
185184
blocksize(blocksizes(block_array), i)
186185

187186
@inline Base.size(arr::AbstractBlockArray) = size(blocksizes(arr))

src/pseudo_blockarray.jl

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ julia> A = PseudoBlockArray(rand(2,3), [1,1], [2,1])
3030
3131
julia> A = PseudoBlockArray(sprand(6, 0.5), [3,2,1])
3232
3-blocked 6-element PseudoBlockArray{Float64,1,SparseVector{Float64,Int64},BlockArrays.BlockSizes{1,Array{Int64,1}}}:
33-
0.0
34-
0.5865981007905481
35-
0.0
33+
0.0
34+
0.5865981007905481
35+
0.0
3636
───────────────────
3737
0.05016684053503706
38-
0.0
38+
0.0
3939
───────────────────
40-
0.0
40+
0.0
4141
```
4242
"""
4343
struct PseudoBlockArray{T, N, R<:AbstractArray{T,N}, BS<:AbstractBlockSizes{N}} <: AbstractBlockArray{T, N}
@@ -64,7 +64,6 @@ PseudoBlockArray(blocks::R, block_sizes::Vararg{AbstractVector{Int}, N}) where {
6464
PseudoBlockArray(blocks, Vector{Int}.(block_sizes)...)
6565

6666

67-
6867
@inline function PseudoBlockArray{T}(::UndefInitializer, block_sizes::BlockSizes{N}) where {T, N}
6968
PseudoBlockArray(similar(Array{T, N}, size(block_sizes)), block_sizes)
7069
end
@@ -118,14 +117,14 @@ function Base.similar(block_array::PseudoBlockArray{T,N}, ::Type{T2}) where {T,N
118117
PseudoBlockArray(similar(block_array.blocks, T2), copy(blocksizes(block_array)))
119118
end
120119

121-
@inline function Base.getindex(block_arr::PseudoBlockArray{T, N}, i::Vararg{Int, N}) where {T,N}
120+
@inline function Base.getindex(block_arr::PseudoBlockArray{T, N}, i::Vararg{Integer, N}) where {T,N}
122121
@boundscheck checkbounds(block_arr, i...)
123122
@inbounds v = block_arr.blocks[i...]
124123
return v
125124
end
126125

127126

128-
@inline function Base.setindex!(block_arr::PseudoBlockArray{T, N}, v, i::Vararg{Int, N}) where {T,N}
127+
@inline function Base.setindex!(block_arr::PseudoBlockArray{T, N}, v, i::Vararg{Integer, N}) where {T,N}
129128
@boundscheck checkbounds(block_arr, i...)
130129
@inbounds block_arr.blocks[i...] = v
131130
return block_arr
@@ -147,12 +146,12 @@ end
147146
return v
148147
end
149148

150-
@inline function getblock(block_arr::PseudoBlockArray{T,N}, block::Vararg{Int, N}) where {T,N}
149+
@inline function getblock(block_arr::PseudoBlockArray{T,N}, block::Vararg{Integer, N}) where {T,N}
151150
range = globalrange(blocksizes(block_arr), block)
152151
return block_arr.blocks[range...]
153152
end
154153

155-
@inline function _check_getblock!(blockrange, x, block_arr::PseudoBlockArray{T,N}, block::NTuple{N, Int}) where {T,N}
154+
@inline function _check_getblock!(blockrange, x, block_arr::PseudoBlockArray{T,N}, block::NTuple{N, Integer}) where {T,N}
156155
for i in 1:N
157156
if size(x, i) != length(blockrange[i])
158157
throw(DimensionMismatch(string("tried to assign ", blocksize(block_arr, block), " block to $(size(x)) array")))
@@ -161,7 +160,7 @@ end
161160
end
162161

163162

164-
@generated function getblock!(x, block_arr::PseudoBlockArray{T,N}, block::Vararg{Int, N}) where {T,N}
163+
@generated function getblock!(x, block_arr::PseudoBlockArray{T,N}, block::Vararg{Integer, N}) where {T,N}
165164
return quote
166165
blockrange = globalrange(blocksizes(block_arr), block)
167166
@boundscheck _check_getblock!(blockrange, x, block_arr, block)
@@ -184,7 +183,7 @@ end
184183
return block_arr
185184
end
186185

187-
@inline function _check_setblock!(blockrange, x, block_arr::PseudoBlockArray{T,N}, block::NTuple{N, Int}) where {T,N}
186+
@inline function _check_setblock!(blockrange, x, block_arr::PseudoBlockArray{T,N}, block::NTuple{N, Integer}) where {T,N}
188187
blocksizes = blocksize(block_arr, block)
189188
for i in 1:N
190189
if size(x, i) != blocksizes[i]
@@ -193,7 +192,7 @@ end
193192
end
194193
end
195194

196-
@generated function setblock!(block_arr::PseudoBlockArray{T, N}, x, block::Vararg{Int, N}) where {T,N}
195+
@generated function setblock!(block_arr::PseudoBlockArray{T, N}, x, block::Vararg{Integer, N}) where {T,N}
197196
return quote
198197
blockrange = globalrange(blocksizes(block_arr), block)
199198
@boundscheck _check_setblock!(blockrange, x, block_arr, block)
@@ -208,7 +207,6 @@ end
208207
end
209208

210209

211-
212210
########
213211
# Misc #
214212
########
@@ -246,5 +244,5 @@ end
246244
###########################
247245

248246
Base.strides(A::PseudoBlockArray) = strides(A.blocks)
249-
Base.stride(A::PseudoBlockArray, i::Int) = stride(A.blocks, i)
247+
Base.stride(A::PseudoBlockArray, i::Integer) = stride(A.blocks, i)
250248
Base.unsafe_convert(::Type{Ptr{T}}, A::PseudoBlockArray) where T = Base.unsafe_convert(Ptr{T}, A.blocks)

src/views.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ for f in (:axes, :unsafe_indices, :axes1, :first, :last, :size, :length,
2121
@eval $f(S::BlockSlice) = $f(S.indices)
2222
end
2323

24-
getindex(S::BlockSlice, i::Int) = getindex(S.indices, i)
24+
getindex(S::BlockSlice, i::Integer) = getindex(S.indices, i)
2525
show(io::IO, r::BlockSlice) = print(io, "BlockSlice(", r.block, ",", r.indices, ")")
2626
next(S::BlockSlice, s) = next(S.indices, s)
2727
done(S::BlockSlice, s) = done(S.indices, s)
@@ -36,7 +36,6 @@ function _unblock(cum_sizes, I::Tuple{Block{1, T},Vararg{Any}}) where {T}
3636
end
3737

3838

39-
4039
function _unblock(cum_sizes, I::Tuple{BlockRange{1,R}, Vararg{Any}}) where {R}
4140
B = first(I)
4241
b_start = first(first(B.indices))

0 commit comments

Comments
 (0)