-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow non-string indices for Trie #759
Conversation
This seems sensible. |
node = subtrie(t, key) | ||
if node != nothing && node.is_key | ||
return node.value | ||
end | ||
return notfound | ||
end | ||
|
||
function Base.keys(t::Trie, prefix::AbstractString="", found=AbstractString[]) | ||
_concat(prefix::String, char::Char) = string(prefix, char) | ||
_concat(prefix::Vector{T}, char::T) where {T} = vcat(prefix, char) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is all we need to add in order to support arbitrary iterators just:
_concat(prefix::Vector{T}, char::T) where {T} = vcat(prefix, char) | |
_concat(prefix, char) = Iterators.flatten(prefix, (char,)) |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean Iterators.flatten((prefix, (char,)))
?
That would cause the types to explode:
julia> p1 = [1]
1-element Vector{Int64}:
1
julia> p2 = Iterators.flatten((p1, (2,)))
Base.Iterators.Flatten{Tuple{Vector{Int64}, Tuple{Int64}}}(([1], (2,)))
julia> p3 = Iterators.flatten((p2, (3,)))
Base.Iterators.Flatten{Tuple{Base.Iterators.Flatten{Tuple{Vector{Int64}, Tuple{Int64}}}, Tuple{Int64}}}((Base.Iterators.Flatten{Tuple{Vector{Int64}, Tuple{Int64}}}(([1], (2,))), (3,)))
As _concat
only receives objects derived from _empty_prefix
, which are either String
s or Vector
s, I don't think this needs to be generalized further. Instead, it might be good to wrap the 3-argument keys (Base.keys(t) = _keys(t)
), such that arguments 2 and 3 are not accessible to the average user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a really good change and well written.
I will wait a bit re: my question about arbitrary iterators before merge,
but this basically looks great
by dropping the
::AbstractString
restriction. I considered replacing that by::Union{AbstractString,AbstractVector}
(thereby wondering whyAbstractString <: AbstractVector
doesn't hold). Which one would you prefer?This is breaking as the original type parameter (denoting the value type) is now in second position, such that the parameters follow the common key-value pattern like e.g.
Dict
has. I noticed that you are feature-frozen (#479), but I think it would be better to consider changing the order now instead of adding this feature later in a non-breaking manner (i.e. reversing the type parameters).This is ready for comments. If there is anything I can change to improve this PR, I'm happy to do so. 🙂