IndieWafflus

SerializableDictionary

Jul 13th, 2021 (edited)
4,360
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 1 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.Serialization;
  7.  
  8. public class SerializableDictionary
  9. {
  10. }
  11.  
  12. [Serializable]
  13. public class SerializableDictionary<TKey, TValue> : SerializableDictionary, IDictionary<TKey, TValue>, ISerializationCallbackReceiver
  14. {
  15.     [SerializeField]
  16.     private List<SerializableKeyValuePair> list = new List<SerializableKeyValuePair>();
  17.  
  18.     [Serializable]
  19.     public struct SerializableKeyValuePair
  20.     {
  21.         public TKey Key;
  22.         public TValue Value;
  23.  
  24.         public SerializableKeyValuePair(TKey key, TValue value)
  25.         {
  26.             Key = key;
  27.             Value = value;
  28.         }
  29.  
  30.         public void SetValue(TValue value)
  31.         {
  32.             Value = value;
  33.         }
  34.     }
  35.  
  36.     private Dictionary<TKey, uint> KeyPositions => _keyPositions.Value;
  37.     private Lazy<Dictionary<TKey, uint>> _keyPositions;
  38.  
  39.     public SerializableDictionary()
  40.     {
  41.         _keyPositions = new Lazy<Dictionary<TKey, uint>>(MakeKeyPositions);
  42.     }
  43.  
  44.     public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
  45.     {
  46.         _keyPositions = new Lazy<Dictionary<TKey, uint>>(MakeKeyPositions);
  47.  
  48.         if (dictionary == null)
  49.         {
  50.             throw new ArgumentException("The passed dictionary is null.");
  51.         }
  52.  
  53.         foreach (KeyValuePair<TKey, TValue> pair in dictionary)
  54.         {
  55.             Add(pair.Key, pair.Value);
  56.         }
  57.     }
  58.  
  59.     private Dictionary<TKey, uint> MakeKeyPositions()
  60.     {
  61.         int numEntries = list.Count;
  62.  
  63.         Dictionary<TKey, uint> result = new Dictionary<TKey, uint>(numEntries);
  64.  
  65.         for (int i = 0; i < numEntries; ++i)
  66.         {
  67.             result[list[i].Key] = (uint) i;
  68.         }
  69.  
  70.         return result;
  71.     }
  72.  
  73.     public void OnBeforeSerialize()
  74.     {
  75.     }
  76.  
  77.     public void OnAfterDeserialize()
  78.     {
  79.         // After deserialization, the key positions might be changed
  80.         _keyPositions = new Lazy<Dictionary<TKey, uint>>(MakeKeyPositions);
  81.     }
  82.  
  83.     #region IDictionary
  84.     public TValue this[TKey key]
  85.     {
  86.         get => list[(int) KeyPositions[key]].Value;
  87.         set
  88.         {
  89.             if (KeyPositions.TryGetValue(key, out uint index))
  90.             {
  91.                 list[(int) index].SetValue(value);
  92.             }
  93.             else
  94.             {
  95.                 KeyPositions[key] = (uint) list.Count;
  96.  
  97.                 list.Add(new SerializableKeyValuePair(key, value));
  98.             }
  99.         }
  100.     }
  101.  
  102.     public ICollection<TKey> Keys => list.Select(tuple => tuple.Key).ToArray();
  103.     public ICollection<TValue> Values => list.Select(tuple => tuple.Value).ToArray();
  104.  
  105.     public void Add(TKey key, TValue value)
  106.     {
  107.         if (KeyPositions.ContainsKey(key))
  108.         {
  109.             throw new ArgumentException("An element with the same key already exists in the dictionary.");
  110.         }
  111.         else
  112.         {
  113.             KeyPositions[key] = (uint) list.Count;
  114.  
  115.             list.Add(new SerializableKeyValuePair(key, value));
  116.         }
  117.     }
  118.  
  119.     public bool ContainsKey(TKey key) => KeyPositions.ContainsKey(key);
  120.  
  121.     public bool Remove(TKey key)
  122.     {
  123.         if (KeyPositions.TryGetValue(key, out uint index))
  124.         {
  125.             Dictionary<TKey, uint> kp = KeyPositions;
  126.  
  127.             kp.Remove(key);
  128.  
  129.             list.RemoveAt((int) index);
  130.  
  131.             int numEntries = list.Count;
  132.  
  133.             for (uint i = index; i < numEntries; i++)
  134.             {
  135.                 kp[list[(int) i].Key] = i;
  136.             }
  137.  
  138.             return true;
  139.         }
  140.  
  141.         return false;
  142.     }
  143.  
  144.     public bool TryGetValue(TKey key, out TValue value)
  145.     {
  146.         if (KeyPositions.TryGetValue(key, out uint index))
  147.         {
  148.             value = list[(int) index].Value;
  149.  
  150.             return true;
  151.         }
  152.  
  153.         value = default;
  154.            
  155.         return false;
  156.     }
  157.     #endregion
  158.  
  159.     #region ICollection
  160.     public int Count => list.Count;
  161.     public bool IsReadOnly => false;
  162.  
  163.     public void Add(KeyValuePair<TKey, TValue> kvp) => Add(kvp.Key, kvp.Value);
  164.  
  165.     public void Clear()
  166.     {
  167.         list.Clear();
  168.         KeyPositions.Clear();
  169.     }
  170.  
  171.     public bool Contains(KeyValuePair<TKey, TValue> kvp) => KeyPositions.ContainsKey(kvp.Key);
  172.  
  173.     public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  174.     {
  175.         int numKeys = list.Count;
  176.  
  177.         if (array.Length - arrayIndex < numKeys)
  178.         {
  179.             throw new ArgumentException("arrayIndex");
  180.         }
  181.  
  182.         for (int i = 0; i < numKeys; ++i, ++arrayIndex)
  183.         {
  184.             SerializableKeyValuePair entry = list[i];
  185.  
  186.             array[arrayIndex] = new KeyValuePair<TKey, TValue>(entry.Key, entry.Value);
  187.         }
  188.     }
  189.  
  190.     public bool Remove(KeyValuePair<TKey, TValue> kvp) => Remove(kvp.Key);
  191.     #endregion
  192.  
  193.     #region IEnumerable
  194.     public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  195.     {
  196.         return list.Select(ToKeyValuePair).GetEnumerator();
  197.  
  198.         KeyValuePair<TKey, TValue> ToKeyValuePair(SerializableKeyValuePair skvp)
  199.         {
  200.             return new KeyValuePair<TKey, TValue>(skvp.Key, skvp.Value);
  201.         }
  202.     }
  203.     IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  204.     #endregion
  205. }
Advertisement
Add Comment
Please, Sign In to add comment