using System; using System.Collections.Generic; namespace Cosmos.System.Network { /// /// TempDictionary template class. /// /// TempDictionary type name. internal class TempDictionary { private List mKeys; private List mValues; /// /// Get the number of elements in the list. /// public int Count { get; private set; } /// /// Create new inctanse of the class. /// /// Thrown on fatal error (contact support). public TempDictionary() :this(2) { } /// /// Create new inctanse of the class, with a specified initial size. /// /// Initial size. /// Thrown if initialSize is less than 0. public TempDictionary(int initialSize) { this.mKeys = new List(initialSize); this.mValues = new List(initialSize); } /// /// Get TempDictionary{TValue} keys array. /// public UInt32[] Keys { get { return this.mKeys.ToArray(); } } /// /// Get TempDictionary{TValue} values array. /// public TValue[] Values { get { return this.mValues.ToArray(); } } /// /// Get and set the element with the specified key. /// /// Key of an element. /// TValue value. /// (set) Thrown if no element with the specified key is found. public TValue this[UInt32 key] { get { for (int i = 0; i < mKeys.Count; i++) { if (mKeys[i].CompareTo(key) == 0) { return mValues[i]; } } return default(TValue); } set { for (int i = 0; i < mKeys.Count; i++) { if (mKeys[i].CompareTo(key) == 0) { mValues[i] = value; return; } } throw new ArgumentOutOfRangeException("key", "Key not found"); } } /// /// Check if the key exists in the TempDictionary{TValue}. /// /// Key to check if exists. /// bool value. public bool ContainsKey(UInt32 key) { for (int i = 0; i < mKeys.Count; i++) { if (mKeys[i] == key) { return true; } } return false; } /// /// Adds an object at the end of the TempDictionary{TValue}. /// /// Object key. /// Object value. /// Thrown if key already exists. public void Add(UInt32 key, TValue val) { if (ContainsKey(key) == true) { throw new ArgumentException("key", "Key already exists"); } mKeys.Add(key); mValues.Add(val); } /// /// Removes the element with the specified key of the TempDictionary{TValue}. /// /// Key of the item to remove. /// Thrown on fatal error (contact support). public void Remove(UInt32 key) { int idx = mKeys.IndexOf(key); if (idx != -1) { this.Remove(idx); } } /// /// Removes the element at the specified index of the TempDictionary{TValue}. /// /// Index of the item to remove. /// Thrown if no such index exists in the TempDictionary. public void Remove(int index) { if (index > mKeys.Count - 1) { throw new ArgumentOutOfRangeException("index", "No such index"); } mKeys.RemoveAt(index); mValues.RemoveAt(index); } /// /// Removes all elements from the TempDictionary{TValue}. /// public void Clear() { mKeys.Clear(); mValues.Clear(); } } }