C#:数字的通用接口

Dav*_*man 6 c# generics

我试图执行一些独立于数字类型的通用数字操作.但是,我知道无法使用泛型来做到这一点.第一个想法是使用where语句过滤传入的类型,但所有数字类型都是关闭的,因此对于通用过滤器无效.此外,泛型不允许标准的数字运算(加法,移位等),因此我能想出的唯一解决方案是非通用地重写每个方法.还有其他想法吗?

作为参考,以下是我最初尝试的代码:

    private const int BYTE_SIZE = 8;

    private const int UINT16_SIZE = 16;

    private const int UINT32_SIZE = 32;

    private const int UINT64_SIZE = 64;

    public static byte[] ToBytes(UInt16[] pnaValues)
    {
        return ToSmaller<byte, UInt16>(pnaValues, BYTE_SIZE, UINT16_SIZE);
    }

    public static byte[] ToBytes(UInt32[] pnaValues)
    {
        return ToSmaller<byte, UInt32>(pnaValues, BYTE_SIZE, UINT32_SIZE);
    }

    ....

    public static UInt16[] ToUInt16s(byte[] pnaValues)
    {
        return ToLarger<UInt16, byte>(pnaValues, UINT16_SIZE, BYTE_SIZE);
    }

    public static UInt16[] ToUInt16s(UInt32[] pnaValues)
    {
        return ToSmaller<UInt16, UInt32>(pnaValues, UINT16_SIZE, UINT32_SIZE);
    }

    ...

    public static UInt64[] ToUInt64s(UInt32[] pnaValues)
    {
        return ToLarger<UInt64, UInt32>(pnaValues, UINT64_SIZE, UINT32_SIZE);
    }

    private static TLarger[] ToLarger<TLarger, TSmaller>(TSmaller[] pnaSmaller, int pnLargerSize, int pnSmallerSize)
        where TLarger : byte, UInt16, UInt32, UInt64
        where TSmaller : byte, UInt16, UInt32, UInt64
    {
        TLarger[] lnaRetVal = null;
        int lnSmallerPerLarger = pnLargerSize / pnSmallerSize;

        System.Diagnostics.Debug.Assert((pnLargerSize % pnSmallerSize) == 0);

        if (pnaSmaller != null)
        {
            System.Diagnostics.Debug.Assert((pnaSmaller % lnSmallerPerLarger) == 0);

            lnaRetVal = new TLarger[pnaSmaller.Length / lnSmallerPerLarger];

            for (int i = 0; i < lnaRetVal.Length; i++)
            {
                lnaRetVal[i] = 0;

                for (int j = 0; j < lnSmallerPerLarger; j++)
                {
                    lnaRetVal[i] = (lnaRetVal[i] << pnLargerSize) + pnaSmaller[i * lnSmallerPerLarger + j];
                }
            }
        }

        return lnaRetVal;
    }

    private static TSmaller[] ToSmaller<TSmaller, TLarger>(TLarger[] pnaLarger, int pnSmallerSize, int pnLargerSize)
        where TSmaller : byte, UInt16, UInt32, UInt64
        where TLarger : byte, UInt16, UInt32, UInt64
    {
        TSmaller[] lnaRetVal = null;
        int lnSmallerPerLarger = pnLargerSize / pnSmallerSize;

        System.Diagnostics.Debug.Assert((pnLargerSize % pnSmallerSize) == 0);

        if (pnaSmaller != null)
        {
            lnaRetVal = new TSmaller[pnaLarger.Length * lnSmallerPerLarger];

            for (int i = 0; i < lnaRetVal.Length; i++)
            {
                for (int j = 0; j < lnSmallerPerLarger; j++)
                {
                    lnaRetVal[i * lnSmallerPerLarger + (lnSmallerPerLarger - 1 - j)]
                        = pnaLarger[i] >> (j * pnLargerSize);
                }
            }
        }

        return lnaRetVal;
    }
Run Code Online (Sandbox Code Playgroud)

Meh*_*ari 11

数字类型实现的算术运算没有通用接口.通用运算符可能有助于解决您的问题.

  • 特别是对于这种情况,`Operator.Convert <TFrom,TTo>(TFrom值)`可能是有用的. (4认同)