Files
stgsharp/编码器/StgSharpData/Serialize/BitArray.cs
T

123 lines
3.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace StgSharp.Data
{
public static partial class Serializer
{
public static byte[] Serialize(BitArray bits)
{
int size = bits.Count/4 +1;
List<byte> ret = new List<byte>(size);
int count = 0, total = 0; byte temp = new byte();
for (int i = 0; i < bits.Count; i++)
{
if (count == 4)
{
ret.Add(temp);
count = 0;
temp = 0;
total++;
}
switch (count)
{
case 0:
temp |= (byte)(bits[i] ? BoolMap.True_0 : BoolMap.False_0);
break;
case 1:
temp |= (byte)(bits[i] ? BoolMap.True_1 : BoolMap.False_1);
break;
case 2:
temp |= (byte)(bits[i] ? BoolMap.True_2 : BoolMap.False_2);
break;
case 3:
temp |= (byte)(bits[i] ? BoolMap.True_3 : BoolMap.False_3);
break;
default:
throw new Exception();
}
count++;
}
for (; count < 4; count++)
{
switch (count)
{
case 1:
temp |= (byte)(BoolMap.None_1);
break;
case 2:
temp |= (byte)(BoolMap.None_2);
break;
case 3:
temp |= (byte)(BoolMap.None_3);
break;
default:
throw new Exception();
}
continue;
}
ret.Add( temp);
return ret.ToArray();
}
public static void Deserialize(byte[] stream, out BitArray target)
{
List<bool> boolStream = new List<bool>( 4 * stream.Length);
int i = 0, count = 0;
foreach (byte b in stream)
{
uint raw = b;
for (i = 0; i < 4; i++)
{
switch (raw & 0b11)
{
case (uint)BoolMap.True_0:
boolStream.Add(true);
break;
case (uint)BoolMap.False_0:
boolStream.Add(false);
break;
case (uint)BoolMap.None_0:
goto end;
default:
throw new Exception();
}
raw >>= 2;
count++;
continue;
end:
{
break;
}
}
}
while (boolStream.Count > count)
{
boolStream.RemoveAt(count);
}
target = new BitArray(boolStream.ToArray());
}
}
internal enum BoolMap : byte
{
False_0 = 0b00000010,
True_0 = 0b00000001,
None_0 = 0b00000011,
False_1 = 0b00001000,
True_1= 0b00000100,
None_1 = 0b00001100,
False_2 = 0b00100000,
True_2 = 0b00010000,
None_2 = 0b00110000,
False_3 = 0b10000000,
True_3 = 0b01000000,
None_3 = 0b11000000,
}
}