Skip to content

Commit

Permalink
Preserve the input element type in unique (#23208)
Browse files Browse the repository at this point in the history
Previously the element type of the output was the smallest type that
would fit the union of the input's individual element types. Now the
output has an identical element type to the input. Fixes #22696.
  • Loading branch information
ararslan authored and StefanKarpinski committed Aug 24, 2017
1 parent 2406b6e commit b46c74c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ This section lists changes that do not have deprecation warnings.
way as `^(A::Integer, p::Integer)`. This means, for instance, that `[1 1; 0 1]^big(1)`
will return a `Matrix{BigInt}` instead of a `Matrix{Int}` ([#23366]).

* The element type of the input is now preserved in `unique`. Previously the element type
of the output was shrunk to fit the union of the type of each element in the input.
([#22696])

Library improvements
--------------------

Expand Down Expand Up @@ -1204,6 +1208,7 @@ Command-line option changes
[#22588]: https://github.com/JuliaLang/julia/issues/22588
[#22605]: https://github.com/JuliaLang/julia/issues/22605
[#22666]: https://github.com/JuliaLang/julia/issues/22666
[#22696]: https://github.com/JuliaLang/julia/issues/22696
[#22703]: https://github.com/JuliaLang/julia/issues/22703
[#22712]: https://github.com/JuliaLang/julia/issues/22712
[#22718]: https://github.com/JuliaLang/julia/issues/22718
Expand Down
10 changes: 8 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ const ⊆ = issubset
Return an array containing only the unique elements of collection `itr`,
as determined by [`isequal`](@ref), in the order that the first of each
set of equivalent elements originally appears.
set of equivalent elements originally appears. The element type of the
input is preserved.
# Examples
```jldoctest
Expand All @@ -249,6 +250,11 @@ julia> unique([1, 2, 6, 2])
1
2
6
julia> unique(Real[1, 1.0, 2])
2-element Array{Real,1}:
1
2
```
"""
function unique(itr)
Expand All @@ -260,7 +266,7 @@ function unique(itr)
return out
end
x, i = next(itr, i)
if !isleaftype(T)
if !isleaftype(T) && iteratoreltype(itr) == EltypeUnknown()
S = typeof(x)
return _unique_from(itr, S[x], Set{S}((x,)), i)
end
Expand Down
3 changes: 2 additions & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5382,7 +5382,8 @@ for U in unboxedunions
# deleteat!
F = Base.uniontypes(U)[2]
A = U[rand(F(1):F(len)) for i = 1:len]
deleteat!(A, map(Int, sort!(unique(A[1:4]))))
# The 2-arg `unique` method works around #22688
deleteat!(A, map(Int, sort!(unique(identity, A[1:4]))))
A = U[initvalue2(F2) for i = 1:len]
deleteat!(A, 1:2)
@test length(A) == len - 2
Expand Down
2 changes: 2 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ u = unique([1,1,2])
# issue 20105
@test @inferred(unique(x for x in 1:1)) == [1]
@test unique(x for x in Any[1,1.0])::Vector{Real} == [1]
@test unique(x for x in Real[1,1.0])::Vector{Real} == [1]
@test unique(Integer[1,1,2])::Vector{Integer} == [1,2]

# unique!
@testset "unique!" begin
Expand Down

2 comments on commit b46c74c

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.