Mercurial > repos > public > sbplib_julia
comparison src/LazyTensors/tuple_manipulation.jl @ 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 |
comparison
equal
deleted
inserted
replaced
1226:ea5b4fca85e0 | 1227:06b983f0d236 |
---|---|
17 The returned values satisfy | 17 The returned values satisfy |
18 * `length(view_index) == dim_before + dim_view + dim_after` | 18 * `length(view_index) == dim_before + dim_view + dim_after` |
19 * `length(I_middle) == dim_index` | 19 * `length(I_middle) == dim_index` |
20 """ | 20 """ |
21 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} | 21 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} |
22 I_before, I_middle, I_after = split_tuple(I, Val(dim_before), Val(dim_index)) | 22 I_before, I_middle, I_after = @inline split_tuple(I, (dim_before, dim_index, dim_after)) |
23 | 23 |
24 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...) | 24 view_index = (I_before..., ntuple((i)->:, dim_view)..., I_after...) |
25 | 25 |
26 return view_index, I_middle | 26 return view_index, I_middle |
27 end | 27 end |
28 # TBD: If the nice split_tuple works, can this be cleaned up as well? | |
29 | 28 |
30 # TODO: Can this be replaced by something more elegant while still being type stable? 2020-10-21 | |
31 # See: | |
32 # https://github.com/JuliaLang/julia/issues/34884 | |
33 # https://github.com/JuliaLang/julia/issues/30386 | |
34 """ | |
35 slice_tuple(t, Val(l), Val(u)) | |
36 | |
37 Get a slice of a tuple in a type stable way. | |
38 Equivalent to `t[l:u]` but type stable. | |
39 """ | |
40 function slice_tuple(t,::Val{L},::Val{U}) where {L,U} | |
41 return ntuple(i->t[i+L-1], U-L+1) | |
42 end | |
43 | |
44 """ | |
45 split_tuple(t::Tuple{...}, ::Val{M}) where {N,M} | |
46 | |
47 Split the tuple `t` into two parts. the first part is `M` long. | |
48 E.g | |
49 ```julia | |
50 split_tuple((1,2,3,4),Val(3)) -> (1,2,3), (4,) | |
51 ``` | |
52 """ | |
53 function split_tuple(t::NTuple{N,Any},::Val{M}) where {N,M} | |
54 return slice_tuple(t,Val(1), Val(M)), slice_tuple(t,Val(M+1), Val(N)) | |
55 end | |
56 | |
57 """ | |
58 split_tuple(t::Tuple{...},::Val{M},::Val{K}) where {N,M,K} | |
59 | |
60 Same as `split_tuple(t::NTuple{N},::Val{M})` but splits the tuple in three parts. With the first | |
61 two parts having lenght `M` and `K`. | |
62 """ | |
63 function split_tuple(t::NTuple{N,Any},::Val{M},::Val{K}) where {N,M,K} | |
64 p1, tail = split_tuple(t, Val(M)) | |
65 p2, p3 = split_tuple(tail, Val(K)) | |
66 return p1,p2,p3 | |
67 end | |
68 | |
69 # TBD Are the above defs even needed? Can the below one be used without problems? | |
70 | 29 |
71 """ | 30 """ |
72 split_tuple(t, szs) | 31 split_tuple(t, szs) |
73 | 32 |
74 Split the tuple `t` into a set of tuples of the sizes given in `szs`. | 33 Split the tuple `t` into a set of tuples of the sizes given in `szs`. |