Skip to content

Commit

Permalink
Merge pull request #6 from tkoolen/fix-ambiguities
Browse files Browse the repository at this point in the history
Fix ambiguities with Base.map!, add test.
  • Loading branch information
tkoolen authored Aug 13, 2017
2 parents 37281df + 985e95d commit fc46868
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/TypeSortedCollections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,14 @@ end
@inline indices_match(vali::Val, indices::Vector{Int}, a1, as...) = indices_match(vali, indices, a1) && indices_match(vali, indices, as...)
@noinline indices_match_fail() = throw(ArgumentError("Indices of TypeSortedCollections do not match."))

@generated function Base.map!(f, As::Union{<:TypeSortedCollection{<:Any, N}, AbstractVector}...) where {N}
@generated function Base.map!(f, dest::Union{TypeSortedCollection{<:Any, N}, AbstractArray}, args::Union{TypeSortedCollection{<:Any, N}, AbstractArray}...) where {N}
expr = Expr(:block)
push!(expr.args, :(leading_tsc = first_tsc(As...)))
push!(expr.args, :(dest = As[1]))
push!(expr.args, :(args = Base.tail(As)))
push!(expr.args, :(leading_tsc = first_tsc(dest, args...)))
for i = 1 : N
vali = Val(i)
push!(expr.args, quote
let inds = leading_tsc.indices[$i]
@boundscheck indices_match($vali, inds, As...) || indices_match_fail()
@boundscheck indices_match($vali, inds, dest, args...) || indices_match_fail()
for j in linearindices(inds)
vecindex = inds[j]
_setindex!($vali, j, vecindex, dest, f(_getindex_all($vali, j, vecindex, args...)...))
Expand Down
10 changes: 9 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using TypeSortedCollections
using Base.Test

base_ambiguities = detect_ambiguities(Base, Core)

using TypeSortedCollections

module M
f(x::Int64) = 3 * x
f(x::Float64) = round(Int64, x / 2)
Expand All @@ -11,6 +14,11 @@ g(x::Int64, y1::Float64, y2::Float64) = x * y1 - y2
g(x::Float64, y1::Float64, y2::Float64) = x + y1 - y2
end

@testset "ambiguities" begin
tsc_ambiguities = setdiff(detect_ambiguities(TypeSortedCollections, Base, Core), base_ambiguities)
@test isempty(tsc_ambiguities)
end

@testset "general collection interface" begin
x = Number[3.; 4; 5]
sortedx = TypeSortedCollection(x)
Expand Down

0 comments on commit fc46868

Please sign in to comment.