示例#1
0
 private static Type GetRawType(PrimitiveType itemType)
 {
     Contracts.CheckValue(itemType, nameof(itemType));
     return(typeof(VBuffer <>).MakeGenericType(itemType.RawType));
 }
示例#2
0
        private static ValueGetter <VBuffer <TDst> > GetVecGetterAsCore <TSrc, TDst>(VectorType typeSrc, PrimitiveType typeDst, GetterFactory getterFact)
        {
            Contracts.Assert(typeof(TSrc) == typeSrc.ItemType.RawType);
            Contracts.Assert(typeof(TDst) == typeDst.RawType);
            Contracts.AssertValue(getterFact);

            var  getter = getterFact.GetGetter <VBuffer <TSrc> >();
            bool identity;
            var  conv = Conversions.Instance.GetStandardConversion <TSrc, TDst>(typeSrc.ItemType, typeDst, out identity);

            if (identity)
            {
                Contracts.Assert(typeof(TSrc) == typeof(TDst));
                return((ValueGetter <VBuffer <TDst> >)(Delegate) getter);
            }

            int size = typeSrc.VectorSize;
            var src  = default(VBuffer <TSrc>);

            return((ref VBuffer <TDst> dst) =>
            {
                getter(ref src);
                if (size > 0)
                {
                    Contracts.Check(src.Length == size);
                }

                var values = dst.Values;
                var indices = dst.Indices;
                int count = src.Count;
                if (count > 0)
                {
                    if (Utils.Size(values) < count)
                    {
                        values = new TDst[count];
                    }

                    // REVIEW: This would be faster if there were loops for each std conversion.
                    // Consider adding those to the Conversions class.
                    for (int i = 0; i < count; i++)
                    {
                        conv(in src.Values[i], ref values[i]);
                    }

                    if (!src.IsDense)
                    {
                        if (Utils.Size(indices) < count)
                        {
                            indices = new int[count];
                        }
                        Array.Copy(src.Indices, indices, count);
                    }
                }
                dst = new VBuffer <TDst>(src.Length, count, values, indices);
            });
        }