Advertisement
Guest User

SerializableDictionary

a guest
Mar 14th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Linq;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7. [Serializable]
  8. public class SerializableDictionary<K,V> : ScriptableObject where K : IEquatable<K>
  9. {
  10.     private Dictionary<K, V> m_internalDictionary;
  11.     public Dictionary<K, V> Dictionary
  12.     {
  13.         get { return m_internalDictionary;  }
  14.     }
  15.  
  16.     [SerializeField]
  17.     private List<K> m_keys = new List<K>();
  18.     public List<K> Keys
  19.     {
  20.         get{ return m_keys; }
  21.         private set{ m_keys = value; }
  22.     }
  23.  
  24.     [SerializeField]
  25.     private List<V> m_values = new List<V>();
  26.     public List<V> Values
  27.     {
  28.         get { return m_values; }
  29.         private set { m_values = value; }
  30.     }
  31.  
  32.     public void OnEnable()
  33.     {
  34.         if (m_internalDictionary == null)
  35.         {
  36.             m_internalDictionary = new Dictionary<K, V>();
  37.         }
  38.  
  39.         if (m_keys == null)
  40.         {
  41.             m_keys = new List<K>();
  42.         }
  43.  
  44.         if (m_values == null)
  45.         {
  46.             m_values = new List<V>();
  47.         }
  48.  
  49.         for (int i = 0; i < m_keys.Count; i++)
  50.         {
  51.             m_internalDictionary.Add(m_keys[i], m_values[i]);
  52.         }
  53.     }
  54.  
  55.     public void Add(K key, V value)
  56.     {
  57.         if (!ContainsKey(key))
  58.         {
  59.             m_keys.Add(key);
  60.             m_values.Add(value);
  61.             m_internalDictionary.Add(key, value);
  62.         }
  63.         else
  64.         {
  65.             SetValue(key, value);
  66.         }
  67.     }
  68.  
  69.     public void SetValue(K key, V value)
  70.     {
  71.         int index = GetKeyIndex(key);
  72.         m_values[index] = value;
  73.         m_internalDictionary[key] = value;
  74.     }
  75.  
  76.     public void Remove(K key)
  77.     {
  78.         m_values.Remove(GetValueFromKey(key));
  79.         m_keys.Remove(key);
  80.         m_internalDictionary.Remove(key);
  81.     }
  82.  
  83.     public bool ContainsKey(K key)
  84.     {
  85.         return m_internalDictionary.ContainsKey(key);
  86.     }
  87.  
  88.     public bool ContainsValue(V data)
  89.     {
  90.         return m_internalDictionary.ContainsValue(data);
  91.     }
  92.  
  93.     private int GetKeyIndex(K key)
  94.     {
  95.         int index = 0;
  96.         for (int i = 0; i < m_keys.Count; i++)
  97.         {
  98.             if (m_keys[i].Equals(key))
  99.             {
  100.                 index = i;
  101.             }
  102.         }
  103.  
  104.         return index;
  105.     }
  106.  
  107.     private V GetValueFromKey(K key)
  108.     {
  109.         int index = 0;
  110.         for (int i = 0; i < m_keys.Count; i++)
  111.         {
  112.             if (m_keys[i].Equals(key))
  113.             {
  114.                 index = i;
  115.             }
  116.         }
  117.  
  118.         return m_values[index];
  119.     }
  120.  
  121.     public V this[K key]
  122.     {
  123.         get
  124.         {
  125.             return m_internalDictionary[key];
  126.         }
  127.         set
  128.         {
  129.             SetValue(key, value);
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement