Skip to content

Commit

Permalink
Remove all @pure, fixes #115.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Feb 3, 2021
1 parent 854a4b8 commit e97dde5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
deps/deps.jl
Manifest.toml
*~
*#*
*#*#
4 changes: 2 additions & 2 deletions src/ArrayInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ using Requires
using LinearAlgebra
using SparseArrays

using Base: @pure, @propagate_inbounds, tail, OneTo, LogicalIndex, Slice, ReinterpretArray
using Base: @propagate_inbounds, tail, OneTo, LogicalIndex, Slice, ReinterpretArray

Base.@pure __parameterless_type(T) = Base.typename(T).wrapper
@generated __parameterless_type(::Type{T}) where {T} = Base.typename(T).wrapper
parameterless_type(x) = parameterless_type(typeof(x))
parameterless_type(x::Type) = __parameterless_type(x)

Expand Down
31 changes: 17 additions & 14 deletions src/dimensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,35 @@ to_dims(x::Tuple{Vararg{Symbol}}, d::Colon) = d # `:` is the default for most
end
return i
end
Base.@pure function _sym_to_dim(x::Tuple{Vararg{Symbol,N}}, sym::Symbol) where {N}
for i in 1:N
getfield(x, i) === sym && return i
@generated function _sym_to_dim(x::Tuple{Vararg{Symbol,N}}, sym::Symbol) where {N}
quote
$(Expr(:meta,:inline))
Base.Cartesian.@nexprs $N n -> getfield(x, n) === sym && return n
return 0
end
return 0
end

"""
tuple_issubset
A version of `issubset` sepecifically for `Tuple`s of `Symbol`s, that is `@pure`.
This helps it get optimised out of existance. It is less of an abuse of `@pure` than
most of the stuff for making `NamedTuples` work.
A version of `issubset` sepecifically for `Tuple`s of `Symbol`s.
"""
Base.@pure function tuple_issubset(
@generated function tuple_issubset(
lhs::Tuple{Vararg{Symbol,N}}, rhs::Tuple{Vararg{Symbol,M}}
) where {N,M}
N <= M || return false
for a in lhs
found = false
for b in rhs
found |= a === b
quote
$(Expr(:meta,:inline))
Base.Cartesian.@nexprs $N n -> begin
a = lhs[n]
found = false
Base.Cartesian.@nexprs $M m -> begin
found |= a === rhs[m]
end
found || return false
end
found || return false
return true
end
return true
end

"""
Expand Down
12 changes: 6 additions & 6 deletions src/static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const One = StaticInt{1}

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

Base.@pure StaticInt(N::Int) = StaticInt{N}()
@inline StaticInt(N::Int) = StaticInt{N}()
StaticInt(N::Integer) = StaticInt(convert(Int, N))
StaticInt(::StaticInt{N}) where {N} = StaticInt{N}()
StaticInt(::Val{N}) where {N} = StaticInt{N}()
Expand Down Expand Up @@ -195,7 +195,7 @@ Base.:(&)(x::Bool, y::StaticBool) = x & Bool(y)
Base.:(&)(x::StaticBool, y::Bool) = Bool(x) & y

Base.xor(y::StaticBool, x::StaticBool) = _xor(x, y)
_xor(::True, ::True) = False()
p_xor(::True, ::True) = False()
_xor(::True, ::False) = True()
_xor(::False, ::True) = True()
_xor(::False, ::False) = False()
Expand Down Expand Up @@ -250,7 +250,7 @@ Base.promote_rule(::Type{<:StaticBool}, ::Type{<:StaticBool}) = StaticBool
Base.promote_rule(::Type{<:StaticBool}, ::Type{Bool}) = Bool
Base.promote_rule(::Type{Bool}, ::Type{<:StaticBool}) = Bool

Base.@pure _get_tuple(::Type{T}, ::StaticInt{i}) where {T<:Tuple, i} = T.parameters[i]
@generated _get_tuple(::Type{T}, ::StaticInt{i}) where {T<:Tuple, i} = T.parameters[i]

Base.all(::Tuple{Vararg{True}}) = true
Base.all(::Tuple{Vararg{Union{True,False}}}) = false
Expand All @@ -260,14 +260,14 @@ Base.any(::Tuple{Vararg{True}}) = true
Base.any(::Tuple{Vararg{Union{True,False}}}) = true
Base.any(::Tuple{Vararg{False}}) = false

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

# I is a tuple of Int
@pure function _val_to_static(::Val{I}) where {I}
@inline function _val_to_static(::Val{I}) where {I}
return ntuple(i -> StaticInt(getfield(I, i)), Val(length(I)))
end

@pure is_permuting(perm::Tuple{Vararg{StaticInt,N}}) where {N} = perm !== nstatic(Val(N))
@inline is_permuting(perm::Tuple{Vararg{StaticInt,N}}) where {N} = perm !== nstatic(Val(N))

permute(x::Tuple, perm::Tuple) = eachop(getindex, x, perm)
function permute(x::Tuple{Vararg{Any,N}}, perm::Tuple{Vararg{Any,N}}) where {N}
Expand Down
8 changes: 6 additions & 2 deletions src/stridelayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ function contiguous_axis_indicator(::Type{A}) where {D,A<:AbstractArray{<:Any,D}
end
contiguous_axis_indicator(::A) where {A<:AbstractArray} = contiguous_axis_indicator(A)
contiguous_axis_indicator(::Nothing, ::Val) = nothing
Base.@pure function contiguous_axis_indicator(::StaticInt{N}, ::Val{D}) where {N,D}
return ntuple(d -> StaticBool(d === N), Val{D}())
@generated function contiguous_axis_indicator(::StaticInt{N}, ::Val{D}) where {N,D}
t = Expr(:tuple)
for d in 1:D
push!(t.args, Expr(:call, d == N ? :True : :False))
end
t
end

function rank_to_sortperm(R::Tuple{Vararg{StaticInt,N}}) where {N}
Expand Down

0 comments on commit e97dde5

Please sign in to comment.