Skip to content

Instantly share code, notes, and snippets.

@antonfirsov
Created December 15, 2016 10:45
Show Gist options
  • Save antonfirsov/6c11a2f0a1048e8bcca455c072d14810 to your computer and use it in GitHub Desktop.
Save antonfirsov/6c11a2f0a1048e8bcca455c072d14810 to your computer and use it in GitHub Desktop.
public struct Fast2DArray<T>
{
public T[] Data;
public int Width;
public int Height;
public Fast2DArray(T[,] data)
{
this.Height = data.GetLength(0);
this.Width = data.GetLength(1);
this.Data = new T[this.Width*this.Height];
// TODO: Flatten data into Data
}
public T this[int i, int j]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return this.Data[i * this.Width + j];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
this.Data[i * this.Width + j] = value;
}
}
}
public class ArrayUser
{
public static readonly Fast2DArray<bool> Pattern = new Fast2DArray<bool>(
new bool[,]
{
{true, false, true },
{false, true, false}
}
);
public void Foo()
{
var blah = Pattern[1, 0];
Console.WriteLine(blah);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment