C# Quickest Way to Shift Array

C# shifting array to right

Shifting to the right is quite similar so shifting to the left, you just need to save the last element instead of the first one, and iterate from the end backwards:

int t = tab[tab.Length - 1];
for (int i = tab.Length - 1; i > 0; i--)
tab[i] = tab[i - 1];
tab[0] = t;

A fast array shift implementation in C#?

You should use Buffer.BlockCopy instead. It bypasses the array indexing and performs fast memory copies. Keep in mind, BlockCopy copies data in bytes, not in terms of the size of array element, so make sure to use sizeof() to account for that.

C# shift two dimension array fast method

If you want to shift large amounts of data around quickly, use Array.Copy rather than a loop that copies individual characters.

If you swap to a byte array and use Array.Copy or Buffer.BlockCopy you will probably improve the performance a bit more (but if you have to convert to/from character arrays you may lose everything you've gained).

(edit: Now that you've posted example code): If you use references to the array rows then you may be able to shift the references rather than having to move the data itself. Any you can still shift the references using Array.Copy)

But if you change your approach so you don't need to shift the data, you'll gain considerably better performance - not doing the work at all if you can avoid it is always faster! Chances are you can wrap the data in an accessor layer that keeps track of how much the data has been shifted and modifies your indexes to return the data you are after. (This will slightly slow down access to the data, but saves you shifting the data, so may result in a net win - depending on how much you access relative to how much you shift)

shifting array elements to right?

//right shift with modulus
for (int i = 0; i < arr.length; i++) {
demo[(i+1) % demo.length] = arr[i];
}

C# Shift entire array by 1

Moving the elements to the right will require a new array because of the resizing. There is no way to make index 0 disappear. It will have to exist but it can be null.

The code below will accomplish this:

public Operation3D[] shiftRight(Operation3D[] arr)
{
Operation3D[] result = new Operation3D[arr.Length + 1];
Array.Copy(arr, 0, result, 1, arr.Length);
return result;
}

How to shift one array element wrap around?

Just use modulo arithmetic so that instead of writing to the element at index j as you shift, instead write to the element at index j % array.Length. Thusly:

public void FindAndShift<T>(T[] array, T value, int shift) {
int index = Array.IndexOf(array, value);
int shiftsRemaining = shift;
for(int currentPosition = index; shiftsRemaining > 0; shiftsRemaining--) {
array[currentPosition % array.Length] = array[(currentPosition + 1) % array.Length];
}
array[(index + shift) % array.Length] = value;
}

I have excluded error checking.

How to shift items in an array?

I would suggest using a queue, just a special instance of an array or list. When your timed event occurs, pop the last item from the queue, and then push your new item on.



Related Topics



Leave a reply



Submit