Skip to content

Commit

Permalink
Preserve the input element type in unique
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 committed Aug 17, 2017
1 parent a945af3 commit c9ade2a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ This section lists changes that do not have deprecation warnings.
* Worker-worker connections are setup lazily for an `:all_to_all` topology. Use keyword
arg `lazy=false` to force all connections to be setup during a `addprocs` call. ([#22814])

* 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 @@ -1150,6 +1154,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
12 changes: 7 additions & 5 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,10 +266,6 @@ function unique(itr)
return out
end
x, i = next(itr, i)
if !isleaftype(T)
S = typeof(x)
return _unique_from(itr, S[x], Set{S}((x,)), i)
end
push!(seen, x)
push!(out, x)
return unique_from(itr, out, seen, i)
Expand Down
4 changes: 3 additions & 1 deletion test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ u = unique([1,1,2])
@test unique(n->n % 3, [5,1,8,9,3,4,10,7,2,6]) == [5,1,9]
# 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 Any[1,1.0])::Vector{Any} == [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

0 comments on commit c9ade2a

Please sign in to comment.