Skip to content

Commit c3687a8

Browse files
authored
Merge pull request #105 from JuliaCollections/constructors
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.
2 parents b6427c0 + 0d2eeea commit c3687a8

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OrderedCollections"
22
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
3-
version = "1.6.1"
3+
version = "1.6.2"
44

55
[compat]
66
julia = "1.6"

src/little_dict.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const StoreType = Union{<:Tuple, <:Vector}
1+
const StoreType{T} = Union{Tuple{Vararg{T}}, AbstractVector{T}}
2+
3+
@noinline function _throw_unequal_lengths(nk::Int, nv::Int)
4+
throw(ArgumentError("Number of keys ($nk) differs from number of values ($nv)."))
5+
end
26

37
"""
48
LittleDict(keys, vals)<:AbstractDict
@@ -28,22 +32,18 @@ as well as on how many hash collisions occur etc.
2832
copies to create the `LittleDict`, so `LittleDict(ks::Tuple, vs::Tuple)`
2933
is the fastest constructor of all.
3034
"""
31-
struct LittleDict{K,V,KS<:StoreType,VS<:StoreType} <: AbstractDict{K, V}
35+
struct LittleDict{K, V, KS<:StoreType{K}, VS<:StoreType{V}} <: AbstractDict{K, V}
3236
keys::KS
3337
vals::VS
3438

35-
function LittleDict{K,V,KS,VS}(keys,vals) where {K,V,KS,VS}
36-
if length(keys) != length(vals)
37-
throw(ArgumentError(
38-
"Number of keys ($(length(keys))) differs from " *
39-
"number of values ($(length(vals))"
40-
))
41-
end
42-
K<:eltype(KS) || ArgumentError("Invalid store type $KS, for key type $K")
43-
V<:eltype(VS) || ArgumentError("Invalid store type $VS, for value type $K")
44-
45-
return new(keys,vals)
39+
function LittleDict{K, V, KS, VS}(keys, vals) where {K, V, KS, VS}
40+
nk = length(keys)
41+
nv = length(vals)
42+
nk == nv || _throw_unequal_lengths(Int(nk), Int(nv))
43+
return new{K, V, KS, VS}(keys, vals)
4644
end
45+
LittleDict{K, V, <:Tuple, <:Tuple}() where {K, V} = new{K, V, Tuple{}, Tuple{}}((), ())
46+
LittleDict{K, V, KS, VS}() where {K, V, KS, VS} = LittleDict{K, V, KS, VS}(KS(), VS())
4747
end
4848

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

57-
5857
# Other iterators should be copied to a Vector
5958
LittleDict(ks, vs) = LittleDict(collect(ks), collect(vs))
6059

@@ -110,8 +109,8 @@ end
110109
isordered(::Type{<:LittleDict}) = true
111110

112111
# For now these are internal UnionAlls for dispatch purposes
113-
const UnfrozenLittleDict{K,V} = LittleDict{K,V, Vector{K}, Vector{V}}
114-
const FrozenLittleDict{K,V} = LittleDict{K,V, <:Tuple, <:Tuple}
112+
const UnfrozenLittleDict{K, V} = LittleDict{K, V, <:AbstractVector{K}, <:AbstractVector{V}}
113+
const FrozenLittleDict{K, V} = LittleDict{K, V, <:Tuple, <:Tuple}
115114

116115
##### Methods that all AbstractDicts should implement
117116

@@ -184,7 +183,9 @@ function merge(
184183
end
185184

186185

187-
Base.empty(dd::LittleDict{K,V}) where {K,V} = LittleDict{K,V}()
186+
function Base.empty(dd::LittleDict{K,V}) where {K,V}
187+
LittleDict{K, V}(empty(getfield(dd, :keys)), empty(getfield(dd, :vals)))
188+
end
188189

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

test/test_little_dict.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using OrderedCollections: FrozenLittleDict, UnfrozenLittleDict
3939
end
4040

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

0 commit comments

Comments
 (0)