Skip to content
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

Unique may return an AbstractFill #382

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@
# unique
#########

unique(x::AbstractFill{T}) where T = isempty(x) ? T[] : T[getindex_value(x)]
unique(x::AbstractFill) = fillsimilar(x, Int(!isempty(x)))

Check warning on line 610 in src/FillArrays.jl

View check run for this annotation

Codecov / codecov/patch

src/FillArrays.jl#L610

Added line #L610 was not covered by tests
allunique(x::AbstractFill) = length(x) < 2

#########
Expand Down
8 changes: 8 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@
permutedims(o::OneElementVector) = reshape(o, (1, length(o)))
permutedims(o::OneElement, dims) = OneElement(o.val, _permute(o.ind, dims), _permute(o.axes, dims))

# unique
function unique(O::OneElement)
v = getindex_value(O)
len = iszero(v) ? 1 : min(2, length(O))
OneElement(getindex_value(O), len, len)

Check warning on line 443 in src/oneelement.jl

View check run for this annotation

Codecov / codecov/patch

src/oneelement.jl#L440-L443

Added lines #L440 - L443 were not covered by tests
dlfivefifty marked this conversation as resolved.
Show resolved Hide resolved
end
allunique(O::OneElement) = length(O) <= 1 || (length(O) < 3 && !iszero(getindex_value(O)))

Check warning on line 445 in src/oneelement.jl

View check run for this annotation

Codecov / codecov/patch

src/oneelement.jl#L445

Added line #L445 was not covered by tests

# show
_maybesize(t::Tuple{Base.OneTo{Int}, Vararg{Base.OneTo{Int}}}) = size.(t,1)
_maybesize(t) = t
Expand Down
21 changes: 20 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ end
@testset "unique" begin
@test unique(Fill(12, 20)) == unique(fill(12, 20))
@test unique(Fill(1, 0)) == []
@test unique(Zeros(0)) isa Vector{Float64}
@test unique(Zeros(0)) == Zeros(0)
@test !allunique(Fill("a", 2))
@test allunique(Ones(0))
end
Expand Down Expand Up @@ -2708,6 +2708,25 @@ end
@test repr(B) == "OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))"
end

@testset "unique" begin
@testset for n in 1:3
O = OneElement(5, 2, n)
@test unique(O) == unique(Array(O))
@test allunique(O) == allunique(Array(O))
O = OneElement(0, 2, n)
@test unique(O) == unique(Array(O))
@test allunique(O) == allunique(Array(O))
@testset for m in 1:4
O2 = OneElement(2, (2,1), (m,n))
@test unique(O2) == unique(Array(O2))
@test allunique(O2) == allunique(Array(O2))
O2 = OneElement(0, (2,1), (m,n))
@test unique(O2) == unique(Array(O2))
@test allunique(O2) == allunique(Array(O2))
end
end
end

@testset "sum" begin
@testset "OneElement($v, $ind, $sz)" for (v, ind, sz) in (
(Int8(2), 3, 4),
Expand Down
Loading