using System.Collections; using System.Collections.Generic; using System.Linq; namespace SonicAudioLib.Collections { /// /// Represents a key/value pair for an . /// /// /// public class KeyValuePair { public TKey Key { get; set; } public TValue Value { get; set; } public KeyValuePair(TKey key, TValue value) { Key = key; Value = value; } } /// /// Represents a key/value pair collection that is accessable by its key or index. /// public class OrderedDictionary : IEnumerable> { private List> items = new List>(); /// /// Gets the count of key/value pairs. /// public int Count { get { return items.Count; } } /// /// Gets the value at the specified index. /// public TValue this[int index] { get { return items[index].Value; } set { items[index].Value = value; } } /// /// Gets the value by the specified key. /// public TValue this[TKey key] { get { return items.Single(k => (k.Key).Equals(key)).Value; } set { items.Single(k => (k.Key).Equals(key)).Value = value; } } /// /// Determines whether the collection contains the specified key. /// public bool ContainsKey(TKey key) { return items.Any(k => (k.Key).Equals(key)); } /// /// Adds a key/value pair to end of the collection. /// public void Add(TKey key, TValue value) { items.Add(new KeyValuePair(key, value)); } /// /// Removes the key/value pair by its key. /// public bool Remove(TKey key) { return items.Remove(items.Single(k => (k.Key).Equals(key))); } /// /// Clears all the key/value pairs. /// public void Clear() { items.Clear(); } /// /// Gets the index of the specified key. /// public int IndexOf(TKey key) { return items.IndexOf(items.Single(k => (k.Key).Equals(key))); } /// /// Inserts a key/value pair to the specified index. /// public void Insert(int index, TKey key, TValue value) { items.Insert(index, new KeyValuePair(key, value)); } /// /// Removes key/value pair at the specified index. /// public void RemoveAt(int index) { items.RemoveAt(index); } /// /// Gets key at the specified index. /// public TKey GetKeyByIndex(int index) { return items[index].Key; } /// /// Gets value at the specified index. /// public TValue GetValueByIndex(int index) { return items[index].Value; } /// /// Sets key at the specified index. /// public void SetKeyByIndex(int index, TKey key) { items[index].Key = key; } /// /// Sets value at the specified index. /// public void SetValueByIndex(int index, TValue value) { items[index].Value = value; } /// /// Returns an enumerator of key/value pairs. /// public IEnumerator> GetEnumerator() { return items.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return items.GetEnumerator(); } } }