diff src/LazyTensors/tuple_manipulation.jl @ 1232:a8fa8c1137cc refactor/grids

Merge refactor/LazyTensors/tuple_manipulation
author Jonatan Werpers <jonatan@werpers.com>
date Sun, 19 Feb 2023 22:07:57 +0100
parents de6a9635f293
children d7bc11053951
line wrap: on
line diff
--- a/src/LazyTensors/tuple_manipulation.jl	Sat Feb 18 11:37:35 2023 +0100
+++ b/src/LazyTensors/tuple_manipulation.jl	Sun Feb 19 22:07:57 2023 +0100
@@ -1,11 +1,12 @@
 """
-    split_index(::Val{dim_before}, ::Val{dim_view}, ::Val{dim_index}, ::Val{dim_after}, I...)
+    split_index(dim_before, dim_view, dim_index, 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)
+```julia-repl
+julia> LazyTensors.split_index(1, 3, 2, 1, (1,2,3,4)...)
+((1, Colon(), Colon(), Colon(), 4), (2, 3))
 ```
 
 `dim_view` controls how many colons are in the view, and `dim_index` controls
@@ -18,62 +19,52 @@
  * `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 = split_tuple(I, Val(dim_before), Val(dim_index))
+function split_index(dim_before, dim_view, dim_index, dim_after, I...)
+    @inline
+    I_before, I_middle, I_after = 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
 
-# TODO: Can this be replaced by something more elegant while still being type stable? 2020-10-21
-# See:
-# https://github.com/JuliaLang/julia/issues/34884
-# https://github.com/JuliaLang/julia/issues/30386
-"""
-    slice_tuple(t, Val(l), Val(u))
-
-Get a slice of a tuple in a type stable way.
-Equivalent to `t[l:u]` but type stable.
-"""
-function slice_tuple(t,::Val{L},::Val{U}) where {L,U}
-    return ntuple(i->t[i+L-1], U-L+1)
-end
 
 """
-    split_tuple(t::Tuple{...}, ::Val{M}) where {N,M}
+    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)`.
 
-Split the tuple `t` into two parts. the first part is `M` long.
 E.g
-```julia
-split_tuple((1,2,3,4),Val(3)) -> (1,2,3), (4,)
+```julia-repl
+julia> LazyTensors.split_tuple((1,2,3,4,5,6), (3,1,2))
+((1, 2, 3), (4,), (5, 6))
 ```
 """
-function split_tuple(t::NTuple{N,Any},::Val{M}) where {N,M}
-    return slice_tuple(t,Val(1), Val(M)), slice_tuple(t,Val(M+1), Val(N))
+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
 
-"""
-    split_tuple(t::Tuple{...},::Val{M},::Val{K}) where {N,M,K}
-
-Same as `split_tuple(t::NTuple{N},::Val{M})` but splits the tuple in three parts. With the first
-two parts having lenght `M` and `K`.
-"""
-function split_tuple(t::NTuple{N,Any},::Val{M},::Val{K}) where {N,M,K}
-    p1, tail = split_tuple(t, Val(M))
-    p2, p3 = split_tuple(tail, Val(K))
-    return p1,p2,p3
+function sizes_to_ranges(szs)
+    cum_szs = cumsum((0, szs...))
+    return ntuple(i->cum_szs[i]+1:cum_szs[i+1], length(szs))
 end
 
 
 """
-    flatten_tuple(t)
+    concatenate_tuples(t...)
 
-Takes a nested tuple and flattens the whole structure
+Concatenate tuples.
 """
-flatten_tuple(t::NTuple{N, Number} where N) = t
-flatten_tuple(t::Tuple) = ((flatten_tuple.(t)...)...,) # simplify?
-flatten_tuple(ts::Vararg) = flatten_tuple(ts)
+concatenate_tuples(t::Tuple,ts::Vararg{Tuple}) = (t..., concatenate_tuples(ts...)...)
+concatenate_tuples(t::Tuple) = t
+
 
 """
     left_pad_tuple(t, val, N)