-
-
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
We need more unique
and related methods
#14649
Comments
Can't ranges possibly have step 0? |
Well I guess you can have julia> x = 0:0
1-element UnitRange{Int64}:
0 in which case the julia> unique(x)
1-element Array{Int64,1}:
0
julia> unique(x) == unique(collect(x))
true I tried constructing float range with zero step sizes but got error that told me I could not. Curious though, in the above example julia> step(x)
1 which is possibly a bug? |
OK, I guess I wasn't trying hard enough. If you make a call to the julia> range(1,0.,3)
3-element FloatRange{Float64}:
1.0,1.0,1.0 such that In particular, if you do something like this Base.unique{T}(x::Range{T}) = step(x) == zero(T) ? x[1:1] : x you get the correct behavior and remain type stable. |
i think that is a lack of error checking on the part of the float range constructor -- a step of zero isn't logical so it is probably a user bug. the same arguments as Integers throw an argument error:
|
@vtjnash - so are you are saying that I am interested to know what others actually think should be done though, and have gone on a hunt for relevant discussions. Issues about ranges are myriad. I see a bunch of commits here #3121 referring to zero sized steps. There is an umbrella issue some time later #5585. Perhaps someone knows of an issue specific to step sizes equal to zero? |
I agree that |
I think zero step should be allowed. Specifically that'd fix #10391... That issue also mentions:
Edit: zero step ranges were removed in #6326 by @JeffBezanson |
That also allows |
I think the issue is that SteppedRange doesn't allow a len, whereas FloatRange does. The options IMO:
Not sure what is the best, although this should probably be it's own issue. Edit: |
Well it seems like there is some interest, so I started working on the feature(s). In the process of working on this it seemed reasonable to slightly expand the scope of the issue to other methods that are doing stuff closely related to |
unique
methodsunique
and related methods
Pull request with features described above at #15009. |
@gajomi I see all the boxes are checked. Is it just that open PR that you need to finish before this issue can be closed? |
I'm closing this since it's been nearly a year. Reopen if there's more to add here. |
But #15009 is still open... I don't think this has been resolved yet. |
There is quite a bit of room to add optimized
unique
methods. The only extant single argumentunique
methodunique(C) at set.jl:107
explicitly loops through all the elements ofC
to build up the unique result, and so has cost proportional to the lengthC
. However, in the case of ranges, something likereduces the cost to
O(1)
, which could improve performance in a number of places (for example see #14004 and the fix at 75336c6).Some of the optimizations that apply to
unique
also benefitSet
construction (or methods involved therein), and will be noted.I am planning to make a PR to implement these kinds of optimizations, and would like to use this issue to list out types where this optimization would be helpful. The checked off features in the list below refer to working implementations in this branch (I didn't think I should make a feature request until the branch was feature complete).
Targets:
Range
types (maximally trivial assuming zero step sizes disallowed) a872404Dict
,Set
andIntSet
types (trivial cases) 990a443AbstractArray{Bool}
,AbstractArray{Int8}
and otherAbstractArray{T}
with "small" bounds on total unique elements. Build up of unique list terminates when all possible elements are seen. Also applies tounion!(s::Set{T},AbstractArray{T})
methods, which are used inSet
construction, among other things. c8b02c4Diagonal
,Tridiagonal
and other matrices with many repeated structural zeros 5d0e097...and possibly more? If you have a target in mind please leave a comment.
The text was updated successfully, but these errors were encountered: