Skip to content

Commit d7d8640

Browse files
committed
Better support for unitful numbers
1 parent 099eafc commit d7d8640

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/blockaxis.jl

+17-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function findblockindex(b::AbstractVector, k::Integer)
1616
bl = blocklasts(b)
1717
blockidx = _searchsortedfirst(bl, k)
1818
@assert blockindex != lastindex(bl) + 1 # guaranteed by the @boundscheck above
19-
prevblocklast = blockidx == firstindex(bl) ? first(b)-1 : bl[blockidx-1]
19+
prevblocklast = blockidx == firstindex(bl) ? first(b)-oneunit(eltype(b)) : bl[blockidx-1]
2020
local_index = k - prevblocklast
2121
return BlockIndex(blockidx, local_index)
2222
end
@@ -60,8 +60,8 @@ end
6060

6161
const DefaultBlockAxis = BlockedUnitRange{Int,Vector{Int}}
6262

63-
@inline _BlockedUnitRange(cs::AbstractVector) = _BlockedUnitRange(one(eltype(cs)), cs)
64-
@inline _BlockedUnitRange(cs::NTuple) = _BlockedUnitRange(one(eltype(cs)), cs)
63+
@inline _BlockedUnitRange(cs::AbstractVector) = _BlockedUnitRange(oneunit(eltype(cs)), cs)
64+
@inline _BlockedUnitRange(cs::NTuple) = _BlockedUnitRange(oneunit(eltype(cs)), cs)
6565
_BlockedUnitRange(cs::Tuple) = _BlockedUnitRange(promote(cs...))
6666
_BlockedUnitRange(f, cs::Tuple) = _BlockedUnitRange(f, promote(cs...))
6767

@@ -74,21 +74,21 @@ _blocklengths2blocklasts(blocks::Fill) = cumsum(blocks)
7474
_blocklengths2blocklasts(blocks::Ones) = cumsum(blocks)
7575
@inline blockedrange(blocks::Union{Tuple,AbstractVector}) = _BlockedUnitRange(_blocklengths2blocklasts(blocks))
7676

77-
@inline blockfirsts(a::BlockedUnitRange) = [first(a); @views(blocklasts(a)[1:end-1]) .+ 1]
77+
@inline blockfirsts(a::BlockedUnitRange) = [first(a); @views(blocklasts(a)[1:end-1]) .+ oneunit(eltype(a))]
7878
# optimize common cases
7979
@inline function blockfirsts(a::BlockedUnitRange{<:Union{Vector, RangeCumsum{<:Any, <:UnitRange}}})
8080
v = Vector{eltype(a)}(undef, length(blocklasts(a)))
8181
v[1] = first(a)
82-
v[2:end] .= @views(blocklasts(a)[oneto(end-1)]) .+ 1
82+
v[2:end] .= @views(blocklasts(a)[oneto(end-1)]) .+ oneunit(eltype(a))
8383
return v
8484
end
8585
@inline blocklasts(a::BlockedUnitRange) = a.lasts
8686

8787
_diff(a::AbstractVector) = diff(a)
8888
_diff(a::Tuple) = diff(collect(a))
89-
@inline blocklengths(a::BlockedUnitRange) = isempty(blocklasts(a)) ? [_diff(blocklasts(a));] : [first(blocklasts(a))-first(a)+one(eltype(a)); _diff(blocklasts(a))]
89+
@inline blocklengths(a::BlockedUnitRange) = isempty(blocklasts(a)) ? [_diff(blocklasts(a));] : [first(blocklasts(a))-first(a)+oneunit(eltype(a)); _diff(blocklasts(a))]
9090

91-
length(a::BlockedUnitRange) = isempty(blocklasts(a)) ? zero(eltype(a)) : Integer(last(blocklasts(a))-first(a)+one(eltype(a)))
91+
length(a::BlockedUnitRange) = isempty(blocklasts(a)) ? zero(eltype(a)) : Integer(last(blocklasts(a))-first(a)+oneunit(eltype(a)))
9292

9393
"""
9494
blockisequal(a::AbstractUnitRange{Int}, b::AbstractUnitRange{Int})
@@ -257,11 +257,11 @@ julia> blocksizes(A,2)
257257
blocksizes(A) = map(blocklengths, axes(A))
258258
blocksizes(A,i) = blocklengths(axes(A,i))
259259

260-
axes(b::BlockedUnitRange) = (_BlockedUnitRange(blocklasts(b) .- (first(b)-1)),)
260+
axes(b::BlockedUnitRange) = (_BlockedUnitRange(blocklasts(b) .- (first(b)-oneunit(eltype(b)))),)
261261
unsafe_indices(b::BlockedUnitRange) = axes(b)
262262
first(b::BlockedUnitRange) = b.first
263263
# ::Integer works around case where blocklasts might return different type
264-
last(b::BlockedUnitRange)::Integer = isempty(blocklasts(b)) ? first(b)-1 : last(blocklasts(b))
264+
last(b::BlockedUnitRange) = isempty(blocklasts(b)) ? first(b)-oneunit(eltype(b)) : last(blocklasts(b))
265265

266266
# view and indexing are identical for a unitrange
267267
Base.view(b::BlockedUnitRange, K::Block{1}) = b[K]
@@ -273,24 +273,24 @@ Base.view(b::BlockedUnitRange, K::Block{1}) = b[K]
273273
@boundscheck K in bax || throw(BlockBoundsError(b, k))
274274
S = first(bax)
275275
K == S && return first(b):first(cs)
276-
return cs[k-1]+one(eltype(b)):cs[k]
276+
return cs[k-1]+oneunit(eltype(b)):cs[k]
277277
end
278278

279279
@propagate_inbounds function getindex(b::BlockedUnitRange, KR::BlockRange{1})
280280
cs = blocklasts(b)
281-
isempty(KR) && return _BlockedUnitRange(one(eltype(b)),cs[1:0])
281+
isempty(KR) && return _BlockedUnitRange(oneunit(eltype(b)),cs[1:0])
282282
K,J = first(KR),last(KR)
283283
k,j = Integer(K),Integer(J)
284284
bax = blockaxes(b,1)
285285
@boundscheck K in bax || throw(BlockBoundsError(b,K))
286286
@boundscheck J in bax || throw(BlockBoundsError(b,J))
287287
K == first(bax) && return _BlockedUnitRange(first(b),cs[k:j])
288-
_BlockedUnitRange(cs[k-1]+one(eltype(b)),cs[k:j])
288+
_BlockedUnitRange(cs[k-1]+oneunit(eltype(b)),cs[k:j])
289289
end
290290

291291
@propagate_inbounds function getindex(b::BlockedUnitRange, KR::BlockRange{1,Tuple{Base.OneTo{Int}}})
292292
cs = blocklasts(b)
293-
isempty(KR) && return _BlockedUnitRange(one(eltype(b)),cs[Base.OneTo(0)])
293+
isempty(KR) && return _BlockedUnitRange(oneunit(eltype(b)),cs[Base.OneTo(0)])
294294
J = last(KR)
295295
j = Integer(J)
296296
bax = blockaxes(b,1)
@@ -422,7 +422,7 @@ julia> blocklengths(b)
422422
3
423423
```
424424
"""
425-
blocklengths(a::AbstractUnitRange{<:Integer}) = blocklasts(a) .- blockfirsts(a) .+ one(eltype(a))
425+
blocklengths(a::AbstractUnitRange{<:Integer}) = blocklasts(a) .- blockfirsts(a) .+ oneunit(eltype(a))
426426

427427
Base.summary(a::BlockedUnitRange) = _block_summary(a)
428428
Base.summary(io::IO, a::BlockedUnitRange) = _block_summary(io, a)
@@ -463,13 +463,13 @@ end
463463
function blockfirsts(a::BlockedUnitRange{<:Integer, <:AbstractRange})
464464
st = step(blocklasts(a))
465465
first(a) == 1 || error("Offset axes not supported")
466-
@assert first(blocklasts(a))-first(a)+1 == st
467-
range(one(eltype(a)); step=st, length=eltype(a)(length(blocklasts(a))))
466+
@assert first(blocklasts(a))-first(a)+oneunit(eltype(a)) == st
467+
range(oneunit(eltype(a)); step=st, length=eltype(a)(length(blocklasts(a))))
468468
end
469469
function blocklengths(a::BlockedUnitRange{<:Integer, <:AbstractRange})
470470
st = step(blocklasts(a))
471471
first(a) == 1 || error("Offset axes not supported")
472-
@assert first(blocklasts(a))-first(a)+1 == st
472+
@assert first(blocklasts(a))-first(a)+oneunit(eltype(a)) == st
473473
Fill(st,length(blocklasts(a)))
474474
end
475475

0 commit comments

Comments
 (0)