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

Widening-based map() to remove return_type #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 29 additions & 5 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
function Base.map(f, a::StaticArrayLite)
T = Core.Compiler.return_type(f, Tuple{eltype(a)})
out = similar(a, T, size(a))
map!(f, out, a)
return freeze(out)
if length(a) == 0
T = Core.Compiler.return_type(f, Tuple{eltype(a)})
return freeze(similar(a, T, size(a)))
end
@inbounds x = f(a[first(LinearIndices(a))])
out = similar(a, typeof(x), size(a))
freeze(_map_widen!(out, 0, x, f, a))
end

function _map_widen!(out, offset, x, f, a)
a_i1 = first(LinearIndices(a))
out_i1 = first(LinearIndices(out))
T = eltype(out)
while true
@inbounds out[out_i1+offset] = x
offset += 1
if offset >= length(a)
break
end
@inbounds x = f(a[a_i1+offset])
if !(typeof(x) === T || x isa T)
T2 = Base.promote_typejoin(T, typeof(x))
out2 = similar(a, T2, size(a))
copyto!(out2, out_i1, out, out_i1, offset)
return _map_widen!(out2, )
end
end
return out
end

function Base.map(f, a::StaticArrayLite, b::StaticArrayLite)
Expand Down Expand Up @@ -44,4 +68,4 @@ function Base.mapreduce(f, op, a::StaticArrayLite, b::StaticArrayLite; init)
out = op(f(@inbounds(a[i]),@inbounds(b[i])), out)
end
return out
end
end