|
1 | 1 |
|
2 | 2 | """
|
3 | 3 | A statically sized `Int`.
|
4 |
| -Use `Static(N)` instead of `Val(N)` when you want it to behave like a number. |
| 4 | +Use `StaticInt(N)` instead of `Val(N)` when you want it to behave like a number. |
5 | 5 | """
|
6 |
| -struct Static{N} <: Integer |
7 |
| - Static{N}() where {N} = new{N::Int}() |
| 6 | +struct StaticInt{N} <: Integer |
| 7 | + StaticInt{N}() where {N} = new{N::Int}() |
8 | 8 | end
|
9 | 9 |
|
10 |
| -const Zero = Static{0} |
11 |
| -const One = Static{1} |
| 10 | +const Zero = StaticInt{0} |
| 11 | +const One = StaticInt{1} |
12 | 12 |
|
13 |
| -Base.@pure Static(N::Int) = Static{N}() |
14 |
| -Static(N::Integer) = Static(convert(Int, N)) |
15 |
| -Static(::Static{N}) where {N} = Static{N}() |
16 |
| -Static(::Val{N}) where {N} = Static{N}() |
17 |
| -# Base.Val(::Static{N}) where {N} = Val{N}() |
18 |
| -Base.convert(::Type{T}, ::Static{N}) where {T<:Number,N} = convert(T, N) |
19 |
| -Base.convert(::Type{Static{N}}, ::Static{N}) where {N} = Static{N}() |
| 13 | +Base.@pure StaticInt(N::Int) = StaticInt{N}() |
| 14 | +StaticInt(N::Integer) = StaticInt(convert(Int, N)) |
| 15 | +StaticInt(::StaticInt{N}) where {N} = StaticInt{N}() |
| 16 | +StaticInt(::Val{N}) where {N} = StaticInt{N}() |
| 17 | +# Base.Val(::StaticInt{N}) where {N} = Val{N}() |
| 18 | +Base.convert(::Type{T}, ::StaticInt{N}) where {T<:Number,N} = convert(T, N) |
| 19 | +# (::Type{T})(::ArrayInterface.StaticInt{N}) where {T,N} = T(N) |
| 20 | +Base.convert(::Type{StaticInt{N}}, ::StaticInt{N}) where {N} = StaticInt{N}() |
20 | 21 |
|
21 |
| -Base.promote_rule(::Type{<:Static}, ::Type{T}) where {T <: AbstractIrrational} = promote_rule(Int, T) |
22 |
| -Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T <: AbstractIrrational} = promote_rule(T, Int) |
| 22 | +Base.promote_rule(::Type{<:StaticInt}, ::Type{T}) where {T <: AbstractIrrational} = promote_rule(Int, T) |
| 23 | +Base.promote_rule(::Type{T}, ::Type{<:StaticInt}) where {T <: AbstractIrrational} = promote_rule(T, Int) |
23 | 24 | for (S,T) ∈ [(:Complex,:Real), (:Rational, :Integer), (:(Base.TwicePrecision),:Any)]
|
24 |
| - @eval Base.promote_rule(::Type{$S{T}}, ::Type{<:Static}) where {T <: $T} = promote_rule($S{T}, Int) |
| 25 | + @eval Base.promote_rule(::Type{$S{T}}, ::Type{<:StaticInt}) where {T <: $T} = promote_rule($S{T}, Int) |
25 | 26 | end
|
26 |
| -Base.promote_rule(::Type{Union{Nothing,Missing}}, ::Type{<:Static}) = Union{Nothing, Missing, Int} |
27 |
| -Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T >: Union{Missing,Nothing}} = promote_rule(T, Int) |
28 |
| -Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T >: Nothing} = promote_rule(T, Int) |
29 |
| -Base.promote_rule(::Type{T}, ::Type{<:Static}) where {T >: Missing} = promote_rule(T, Int) |
| 27 | +Base.promote_rule(::Type{Union{Nothing,Missing}}, ::Type{<:StaticInt}) = Union{Nothing, Missing, Int} |
| 28 | +Base.promote_rule(::Type{T}, ::Type{<:StaticInt}) where {T >: Union{Missing,Nothing}} = promote_rule(T, Int) |
| 29 | +Base.promote_rule(::Type{T}, ::Type{<:StaticInt}) where {T >: Nothing} = promote_rule(T, Int) |
| 30 | +Base.promote_rule(::Type{T}, ::Type{<:StaticInt}) where {T >: Missing} = promote_rule(T, Int) |
30 | 31 | for T ∈ [:Bool, :Missing, :BigFloat, :BigInt, :Nothing, :Any]
|
31 | 32 | # let S = :Any
|
32 | 33 | @eval begin
|
33 |
| - Base.promote_rule(::Type{S}, ::Type{$T}) where {S <: Static} = promote_rule(Int, $T) |
34 |
| - Base.promote_rule(::Type{$T}, ::Type{S}) where {S <: Static} = promote_rule($T, Int) |
| 34 | + Base.promote_rule(::Type{S}, ::Type{$T}) where {S <: StaticInt} = promote_rule(Int, $T) |
| 35 | + Base.promote_rule(::Type{$T}, ::Type{S}) where {S <: StaticInt} = promote_rule($T, Int) |
35 | 36 | end
|
36 | 37 | end
|
37 |
| -Base.promote_rule(::Type{<:Static}, ::Type{<:Static}) = Int |
38 |
| -Base.:(%)(::Static{N}, ::Type{Integer}) where {N} = N |
| 38 | +Base.promote_rule(::Type{<:StaticInt}, ::Type{<:StaticInt}) = Int |
| 39 | +Base.:(%)(::StaticInt{N}, ::Type{Integer}) where {N} = N |
39 | 40 |
|
40 |
| -Base.eltype(::Type{T}) where {T<:Static} = Int |
| 41 | +Base.eltype(::Type{T}) where {T<:StaticInt} = Int |
41 | 42 | Base.iszero(::Zero) = true
|
42 |
| -Base.iszero(::Static) = false |
| 43 | +Base.iszero(::StaticInt) = false |
43 | 44 | Base.isone(::One) = true
|
44 |
| -Base.isone(::Static) = false |
45 |
| -Base.zero(::Type{T}) where {T<:Static} = Zero() |
46 |
| -Base.one(::Type{T}) where {T<:Static} = One() |
| 45 | +Base.isone(::StaticInt) = false |
| 46 | +Base.zero(::Type{T}) where {T<:StaticInt} = Zero() |
| 47 | +Base.one(::Type{T}) where {T<:StaticInt} = One() |
47 | 48 |
|
48 | 49 | for T = [:Real, :Rational, :Integer]
|
49 | 50 | @eval begin
|
50 | 51 | @inline Base.:(+)(i::$T, ::Zero) = i
|
51 |
| - @inline Base.:(+)(i::$T, ::Static{M}) where {M} = i + M |
| 52 | + @inline Base.:(+)(i::$T, ::StaticInt{M}) where {M} = i + M |
52 | 53 | @inline Base.:(+)(::Zero, i::$T) = i
|
53 |
| - @inline Base.:(+)(::Static{M}, i::$T) where {M} = M + i |
| 54 | + @inline Base.:(+)(::StaticInt{M}, i::$T) where {M} = M + i |
54 | 55 | @inline Base.:(-)(i::$T, ::Zero) = i
|
55 |
| - @inline Base.:(-)(i::$T, ::Static{M}) where {M} = i - M |
| 56 | + @inline Base.:(-)(i::$T, ::StaticInt{M}) where {M} = i - M |
56 | 57 | @inline Base.:(*)(i::$T, ::Zero) = Zero()
|
57 | 58 | @inline Base.:(*)(i::$T, ::One) = i
|
58 |
| - @inline Base.:(*)(i::$T, ::Static{M}) where {M} = i * M |
| 59 | + @inline Base.:(*)(i::$T, ::StaticInt{M}) where {M} = i * M |
59 | 60 | @inline Base.:(*)(::Zero, i::$T) = Zero()
|
60 | 61 | @inline Base.:(*)(::One, i::$T) = i
|
61 |
| - @inline Base.:(*)(::Static{M}, i::$T) where {M} = M * i |
| 62 | + @inline Base.:(*)(::StaticInt{M}, i::$T) where {M} = M * i |
62 | 63 | end
|
63 | 64 | end
|
64 | 65 | @inline Base.:(+)(::Zero, ::Zero) = Zero()
|
65 |
| -@inline Base.:(+)(::Zero, ::Static{M}) where {M} = Static{M}() |
66 |
| -@inline Base.:(+)(::Static{M}, ::Zero) where {M} = Static{M}() |
| 66 | +@inline Base.:(+)(::Zero, ::StaticInt{M}) where {M} = StaticInt{M}() |
| 67 | +@inline Base.:(+)(::StaticInt{M}, ::Zero) where {M} = StaticInt{M}() |
67 | 68 |
|
68 |
| -@inline Base.:(-)(::Static{M}, ::Zero) where {M} = Static{M}() |
| 69 | +@inline Base.:(-)(::StaticInt{M}, ::Zero) where {M} = StaticInt{M}() |
69 | 70 |
|
70 | 71 | @inline Base.:(*)(::Zero, ::Zero) = Zero()
|
71 | 72 | @inline Base.:(*)(::One, ::Zero) = Zero()
|
72 | 73 | @inline Base.:(*)(::Zero, ::One) = Zero()
|
73 | 74 | @inline Base.:(*)(::One, ::One) = One()
|
74 |
| -@inline Base.:(*)(::Static{M}, ::Zero) where {M} = Zero() |
75 |
| -@inline Base.:(*)(::Zero, ::Static{M}) where {M} = Zero() |
76 |
| -@inline Base.:(*)(::Static{M}, ::One) where {M} = Static{M}() |
77 |
| -@inline Base.:(*)(::One, ::Static{M}) where {M} = Static{M}() |
| 75 | +@inline Base.:(*)(::StaticInt{M}, ::Zero) where {M} = Zero() |
| 76 | +@inline Base.:(*)(::Zero, ::StaticInt{M}) where {M} = Zero() |
| 77 | +@inline Base.:(*)(::StaticInt{M}, ::One) where {M} = StaticInt{M}() |
| 78 | +@inline Base.:(*)(::One, ::StaticInt{M}) where {M} = StaticInt{M}() |
78 | 79 | for f ∈ [:(+), :(-), :(*), :(/), :(÷), :(%), :(<<), :(>>), :(>>>), :(&), :(|), :(⊻)]
|
79 |
| - @eval @generated Base.$f(::Static{M}, ::Static{N}) where {M,N} = Expr(:call, Expr(:curly, :Static, $f(M, N))) |
| 80 | + @eval @generated Base.$f(::StaticInt{M}, ::StaticInt{N}) where {M,N} = Expr(:call, Expr(:curly, :StaticInt, $f(M, N))) |
80 | 81 | end
|
81 | 82 | for f ∈ [:(==), :(!=), :(<), :(≤), :(>), :(≥)]
|
82 | 83 | @eval begin
|
83 |
| - @inline Base.$f(::Static{M}, ::Static{N}) where {M,N} = $f(M, N) |
84 |
| - @inline Base.$f(::Static{M}, x::Int) where {M} = $f(M, x) |
85 |
| - @inline Base.$f(x::Int, ::Static{M}) where {M} = $f(x, M) |
| 84 | + @inline Base.$f(::StaticInt{M}, ::StaticInt{N}) where {M,N} = $f(M, N) |
| 85 | + @inline Base.$f(::StaticInt{M}, x::Int) where {M} = $f(M, x) |
| 86 | + @inline Base.$f(x::Int, ::StaticInt{M}) where {M} = $f(x, M) |
86 | 87 | end
|
87 | 88 | end
|
88 | 89 |
|
89 | 90 | @inline function maybe_static(f::F, g::G, x) where {F, G}
|
90 | 91 | L = f(x)
|
91 |
| - isnothing(L) ? g(x) : Static(L) |
| 92 | + isnothing(L) ? g(x) : StaticInt(L) |
92 | 93 | end
|
93 | 94 | @inline static_length(x) = maybe_static(known_length, length, x)
|
94 | 95 | @inline static_first(x) = maybe_static(known_first, first, x)
|
|
0 commit comments