[英]C#: Generic Interface for Numbers
我试图执行一些独立于数字类型的通用数字操作。 但是,我知道无法使用泛型来做到这一点。 第一个想法是使用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;
}
数字类型实现的算术运算没有通用接口。 通用运算符可能有助于解决您的问题。
重写可能是最简单的。 我能想到的唯一其他解决方案是超越模板,编写代表你的函数的String,使用类型的占位符字符,然后将其替换为每个类型名称并在运行时编译它。
除了由于编译而导致的初始性能损失之外,调用这些功能也很困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.