Skip to content
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

Merged
merged 1 commit into from
Oct 8, 2021

Conversation

jonas-schulze
Copy link
Contributor

by dropping the ::AbstractString restriction. I considered replacing that by ::Union{AbstractString,AbstractVector} (thereby wondering why AbstractString <: 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. 🙂

@oxinabox
Copy link
Member

oxinabox commented Oct 4, 2021

This seems sensible.
I will try and find time to review properly later this week

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)
Copy link
Member

@oxinabox oxinabox Oct 8, 2021

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:

Suggested change
_concat(prefix::Vector{T}, char::T) where {T} = vcat(prefix, char)
_concat(prefix, char) = Iterators.flatten(prefix, (char,))

?

Copy link
Contributor Author

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 Strings or Vectors, 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.

Copy link
Member

@oxinabox oxinabox left a 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

@oxinabox oxinabox merged commit 214f3b3 into JuliaCollections:master Oct 8, 2021
@jonas-schulze jonas-schulze deleted the vector-trie branch October 8, 2021 21:40
@KristofferC KristofferC mentioned this pull request Nov 3, 2021
@jonas-schulze jonas-schulze mentioned this pull request Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants