-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make sets indexable #24508
Make sets indexable #24508
Conversation
base/set.jl
Outdated
@@ -64,7 +78,7 @@ rehash!(s::Set) = (rehash!(s.dict); s) | |||
start(s::Set) = start(s.dict) | |||
done(s::Set, state) = done(s.dict, state) | |||
# NOTE: manually optimized to take advantage of Dict representation | |||
next(s::Set, i) = (s.dict.keys[i], skip_deleted(s.dict, i+1)) | |||
@propagate_inbounds next(s::Set, i) = (s.dict.keys[i], skip_deleted(s.dict, i+1)) |
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.
Remove additional spaces.
base/set.jl
Outdated
end | ||
return x | ||
end | ||
eltype(set::AbstractSet) = eltype(typeof(set)) |
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.
AFAICT that definition is already present in array.jl.
@@ -23,9 +39,7 @@ function Set(g::Generator) | |||
return Set{T}(g) | |||
end | |||
|
|||
eltype(::Type{Set{T}}) where {T} = T | |||
similar(s::Set{T}) where {T} = Set{T}() |
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 this related to this PR?
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.
it's now just a clean-up
I'm still confused by this. What kind of code would you write that uses |
A very good question! :) Sorry, I didn't really explain this in the OP, but it has a lot more to do with indexing of In #24019 (and other places) the idea of making the array and associative indexing interface more similar is discussed. This PR is a culmination of a few facts:
So this PR is a tiny piece of work (which I had enough time to complete) which facilitates some harmony between these three points. The To lay my plan for the requisite breaking changes for v1.0, I hope to follow up by making |
0da600d
to
ae27e99
Compare
* All `AbstractSet`s can be indexed. They are containers with equal values and keys, much like `Base.OneTo(n)`. This will enable a certain semantic with `keys` and indexing common across arrays, associatives and sets.
I still don’t understand why your roadmap requires that sets be indexable. It seems sufficient that they are iterable. I’m skeptical of |
I, too, am not sure sets should be considered to have indices. If sets are index-less, they can more easily be used as informal relations (sets of pairs). Otherwise you'd get e.g.
I'm not sure what if anything In the same vein, I'm realizing arrays have a semantic ambiguity similar to that of dictionaries. Although they have indices, they are often used as kind of "ordered multi-sets", where they're just for passing some ordered values around and the indices don't matter. This is evinced by set-like functions on vectors like To fix this, we might want to add a List type, or do something drastic like considering |
The issues here clearly need more thought, and the grand indexing scheme mentioned in the OP also clearly won't make v1.0, so I'll close this PR. We'll see how this plays out when prototyping a |
OK, in the spirit of "show, don't tell" (I'll admit that this PR came across as a little abstract!), I created a package called Dictionaries.jl, which I recently released. Here you can see a system where the |
This PR makes all
AbstractSet
s indexable. They are containers with equal values and keys, and they iterate values (this hasn't changed).The goal here is to enable a certain semantic with
keys
and indexing common across arrays, associatives and sets. For example, with this, we can havekeys(dict)
be anAbstractSet
. Like vectors which havekeys(vec) = OneTo(length(vec))
, in the future we can have the idempotent indexing ofkeys
of dictionaries as we do for vectors (and offset vectors):Part of #24019.
(I also updated
similar
toempty
but this probably belongs in #24390)