Skip to content

Commit 9c9365b

Browse files
committed
Revert "StaticSymbol and improvements on static"
This reverts commit 2a38f11.
1 parent 274b9b1 commit 9c9365b

File tree

1 file changed

+8
-92
lines changed

1 file changed

+8
-92
lines changed

src/static.jl

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ const One = StaticInt{1}
1414

1515
Base.show(io::IO, ::StaticInt{N}) where {N} = print(io, "static($N)")
1616

17-
@pure StaticInt(N::Int) = StaticInt{N}()
17+
Base.@pure StaticInt(N::Int) = StaticInt{N}()
1818
StaticInt(N::Integer) = StaticInt(convert(Int, N))
1919
StaticInt(::StaticInt{N}) where {N} = StaticInt{N}()
2020
StaticInt(::Val{N}) where {N} = StaticInt{N}()
2121
# Base.Val(::StaticInt{N}) where {N} = Val{N}()
2222
Base.convert(::Type{T}, ::StaticInt{N}) where {T<:Number,N} = convert(T, N)
2323
Base.Bool(x::StaticInt{N}) where {N} = Bool(N)
2424
Base.BigInt(x::StaticInt{N}) where {N} = BigInt(N)
25-
Base.Integer(x::StaticInt{N}) where {N} = x::Int
25+
Base.Integer(x::StaticInt{N}) where {N} = x
2626
(::Type{T})(x::StaticInt{N}) where {T<:Integer,N} = T(N)
2727
(::Type{T})(x::Int) where {T<:StaticInt} = StaticInt(x)
2828
Base.convert(::Type{StaticInt{N}}, ::StaticInt{N}) where {N} = StaticInt{N}()
@@ -262,6 +262,11 @@ Base.any(::Tuple{Vararg{False}}) = false
262262

263263
Base.@pure nstatic(::Val{N}) where {N} = ntuple(i -> StaticInt(i), Val(N))
264264

265+
# I is a tuple of Int
266+
@pure function _val_to_static(::Val{I}) where {I}
267+
return ntuple(i -> StaticInt(getfield(I, i)), Val(length(I)))
268+
end
269+
265270
@pure is_permuting(perm::Tuple{Vararg{StaticInt,N}}) where {N} = perm !== nstatic(Val(N))
266271

267272
permute(x::Tuple, perm::Tuple) = eachop(getindex, x, perm)
@@ -272,7 +277,7 @@ function permute(x::Tuple{Vararg{Any,N}}, perm::Tuple{Vararg{Any,N}}) where {N}
272277
return x
273278
end
274279
end
275-
permute(x::Tuple, perm::Val) = permute(x, static(perm))
280+
permute(x::Tuple, perm::Val) = permute(x, _val_to_static(perm))
276281

277282
@generated function eachop(op, x, y, ::I) where {I}
278283
t = Expr(:tuple)
@@ -361,100 +366,11 @@ IfElse.ifelse(::True, x, y) = x
361366

362367
IfElse.ifelse(::False, x, y) = y
363368

364-
"""
365-
StaticSymbol
366-
367-
A statically typed `Symbol`.
368-
"""
369-
struct StaticSymbol{s}
370-
StaticSymbol{s}() where {s} = new{s::Symbol}()
371-
StaticSymbol(s::Symbol) = new{s}()
372-
end
373-
374-
Base.Symbol(::StaticSymbol{s}) where {s} = s::Symbol
375-
376-
Base.show(io::IO, ::StaticSymbol{s}) where {s} = print(io, "static(:$s)")
377-
378-
is_static(x) = is_static(typeof(x))
379-
is_static(::Type{T}) where {T<:StaticInt} = True()
380-
is_static(::Type{T}) where {T<:StaticBool} = True()
381-
is_static(::Type{T}) where {T<:StaticSymbol} = True()
382-
is_static(::Type{T}) where {T} = False()
383-
384-
function _no_static_type(@nospecialize(x))
385-
error("There is no static alternative for type $(typeof(x)).")
386-
end
387-
388369
"""
389370
static(x)
390371
391372
Returns a static form of `x`.
392373
"""
393-
function static(x)
394-
if is_static(x)
395-
return x
396-
else
397-
_no_static_type(x)
398-
end
399-
end
400374
static(x::Int) = StaticInt(x)
401375
static(x::Bool) = StaticBool(x)
402-
static(x::Symbol) = StaticSymbol(x)
403-
static(x::Tuple{Vararg{Any}}) = map(static, x)
404-
@generated static(::Val{V}) where {V} = :($(static(V)))
405-
406-
407-
#=
408-
static_issubset
409-
410-
A version of `issubset` sepecifically for `Tuple`s of static types, that is generated at
411-
compile time.
412-
=#
413-
@generated function static_issubset(::L, ::R) where {L<:Tuple,R<:Tuple}
414-
N = length(L.parameters)
415-
R = length(R.parameters)
416-
if N <+ M
417-
return :(ArrayInterface.False())
418-
else
419-
for l in L.parameters
420-
found = false
421-
for b in rhs
422-
found |= a === b
423-
end
424-
found || return :(ArrayInterface.False())
425-
end
426-
return :(ArrayInterface.True())
427-
end
428-
end
429-
430-
#=
431-
static_find_first_eq(::EQ, ::T) -> StaticInt
432-
433-
Finds the position in the tuple `T` that is exactly equal to `EQ`. If `EQ` is not found
434-
then `Zero()` is returned.
435-
=#
436-
@generated function static_find_first_eq(::EQ, ::T) where {T<:Tuple}
437-
loop = true
438-
i = 1
439-
out = 0
440-
while loop
441-
p = T.parameters[i]
442-
if p === EQ
443-
out = i
444-
loop = false
445-
else
446-
i += 1
447-
end
448-
end
449-
return :(StaticInt($i))
450-
end
451-
452-
#=
453-
static_find_all_in(x::Tuple, collection::Tuple) -> StaticInt
454-
455-
Finds the position in the tuple `collection` that is exactly equal to each element of `x`.
456-
=#
457-
@inline function static_find_all_in(x::Tuple{Vararg{Any,N}}, collection::Tuple) where {N}
458-
return ntuple(i -> static_find_first_eq(getfield(x, i), collection), Val(N))
459-
end
460376

0 commit comments

Comments
 (0)