Advertisement
vexe

Unity-friendly SerializbleDictionary

Sep 2nd, 2014
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1.     [Serializable]
  2.     public class KVPListsDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
  3.     {
  4.         [SerializeField] private List<TKey> keys;
  5.         [SerializeField] private List<TValue> values;
  6.  
  7.         public TKey[] Keys { get { return keys.ToArray(); } }
  8.         public TValue[] Values { get { return values.ToArray(); } }
  9.         public int Count { get { return keys.Count; } }
  10.  
  11.         public KVPListsDictionary()
  12.         {
  13.             keys = new List<TKey>();
  14.             values = new List<TValue>();
  15.         }
  16.  
  17.         public TValue this[TKey key]
  18.         {
  19.             get
  20.             {
  21.                 int index;
  22.                 if (!TryGetIndex(key, out index))
  23.                 {
  24.                     throw new KeyNotFoundException(key.ToString());
  25.                 }
  26.                 return values[index];
  27.             }
  28.             set
  29.             {
  30.                 int index;
  31.                 if (!TryGetIndex(key, out index))
  32.                 {
  33.                     Add(key, value);
  34.                 }
  35.                 else values[index] = value;
  36.             }
  37.         }
  38.  
  39.         public void SetKeyAt(int i, TKey value)
  40.         {
  41.             AssertIndexInBounds(i);
  42.             AssertUniqueKey(value);
  43.             keys[i] = value;
  44.         }
  45.  
  46.         public TKey GetKeyAt(int i)
  47.         {
  48.             AssertIndexInBounds(i);
  49.             return keys[i];
  50.         }
  51.  
  52.         public TValue GetValueAt(int i)
  53.         {
  54.             AssertIndexInBounds(i);
  55.             return values[i];
  56.         }
  57.  
  58.         public KeyValuePair<TKey, TValue> GetPairAt(int i)
  59.         {
  60.             AssertIndexInBounds(i);
  61.             return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
  62.         }
  63.  
  64.         private void AssertIndexInBounds(int i)
  65.         {
  66.             if (!keys.InBounds(i))
  67.                 throw new IndexOutOfRangeException("i");
  68.         }
  69.  
  70.         public void Clear()
  71.         {
  72.             keys.Clear();
  73.             values.Clear();
  74.         }
  75.  
  76.         public void Insert(int i, TKey key, TValue value)
  77.         {
  78.             AssertUniqueKey(key);
  79.             keys.Insert(i, key);
  80.             values.Insert(i, value);
  81.         }
  82.  
  83.         private void AssertUniqueKey(TKey key)
  84.         {
  85.             if (ContainsKey(key))
  86.                 throw new ArgumentException(string.Format("There's already a key `{0}` defined in the dictionary", key.ToString()));
  87.         }
  88.  
  89.         public void Insert(TKey key, TValue value)
  90.         {
  91.             Insert(0, key, value);
  92.         }
  93.  
  94.         public void Add(TKey key, TValue value)
  95.         {
  96.             Insert(Count, key, value);
  97.         }
  98.  
  99.         public void Remove(TKey key)
  100.         {
  101.             int index;
  102.             if (TryGetIndex(key, out index))
  103.             {
  104.                 keys.RemoveAt(index);
  105.                 values.RemoveAt(index);
  106.             }
  107.         }
  108.  
  109.         public void RemoveAt(int i)
  110.         {
  111.             AssertIndexInBounds(i);
  112.             keys.RemoveAt(i);
  113.             values.RemoveAt(i);
  114.         }
  115.  
  116.         public bool TryGetValue(TKey key, out TValue result)
  117.         {
  118.             int index;
  119.             if (!TryGetIndex(key, out index))
  120.             {
  121.                 result = default(TValue);
  122.                 return false;
  123.             }
  124.             result = values[index];
  125.             return true;
  126.         }
  127.  
  128.         public bool ContainsValue(TValue value)
  129.         {
  130.             return values.Contains(value);
  131.         }
  132.  
  133.         public bool ContainsKey(TKey key)
  134.         {
  135.             return keys.Contains(key);
  136.         }
  137.  
  138.         private bool TryGetIndex(TKey key, out int index)
  139.         {
  140.             return (index = keys.IndexOf(key)) != -1;
  141.         }
  142.  
  143.         public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  144.         {
  145.             for (int i = 0; i < Count; i++)
  146.             {
  147.                 yield return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
  148.             }
  149.         }
  150.  
  151.         IEnumerator IEnumerable.GetEnumerator()
  152.         {
  153.             return GetEnumerator();
  154.         }
  155.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement