Ejemplo n.º 1
0
        /// <summary>
        ///     Converts current array to an Array of type <typeparamref name="TDestStruct"/>.
        /// </summary>
        /// <typeparam name="TDestStruct"></typeparam>
        /// <param name="method">ArrayConversionMethod,</param>
        /// <param name="propertiesPerItem">Used for ArrayConversionMethod.Cast: describes how many doubles to copy to and per TDestStruct. If -1 is specified then sizeof(TDestStruct)/sizeof(double) will be used.<br></br></param>
        public virtual unsafe TDestStruct[] ToArray <TDestStruct>(ArrayConversionMethod method, int propertiesPerItem = -1) where TDestStruct : unmanaged, DataStruct
        {
            if (typeof(TDestStruct) == typeof(double))
                fixed(double *_ = this)
                return((TDestStruct[])(object)AsDoubleSpan.ToArray());

            var totalBytes = this.SizeOf;

            if (method == ArrayConversionMethod.Reinterpret)
            {
                if (totalBytes % sizeof(TDestStruct) != 0)
                {
                    throw new ArgumentException($"Can't reinterpret to TDestStruct because: totalBytes ({totalBytes}) % sizeof(TDestStruct) ({sizeof(TDestStruct)}) != 0 ({totalBytes % sizeof(TDestStruct)})");
                    fixed(double *_ = this)
                    return(MemoryMarshal.Cast <double, TDestStruct>(AsDoubleSpan).ToArray());
            }
            else if (method == ArrayConversionMethod.Cast)
            {
                if (propertiesPerItem == -1)
                {
                    propertiesPerItem = sizeof(TDestStruct) / sizeof(double);
                }
                if (totalBytes % (propertiesPerItem * sizeof(double)) != 0)
                    throw new ArgumentException($"Can't reinterpret to TDestStruct because: totalBytes ({totalBytes}) % (propertiesPerItem * sizeof(double)) (({propertiesPerItem} * sizeof(double))) != 0 ({totalBytes % sizeof(TDestStruct)})"); }
                var ret         = new TDestStruct[(totalBytes) / (propertiesPerItem * sizeof(double))];
                TDestStruct tmp = new TDestStruct();
                double *tmpPtr  = (double *)&tmp;
                var len         = Count * (Properties / propertiesPerItem);
                for (int i = 0, linearI = 0; i < len; i++)
                {
                    for (int j = 0; j < propertiesPerItem; j++, linearI++)
                    {
                        tmpPtr[j] = this.GetLinear(linearI);
                    }

                    ret[i] = tmp; //copies because tmp is struct.
                }

                return(ret);
            }
            else
            {
                throw new NotSupportedException(method.ToString());
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 ///     Converts this <see cref="DoubleArray"/> to a linear <see cref="double"/>.
 /// </summary>
 /// <returns></returns>
 public virtual double[] ToArray() {
     fixed(double *_ = this)  //fix to prevent gc from moving them during the operation.
     return(AsDoubleSpan.ToArray());
 }