From b41146a4289c7333a9e5c20c253d0ccbd9d02140 Mon Sep 17 00:00:00 2001 From: Carlo Lucibello Date: Tue, 28 Jun 2022 15:12:47 +0200 Subject: [PATCH] implement getrows for RowTable and ColumnTable --- src/Tables.jl | 5 +++-- src/namedtuples.jl | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Tables.jl b/src/Tables.jl index f4bed73..06cb85c 100644 --- a/src/Tables.jl +++ b/src/Tables.jl @@ -568,14 +568,15 @@ end """ getrows(x, inds; view=nothing) -Return one or more rows from table `x` according to the position(s) specified by `inds`. +Return one or more rows from table `x` according to the position(s) specified by `inds`: - If `inds` is a single integer return a row object. - If `inds` is a collection of integers, return a table object. - In this case,t he returned type is not necessarily the same as the original table type. + In this case, the returned type is not necessarily the same as the original table type. The `view` argument influences whether the returned object is a view of the original table or an independent copy: + - If `view=nothing` (the default) then the implementation for a specific table type is free to decide whether to return a copy or a view. - If `view=true` then a view is returned and if `view=false` a copy is returned. diff --git a/src/namedtuples.jl b/src/namedtuples.jl index 7b2f3e5..e29c3e7 100644 --- a/src/namedtuples.jl +++ b/src/namedtuples.jl @@ -106,6 +106,14 @@ function rowtable(itr::T) where {T} return collect(namedtupleiterator(eltype(r), r)) end +function getrows(x::RowTable, inds; view::Union{Bool,Nothing} = nothing) + if view === true + return view(x, inds) + else + return x[inds] + end +end + # NamedTuple of arrays of matching dimensionality const ColumnTable = NamedTuple{names, T} where {names, T <: NTuple{N, AbstractArray{S, D} where S}} where {N, D} rowcount(c::ColumnTable) = length(c) == 0 ? 0 : length(c[1]) @@ -173,3 +181,11 @@ function columntable(itr::T) where {T} return columntable(schema(cols), cols) end columntable(x::ColumnTable) = x + +function getrows(x::ColumnTable, inds; view::Union{Bool,Nothing} = nothing) + if view === true + return map(c -> view(c, inds), x) + else + return map(c -> c[inds], x) + end +end