数组中的位置(偏移量)c#
Position in arrays (offset) c#
我遇到了无法有效解决的问题。
我需要做的是:
我在数组中有一个起始位置(在我的例子中是列表),我也有一个偏移量。
int类型的偏移量:
当偏移量 > 0 时,我用这个来计算新位置:
if (currentPosition + offset < lenght)
{
return currentPosition + offset;
}
return (currentPosition + offset)%lenght;
问题是当偏移量 < 0 时:
for (int i = 0; i < offset * -1; i++)
{
currentPosition -= 1;
if (currentPosition == -1)
{
currentPosition = lenght - 1;
}
}
return currentPosition;
但是这个解决方案真的很慢。
你们有什么想法吗?
提前谢谢你。
看起来 currentPosition
是一个整数。所以你可以只做计算,如果小于零则在之后更正;
currentPosition = (currentPosition + offset) % lenght;
if (currentPosition<0)
currentPosition += lenght;
return currentPosition;
我想出了这个功能,希望它能有所帮助(为清楚起见添加了 in-code 评论):
private int CalcNewPosition(int[] arr, int position, int offset)
{
if (position < 0 || position >= arr.Length)
throw new ArgumentOutOfRangeException("position");
// Calculate correct offset that is within bounds of array
// by using modulus of offset divided by array length.
var offsetOk = offset % arr.Length;
// If offset is negative, calculate how many steps to
// move forward instead of backwards.
if (offsetOk < 0)
{
offsetOk = arr.Length + offsetOk;
}
// Calculate new offset
var result = position + offsetOk;
// If offset is greater or equal than length of array
// set it to number of elements from beginning by
// calculating the difference between length and new offset
if (result >= arr.Length)
{
result = result - arr.Length;
}
return result;
}
我已经用这个调用试过了,它们都工作正常(我希望):
var pos1 = CalcNewPosition(arr, 3, 2);
var pos2 = CalcNewPosition(arr, 3, -1);
var pos3 = CalcNewPosition(arr, 3, -56);
希望对您有所帮助。
给出
(A) 0 < length && length <= int.MaxValue / 3
(B) 0 <= position && position < length
(C) -length < offset && offset < length
计算可以是
position = (position + offset + length) % length;
如果(C)不成立,我们可以把它变成与offset % length
相同的情况,公式将改为
position = (position + (offset % length) + length) % length;
我遇到了无法有效解决的问题。
我需要做的是:
我在数组中有一个起始位置(在我的例子中是列表),我也有一个偏移量。
int类型的偏移量:
当偏移量 > 0 时,我用这个来计算新位置:
if (currentPosition + offset < lenght)
{
return currentPosition + offset;
}
return (currentPosition + offset)%lenght;
问题是当偏移量 < 0 时:
for (int i = 0; i < offset * -1; i++)
{
currentPosition -= 1;
if (currentPosition == -1)
{
currentPosition = lenght - 1;
}
}
return currentPosition;
但是这个解决方案真的很慢。 你们有什么想法吗? 提前谢谢你。
看起来 currentPosition
是一个整数。所以你可以只做计算,如果小于零则在之后更正;
currentPosition = (currentPosition + offset) % lenght;
if (currentPosition<0)
currentPosition += lenght;
return currentPosition;
我想出了这个功能,希望它能有所帮助(为清楚起见添加了 in-code 评论):
private int CalcNewPosition(int[] arr, int position, int offset)
{
if (position < 0 || position >= arr.Length)
throw new ArgumentOutOfRangeException("position");
// Calculate correct offset that is within bounds of array
// by using modulus of offset divided by array length.
var offsetOk = offset % arr.Length;
// If offset is negative, calculate how many steps to
// move forward instead of backwards.
if (offsetOk < 0)
{
offsetOk = arr.Length + offsetOk;
}
// Calculate new offset
var result = position + offsetOk;
// If offset is greater or equal than length of array
// set it to number of elements from beginning by
// calculating the difference between length and new offset
if (result >= arr.Length)
{
result = result - arr.Length;
}
return result;
}
我已经用这个调用试过了,它们都工作正常(我希望):
var pos1 = CalcNewPosition(arr, 3, 2);
var pos2 = CalcNewPosition(arr, 3, -1);
var pos3 = CalcNewPosition(arr, 3, -56);
希望对您有所帮助。
给出
(A) 0 < length && length <= int.MaxValue / 3
(B) 0 <= position && position < length
(C) -length < offset && offset < length
计算可以是
position = (position + offset + length) % length;
如果(C)不成立,我们可以把它变成与offset % length
相同的情况,公式将改为
position = (position + (offset % length) + length) % length;