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:
parent
2890f5c3ec
commit
a4d9a3bdc2
4 changed files with 285 additions and 2 deletions
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue