I must admit that I'm absolutely new to Expression Trees in C#, but currently there is a necessity to get used to it. My problem is that I have two DataTypes that contain an array of arrays. For that reason I created an interface which is called IArray<>
/// <summary>
/// Interface for all classes that provide a list of IArrayData
/// </summary>
public interface IArray<ElementDataType>
where ElementDataType : struct
{
/// <summary>
/// Returns a single data out fo the array container.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
IArrayData<ElementDataType> GetElementData(int index);
/// <summary>
/// Returns the amount of ArrayData.
/// </summary>
int Count
{
get;
}
/// <summary>
/// Returns the size of a single dataset in number of elements
/// (not in bytes!).
/// </summary>
/// <returns></returns>
int GetDataSize();
/// <summary>
/// Creates a copy of the array data provider, by copying the metainformation, but not the
/// contents.
/// </summary>
/// <returns></returns>
IArray<ElementDataType> CloneStub();
}
IArrayData is defined as following:
/// <summary>
/// DataProvider that provides the internal data as an array.
/// </summary>
/// <typeparam name="NativeDataType"></typeparam>
public interface IArrayData<NativeDataType> where NativeDataType : struct
{
/// <summary>
/// Returns the data in an arbitrary format.
/// The implementor must take care of an appropriate cast.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T[] GetData<T>() where T : struct;
/// <summary>
/// Returns the data as float[].
/// </summary>
/// <returns></returns>
float[] GetData();
/// <summary>
/// Sets the data in an arbitrary format.
/// The implementor must take care of an appropriate cast.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data_in"></param>
void SetData<T>(T[] data_in) where T : struct;
/// <summary>
/// Sets the data as float[].
/// </summary>
/// <param name="data_in"></param>
void SetData(float[] data_in);
}
There are two types that implement the interface. On most of the data I have to perform mathematical operations on the data. Currently I have created my own expression evaluator, but I would love to use the expression trees because it seems to me that it's more flexible. How would I implement a mathematical operation like +, - on the given interface?
One type I have is what I call VoxelVolume, which is to store a 3d block of image data. VoxelVolume implements IArray:
public abstract class VoxelVolume : IArray<float>
{
}
Lets assume I have 3 VoxelVolumes A, B, and C. Now I want to perform an operation:
VoxelVolume D = (A + B) * C;
Currently I'm doing this with operator overloading and it works quite well. The only problem is, that the expression is evaluated operation by operation and the longer the expression is the more time and memory it takes. I would prefer to combine the operation in one single step. This is what my current implementation does
public static IArray<float> AddMul(IArray<float> A, IArray<float> B, IArray<float> C)
{
IArray<float> Result = A.CloneStub();
int L = A.Count;
int N = A.GetDataSize();
for (int l = 0; l < L; l++)
{
float[] a = A.GetElementData(l).GetData();
float[] b = B.GetElementData(l).GetData();
float[] c = C.GetElementData(l).GetData();
float[] d = new float[a.Length];
for (int n = 0; n < N; n++)
{
d[n] = (a[n] + b[n]) * c[n];
}
Result.GetElementData(l).SetData(d);
}
return Result;
}
But as you may recognize I have to type a lot for all different operations +,-,*,/ and a lot more. For that reasons I'd like to have a more generic and flexible way to perform this operations.
Thanks Martin