Skip to content

Standard API for push_widen! pattern #34478

Open

Description

There is a common pattern for handling not knowing the type of something produced in an iterative fashion, and gathering it up into the smallest container.
Basically you start with the container able something (This bit I think sometimes varies, it could be the first elements eltype, could be result of @default_eltype),
then you get the next thing you want to put in, if it fits then you push it in and continue,
if not then you make a new collection that can hold the union, copy everything over and then repeat.

The core of it is in push_widen! but bits of the setup are in the Set construct, in unique! and in other places

julia/base/array.jl

Lines 681 to 716 in 2d57411

function grow_to!(dest, itr)
y = iterate(itr)
y === nothing && return dest
dest2 = empty(dest, typeof(y[1]))
push!(dest2, y[1])
grow_to!(dest2, itr, y[2])
end
function push_widen(dest, el)
@_inline_meta
new = sizehint!(empty(dest, promote_typejoin(eltype(dest), typeof(el))), length(dest))
if new isa AbstractSet
# TODO: merge back these two branches when copy! is re-enabled for sets/vectors
union!(new, dest)
else
append!(new, dest)
end
push!(new, el)
return new
end
function grow_to!(dest, itr, st)
T = eltype(dest)
y = iterate(itr, st)
while y !== nothing
el, st = y
if el isa T || typeof(el) === T
push!(dest, el::T)
else
new = push_widen(dest, el)
return grow_to!(new, itr, st)
end
y = iterate(itr, st)
end
return dest
end

We should look into a standard and public way to do this.
Its wanted in JuliaCollections/DataStructures.jl#568 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    collectionsData structures holding multiple items, e.g. setsfeatureIndicates new feature / enhancement requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions