andyferris / Dictionaries.jl

An alternative interface for dictionaries in Julia, for improved productivity and performance
Other
278 stars 28 forks source link

`sizehint!` doesn't work with an `UnorderedDictionary` #126

Open Tortar opened 10 months ago

Tortar commented 10 months ago

I see that the method is implemented here: https://github.com/andyferris/Dictionaries.jl/blob/master/src/UnorderedDictionary.jl#L162

but it seems not to work:

julia> using Dictionaries

julia> d = UnorderedDictionary(x => x for x in 1:10^4);

julia> sizehint!(d, 10)
ERROR: MethodError: Cannot `convert` an object of type typeof(hash) to an object of type Vector{Pair{Int64, Int64}}

Closest candidates are:
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray
   @ LinearAlgebra ~/julia-1.9.3/share/julia/stdlib/v1.9/LinearAlgebra/src/factorization.jl:59
  convert(::Type{T}, ::AbstractArray) where T<:Array
   @ Base array.jl:613
  convert(::Type{T}, ::T) where T<:AbstractArray
   @ Base abstractarray.jl:16
  ...

Stacktrace:
 [1] setproperty!(x::UnorderedDictionary{Int64, Pair{Int64, Int64}}, f::Symbol, v::Function)
   @ Base ./Base.jl:38
 [2] sizehint!(d::UnorderedDictionary{Int64, Pair{Int64, Int64}}, sz::Int64)
   @ Dictionaries ~/.julia/packages/Dictionaries/7aBxp/src/UnorderedDictionary.jl:163
 [3] top-level scope
   @ REPL[3]:1
Tortar commented 10 months ago

Also, I'm not sure if this is intended but for a Dictionary it says it is not defined:

julia> d = Dictionary(x => x for x in 1:10^4);

julia> sizehint!(d, 10)
ERROR: MethodError: no method matching sizehint!(::Dictionary{Int64, Pair{Int64, Int64}}, ::Int64)

Closest candidates are:
  sizehint!(::Base.IdSet, ::Any)
   @ Base idset.jl:26
  sizehint!(::IdDict, ::Any)
   @ Base iddict.jl:76
  sizehint!(::Set, ::Any)
   @ Base set.jl:120
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[5]:1

Is this due to the auto-shrinking behaviour of Dictionary (it seems to me that indeed UnorderedDictionary doesn't have such a feature)?