Adds dictionary to codegen (#838)

* Adds Dictionary serialization rule for codegen
* Adds Tidy for Dictionary. By default will remove key/value pairs where the key or value is either null or deleted. Only works for ISerializable keys or values (or both).
This commit is contained in:
Kamron Batman 2021-11-07 13:20:11 -08:00 committed by GitHub
parent 2890f5c3ec
commit a4d9a3bdc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 285 additions and 2 deletions

View file

@ -10,6 +10,7 @@ using System.Text;
using System.Xml;
using Microsoft.Toolkit.HighPerformance;
using Server.Buffers;
using Server.Collections;
using Server.Random;
using Server.Text;
@ -1188,6 +1189,45 @@ namespace Server
set.RemoveWhere(entry => entry?.Deleted != false);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Tidy<K, V>(this Dictionary<K, V> dictionary)
{
var serializable = typeof(ISerializable);
var serializableKey = typeof(K).IsAssignableTo(serializable);
var serializableValue = typeof(V).IsAssignableTo(serializable);
if (!serializableKey && !serializableValue)
{
return;
}
using var queue = PooledRefQueue<K>.Create();
foreach (var (key, value) in dictionary)
{
if (serializableKey)
{
if (key == null || ((ISerializable)key).Deleted)
{
queue.Enqueue(key);
}
}
else
{
if (value == null || ((ISerializable)value).Deleted)
{
queue.Enqueue(key);
}
}
}
while (queue.Count > 0)
{
dictionary.Remove(queue.Dequeue());
}
dictionary.TrimExcess();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int NumberOfSetBits(this ulong i)
{