|
1 | 1 | # `Start` and `End` should be `Int`
|
2 |
| -struct SUnitRange{Start, End, L} <: StaticVector{L, Int} |
3 |
| - function SUnitRange{Start, End, L}() where {Start, End, L} |
4 |
| - check_sunitrange_params(Start, End, L) |
5 |
| - new{Start, End, L}() |
| 2 | +struct SUnitRange{Start, L} <: StaticVector{L, Int} |
| 3 | + function SUnitRange{Start, L}() where {Start, L} |
| 4 | + check_sunitrange_params(L) |
| 5 | + new{Start, L}() |
6 | 6 | end
|
7 | 7 | end
|
8 | 8 |
|
9 |
| -@pure function check_sunitrange_params(a::Int, b::Int, c::Int) |
10 |
| - if max(0, b - a + 1) != c |
11 |
| - throw(DimensionMismatch("Static unit range $a:$b does not have length $c")) |
| 9 | +@pure function check_sunitrange_params(L::Int) |
| 10 | + if L < 0 |
| 11 | + error("Static unit range length is negative") |
12 | 12 | end
|
13 | 13 | end
|
14 | 14 |
|
15 |
| -function check_sunitrange_params(a, b, c) |
| 15 | +function check_sunitrange_params(L) |
16 | 16 | throw(TypeError(:SUnitRange, "type parameters must be `Int`s", Tuple{Int, Int, Int}, Tuple{typeof(a), typeof(b), typeof(c)}))
|
17 | 17 | end
|
18 | 18 |
|
19 |
| -@pure SUnitRange{Start, End}() where {Start, End} = SUnitRange{Start, End, max(0, End - Start + 1)}() |
20 |
| -@pure SUnitRange(a::Int, b::Int) = SUnitRange{a, b}() |
| 19 | +@pure SUnitRange(a::Int, b::Int) = SUnitRange{a, max(0, b - a + 1)}() |
21 | 20 |
|
22 |
| -@pure @propagate_inbounds function getindex(x::SUnitRange{Start,End}, i::Int) where {Start, End} |
23 |
| - @boundscheck if i < Start || i > End |
| 21 | +@pure @propagate_inbounds function getindex(x::SUnitRange{Start, L}, i::Int) where {Start, L} |
| 22 | + @boundscheck if i < Start || i >= (Start + L) |
24 | 23 | throw(BoundsError(x, i))
|
25 | 24 | end
|
26 |
| - return i |
| 25 | + return Start + i - 1 |
| 26 | +end |
| 27 | + |
| 28 | +# Shorten show for REPL use. |
| 29 | +show(io::IO, ::Type{SUnitRange}) = print(io, "SUnitRange") |
| 30 | +function show(io::IO, ::MIME"text/plain", ::SUnitRange{Start, L}) where {Start, L} |
| 31 | + print(io, "SUnitRange($Start,$(Start + L - 1))") |
27 | 32 | end
|
0 commit comments