简体   繁体   English

如何创建未知多维参数?

[英]How To create unknown-multidimensional parameter?

I would Like to know if it is possible to create a generic multidimensional parameter.我想知道是否可以创建一个通用的多维参数。 I am making a generic list/array shuffle class which would have a static method which gets an IEnumerable parameter and returns the same but already shuffled.我正在制作一个通用列表/数组洗牌 class ,它有一个 static 方法,它获取一个 IEnumerable 参数并返回相同但已经洗牌。 The Problem is I don't know how to make the parameter so generic that it would get any number of dimensions array/list.问题是我不知道如何使参数如此通用以至于它可以获得任意数量的维度数组/列表。

Thank you in advance.先感谢您。

Here is the Code I Wrote For Now:这是我现在写的代码:

 public static IEnumerable<T> OneDimensionalShuffle<T>(IEnumerable<T> array)
    {
        var originalList = array.ToList();
        var shuffledList = new List<T>();

        var rand = new Random();

        for (var i = originalList.Count - 1; i >= 0; i--)
        {
            var randomArrayPosition = rand.Next(i + 1); //i+1 because maxValue is exclusive
            shuffledList.Add(originalList[randomArrayPosition]); //Add random point to the shuffledList
            originalList.RemoveAt(randomArrayPosition); //Remove element which was already added to shuffledList
        }

        array = shuffledList;
        return array;
    }

As far as I know there is no way to access a generic typed multi-dimensional array of arbitrary rank.据我所知,没有办法访问任意等级的通用类型多维数组。

Alternative 1 : Use a untyped array备选方案 1 :使用无类型数组

Array arr = new int[1,1];
var rank = arr.Rank;
var value = arr.GetValue(0,0);

Downside with this is that it is rather slow, and will cause a bunch of boxing.这样做的缺点是它相当慢,并且会导致一堆拳击。

Alternative 2 : Use a custom multidimensional type备选方案 2 :使用自定义多维类型

 public class MyMultiDimensionalArray<T>{
      public T[] backingDataStorage {get;}
      public int Rank {get;}
      public int[] Length {get;}
      public T GetValue(params int[] indices){
           // Math to convert the indices to a one dimensional value
      }
 }

An advantage of this is that you can use generic types, and allow direct access to the underlying array.这样做的一个优点是您可以使用泛型类型,并允许直接访问底层数组。 The downside is that you need to write a bunch of math to convert a index of arbitrary rank to a 1d index.缺点是您需要编写一堆数学来将任意等级的索引转换为一维索引。 A variant of this is to have different types, My2DArray, My3DArray etc, and a common interface.这种情况的一个变体是具有不同的类型,My2DArray、My3DArray 等,以及一个通用接口。

Alternative 3 : Define as many methods as you need dimensions备选方案 3 :根据需要定义尽可能多的方法

 public static void MyMethod<T>(T[]) { /*...*/}
 public static void MyMethod<T>(T[,]) { /*...*/}
 public static void MyMethod<T>(T[,,]) { /*...*/}
 public static void MyMethod<T>(T[,,,]) { /*...*/}

this is fairly easy to do and allow generic typing, but will require a bunch of code-duplication.这很容易做到并允许通用类型,但需要大量重复代码。

In my experience 1d, 2d and 3d arrays are most common.根据我的经验,1d、2d 和 3d arrays 是最常见的。 I have never seen a 4d array used in the wild.我从未见过在野外使用的 4d 数组。 And even 3d arrays can often become so large that it can be better to store one dimension as a jagged array, ie int[][,], since it avoids the need for a huge continuous memory block, allow locking individual slices etc. I prefer to use custom multidimensional array types, since I find it quite common to want linear access to the data, for things like serialization, or shuffling.甚至 3d arrays 通常会变得如此之大,以至于将一维存储为锯齿状数组(即 int[][,])会更好,因为它避免了对巨大连续 ZCD69B4957F06CD08 等 I218D7BF3D69 切片的需要。更喜欢使用自定义多维数组类型,因为我发现想要对数据进行线性访问是很常见的,比如序列化或洗牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM