Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed Nov 28, 2024
1 parent 5a89a88 commit c12124f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Exts"
uuid = "0b12d779-4123-4875-9d6c-e33c2e29e2c9"
authors = ["Heptazhou <zhou at 0h7z dot com>"]
version = "0.2.6"
version = "0.2.7"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand Down
13 changes: 9 additions & 4 deletions src/BaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,22 @@ end
let UT = Union{Union, DataType}, UA = UnionAll,
UU = Union{Union, UA}, TT = NTuple{2, Type}
#! format: noindent
@inline rewrap(r::Type, ::UU) = r
@inline rewrap(T::DataType, U::UA)::UA = rewrap_unionall(T, U)
@inline unwrap(r::Union) = r
@inline unwrap(T::UA)::UT = unwrap_unionall(T)
@inline iter_t(U::UA, T::Type, ::Nothing) = nothing
@inline iter_t(U::UA, T::Type, x::Tuple) = rewrap(x[1], U)::Any, (T, x[2]::Int)
@inline rewrap(r::Any, ::UU) = r
@inline rewrap(T::DataType, U::UA)::Type = rewrap_unionall(T, U)
@inline unwrap(r::Union) = r
@inline unwrap(T::UA)::UT = unwrap_unionall(T)

@inline Base.iterate(::TypeofBottom) = nothing
@inline Base.iterate(::UA, ::DataType) = nothing
@inline Base.iterate(::UA, ::TypeofBottom) = nothing # fix ambiguity on v1.9
@inline Base.iterate(::UU, ::TypeofBottom) = nothing
@inline Base.iterate(::UU, T::Type) = T, Bottom
@inline Base.iterate(T::DataType, i::Int = 1) = iterate(T.parameters, i)
@inline Base.iterate(T::UU) = iterate(T, unwrap(T)::UT)
@inline Base.iterate(U::UA, T::Type{<:Tuple}) = iter_t(U, T, iterate(T))
@inline Base.iterate(U::UA, x::Tuple) = iter_t(U, x[1], iterate(x[1], x[2]))
@inline Base.iterate(U::UU, T::Union)::TT = rewrap(T.a, U), rewrap(T.b, U)
end

Expand Down
6 changes: 3 additions & 3 deletions src/Function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ function ext(x::Symbol)::Maybe{Module}
end

"""
freeze(d::AbstractDict{K, V}) where {K, V} -> LDict{K, V}
freeze(d::AbstractDict{K, V}) where {K, V} -> LDict{<:K, <:V}
Render a dictionary immutable by converting it to a `Tuple`-backed
[`LittleDict`](@ref LDict). The `Tuple`-backed `LittleDict` is faster than
the `Vector`-backed `LittleDict`, particularly when the keys are all
concretely typed.
"""
function freeze(d::AbstractDict{K, V})::FrozenLittleDict{K, V} where {K, V}
OrderedCollections.freeze(d)
function freeze(d::AbstractDict{K, V})::AbstractDict where {K, V}
OrderedCollections.freeze(d)::FrozenLittleDict{<:K, <:V}
end

"""
Expand Down
14 changes: 6 additions & 8 deletions src/OrderedCollectionsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

module OrderedCollectionsExt

using Base: isbitsunion, unwrap_unionall
using Base: isbitsunion
using Exts: ODict
using OrderedCollections: OrderedCollections, _tablesz, hashindex, rehash!

@static if [unwrap_unionall(methods(rehash!, (ODict, Int))[1].sig)...][3] == Any
# COV_EXCL_START
@static if [only(methods(rehash!, (ODict, Int))).sig...][3] == Any
#! format: noindent
function OrderedCollections.rehash!(h::ODict{K, V}, newsz::Integer) where {K, V}
olds = h.slots
Expand Down Expand Up @@ -63,11 +64,9 @@ function OrderedCollections.rehash!(h::ODict{K, V}, newsz::Integer) where {K, V}
end
if !isdeleted
index0 = index = (hashk & (newsz - 1)) + 1
# COV_EXCL_START
while slots[index] != 0
index = (index & (newsz - 1)) + 1
end
# COV_EXCL_STOP
probe = (index - index0) & (newsz - 1)
probe > maxprobe && (maxprobe = probe)
slots[index] = to
Expand All @@ -76,7 +75,7 @@ function OrderedCollections.rehash!(h::ODict{K, V}, newsz::Integer) where {K, V}
to += 1
end
if h.ndel != ndel0
return rehash!(h, newsz) # COV_EXCL_LINE
return rehash!(h, newsz)
end
end
end
Expand All @@ -87,16 +86,14 @@ function OrderedCollections.rehash!(h::ODict{K, V}, newsz::Integer) where {K, V}
@inbounds for i 1:count0
k = keys[i]
index0 = index = hashindex(k, newsz)
# COV_EXCL_START
while slots[index] != 0
index = (index & (newsz - 1)) + 1
end
# COV_EXCL_STOP
probe = (index - index0) & (newsz - 1)
probe > maxprobe && (maxprobe = probe)
slots[index] = i
if h.ndel > 0
return rehash!(h, newsz) # COV_EXCL_LINE
return rehash!(h, newsz)
end
end
end
Expand All @@ -105,6 +102,7 @@ function OrderedCollections.rehash!(h::ODict{K, V}, newsz::Integer) where {K, V}
return h
end
end
# COV_EXCL_STOP

end # module

28 changes: 23 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ end
@test_throws MethodError collect(Tuple{})
@test_throws MethodError convert(Set, 1:3)
@test_throws MethodError convert(Set{Int}, 1:3)
@test_throws MethodError first(only(methods(identity)).sig)
@test_throws MethodError log10(11, 2)
@test_throws MethodError ntuple(2, 1)
@test_throws MethodError sprint(display, [:_, -1]')
Expand All @@ -193,7 +194,7 @@ end
@test ['1' '2'] == ['1', '2']'
@test ["x" "y"] == ["x", "y"]'
@test [(r"^") (r"$")] == [(r"^"), (r"$")]'
@test [a_ua_tuple::UnionAll...] == Any[]
@test [a_ua_tuple::UnionAll...] == UnionAll[Vector, Matrix, Array{T, 3} where T]
@test [a_ua_union::UnionAll...] == UnionAll[Vector, Matrix, Array{T, 3} where T]
@test [AbstractMatrix...] == [Vector...] == Any[]
@test [AbstractMatrix{UInt8}...] == Any[UInt8, 2]
Expand Down Expand Up @@ -224,8 +225,9 @@ end
@test dropnothing(x for x a3_nothing) == dropnothing(a3_nothing) == []
@test ensure_vector(a3_nothing) isa AbstractVector{<:AbstractMatrix{Nothing}}
@test flatten(rand(UInt8, 3, 3, 3)::Array{UInt8, 3}) isa Vector{UInt8}
@test freeze(Dict(1 => 2)) isa FrozenLittleDict{Int, Int}
@test freeze(Dict(1 => 2)) isa LDict{Int, Int}
@test freeze(ODict(1 => 0)) isa LDict{Int, Int}
@test freeze(ODict{Maybe{UInt}, Datum{Int}}(1 => 10)) isa LDict{UInt, Int}
@test freeze(ODict{Maybe{UInt}, Datum{Int}}(1 => missing)) isa LDict{UInt, Missing}
@test getfirst(iseven, 1:9) == getfirst(iseven)(1:9) == 2
@test getlast(iseven, 1:9) == getlast(iseven)(1:9) == 8
@test invsqrt(2^-2) == 2
Expand All @@ -252,16 +254,24 @@ end
@test polar(1, 180) === (180) == -(1.0)
@test polar(1, 360) === (000) == +(1.0)
@test polar(1, rad2deg(1)) === (rad2deg(1)) === Exts.cis(1)
@test return_type(invsqrt, ntuple(1, Any)) == AbstractFloat
@test return_type(invsqrt, (Any,)) == AbstractFloat
@test return_type(iterate, (DataType, Int)) == Maybe{Tuple{Any, Int}}
@test return_type(iterate, (TypeofBottom,)) == Nothing
@test return_type(iterate, (Union, DataType)) == Tuple{DataType, TypeofBottom}
@test return_type(iterate, (Union, TypeofBottom)) == Nothing
@test return_type(iterate, (Union, Union)) == NTuple{2, Type}
@test return_type(iterate, (Union, UnionAll)) == Tuple{UnionAll, TypeofBottom}
@test return_type(iterate, (UnionAll, Type{Any})) == Nothing
@test return_type(iterate, (UnionAll, TypeofBottom)) == Nothing
@test return_type(log10, ntuple(2, Any)) == NTuple{2, AbstractFloat}
@test stdpath("...") == "..."
@test stdpath("..") == "../"
@test stdpath(".") == "./"
@test stdpath("") == ""
@test Tuple === Tuple{Vararg{Any}} === VTuple{Any}
@test typeof(identity) === first(only(methods(identity)).sig)
@test_throws ArgumentError notmissing(missing)
@test_throws ArgumentError notnothing(nothing)
@test_throws MethodError freeze(Dict())

@test Bool(0) == Exts.isdir("")
@test Bool(1) == Exts.isdir(".")
Expand All @@ -288,6 +298,14 @@ end
@test S":" === :(:)
end

@static if VERSION < v"1.10"
@test freeze(Dict()) isa LittleDict{Bottom, Bottom}
@test return_type(freeze, (Any,)) == LittleDict
else
@test return_type(freeze, (Any,)) == FrozenLittleDict
@test_throws MethodError freeze(Dict())
end

fi, i = mktemp()
fo, o = mktemp()
redirect_stdio(stdin = i, stdout = o) do
Expand Down

0 comments on commit c12124f

Please sign in to comment.