-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
335 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using MemoryPack.Internal; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace MemoryPack.Compression; | ||
|
||
[Preserve] | ||
public sealed class BitPackFormatter : MemoryPackFormatter<bool[]> | ||
{ | ||
public static readonly BitPackFormatter Default = new BitPackFormatter(); | ||
|
||
[Preserve] | ||
public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref bool[]? value) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNullCollectionHeader(); | ||
return; | ||
} | ||
writer.WriteCollectionHeader(value.Length); | ||
|
||
var data = 0; | ||
var bit = 0; | ||
foreach (var item in value) | ||
{ | ||
Set(ref data, bit, item); | ||
|
||
bit += 1; | ||
|
||
if (bit == 32) | ||
{ | ||
writer.WriteUnmanaged(data); | ||
data = 0; | ||
bit = 0; | ||
} | ||
} | ||
|
||
if (bit != 0) | ||
{ | ||
writer.WriteUnmanaged(data); | ||
} | ||
} | ||
|
||
[Preserve] | ||
public override void Deserialize(ref MemoryPackReader reader, scoped ref bool[]? value) | ||
{ | ||
if (!reader.DangerousTryReadCollectionHeader(out var length)) | ||
{ | ||
value = null; | ||
return; | ||
} | ||
|
||
if (length == 0) | ||
{ | ||
value = Array.Empty<bool>(); | ||
return; | ||
} | ||
|
||
var readCount = ((length - 1) / 32) + 1; | ||
var requireSize = readCount * 4; | ||
if (reader.Remaining < requireSize) | ||
{ | ||
MemoryPackSerializationException.ThrowInsufficientBufferUnless(length); | ||
} | ||
|
||
if (value == null || value.Length != length) | ||
{ | ||
value = new bool[length]; | ||
} | ||
|
||
var bit = 0; | ||
var data = 0; | ||
for (int i = 0; i < value.Length; i++) | ||
{ | ||
if (bit == 0) | ||
{ | ||
reader.ReadUnmanaged(out data); | ||
} | ||
|
||
value[i] = Get(data, bit); | ||
|
||
bit += 1; | ||
|
||
if (bit == 32) | ||
{ | ||
data = 0; | ||
bit = 0; | ||
} | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Get(int data, int index) | ||
{ | ||
return (data & (1 << index)) != 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Set(ref int data, int index, bool value) | ||
{ | ||
int bitMask = 1 << index; | ||
if (value) | ||
{ | ||
data |= bitMask; | ||
} | ||
else | ||
{ | ||
data &= ~bitMask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...k.Unity/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Core/Compression/BitPackFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
#nullable enable | ||
using MemoryPack.Internal; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace MemoryPack.Compression { | ||
|
||
[Preserve] | ||
public sealed class BitPackFormatter : MemoryPackFormatter<bool[]> | ||
{ | ||
public static readonly BitPackFormatter Default = new BitPackFormatter(); | ||
|
||
[Preserve] | ||
public override void Serialize(ref MemoryPackWriter writer, ref bool[]? value) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNullCollectionHeader(); | ||
return; | ||
} | ||
writer.WriteCollectionHeader(value.Length); | ||
|
||
var data = 0; | ||
var bit = 0; | ||
foreach (var item in value) | ||
{ | ||
Set(ref data, bit, item); | ||
|
||
bit += 1; | ||
|
||
if (bit == 32) | ||
{ | ||
writer.WriteUnmanaged(data); | ||
data = 0; | ||
bit = 0; | ||
} | ||
} | ||
|
||
if (bit != 0) | ||
{ | ||
writer.WriteUnmanaged(data); | ||
} | ||
} | ||
|
||
[Preserve] | ||
public override void Deserialize(ref MemoryPackReader reader, ref bool[]? value) | ||
{ | ||
if (!reader.DangerousTryReadCollectionHeader(out var length)) | ||
{ | ||
value = null; | ||
return; | ||
} | ||
|
||
if (length == 0) | ||
{ | ||
value = Array.Empty<bool>(); | ||
return; | ||
} | ||
|
||
var readCount = ((length - 1) / 32) + 1; | ||
var requireSize = readCount * 4; | ||
if (reader.Remaining < requireSize) | ||
{ | ||
MemoryPackSerializationException.ThrowInsufficientBufferUnless(length); | ||
} | ||
|
||
if (value == null || value.Length != length) | ||
{ | ||
value = new bool[length]; | ||
} | ||
|
||
var bit = 0; | ||
var data = 0; | ||
for (int i = 0; i < value.Length; i++) | ||
{ | ||
if (bit == 0) | ||
{ | ||
reader.ReadUnmanaged(out data); | ||
} | ||
|
||
value[i] = Get(data, bit); | ||
|
||
bit += 1; | ||
|
||
if (bit == 32) | ||
{ | ||
data = 0; | ||
bit = 0; | ||
} | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool Get(int data, int index) | ||
{ | ||
return (data & (1 << index)) != 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Set(ref int data, int index, bool value) | ||
{ | ||
int bitMask = 1 << index; | ||
if (value) | ||
{ | ||
data |= bitMask; | ||
} | ||
else | ||
{ | ||
data &= ~bitMask; | ||
} | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...ty/Assets/Plugins/MemoryPack/Runtime/MemoryPack.Core/Compression/BitPackFormatter.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.