publicSDictionary() { _keyPositions = new Lazy<Dictionary<TKey, int>>(MakeKeyPositions); }
private Dictionary<TKey, int> MakeKeyPositions() { var dictionary = new Dictionary<TKey, int>(list.Count); for (var i = 0; i < list.Count; i++) { dictionary[list[i].Key] = i; } return dictionary; }
publicvoidOnBeforeSerialize() { }
publicvoidOnAfterDeserialize() { _keyPositions = new Lazy<Dictionary<TKey, int>>(MakeKeyPositions); }
#region IDictionary<TKey, TValue>
public TValue this[TKey key] { get => list[KeyPositions[key]].Value; set { var pair = new SKeyValue<TKey,TValue>(key, value); if (KeyPositions.ContainsKey(key)) { list[KeyPositions[key]] = pair; } else { KeyPositions[key] = list.Count; list.Add(pair); } } }
public ICollection<TKey> Keys => list.Select(tuple => tuple.Key).ToArray(); public ICollection<TValue> Values => list.Select(tuple => tuple.Value).ToArray();
publicvoidAdd(TKey key, TValue value) { if (KeyPositions.ContainsKey(key)) thrownew ArgumentException("An element with the same key already exists in the dictionary."); else { KeyPositions[key] = list.Count; list.Add(new SKeyValue<TKey, TValue>(key, value)); } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using UnityEngine;
[Serializable] publicclassSList<TValue> : ISerializationCallbackReceiver, IList<TValue> { [SerializeField] private List<SValue<TValue>> list = new List<SValue<TValue>>();