@@ -111,6 +111,9 @@ mutable struct Dict{K,V} <: Associative{K,V}
111111 new (copy (d. slots), copy (d. keys), copy (d. vals), 0 , d. count, d. age, d. idxfloor,
112112 d. maxprobe)
113113 end
114+ function Dict {K, V} (slots, keys, ndel, count, age, idxfloor, maxprobe) where {K, V}
115+ new (slots, keys, Vector {V} (length (keys)), ndel, count, age, idxfloor, maxprobe)
116+ end
114117end
115118function Dict {K,V} (kv) where V where K
116119 h = Dict {K,V} ()
170173# this is a special case due to (1) allowing both Pairs and Tuples as elements,
171174# and (2) Pair being invariant. a bit annoying.
172175function grow_to! (dest:: Associative , itr)
173- out = grow_to! (similar (dest, Pair{ Union{},Union{} }), itr, start (itr))
176+ out = grow_to! (empty (dest, Union{}, Union{}), itr, start (itr))
174177 return isempty (out) ? dest : out
175178end
176179
@@ -180,7 +183,7 @@ function grow_to!(dest::Associative{K,V}, itr, st) where V where K
180183 if isa (k,K) && isa (v,V)
181184 dest[k] = v
182185 else
183- new = similar (dest, Pair{ typejoin (K,typeof (k)), typejoin (V,typeof (v))} )
186+ new = empty (dest, typejoin (K,typeof (k)), typejoin (V,typeof (v)))
184187 copy! (new, dest)
185188 new[k] = v
186189 return grow_to! (new, itr, st)
@@ -189,8 +192,23 @@ function grow_to!(dest::Associative{K,V}, itr, st) where V where K
189192 return dest
190193end
191194
192- similar (d:: Dict{K,V} ) where {K,V} = Dict {K,V} ()
193- similar (d:: Dict , :: Type{Pair{K,V}} ) where {K,V} = Dict {K,V} ()
195+ function similar (a:: Associative , :: Type{V} , inds) where {V}
196+ K = eltype (inds)
197+ tmp = Dict {K, Void} ()
198+ for i in inds
199+ tmp[i] = nothing
200+ end
201+ return Dict {K, V} (tmp. slots, tmp. keys, tmp. ndel, tmp. count, tmp. age,
202+ tmp. idxfloor, tmp. maxprobe)
203+ end
204+
205+ # Fast copy of hashed indices from a `Dict`
206+ function similar (a:: Associative , :: Type{V} , inds:: KeyIterator{<:Dict{K}} ) where {K, V}
207+ Dict {K, V} (copy (inds. dict. slots), copy (inds. dict. keys), inds. dict. ndel, inds. dict. count,
208+ inds. dict. age, inds. dict. idxfloor, inds. dict. maxprobe)
209+ end
210+
211+ empty (a:: Associative , :: Type{K} , :: Type{V} ) where {K, V} = Dict {K, V} ()
194212
195213# conversion between Dict types
196214function convert (:: Type{Dict{K,V}} ,d:: Associative ) where V where K
@@ -813,12 +831,7 @@ next(::ImmutableDict{K,V}, t) where {K,V} = (Pair{K,V}(t.key, t.value), t.parent
813831done (:: ImmutableDict , t) = ! isdefined (t, :parent )
814832length (t:: ImmutableDict ) = count (x-> true , t)
815833isempty (t:: ImmutableDict ) = done (t, start (t))
816- function similar (t:: ImmutableDict )
817- while isdefined (t, :parent )
818- t = t. parent
819- end
820- return t
821- end
834+ empty (:: ImmutableDict , :: Type{K} , :: Type{V} ) where {K, V} = ImmutableDict {K,V} ()
822835
823- _similar_for (c:: Dict , :: Type{P} , itr, isz) where {P <: Pair } = similar (c, P )
836+ _similar_for (c:: Dict , :: Type{Pair{K,V}} , itr, isz) where {K, V } = empty (c, K, V )
824837_similar_for (c:: Associative , T, itr, isz) = throw (ArgumentError (" for Associatives, similar requires an element type of Pair;\n if calling map, consider a comprehension instead" ))
0 commit comments