Skip to content

Commit

Permalink
Merge pull request #30141 from raghav9-97/solveissue
Browse files Browse the repository at this point in the history
Added unique!(f, itr) function
  • Loading branch information
andyferris authored Dec 5, 2018
2 parents 44817f0 + c2535b3 commit 5cbbed3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
41 changes: 35 additions & 6 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,41 @@ function unique(f, C)
out
end

# If A is not grouped, then we will need to keep track of all of the elements that we have
# seen so far.
function _unique!(A::AbstractVector)
seen = Set{eltype(A)}()
"""
unique!(f, A::AbstractVector)
Selects one value from `A` for each unique value produced by `f` applied to
elements of `A` , then return the modified A.
# Examples
```jldoctest
julia> unique!(x -> x^2, [1, -1, 3, -3, 4])
3-element Array{Int64,1}:
1
3
4
julia> unique!(n -> n%3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6])
3-element Array{Int64,1}:
5
1
9
julia> unique!(iseven, [2, 3, 5, 7, 9])
2-element Array{Int64,1}:
2
3
```
"""
function unique!(f, A::AbstractVector)
seen = Set()
idxs = eachindex(A)
y = iterate(idxs)
count = 0
for x in A
if x seen
push!(seen, x)
t = f(x)
if t seen
push!(seen,t)
count += 1
A[y[1]] = x
y = iterate(idxs, y[2])
Expand All @@ -196,6 +221,10 @@ function _unique!(A::AbstractVector)
resize!(A, count)
end

# If A is not grouped, then we will need to keep track of all of the elements that we have
# seen so far.
_unique!(A::AbstractVector) = unique!(identity, A::AbstractVector)

# If A is grouped, so that each unique element is in a contiguous group, then we only
# need to keep track of one element at a time. We replace the elements of A with the
# unique elements that we see in the order that we see them. Once we have iterated
Expand Down
4 changes: 4 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ end
unique!(u)
@test u == [5,"w","we","r"]
u = [1,2,5,1,3,2]
@test unique!(x -> x ^ 2, [1, -1, 3, -3, 5, -5]) == [1, 3, 5]
@test unique!(n -> n % 3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6]) == [5, 1, 9]
@test unique!(iseven, [2, 3, 5, 7, 9]) == [2, 3]
@test unique!(x -> x % 2 == 0 ? :even : :odd, [1, 2, 3, 4, 2, 2, 1]) == [1, 2]
end

@testset "allunique" begin
Expand Down

2 comments on commit 5cbbed3

@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.