提交 d9ee5a27 编写于 作者: T Tomas Matousek

Move serialization specific BitArray helpers to where they are used

上级 4b77f764
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Shared.Utilities
{
internal static class BitArrayUtilities
{
private const string SerializationFormat = "0";
public static void WriteTo(this BitArray bitArray, ObjectWriter writer)
{
// Our serialization format doesn't round-trip bit arrays of non-byte lengths
Contract.ThrowIfTrue(bitArray.Length % 8 != 0);
writer.WriteString(SerializationFormat);
writer.WriteInt32(bitArray.Length / 8);
// This will hold the byte that we will write out after we process every 8 bits. This is
// LSB, so we push bits into it from the MSB.
byte b = 0;
for (var i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
b = (byte)(0x80 | b >> 1);
}
else
{
b >>= 1;
}
if ((i + 1) % 8 == 0)
{
// End of a byte, write out the byte
writer.WriteByte(b);
}
}
}
public static BitArray ReadFrom(this ObjectReader reader)
{
var version = reader.ReadString();
if (!string.Equals(version, SerializationFormat, StringComparison.Ordinal))
{
return null;
}
// TODO: find a way to use pool
var length = reader.ReadInt32();
var bytes = new byte[length];
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = reader.ReadByte();
}
return new BitArray(bytes);
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Shared.Utilities
......@@ -14,7 +15,37 @@ public void WriteTo(ObjectWriter writer)
writer.WriteString(SerializationFormat);
writer.WriteBoolean(_isCaseSensitive);
writer.WriteInt32(_hashFunctionCount);
_bitArray.WriteTo(writer);
WriteBitArray(writer, _bitArray);
}
private static void WriteBitArray(ObjectWriter writer, BitArray bitArray)
{
// Our serialization format doesn't round-trip bit arrays of non-byte lengths
Contract.ThrowIfTrue(bitArray.Length % 8 != 0);
writer.WriteInt32(bitArray.Length / 8);
// This will hold the byte that we will write out after we process every 8 bits. This is
// LSB, so we push bits into it from the MSB.
byte b = 0;
for (var i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
b = (byte)(0x80 | b >> 1);
}
else
{
b >>= 1;
}
if ((i + 1) % 8 == 0)
{
// End of a byte, write out the byte
writer.WriteByte(b);
}
}
}
public static BloomFilter ReadFrom(ObjectReader reader)
......@@ -27,13 +58,22 @@ public static BloomFilter ReadFrom(ObjectReader reader)
var isCaseSensitive = reader.ReadBoolean();
int hashFunctionCount = reader.ReadInt32();
var bitArray = BitArrayUtilities.ReadFrom(reader);
if (bitArray == null)
var bitArray = ReadBitArray(reader);
return new BloomFilter(bitArray, hashFunctionCount, isCaseSensitive);
}
private static BitArray ReadBitArray(ObjectReader reader)
{
// TODO: find a way to use pool
var length = reader.ReadInt32();
var bytes = new byte[length];
for (var i = 0; i < bytes.Length; i++)
{
return null;
bytes[i] = reader.ReadByte();
}
return new BloomFilter(bitArray, hashFunctionCount, isCaseSensitive);
return new BitArray(bytes);
}
}
}
......@@ -693,7 +693,6 @@
<Compile Include="Shared\Extensions\TextSpanExtensions.cs" />
<Compile Include="Shared\NormalizedTextSpanCollection.cs" />
<Compile Include="Shared\Utilities\AliasSymbolCache.cs" />
<Compile Include="Shared\Utilities\BitArrayUtilities.cs" />
<Compile Include="Shared\Utilities\BloomFilter.cs" />
<Compile Include="Shared\Utilities\BloomFilter_Serialization.cs" />
<Compile Include="Shared\Utilities\CommonFormattingHelpers.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册