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
2
6
3
7
"""
4
8
LittleDict(keys, vals)<:AbstractDict
@@ -28,22 +32,18 @@ as well as on how many hash collisions occur etc.
28
32
copies to create the `LittleDict`, so `LittleDict(ks::Tuple, vs::Tuple)`
29
33
is the fastest constructor of all.
30
34
"""
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}
32
36
keys:: KS
33
37
vals:: VS
34
38
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)
46
44
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 ())
47
47
end
48
48
49
49
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}
54
54
return LittleDict {eltype(KS), eltype(VS)} (ks, vs)
55
55
end
56
56
57
-
58
57
# Other iterators should be copied to a Vector
59
58
LittleDict (ks, vs) = LittleDict (collect (ks), collect (vs))
60
59
110
109
isordered (:: Type{<:LittleDict} ) = true
111
110
112
111
# 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 }
115
114
116
115
# #### Methods that all AbstractDicts should implement
117
116
@@ -184,7 +183,9 @@ function merge(
184
183
end
185
184
186
185
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
188
189
189
190
# ####### Methods that all mutable AbstractDict's should implement
190
191
0 commit comments