Skip to content

Commit

Permalink
Merge pull request #9 from JuliaCollections/constructors
Browse files Browse the repository at this point in the history
More constructors for `PropertyDict`
  • Loading branch information
Tokazama authored Sep 25, 2022
2 parents 4f28c32 + 22b354b commit 3565839
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 128 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PropertyDicts"
uuid = "f8a19df8-e894-5f55-a973-672c1158cbca"
license = "MIT"
version = "0.2"
version = "0.2.1"

[compat]
julia = "1.6"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# PropertyDicts.jl

[![Build Status](https://github.com/JuliaCollections/PropertyDicts.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/JuliaCollections/PropertyDicts.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Coverage](https://codecov.io/gh/JuliaCollections/PropertyDicts.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaCollections/PropertyDicts.jl)

Wrap an `AbstractDict` to add `getproperty` support for `Symbol` and `String` keys.

```julia
Expand Down
153 changes: 129 additions & 24 deletions src/PropertyDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ module PropertyDicts

export PropertyDict

@static if !hasmethod(reverse, Tuple{NamedTuple})
Base.reverse(nt::NamedTuple) = NamedTuple{reverse(keys(nt))}(reverse(values(nt)))
end
#region catch Julia versions missing these
@static if !hasmethod(mergewith, Tuple{Any,NamedTuple,NamedTuple})
function Base.mergewith(combine, a::NamedTuple{an}, b::NamedTuple{bn}) where {an, bn}
names = Base.merge_names(an, bn)
Expand All @@ -22,27 +20,136 @@ end
end)
end
end
@static if !hasmethod(Base.setindex, Tuple{AbstractDict,Any,Any})
function setindex(d::Base.ImmutableDict, v, k)
if isdefined(d, :parent)
if isequal(d.key, k)
d0 = d.parent
v0 = v
k0 = k
else
d0 = setindex(d.parent, v, k)
v0 = d.value
k0 = d.key
end
else
d0 = d
v0 = v
k0 = k
end
K = promote_type(keytype(d0), typeof(k0))
V = promote_type(valtype(d0), typeof(v0))
Base.ImmutableDict{K,V}(d0, k0, v0)
end
function setindex(src::AbstractDict{K,V}, val::V, key::K) where {K,V}
dst = copy(src)
dst[key] = val
return dst
end
function setindex(src::AbstractDict{K,V}, val, key) where {K,V}
dst = empty(src, promote_type(K,typeof(key)), promote_type(V,typeof(val)))
if haslength(src)
sizehint!(dst, length(src))
end
for (k,v) in src
dst[k] = v
end
dst[key] = val
return dst
end
end
if isdefined(Base, :delete)
import Base: delete
else
delete(collection, k) = delete!(copy(collection), k)
function delete(d::Base.ImmutableDict{K,V}, key) where {K,V}
if isdefined(d, :parent)
if isequal(d.key, key)
d.parent
else
Base.ImmutableDict{K,V}(delete(d.parent, key), d.key, d.value)
end
else
d
end
end
function delete(nt::NamedTuple{syms}, key::Symbol) where {syms}
idx = Base.fieldindex(typeof(nt), key, false)
if idx === 0
return nt
else
nv = Val{nfields(syms) - 1}()
NamedTuple{
ntuple(j -> j < idx ? getfield(syms, j) : getfield(syms, j + 1), nv)
}(ntuple(j -> j < idx ? getfield(nt, j) : getfield(nt, j + 1), nv))
end
end
end
#endregion

struct PropertyDict{K<:Union{String,Symbol}, V, D <: Union{AbstractDict,NamedTuple}} <: AbstractDict{K, V}
struct PropertyDict{K<:Union{String,Symbol}, V, D <: Union{AbstractDict{K,V},NamedTuple{<:Any,<:Tuple{Vararg{V}}}}} <: AbstractDict{K, V}
d::D

PropertyDict(@nospecialize pd::PropertyDict) = pd
PropertyDict(d::AbstractDict{String,V}) where {V} = new{String,V,typeof(d)}(d)
PropertyDict(d::AbstractDict{Symbol,V}) where {V} = new{Symbol,V,typeof(d)}(d)
PropertyDict(nt::NamedTuple) = new{Symbol,eltype(nt),typeof(nt)}(nt)
function PropertyDict(d::AbstractDict)
dsym = Dict{Symbol,valtype(d)}()
# PropertyDict{K,V}(args...)
PropertyDict{Symbol,V}(d::AbstractDict{Symbol,V}) where {V} = new{Symbol,V,typeof(d)}(d)
PropertyDict{String,V}(d::AbstractDict{String,V}) where {V} = new{String,V,typeof(d)}(d)
PropertyDict{Symbol,V}(pd::PropertyDict{Symbol,V}) where {V} = pd
PropertyDict{String,V}(pd::PropertyDict{String,V}) where {V} = pd
function PropertyDict{K,V}(@nospecialize d::PropertyDict) where {K,V}
PropertyDict{K,V}(getfield(d, :d))
end
function PropertyDict{K,V}(d::AbstractDict) where {K,V}
dsym = PropertyDict(Dict{K,V}())
for (k,v) in d
dsym[Symbol(k)] = v
dsym[K(k)] = v
end
PropertyDict(dsym)
dsym
end
function PropertyDict{Symbol,V}(nt::NamedTuple{syms,<:Tuple{Vararg{V}}}) where {syms,V}
new{Symbol,V,typeof(nt)}(nt)
end
function PropertyDict{Symbol,V}(nt::NamedTuple{syms}) where {V,syms}
PropertyDict{Symbol,V}(NamedTuple{syms}(Tuple{Vararg{V}}(Tuple(nt))))
end
PropertyDict{K,V}(arg, args...) where {K,V} = PropertyDict{K,V}(Dict(arg, args...))
PropertyDict{K,V}(; kwargs...) where {K,V} = PropertyDict{K,V}(values(kwargs))

# PropertyDict{K}(args...)
function PropertyDict{K}(@nospecialize(d::AbstractDict)) where {K}
PropertyDict{K,valtype(d)}(d)
end
function PropertyDict{String}(@nospecialize(d::AbstractDict{String}))
new{String,valtype(d),typeof(d)}(d)
end
function PropertyDict{Symbol}(@nospecialize(d::AbstractDict{Symbol}))
new{Symbol,valtype(d),typeof(d)}(d)
end
PropertyDict{Symbol}(@nospecialize(d::NamedTuple)) = new{Symbol,eltype(d),typeof(d)}(d)
PropertyDict{Symbol}(@nospecialize(pd::PropertyDict{Symbol})) = pd
PropertyDict{String}(@nospecialize(pd::PropertyDict{String})) = pd
PropertyDict{K}(arg, args...) where {K} = PropertyDict{K}(Dict(arg, args...))
PropertyDict{K}(; kwargs...) where {K} = PropertyDict{K}(values(kwargs))

# PropertyDict(args...)
PropertyDict(@nospecialize pd::PropertyDict) = pd
PropertyDict(@nospecialize d::AbstractDict{String}) = PropertyDict{String}(d)
function PropertyDict(@nospecialize d::Union{AbstractDict{Symbol},NamedTuple})
PropertyDict{Symbol}(d)
end
PropertyDict(@nospecialize d::AbstractDict) = PropertyDict{Symbol}(d)
PropertyDict(arg, args...) = PropertyDict(Dict(arg, args...))
PropertyDict(; kwargs...) = PropertyDict(values(kwargs))
end

const NamedProperties{syms,T<:Tuple,V} = PropertyDict{Symbol,V,NamedTuple{syms,T}}

@inline function Base.setindex(pd::PropertyDict, val, key::Union{Symbol,AbstractString})
Base.setindex(getfield(pd, :d), val, _tokey(pd, key))
end
@inline function delete(pd::PropertyDict, key::Union{Symbol,AbstractString})
delete(getfield(pd, :d), _tokey(pd, key))
end

Base.IteratorSize(@nospecialize T::Type{<:PropertyDict}) = Base.IteratorSize(fieldtype(T, :d))
Base.IteratorEltype(@nospecialize T::Type{<:PropertyDict}) = Base.IteratorEltype(eltype(T))

Expand All @@ -68,26 +175,26 @@ function Base.empty!(pd::PropertyDict)
empty!(getfield(pd, :d))
return pd
end
Base.isempty(::NamedProperties{(),Tuple{},Union{}}) = true
Base.isempty(@nospecialize(npd::NamedProperties)) = false
Base.isempty(pd::PropertyDict) = isempty(getfield(pd, :d))
function Base.empty(pd::PropertyDict, ::Type{K}=keytype(pd), ::Type{V}=valtype(pd)) where {K,V}
PropertyDict(empty(getfield(pd, :d), K, V))
end
Base.empty(pd::NamedProperties, ::Type{K}, ::Type{V}) where {K,V} = PropertyDict()
function Base.empty(@nospecialize(pd::NamedProperties), ::Type{K}, ::Type{V}) where {K,V}
PropertyDict()
end

function Base.delete!(pd::PropertyDict, k)
delete!(getfield(pd, :d), _tokey(pd, k))
return pd
end

function Base.get(pd::PropertyDict, k, d)
get(getfield(pd, :d), _tokey(pd, k), d)
end
Base.get(pd::PropertyDict, k, d) = get(getfield(pd, :d), _tokey(pd, k), d)
function Base.get(f::Union{Function,Type}, pd::PropertyDict, k)
get(f, getfield(pd, :d), _tokey(pd, k))
end
function Base.get!(pd::PropertyDict, k, d)
get!(getfield(pd, :d), _tokey(pd, k), d)
end
Base.get!(pd::PropertyDict, k, d) = get!(getfield(pd, :d), _tokey(pd, k), d)
function Base.get!(f::Union{Function,Type}, pd::PropertyDict, k)
get!(f, getfield(pd, :d), _tokey(pd, k))
end
Expand All @@ -101,16 +208,14 @@ Base.@propagate_inbounds function Base.setindex!(pd::PropertyDict, v, k)
setindex!(getfield(pd, :d), v, _tokey(pd, k))
end

Base.reverse(pd::PropertyDict) = PropertyDict(reverse(getfield(pd, :d)))

@inline function Base.iterate(pd::NamedProperties)
if isempty(pd)
nothing
else
Pair{Symbol,valtype(pd)}(getfield(keys(pd), 1), getfield(getfield(pd, :d), 1)), 2
end
end
@inline function Base.iterate(pd::NamedProperties, s::Int) where {V}
@inline function Base.iterate(pd::NamedProperties, s::Int)
if length(pd) < s
nothing
else
Expand All @@ -131,9 +236,9 @@ Base.hasproperty(pd::PropertyDict, k::AbstractString) = haskey(pd, _tokey(pd, k)
Base.propertynames(pd::PropertyDict) = keys(getfield(pd, :d))
Base.getproperty(pd::NamedProperties, k::Symbol) = getfield(getfield(pd, :d), k)
Base.getproperty(pd::PropertyDict, k::Symbol) = getindex(pd, k)
Base.getproperty(pd::PropertyDict, k::String) = getindex(pd, k)
Base.getproperty(pd::PropertyDict, k::AbstractString) = getindex(pd, k)
Base.setproperty!(pd::PropertyDict, k::Symbol, v) = setindex!(pd, v, k)
Base.setproperty!(pd::PropertyDict, k::String, v) = setindex!(pd, v, k)
Base.setproperty!(pd::PropertyDict, k::AbstractString, v) = setindex!(pd, v, k)

Base.copy(pd::NamedProperties) = pd
Base.copy(pd::PropertyDict) = PropertyDict(copy(getfield(pd, :d)))
Expand Down
Loading

2 comments on commit 3565839

@Tokazama
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/68919

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" 356583999fd730f3be14f5f0ffddc4b7891d2cab
git push origin v0.2.1

Please sign in to comment.