I am implementing several datastructures and one primitive I want to use is the following: I have a memory chunk A[N] (it has a variable length, but I take 100 for my examples)
** this only works if the length of C is <= half the length of A. But I'm leaving it up here in hopes of fixing it.**
** this solution will not preserve any of the contents of the target range, a behavior which I believe matches the wording of the original question **
;; A function that wraps an out-of-bounds index to its proper location.
mod'(i):
return (i + length(A)) mod length(A)
;; shifts the range A[i]..A[i + n] to A[i - delta]..A[i - delta + n]
move_backward (i,delta,n):
A[mod'(i - delta)] = A[mod'(i)]
if (n > 0):
move_backward (i + 1, delta, n - 1)
;; shifts the range A[i - n]..A[i] to A[i - n + delta]..A[i + delta]
move_forward (i, delta, n):
A[mod'(i + delta)] = A[mod'(i)]
if (n > 0):
move_forward (i - 1, delta, n - 1)
shift_range (source_first, source_last, target_first):
n = mod'(source_last - source_first)
delta = mod'(target_first - source_first)
if (delta > length(A) / 2):
move_backward (source_first, length(A) - delta, n)
else
move_forward (source_last, delta, n)