Skip to content

Commit

Permalink
Merge pull request #105 from JuliaCollections/constructors
Browse files Browse the repository at this point in the history
Adds a couple new constructors for `LittleDict` and expands the vector type allowed from `Vector` to `AbstractVector` (solving #104). This also allows us to simplify the constructor methods while still ensuring that the eltype of the storage type matches the key/value of the dict.
  • Loading branch information
Tokazama authored Jul 22, 2023
2 parents b6427c0 + 0d2eeea commit c3687a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OrderedCollections"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.6.1"
version = "1.6.2"

[compat]
julia = "1.6"
Expand Down
35 changes: 18 additions & 17 deletions src/little_dict.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const StoreType = Union{<:Tuple, <:Vector}
const StoreType{T} = Union{Tuple{Vararg{T}}, AbstractVector{T}}

@noinline function _throw_unequal_lengths(nk::Int, nv::Int)
throw(ArgumentError("Number of keys ($nk) differs from number of values ($nv)."))
end

"""
LittleDict(keys, vals)<:AbstractDict
Expand Down Expand Up @@ -28,22 +32,18 @@ as well as on how many hash collisions occur etc.
copies to create the `LittleDict`, so `LittleDict(ks::Tuple, vs::Tuple)`
is the fastest constructor of all.
"""
struct LittleDict{K,V,KS<:StoreType,VS<:StoreType} <: AbstractDict{K, V}
struct LittleDict{K, V, KS<:StoreType{K}, VS<:StoreType{V}} <: AbstractDict{K, V}
keys::KS
vals::VS

function LittleDict{K,V,KS,VS}(keys,vals) where {K,V,KS,VS}
if length(keys) != length(vals)
throw(ArgumentError(
"Number of keys ($(length(keys))) differs from " *
"number of values ($(length(vals))"
))
end
K<:eltype(KS) || ArgumentError("Invalid store type $KS, for key type $K")
V<:eltype(VS) || ArgumentError("Invalid store type $VS, for value type $K")

return new(keys,vals)
function LittleDict{K, V, KS, VS}(keys, vals) where {K, V, KS, VS}
nk = length(keys)
nv = length(vals)
nk == nv || _throw_unequal_lengths(Int(nk), Int(nv))
return new{K, V, KS, VS}(keys, vals)
end
LittleDict{K, V, <:Tuple, <:Tuple}() where {K, V} = new{K, V, Tuple{}, Tuple{}}((), ())
LittleDict{K, V, KS, VS}() where {K, V, KS, VS} = LittleDict{K, V, KS, VS}(KS(), VS())
end

function LittleDict{K,V}(ks::KS, vs::VS) where {K,V, KS<:StoreType,VS<:StoreType}
Expand All @@ -54,7 +54,6 @@ function LittleDict(ks::KS, vs::VS) where {KS<:StoreType,VS<:StoreType}
return LittleDict{eltype(KS), eltype(VS)}(ks, vs)
end


# Other iterators should be copied to a Vector
LittleDict(ks, vs) = LittleDict(collect(ks), collect(vs))

Expand Down Expand Up @@ -110,8 +109,8 @@ end
isordered(::Type{<:LittleDict}) = true

# For now these are internal UnionAlls for dispatch purposes
const UnfrozenLittleDict{K,V} = LittleDict{K,V, Vector{K}, Vector{V}}
const FrozenLittleDict{K,V} = LittleDict{K,V, <:Tuple, <:Tuple}
const UnfrozenLittleDict{K, V} = LittleDict{K, V, <:AbstractVector{K}, <:AbstractVector{V}}
const FrozenLittleDict{K, V} = LittleDict{K, V, <:Tuple, <:Tuple}

##### Methods that all AbstractDicts should implement

Expand Down Expand Up @@ -184,7 +183,9 @@ function merge(
end


Base.empty(dd::LittleDict{K,V}) where {K,V} = LittleDict{K,V}()
function Base.empty(dd::LittleDict{K,V}) where {K,V}
LittleDict{K, V}(empty(getfield(dd, :keys)), empty(getfield(dd, :vals)))
end

######## Methods that all mutable AbstractDict's should implement

Expand Down
1 change: 1 addition & 0 deletions test/test_little_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ using OrderedCollections: FrozenLittleDict, UnfrozenLittleDict
end

@testset "Constructors" begin
@test isa(@inferred(LittleDict{Any, Any, <:Tuple, <:Tuple}()), LittleDict{Any, Any, Tuple{}, Tuple{}})
@test isa(@inferred(LittleDict()), LittleDict{Any,Any})
@test isa(@inferred(LittleDict([(1,2.0)])), LittleDict{Int,Float64})

Expand Down

2 comments on commit c3687a8

@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/88050

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 v1.6.2 -m "<description of version>" c3687a87d7ce0d134bf3578bb85472c16e903012
git push origin v1.6.2

Please sign in to comment.