I currently have a working method that shifts the values of one property of objects in an array:
public class Group
{
public Position[] Positions { get; private set; }
public void Shift()
{
var last = Positions[Count - 1].Tile;
for( int i = Count - 1 ; i > 0 ; i-- )
{
// Note this shifts Tile value and not the array itself
Positions[i].Tile = Positions[i - 1].Tile;
}
Positions[0].Tile = last;
}
}
public class Position
{
// [other properties]
public Tile Tile { get; set; }
}
The problem is that this method needs to run a lot in an algorithm and it is the one that consumes most CPU time.
I'm searching for a better way to shift these values and found BlockCopy in this excellent answer: https://stackoverflow.com/a/52465132/1554208
But it's used to shift the array itself. Is there a way to copy the content of a property (here a simple pointer)?
I'm open to:
- Use unsafe pointer methods
- Change the Position class if needed
- Any other suggestion
EDIT
Is there some way to "calculate" the size in bytes of a Position object, "calculate" the address of the Tile property within that object, and use something like Buffer.BlockCopy to copy these values at that specific address in the array?
Tilein an array and shift that.