view src/LazyTensors/tuple_manipulation.jl @ 1228:73f262a0a384 refactor/LazyTensors/tuple_manipulation

Add @inline to definition of split_tuple
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 19 Feb 2023 11:41:40 +0100
parents 06b983f0d236
children 8f4259fbd39c
line wrap: on
line source

"""
    split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...)

Splits the multi-index `I` into two parts. One part which is expected to be
used as a view, and one which is expected to be used as an index.
Eg.
```
split_index(Val(1),Val(3),Val(2),Val(1),(1,2,3,4)) -> (1,:,:,:,4), (2,3)
```

`dim_view` controls how many colons are in the view, and `dim_index` controls
how many elements are extracted from the middle.
`dim_before` and `dim_after` decides the length of the index parts before and after the colons in the view index.

Arguments should satisfy `length(I) == dim_before+B_domain+dim_after`.

The returned values satisfy
 * `length(view_index) == dim_before + dim_view + dim_after`
 * `length(I_middle) == dim_index`
"""
function split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...) where {dim_before,dim_view, dim_index,dim_after}
    I_before, I_middle, I_after = @inline split_tuple(I, (dim_before, dim_index, dim_after))

    view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...)

    return view_index, I_middle
end


"""
    split_tuple(t, szs)

Split the tuple `t` into a set of tuples of the sizes given in `szs`.
`sum(szs)` should equal `lenght(t)`.

E.g
```julia
split_tuple((1,2,3,4,5,6), (3,1,2)) -> (1,2,3),(4,),(5,6)
```
"""
function split_tuple(t, szs)
    @inline
    if length(t) != sum(szs; init=0)
        throw(ArgumentError("length(t) must equal sum(szs)"))
    end

    rs = sizes_to_ranges(szs)
    return map(r->t[r], rs)
end

function sizes_to_ranges(szs)
    cum_szs = cumsum((0, szs...))
    return ntuple(i->cum_szs[i]+1:cum_szs[i+1], length(szs))
end

concatenate_tuples(t::Tuple,ts::Vararg{Tuple}) = (t..., concatenate_tuples(ts...)...)
concatenate_tuples(t::Tuple) = t


"""
    left_pad_tuple(t, val, N)

Left pad the tuple `t` to length `N` using the value `val`.
"""
function left_pad_tuple(t, val, N)
    if N < length(t)
        throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements"))
    end

    padding = ntuple(i->val, N-length(t))
    return (padding..., t...)
end

"""
    right_pad_tuple(t, val, N)

Right pad the tuple `t` to length `N` using the value `val`.
"""
function right_pad_tuple(t, val, N)
    if N < length(t)
        throw(DomainError(N, "Can't pad tuple of length $(length(t)) to $N elements"))
    end

    padding = ntuple(i->val, N-length(t))
    return (t..., padding...)
end