简体   繁体   English

是否可以实现通用维数据结构?

[英]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.

相关问题 如何使用注入的功能实现结构的通用层次结构 - How to implement a generic hierarchy of structures with injected functionality 是否可以实现自定义安全转换(在任意抽象数据结构之间使用“as”)? - Is it possible to implement custom safe cast (using "as" between arbitrary abstract data structures)? 是否有一组C#数据只读通用集合/数据结构? - Is there a set of C# data readonly generic collections / data structures? 是否有可能根据通用方法的类型实现注册表? - Is it possible to implement a registry of generic methods base on their types? C# - 修改特定于每种类型的数据结构的通用函数 - C# - Generic function to modify data structures specific to each type 为什么尽管大对象堆碎片,通用集合中的大多数数据结构仍使用数组? - Why most of the data structures in generic collections use array despite of Large Object Heap fragmentation? 来自C背景,在C#中实现const引用数据表/结构的好方法是什么? - Coming from a C background, what's a good way to implement const reference data tables/structures in C#? 在多个类内部实现结构,这些结构实现具有不同数据类型的公共接口 - Implementing structures inside multiple classes, which implement a common interface, with different data types 是否可以使用EF 6对Guid数据类型实现.Equals? - Is it possible to implement .Equals for a Guid data type with EF 6? Json搜索数据结构 - Json Searching Data Structures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM