Skip to content

Instantly share code, notes, and snippets.

@carlobaldassi
Created June 19, 2012 11:12
Show Gist options
  • Save carlobaldassi/2953563 to your computer and use it in GitHub Desktop.
Save carlobaldassi/2953563 to your computer and use it in GitHub Desktop.
flatten nested Arrays in a plain Vector
julia> load("flatten1d.jl")
julia> a = [[randi(3) for k=1:3] for i=1:3, j=1:2]
3x2 Array{Int64,1} Array:
[3, 1, 2] [1, 3, 3]
[1, 1, 3] [2, 3, 3]
[3, 1, 3] [3, 2, 2]
julia> flatten1d(a)
18-element Int64 Array:
3
1
2
1
1
3
3
1
3
1
3
3
2
3
3
3
2
2
function deep_numel{T}(a::Array{T})
if T <: Array
n = 0
for i = 1:length(a)
n += deep_numel(a[i])
end
return n
end
return numel(a)
end
deep_numel(a) = 1
function deep_eltype(T::Type)
if isa(T, BitsKind)
return T
elseif T <: Array
return deep_eltype(eltype(T))
else
return Any
end
end
deep_eltype{T}(a::Array{T}) = deep_eltype(T)
function flat_copy{T}(dest::Array, dest_ind::Int, src::Array{T})
if T <: Array
new_dest_ind = dest_ind
for i = 1:length(src)
new_dest_ind += flat_copy(dest, new_dest_ind, src[i])
end
return new_dest_ind - dest_ind
end
copy_to(dest, dest_ind, src, 1, length(src))
return length(src)
end
function flatten1d(a::Array, T::Type)
n = deep_numel(a)
b = Array(T, n)
flat_copy(b, 1, a)
return b
end
flatten1d(a::Array) = flatten1d(a, deep_eltype(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment