我应该如何 运行 结构数组上的 rle 算法?
How should I run an rle algorithm on a struct array?
我有一堆结构数组。每个结构包含 3 个字节和一个 sbyte。
为了更容易理解这里有一个小代码。
public class Container
{
public A[] structs;
}
public struct A
{
public byte a;
public byte b;
public byte c;
public sbyte d;
public A(byte a, byte b, sbyte d)
{
this.a = a;
this.b = b;
this.c = b;
this.d = d;
}
}
现在我有很多这样的结构,因此我想使用 RLE 来压缩它并使用更少的内存。
启动程序时,方法会按程序为变量生成值,然后对它们执行计算。之后数组就很少用了,不过还是有必要"keep them alive".
我尝试使用二进制串联将结构转换为 uint,但这不是最佳解决方案。我一直在考虑将这些值转换为字符串,但我认为它的性能很差。
无法使用 bytes 代替 sbytes。
您认为使用 RLE 压缩数组的最佳解决方案是什么?
提前致谢。
There's no way using bytes instead of sbytes.
为什么?您可以将 sbyte
转换为 byte
并向后转换。
像 uint
一样保存数据并在需要时生成结构
public struct A
{
public byte a;
public byte b;
public byte c;
public sbyte d;
public A(uint value)
{
this.a = (byte)(value >> 24);
this.b = (byte)(value >> 16);
this.c = (byte)(value >> 8);
this.d = (sbyte)value;
}
public uint Value => (uint)this.a << 24 | (uint)this.b << 16 | (uint)this.c << 8 | (uint)this.d;
}
我有一堆结构数组。每个结构包含 3 个字节和一个 sbyte。
为了更容易理解这里有一个小代码。
public class Container
{
public A[] structs;
}
public struct A
{
public byte a;
public byte b;
public byte c;
public sbyte d;
public A(byte a, byte b, sbyte d)
{
this.a = a;
this.b = b;
this.c = b;
this.d = d;
}
}
现在我有很多这样的结构,因此我想使用 RLE 来压缩它并使用更少的内存。
启动程序时,方法会按程序为变量生成值,然后对它们执行计算。之后数组就很少用了,不过还是有必要"keep them alive".
我尝试使用二进制串联将结构转换为 uint,但这不是最佳解决方案。我一直在考虑将这些值转换为字符串,但我认为它的性能很差。
无法使用 bytes 代替 sbytes。
您认为使用 RLE 压缩数组的最佳解决方案是什么? 提前致谢。
There's no way using bytes instead of sbytes.
为什么?您可以将 sbyte
转换为 byte
并向后转换。
像 uint
一样保存数据并在需要时生成结构
public struct A
{
public byte a;
public byte b;
public byte c;
public sbyte d;
public A(uint value)
{
this.a = (byte)(value >> 24);
this.b = (byte)(value >> 16);
this.c = (byte)(value >> 8);
this.d = (sbyte)value;
}
public uint Value => (uint)this.a << 24 | (uint)this.b << 16 | (uint)this.c << 8 | (uint)this.d;
}