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

Resolve ambiguities #86

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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
6 changes: 0 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
julia = "1.3"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
18 changes: 14 additions & 4 deletions src/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ end

import Base: +, -, *, <, >, <=, >=, ==
for f in (:+, :-, :*, :<, :>, :<=, :>=, :(==))
@eval $f(a::ChainedVectorIndex, b::Integer) = $f(a.i, b)
@eval $f(a::Integer, b::ChainedVectorIndex) = $f(a, b.i)
for inttype in (:Integer, :BigInt)
@eval $f(a::ChainedVectorIndex, b::$inttype) = $f(a.i, b)
@eval $f(a::$inttype, b::ChainedVectorIndex) = $f(a, b.i)
end
@eval $f(a::ChainedVectorIndex, b::ChainedVectorIndex) = $f(a.i, b.i)
end
Base.convert(::Type{T}, x::ChainedVectorIndex) where {T <: Union{Signed, Unsigned}} = convert(T, x.i)
Expand Down Expand Up @@ -380,7 +382,9 @@ end

Base.copyto!(dest::AbstractVector, src::ChainedVector) =
copyto!(dest, 1, src, 1, length(src))
Base.copyto!(dest::AbstractVector, doffs::Union{Signed, Unsigned}, src::ChainedVector) =
Base.copyto!(dest::PermutedDimsArray{T, 1}, src::ChainedVector{T, A} where {A<:AbstractVector{T}}) where {T} =
copyto!(dest, 1, src, 1, length(src))
Base.copyto!(dest::AbstractVector, doffs::Union{Signed,Unsigned}, src::ChainedVector) =
copyto!(dest, doffs, src, 1, length(src))
Base.copyto!(dest::AbstractVector, doffs::Union{Signed, Unsigned}, src::ChainedVector, soffs::Union{Signed, Unsigned}) =
copyto!(dest, doffs, src, soffs, length(src) - soffs + 1)
Expand Down Expand Up @@ -775,7 +779,9 @@ Base.any(x::ChainedVector) = any(y -> any(y), x.arrays)
Base.all(f::Function, x::ChainedVector) = all(y -> all(f, y), x.arrays)
Base.all(x::ChainedVector) = all(y -> all(y), x.arrays)

Base.reduce(op::OP, x::ChainedVector) where {OP} = reduce(op, (reduce(op, y) for y in x.arrays))
for optype in (:Any, :hcat, :vcat)
@eval Base.reduce(op::typeof($optype), x::ChainedVector) = reduce(op, (reduce(op, y) for y in x.arrays))
end
Base.foldl(op::OP, x::ChainedVector) where {OP} = foldl(op, (foldl(op, y) for y in x.arrays))
Base.foldr(op::OP, x::ChainedVector) where {OP} = foldr(op, (foldr(op, y) for y in x.arrays))
Base.mapreduce(f::F, op::OP, x::ChainedVector) where {F, OP} = reduce(op, (mapreduce(f, op, y) for y in x.arrays))
Expand Down Expand Up @@ -900,6 +906,7 @@ function Base.findall(A::ChainedVector{Bool})
end

Base.findall(f::Function, x::ChainedVector) = findall(map(f, x))
Base.findall(f::Base.Fix2{typeof(in)}, x::ChainedVector) = findall(map(f, x))

function Base.filter(f, a::ChainedVector{T}) where {T}
j = 1
Expand Down Expand Up @@ -927,3 +934,6 @@ Base.replace(a::ChainedVector, old_new::Pair...; count::Union{Integer,Nothing}=n
Base.replace!(a::ChainedVector, old_new::Pair...; count::Integer=typemax(Int)) = (foreach(A -> replace!(A, old_new...; count=count), a.arrays); return a)

Base.Broadcast.broadcasted(f::F, A::ChainedVector) where {F} = map(f, A)
function Base.Broadcast.broadcasted(s::S, c::ChainedVector) where {S<:Base.Broadcast.BroadcastStyle}
error("Broadcasting with BroadcastStyle $s and ChainedVector $c is reserved.")
end
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
Copy link
Member

Choose a reason for hiding this comment

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

I'm personally not a fan of test Projects yet; they're undersupported by too many ecosystem tools at the moment.

Copy link
Author

Choose a reason for hiding this comment

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

I wasn't aware or ecosystems that don't support them. But it's OK as long as it works here on Github, right?

Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReTest = "e0db7c4e-2690-44b9-bad6-7687da720f89"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
11 changes: 11 additions & 0 deletions test/SentinelArrayTests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SentinelArrayTests
using ReTest
using Random, SparseArrays

using SentinelArrays

include("sentinelarrays.jl")
include("missingvector.jl")
include("chainedvector.jl")

end
35 changes: 35 additions & 0 deletions test/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,38 @@ end
@test deleteat!(v2, m2) == deleteat!(s2, m2)
end
end

@testset "deleteat! with Bool mask" begin
x = SentinelArray(["a", "b", "c", "d", "e"])
mask = [false, true, false, false, false]
@test deleteat!(x, mask) == ["a", "c", "d", "e"]

for i in 1:100
v1 = collect(string.(1:i))
v2 = copy(v1)
s1 = SentinelArray(copy(v1))
s2 = SentinelArray(copy(v1))
m1 = rand(Bool, i)
m2 = BitVector(m1)
@test deleteat!(v1, m1) == deleteat!(s1, m1)
@test deleteat!(v2, m2) == deleteat!(s2, m2)
end
end

@testset "Method ambiguities" begin
# some objects to use for testing current ambiguities
cv = ChainedVector([[1, 2], [3, 4]], [5, 6])
cv_of_abstractvectors = ChainedVector([[1:4, 2:5], [3:6, 4:7]], [5, 6])
pda_1dim = PermutedDimsArray(1:6 |> collect, [1,])
sv = SparseArrays.SparseVector(6, [2, 3], [2.0, 3.0])
fix2in = Base.Fix2(in, [1, 2])

@test ==(SentinelArrays.ChainedVectorIndex(1, 2, 3, 4), BigInt(21)) isa Any
@test reduce(hcat, cv_of_abstractvectors) isa Any
@test reduce(vcat, cv_of_abstractvectors) isa Any
@test_throws "reserved" Base.broadcasted(Base.Broadcast.ArrayStyle{Matrix}(), cv)
@test copyto!(pda_1dim, cv) isa Any
@test findall(fix2in, cv) isa Any
# I think this should not be fixed by us as long as we don't import SparseArrays:
@test_throws MethodError copyto!(sv, cv) isa Any
end
126 changes: 126 additions & 0 deletions test/missingvector.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

@testset "MissingVector" begin

x = MissingVector(10)
@test all(x .=== missing)
@test length(x) == 10
@test length(x) isa Int
@test Base.IndexStyle(x) == Base.IndexLinear()

y = similar(x, Missing, 5)
@test length(y) == 5

y = empty(x)
@test length(y) == 0

x[1] = missing
x[end] = missing
@test x[1] === missing
@test x[end] === missing

y = similar(x)
@test typeof(y) == MissingVector
@test length(y) == 10
y = similar(x, 20)
@test typeof(y) == MissingVector
@test length(y) == 20

@test isequal(copy(x), x)
empty!(x)
@test length(x) == 0
@test isequal(copy(x), x)

@test_throws ArgumentError resize!(x, -1)
resize!(x, 10)
@test length(x) == 10

push!(x, missing)
@test x[end] === missing
empty!(x)
push!(x, missing)
@test x[1] === x[end] === missing

pushfirst!(x, missing)
@test x[1] === missing
empty!(x)
pushfirst!(x, missing)
@test x[1] === x[end] === missing
pushfirst!(x, missing)

@test pop!(x) === missing
@test popfirst!(x) === missing
@test isempty(x)

@test_throws BoundsError insert!(x, 0, missing)
@test_throws BoundsError insert!(x, 2, missing)
insert!(x, 1, missing)
@test x[1] === missing
insert!(x, 1, missing)
@test x[1] === missing

x = MissingVector(10)
y = MissingVector(10)

z = vcat(x, y)
@test length(z) == 20

empty!(x)
z = vcat(x, y)
@test isequal(z, y)

x = MissingVector(10)
append!(x, y)
@test length(x) == 20

x = MissingVector(10)
append!(x, (missing for _ = 1:10))
@test length(x) == 20

empty!(x)
append!(x, y)
@test isequal(x, y)

x = MissingVector(10)
y = MissingVector(10)

prepend!(y, x)
@test length(y) == 20

y = MissingVector(10)
prepend!(y, (missing for _ = 1:10))
@test length(y) == 20

empty!(y)
prepend!(y, x)
@test isequal(y, x)

x = MissingVector(10)
deleteat!(x, 1)
@test x[1] === missing
deleteat!(x, 1:4)
@test length(x) == 5
deleteat!(x, [2, 4])
@test length(x) == 3

m = similar(x)
@test length(m) == length(x)
m = similar(x, Missing)
@test length(m) == length(x)
@test typeof(m[1:3]) == typeof(m)

deleteat!(x, [true, true, false])
@test length(x) == 1
empty!(x)
@test_throws ArgumentError pop!(x)
@test_throws ArgumentError popfirst!(x)

m = MissingVector(5)
c = ChainedVector([m, m, m])
c2 = copy(c)
@test length(c) == length(c2)
@test c2 isa MissingVector

deleteat!(c2, Int[])
@test length(c2) == 15

end
Loading