changeset 1227:06b983f0d236 refactor/LazyTensors/tuple_manipulation

Delete complicated versions of split_tuple and slice_tuple
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 18 Feb 2023 21:56:29 +0100
parents ea5b4fca85e0
children 73f262a0a384
files src/LazyTensors/tuple_manipulation.jl test/LazyTensors/tuple_manipulation_test.jl
diffstat 2 files changed, 6 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/src/LazyTensors/tuple_manipulation.jl	Sat Feb 18 21:55:55 2023 +0100
+++ b/src/LazyTensors/tuple_manipulation.jl	Sat Feb 18 21:56:29 2023 +0100
@@ -19,54 +19,13 @@
  * `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))
+    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
-# TBD: If the nice split_tuple works, can this be cleaned up as well?
 
-# 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 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,)
-```
-"""
-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))
-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
-end
-
-# TBD Are the above defs even needed? Can the below one be used without problems?
 
 """
     split_tuple(t, szs)
--- a/test/LazyTensors/tuple_manipulation_test.jl	Sat Feb 18 21:55:55 2023 +0100
+++ b/test/LazyTensors/tuple_manipulation_test.jl	Sat Feb 18 21:56:29 2023 +0100
@@ -15,43 +15,7 @@
     @inferred LazyTensors.split_index(Val(2),Val(3),Val(2),Val(2),1,2,3,2,2,4)
 end
 
-@testset "slice_tuple" begin
-    @test LazyTensors.slice_tuple((1,2,3),Val(1), Val(3)) == (1,2,3)
-    @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(2), Val(5)) == (2,3,4,5)
-    @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(1), Val(3)) == (1,2,3)
-    @test LazyTensors.slice_tuple((1,2,3,4,5,6),Val(4), Val(6)) == (4,5,6)
-end
-
 @testset "split_tuple" begin
-    @testset "2 parts" begin
-        @test LazyTensors.split_tuple((),Val(0)) == ((),())
-        @test LazyTensors.split_tuple((1,),Val(0)) == ((),(1,))
-        @test LazyTensors.split_tuple((1,),Val(1)) == ((1,),())
-
-        @test LazyTensors.split_tuple((1,2,3,4),Val(0)) == ((),(1,2,3,4))
-        @test LazyTensors.split_tuple((1,2,3,4),Val(1)) == ((1,),(2,3,4))
-        @test LazyTensors.split_tuple((1,2,3,4),Val(2)) == ((1,2),(3,4))
-        @test LazyTensors.split_tuple((1,2,3,4),Val(3)) == ((1,2,3),(4,))
-        @test LazyTensors.split_tuple((1,2,3,4),Val(4)) == ((1,2,3,4),())
-
-        @test LazyTensors.split_tuple((1,2,true,4),Val(3)) == ((1,2,true),(4,))
-
-        @inferred LazyTensors.split_tuple((1,2,3,4),Val(3))
-        @inferred LazyTensors.split_tuple((1,2,true,4),Val(3))
-    end
-
-    @testset "3 parts" begin
-        @test LazyTensors.split_tuple((),Val(0),Val(0)) == ((),(),())
-        @test LazyTensors.split_tuple((1,2,3),Val(1), Val(1)) == ((1,),(2,),(3,))
-        @test LazyTensors.split_tuple((1,true,3),Val(1), Val(1)) == ((1,),(true,),(3,))
-
-        @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(1),Val(2)) == ((1,),(2,3),(4,5,6))
-        @test LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2)) == ((1,2,3),(4,5),(6,))
-
-        @inferred LazyTensors.split_tuple((1,2,3,4,5,6),Val(3),Val(2))
-        @inferred LazyTensors.split_tuple((1,true,3),Val(1), Val(1))
-    end
-
     @testset "general" begin
         @test LazyTensors.split_tuple((),()) == ()
         @test LazyTensors.split_tuple((),(0,)) == ((),)
@@ -73,6 +37,11 @@
         split_tuple_static(t, ::Val{SZS}) where {SZS} = @inline LazyTensors.split_tuple(t,SZS)
 
         @inferred split_tuple_static((1,2,3,4,5,6), Val((3,1,2)))
+
+        @inferred split_tuple_static((1,2,3,4),Val((3,1)))
+        @inferred split_tuple_static((1,2,true,4),Val((3,1)))
+        @inferred split_tuple_static((1,2,3,4,5,6),Val((3,2,1)))
+        @inferred split_tuple_static((1,true,3),Val((1,1,1)))
     end
 end