Skip to content
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
3 changes: 2 additions & 1 deletion src/ThreadsX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ module Implementations
import SplittablesBase
using ArgCheck: @argcheck, @check
using BangBang: SingletonVector, append!!, push!!, union!!
using Base: Ordering, add_sum, mapreduce_empty, mul_prod, reduce_empty
using Base:
HasShape, IteratorSize, Ordering, add_sum, mapreduce_empty, mul_prod, reduce_empty
using ConstructionBase: setproperties
using InitialValues: asmonoid
using Referenceables: referenceable
Expand Down
4 changes: 4 additions & 0 deletions src/basesizes.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
default_basesize(n::Integer) = max(1, cld(n, (8 * Threads.nthreads())))
default_basesize(xs) =
default_basesize(SplittablesBase.amount(last(extract_transducer(xs))))

default_basesize(_, _, xs) = default_basesize(xs::AbstractArray)

# TODO: handle `Base.Fix2` etc.
Expand Down
16 changes: 12 additions & 4 deletions src/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ __map(f, itr; kwargs...) =
__map(f, itrs...; kwargs...) =
tcollect(MapSplat(f), zip(itrs...); basesize = default_basesize(itrs[1]), kwargs...)

reshape_as(ys, xs) = reshape_as(ys, xs, IteratorSize(xs))
reshape_as(ys, xs, ::IteratorSize) = ys
reshape_as(ys, xs, ::HasShape) = reshape(ys, size(xs))
reshape_as(::Empty{T}, xs, isize::HasShape) where {T<:AbstractVector} =
reshape_as(T(undef, length(xs)), xs, isize)

function _map(f, itr, itrs...; kwargs...)
ys = __map(f, itr, itrs...; kwargs...)
isempty(ys) && return map(f, itr, itrs...)
return ys
return reshape_as(ys, itr)
end

ThreadsX.map(f, itr, itrs...; kwargs...) = _map(f, itr, itrs...; kwargs...)
Expand Down Expand Up @@ -36,8 +42,10 @@ end
struct ConvertTo{T} end
(::ConvertTo{T})(x) where {T} = convert(T, x)

ThreadsX.collect(::Type{T}, itr; kwargs...) where {T} =
tcopy(Map(ConvertTo{T}()), Vector{T}, itr; basesize = default_basesize(itr), kwargs...)
ThreadsX.collect(::Type{T}, itr; kwargs...) where {T} = reshape_as(
tcopy(Map(ConvertTo{T}()), Vector{T}, itr; basesize = default_basesize(itr), kwargs...),
itr,
)

ThreadsX.collect(itr; kwargs...) =
tcollect(itr; basesize = default_basesize(itr), kwargs...)
reshape_as(tcollect(itr; basesize = default_basesize(itr), kwargs...), itr)
3 changes: 0 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
default_basesize(n::Integer) = max(1, cld(n, (8 * Threads.nthreads())))
default_basesize(xs) = default_basesize(length(xs))

function adhoc_partition(xs, n)
@check firstindex(xs) == 1
m = cld(length(xs), n)
Expand Down
10 changes: 10 additions & 0 deletions test/test_with_base.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module TestWithBase

using Base: splat
using Test
using ThreadsX

Expand All @@ -10,9 +11,16 @@ inc(x) = x + 1
raw_testdata = """
collect(1:10)
collect(Float64, 1:10)
collect(x for x in 1:10 if isodd(x))
collect(Float64, (x for x in 1:10 if isodd(x)))
collect(inc(x) for x in 1:10)
collect(Float64, (inc(x) for x in 1:10))
collect(x * y for x in 1:10, y in 11:20)
collect(Float64, (x * y for x in 1:10, y in 11:20))
collect(x * y for x in 1:0, y in 11:20)
collect(Float64, (x * y for x in 1:0, y in 11:20))
map(inc, 1:10)
map(inc, (x for x in 1:10 if isodd(x)))
map(inc, Float64[])
map(inc, ones(3, 3))
map(inc, ones(3, 0))
Expand All @@ -21,6 +29,8 @@ map(*, 1:10, 11:20)
map(*, ones(3, 3), ones(3, 3))
map(*, ones(3, 0), ones(3, 0))
map(*, ones(0, 3), ones(0, 3))
map(splat(*), Iterators.product(1:10, 11:20))
map(splat(*), Iterators.product(1:0, 11:20))
reduce(+, 1:10)
reduce(+, 1:0)
reduce(+, Bool[])
Expand Down