Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a33eebb

Browse files
committed
simplify reduction-dim functions
1 parent b088860 commit a33eebb

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

src/reducedim.jl

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ offset(x::Number) = 0
55

66
## auxiliary functions
77

8-
_rtup(siz::NTuple{1,Int}, d::Integer) = (d == 1 ? (true,) : (false,))
8+
reducindicators(siz::NTuple{1,Int}, d::Integer) = (d == 1 ? (true,) : (false,))
99

10-
_rtup(siz::NTuple{2,Int}, d::Integer) = (d == 1 ? (false, true) :
11-
d == 2 ? (true, false) :
12-
(false, false) )
10+
reducindicators(siz::NTuple{2,Int}, d::Integer) = (d == 1 ? (true, false) :
11+
d == 2 ? (false, true) : (false, false))
1312

14-
_rtup(siz::NTuple{3,Int}, d::Integer) = (d == 1 ? (false, false, true) :
15-
d == 2 ? (false, true, false) :
16-
d == 3 ? (true, false, false) :
17-
(false, false, false))
13+
reducindicators(siz::NTuple{3,Int}, d::Integer) = (d == 1 ? (true, false, false) :
14+
d == 2 ? (false, true, false) :
15+
d == 3 ? (false, false, true) : (false, false, false))
1816

19-
_rtup{N}(siz::NTuple{N,Int}, d::Integer) = (ds = fill(false,N); ds[N+1-d]=true; tuple(ds...))::NTuple{N,Bool}
17+
reducindicators{N}(siz::NTuple{N,Int}, d::Integer) = (ds = fill(false,N); ds[d]=true; tuple(ds...))::NTuple{N,Bool}
2018

21-
function _rtup{N}(siz::NTuple{N,Int}, dims::Union(Dims,Vector))
19+
function reducindicators{N}(siz::NTuple{N,Int}, dims::Union(Dims,Vector))
2220
ds = fill(false,N)
2321
for d in dims
24-
ds[N+1-d] = true
22+
ds[d] = true
2523
end
2624
return tuple(ds...)::NTuple{N,Bool}
2725
end
@@ -226,43 +224,44 @@ macro compose_reducedim(fun, AN)
226224
end
227225

228226
global $(_funimpl!)
229-
function $(_funimpl!){Op<:Functor{2}}(op::Op, dst::DenseArray, $(aparams...), dim::Int, insiz::Dims, r1::Bool)
230-
n = insiz[1]::Int
231-
$(getstrides1)
232-
sdst1 = stride(dst, 1)::Int
233-
$(getoffsets)
234-
idst = offset(dst) + 1
235-
$(_funimpl1d!)(op, r1, n, parent(dst), idst, sdst1, $(imargs1d...))
236-
end
237-
238-
function $(_funimpl!){Op<:Functor{2}}(op::Op, dst::DenseArray, $(aparams...), dim::Int, insiz::Dims, r2::Bool, r1::Bool)
239-
m = insiz[1]::Int
240-
n = insiz[2]::Int
241-
$(getstrides2)
242-
sdst1, sdst2 = strides(dst)::(Int, Int)
243-
$(getoffsets)
244-
idst = offset(dst) + 1
245-
$(_funimpl2d!)(op, r2, r1, m, n, parent(dst), idst, sdst1, sdst2, $(imargs2d...))
246-
end
227+
function $(_funimpl!){Op<:Functor{2},N}(op::Op, dst::DenseArray, $(aparams...), dim::Int,
228+
insiz::NTuple{N,Int}, rtup::NTuple{N,Bool})
229+
230+
if dim == 1
231+
n = insiz[1]::Int
232+
$(getstrides1)
233+
sdst1 = stride(dst, 1)::Int
234+
$(getoffsets)
235+
idst = offset(dst) + 1
236+
$(_funimpl1d!)(op, rtup[1], n, parent(dst), idst, sdst1, $(imargs1d...))
237+
238+
elseif dim == 2
239+
m = insiz[1]::Int
240+
n = insiz[2]::Int
241+
$(getstrides2)
242+
sdst1, sdst2 = strides(dst)::(Int, Int)
243+
$(getoffsets)
244+
idst = offset(dst) + 1
245+
$(_funimpl2d!)(op, rtup[2], rtup[1], m, n, parent(dst), idst, sdst1, sdst2, $(imargs2d...))
247246

248-
function $(_funimpl!){Op<:Functor{2}}(op::Op, dst::DenseArray, $(aparams...), dim::Int, insiz::Dims,
249-
rN::Bool, rNm1::Bool, rNm2::Bool, rr::Bool...)
250-
n = insiz[dim]::Int
251-
if rN
252-
_dst = ellipview(dst, 1)
253-
for i = 1:n
254-
$(_funimpl!)(op, _dst, $(eviewargs...), dim-1, insiz, rNm1, rNm2, rr...)
255-
end
256247
else
257-
for i = 1:n
258-
$(_funimpl!)(op, ellipview(dst, i), $(eviewargs...), dim-1, insiz, rNm1, rNm2, rr...)
248+
n = insiz[dim]::Int
249+
if rtup[dim]
250+
_dst = ellipview(dst, 1)
251+
for i = 1:n
252+
$(_funimpl!)(op, _dst, $(eviewargs...), dim-1, insiz, rtup)
253+
end
254+
else
255+
for i = 1:n
256+
$(_funimpl!)(op, ellipview(dst, i), $(eviewargs...), dim-1, insiz, rtup)
257+
end
259258
end
260259
end
261260
end
262261

263262
global $(_fun!)
264263
function $(_fun!){S<:Number,N}(dst::DenseArray{S,N}, op::Functor{2}, $(aparams...), insiz::NTuple{N,Int}, dims::DimSpec)
265-
$(_funimpl!)(op, dst, $(args...), length(insiz), insiz, _rtup(insiz, dims)...)
264+
$(_funimpl!)(op, dst, $(args...), length(insiz), insiz, reducindicators(insiz, dims))
266265
return dst
267266
end
268267

0 commit comments

Comments
 (0)