代码之家  ›  专栏  ›  技术社区  ›  Hallgrim

可以在不复制的情况下将C Double[,,]数组转换为Double[]

  •  5
  • Hallgrim  · 技术社区  · 16 年前

    我的.NET应用程序中有大量的三维数字数组。我需要将它们转换为一维数组以将其传递到COM库。有没有一种方法可以在不复制所有数据的情况下转换数组?

    我可以这样做转换,但是我使用的内存是我的应用程序的两倍,这是一个问题:

      double[] result = new double[input.GetLength(0) * input.GetLength(1) * input.GetLength(2)];
      for (i = 0; i < input.GetLength(0); i++) 
        for (j = 0; j < input.GetLength(1); j++) 
          for (k = 0; k < input.GetLength(2); k++) 
            result[i * input.GetLength(1) * input.GetLength(2) + j * input.GetLength(2) + k)] = input[i,j,l];
      return result;
    
    5 回复  |  直到 16 年前
        1
  •  8
  •   Loren Segal    16 年前

        2
  •  6
  •   Yes - that Jake.    16 年前

        3
  •  3
  •   Aaron    16 年前

    Proxy

    class Matrix3
    {
        // referece-to-element object
        public struct Matrix3Elem{
            private Matrix3Impl impl;
            private uint dim0, dim1, dim2;
            // other constructors
            Matrix3Elem(Matrix3Impl impl_, uint dim0_, uint dim1_, uint dim2_) {
                impl = impl_; dim0 = dim0_; dim1 = dim1_; dim2 = dim2_;
            }
            public double Value{
                get { return impl.GetAt(dim0,dim1,dim2); }
                set { impl.SetAt(dim0, dim1, dim2, value); }
            }
        }
    
        // implementation object
        internal class Matrix3Impl
        {
            private double[] data;
            uint dsize0, dsize1, dsize2; // dimension sizes
            // .. Resize() 
            public double GetAt(uint dim0, uint dim1, uint dim2) {
                // .. check bounds
                return data[ (dim2 * dsize1 + dim1) * dsize0 + dim0 ];
            }
            public void SetAt(uint dim0, uint dim1, uint dim2, double value) {
                // .. check bounds
                data[ (dim2 * dsize1 + dim1) * dsize0 + dim0 ] = value;
            }
        }
    
        private Matrix3Impl impl;
    
        public Matrix3Elem Elem(uint dim0, uint dim1, uint dim2){
            return new Matrix2Elem(dim0, dim1, dim2);
        }
        // .. Resize
        // .. GetLength0(), GetLength1(), GetLength1()
    }
    

    void normalize(Matrix3 m){
    
        double s = 0;
        for (i = 0; i < input.GetLength0; i++)     
            for (j = 0; j < input.GetLength(1); j++)       
                for (k = 0; k < input.GetLength(2); k++)
        {
            s += m.Elem(i,j,k).Value;
        }
        for (i = 0; i < input.GetLength0; i++)     
            for (j = 0; j < input.GetLength(1); j++)       
                for (k = 0; k < input.GetLength(2); k++)
        {
            m.Elem(i,j,k).Value /= s;
        }
    }
    

        4
  •  1
  •   Tobi    16 年前

        5
  •  1
  •   Robert Jeppesen    16 年前


    推荐文章