gpt4 book ai didi

c# - 如何在复杂数据类型上做表达式树

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:15 28 4
gpt4 key购买 nike

我必须承认,我对 C# 中的表达式树完全陌生,但目前有必要习惯它。我的问题是我有两个包含数组数组的数据类型。为此,我创建了一个名为 IArray<>

的接口(interface)
    /// <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 定义如下:

   /// <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);
}

有两种实现接口(interface)的类型。对于大部分数据,我必须对数据执行数学运算。目前我已经创建了自己的表达式求值器,但我更愿意使用表达式树,因为在我看来它更灵活。我将如何在给定接口(interface)上实现像 +、- 这样的数学运算?

我有一种类型叫做 VoxelVolume,它用于存储 3d 图像数据 block 。 VoxelVolume 实现 IArray:

public abstract class VoxelVolume : IArray<float>
{
}

假设我有 3 个体素体积 A、B 和 C。现在我要执行一个操作:

VoxelVolume D = (A + B) * C;

目前,我正在使用运算符重载来执行此操作,并且效果很好。唯一的问题是,表达式是逐个计算的,表达式越长,花费的时间和内存就越多。我更愿意将操作合并为一个步骤。这就是我当前的实现所做的

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;
}

但正如您可能认识到的那样,我必须为所有不同的操作键入很多 +、-、*、/以及更多。出于这个原因,我希望有一种更通用和更灵活的方式来执行此操作。

谢谢马丁

最佳答案

在您的示例中,我假设在您的标准代码中运行 GetData() 调用是合理的,尽管由于我们可能不知道“深度”,我们可以简化为锯齿状数组(矩形数组也可以,但在所有点上都更难处理,所以我们不要这样做)。所以想象我们有一个锯齿状的数组而不是 a,b,c(尽管我们假设答案是 d 很简单)。因此,我们实际上需要构建:

= ((jagged[0])[n] + (jagged[1])[n]) * (jagged[2])[n]

也就是,作为一棵树:

= *( +((jagged[0])[n], (jagged[1])[n]), (jagged[2])[n])

因此我们可以通过以下方式构建表达式(并求值):

var data = Expression.Parameter(typeof (float[][]));
var n = Expression.Parameter(typeof (int));

var body =
Expression.Multiply( // *
Expression.Add( // +
Expression.ArrayIndex(
Expression.ArrayIndex(data, Expression.Constant(0)), n), // a[n]
Expression.ArrayIndex(
Expression.ArrayIndex(data, Expression.Constant(1)), n) // b[n]
),
Expression.ArrayIndex(
Expression.ArrayIndex(data, Expression.Constant(2)), n) // c[n]
);
var func = Expression.Lambda<Func<float[][], int, float>>(body, data, n).Compile();


// here, a===jagged[0], b===jagged[1], c===jagged[2]
float[][] jagged = new[] { new[] { 1F, 2F }, new[] { 3F, 4F }, new[] { 5F, 6F } };
for(int i = 0; i < 2; i++)
{
Console.WriteLine("{0}: {1}", i, func(jagged, i));
}

很明显,您的实际代码需要解析您的输入表达式并灵活地构建相似的树;但这应该足以说明一般方法。

关于c# - 如何在复杂数据类型上做表达式树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6813133/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com