public void Rotate(int idx, int n) { var t = _stack.Top() - 1; /* end of stack segment being rotated */ var p = _stack.AbsIndex(idx) - 1; /* start of segment */ var m = n >= 0 ? t - n : p - n - 1; /* end of prefix */ _stack.Reverse(p, m); /* reverse the prefix with length 'n' */ _stack.Reverse(m + 1, t); /* reverse the suffix */ _stack.Reverse(p, t); }