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

Relax LazyRow for other packages #271

Open
Moelf opened this issue Apr 7, 2023 · 3 comments
Open

Relax LazyRow for other packages #271

Moelf opened this issue Apr 7, 2023 · 3 comments

Comments

@Moelf
Copy link

Moelf commented Apr 7, 2023

There's no reason why LazyRow can only work with StructArray, for example we had this for a while now:
https://github.com/JuliaHEP/UnROOT.jl/blob/5f9ae4f21f04bf61f56f0ccabe7942754e07f7d9/src/iteration.jl#L196-L199

but essentially for any table-like structure, we simply have:

@inline function Base.getproperty(evt::LazyEvent, s::Symbol)
    @inbounds getproperty(Core.getfield(evt, :tree), s)[Core.getfield(evt, :idx)]
end

and basically this is just saying LazyRow wraps a object that support parent.col[idx]

@Moelf
Copy link
Author

Moelf commented Nov 5, 2023

Bump, any appetite for this?

@aplavin
Copy link
Member

aplavin commented Nov 5, 2023

Would be useful indeed! Also a bit more general, like

struct LazyIndexed{P,I}
    parent::P
    index::I
end

parent(li::LazyIndexed) = getfield(li, :parent)
index(li::LazyIndexed) = getfield(li, :index)
getproperty(li::LazyIndexed, p) = getproperty(parent(li), p)[index(li)]
# StructArrays-specific:
getindex(li::LazyIndexed{<:StructArray}, ix::Union{Integer,Symbol}) = getindex(components(parent(li)), ix)[index(li)]
getindex(li::LazyIndexed{<:StructArray{<:NamedTuple}}, ixs::Tuple{Vararg{Symbol}}) = getindex.(getindex(components(parent(li)), ixs), Ref(index(li)))

This can be a separate small package used in StructArrays, ROOT, etc.
Does that sound reasonable?

On naming, Julia often uses "view" to mean lazy in this context, so I wonder if we can make it more consistent here. view(parent, index) won't work unfortunately, because it already means 0-dim array view and not a scalar.

(disclaimer: not a StructArrays member)

@Moelf
Copy link
Author

Moelf commented Nov 5, 2023

This can be a separate small package used in StructArrays, ROOT, etc.

yeah that makes a lot of sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants