[英]Is it possible to implement generic-dimensional data structures?
I'm writing some of my own data structures, such as binary and quad trees, and kD-trees. 我正在编写一些自己的数据结构,例如二进制树和四叉树以及kD树。 Is it possible to write them in such a way that allows for any number of dimensions?
是否可以以允许任意数量的尺寸来编写它们?
Something like: 就像是:
KDTree<2, string> twoDTree = new KDTree<2, string>();
twoDTree[3,4] = "X,Y = (3,4)";
KDTree<4, string> fourDTree = new KDTree<4, string>();
fourDTree[0,1,2,3] = "x1,x2,x3,x4 = (0,1,2,3)";
The only solution I have now is to explicitly create each dimension as it's own class: 我现在唯一的解决方案是显式创建每个维,因为它们是自己的类:
TwoDTree<string> twoDTree = new TwoDTree<string>();
twoDTree[3,4] = "X,Y = (3,4)";
FourDTree<string> fourDTree = new FourDTree<string>();
fourDTree[0,1,2,3] = "x1,x2,x3,x4 = (0,1,2,3)";
But this copy-pastes a ton of code, which should be able to be reused somehow. 但这会复制粘贴大量的代码,应该可以以某种方式重用它们。
Not really, but I see more options: 并非如此,但我看到了更多选择:
Pass the dimensions into the constructor and use a indexer like this: 将尺寸传递到构造函数中,并使用像这样的索引器:
public string this[params int[] indexes] {
get {
// return the data fr the index
}
}
This has the downside of not being "typesafe" at compile time (eg the dimensions passed in will not be checked). 这具有在编译时不“类型安全”的缺点(例如,将不检查传入的尺寸)。
Or create a bunch of interfaces and use Reflection.Emit to create instances which implement the correct interface at runtime: 或创建一堆接口并使用Reflection.Emit创建实例,这些实例在运行时实现正确的接口:
public interface IMultiDimensional<T> {
int Dimensions {
get;
}
int Rank(int dimension);
}
public interface I1Dimensional<T>: IMultiDimensional<T> {
T this[int index] {
get;
set;
}
}
public interface I2Dimensional<T>: IMultiDimensional<T> {
T this[int index1, int index2] {
get;
set;
}
}
public interface I3Dimensional<T>: IMultiDimensional<T> {
T this[int index1, int index2, int index3] {
get;
set;
}
}
public interface I4Dimensional<T>: IMultiDimensional<T> {
T this[int index1, int index2, int index3, int index4] {
get;
set;
}
}
public static TDimensional CreateMulti<TDimensional, TType>() where T: IMultiDimensional<TType> {
// emit a class with Reflection.Emit here that implements the interface
}
I4Dimensional<string> four = CreateMulti<I4Dimensional<string>, string>();
four[1,2,3,4] = "abc";
You can use a multidimensional array as a generic parameter, like so: 您可以将多维数组用作通用参数,如下所示:
KDTree<string[,,,]>
However, you would not be able to write generic code that indexed into the multidimensional array, without exposing it to the caller: 但是,如果不将其公开给调用者,就无法编写索引到多维数组的通用代码:
public class KDTree<MDT> {
// ...
public MDT Data { get; }
}
var twoTree = new KDTree<string[,]>();
twoTree.Data[3,4] = "X,Y = (3,4)";
You could also consider using jagged arrays rather than multidimensional arrays. 您还可以考虑使用锯齿状数组而不是多维数组。 You could then create a generic class that defined the type of the data, and in the constructor specify how many dimensions to use:
然后,您可以创建定义数据类型的通用类,并在构造函数中指定要使用的维数:
public class KDTree<T> {
private readonly T[][] m_Data;
public KDTree( int rows, int columns ) {
m_Data = new T[rows][];
for( int r = 0; r < rows; r++ )
m_Data[r] = new T[columns];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.