I'm trying to write the following perl subroutine. Given are an array a
of length n
, an index i
in the array (0<=i<n
an upstream window length u
and a downstream window length d
.
I want to iterate over the values in the upstream window and the downstream window to i
. In the simplest case, this will iterating over the values in a[i-u..i-1]
(upstream window) and a[i+1..i+d]
(downstream window).
For example: if my array is 1 2 3 4 5 6 7 8 9 10
, i=5
and both window sizes are 2
, the upstream values are simply 6 7
and the downstream values are9 10
.
However, there are two complications:
I would like to consider my array is cyclic. If
i
is relatively small (close to 0) or large (close ton
), then one of the windows may not fit in the array. In that case, I want to look at the array as a cyclic one. for example, if my array is1 2 3 4 5 6 7 8 9 10
,i=8
and both window sizes are4
, the upstream values are simply4 5 6 7
but the downstream values are9 10 1 2
.I would prefer some way to iterate over these values without explicitly copying them into a new array, since they might be very long.