-
-
Save dterracino/931a9fef06593bc611ca0a69826051ef to your computer and use it in GitHub Desktop.
Algorithm avstraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Towel; | |
class Program | |
{ | |
static void Main() | |
{ | |
int[,] a = new int[,] | |
{ | |
{ 1, 2, 3, }, | |
{ 4, 5, 6, }, | |
{ 7, 8, 9, }, | |
}; | |
int[,] b = new int[,] | |
{ | |
{ 2, 2, 2, }, | |
{ 2, 2, 2, }, | |
{ 2, 2, 2, }, | |
}; | |
int[,] c = new int[3, 3]; | |
Matrix.Add< | |
int, | |
Structs.Get2DArray<int>, | |
Structs.Get2DArray<int>, | |
Structs.IntThree, | |
Structs.IntThree, | |
Structs.IntThree, | |
Structs.IntThree, | |
Structs.Set2DArray<int>, | |
Structs.IntAdd>( | |
getA: a, | |
getB: b, | |
setC: c); | |
Write2DArray(c); | |
int[,] d = new int[3, 3]; | |
Matrix.Add< | |
int, | |
Structs.Get2DArray<int>, | |
Structs.Get2D<int, Structs.IntThree>, | |
Structs.IntThree, | |
Structs.IntThree, | |
Structs.IntThree, | |
Structs.IntThree, | |
Structs.Set2DArray<int>, | |
Structs.IntAdd>( | |
getA: a, | |
setC: d); | |
Console.WriteLine("d:"); | |
Write2DArray(d); | |
Console.WriteLine("Press [enter] to exit..."); | |
Console.ReadLine(); | |
} | |
static void Write2DArray<T>(T[,] array2D) | |
{ | |
for (int i = 0; i < array2D.GetLength(0); i++) | |
{ | |
for (int j = 0; j < array2D.GetLength(1); j++) | |
{ | |
Console.Write(array2D[i, j] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
public static class Structs | |
{ | |
public readonly struct IntThree : IFunc<int> | |
{ | |
public int Do() => 3; | |
} | |
public struct Get2DArray<T> : IFunc<int, int, T> | |
{ | |
internal T[,] _array; | |
public Get2DArray(T[,] array) => _array = array; | |
public T Do(int a, int b) => _array[a, b]; | |
public static implicit operator Get2DArray<T>(T[,] array) => new Get2DArray<T>(array); | |
} | |
public struct Get2D<T, Value> : IFunc<int, int, T> | |
where Value : IFunc<T> | |
{ | |
public T Do(int a, int b) => default(Value).Do(); | |
} | |
public struct Set2DArray<T> : IAction<int, int, T> | |
{ | |
internal T[,] _array; | |
public Set2DArray(T[,] array) => _array = array; | |
public void Do(int a, int b, T c) => _array[a, b] = c; | |
public static implicit operator Set2DArray<T>(T[,] array) => new Set2DArray<T>(array); | |
} | |
public struct IntAdd : IFunc<int, int, int> | |
{ | |
public int Do(int a, int b) => a + b; | |
} | |
} | |
public static class Matrix | |
{ | |
public static void Add< | |
T, | |
GetA, | |
GetB, | |
RowsA, | |
RowsB, | |
ColumnsA, | |
ColumnsB, | |
SetC, | |
Add>( | |
GetA getA = default, | |
GetB getB = default, | |
RowsA rowsA = default, | |
RowsB rowsB = default, | |
ColumnsA columnsA = default, | |
ColumnsB columnsB = default, | |
SetC setC = default, | |
Add add = default) | |
where GetA : IFunc<int, int, T> | |
where GetB : IFunc<int, int, T> | |
where RowsA : IFunc<int> | |
where RowsB : IFunc<int> | |
where ColumnsA : IFunc<int> | |
where ColumnsB : IFunc<int> | |
where SetC : IAction<int, int, T> | |
where Add : IFunc<T, T, T> | |
{ | |
if (rowsA.Do() != rowsB.Do() || columnsA.Do() != columnsB.Do()) | |
{ | |
throw new MathematicsException("dimension miss-match on matrix addition"); | |
} | |
for (int row = 0; row < rowsA.Do(); row++) | |
{ | |
for (int column = 0; column < columnsA.Do(); column++) | |
{ | |
setC.Do(row, column, add.Do(getA.Do(row, column), getB.Do(row, column))); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment