// ************************* methods ***************************************** /// <summary> /// Add an item to the end of the table with the given key value. /// </summary> /// <param name="key"> /// The key value of the item. An ArgumentNullException exception is raised if /// key is null. An ArgumentException exception is raised if the key already /// exists in the collection. /// </param> /// <param name="item">The item being added to the table.</param> public void Add(string key, object item) { // add value to Values list Values.Add(key, item); // add key to list of keys KeysByIndex.Add(key); }
/// <summary> /// Sort the list by the key values. This affects the order returned /// by integer index values and the enumerator. /// </summary> public void SortKeys() { // only do this if there are two or more items in the list if (Values.Count > 1) { KeysByIndex.Sort(); } }
/// <summary> /// Insert an item into the table at a given position. A unique key value /// is automatically created for the item. This key value will take the form /// "cIndexedHashTable_Key_XXXX" (where XXXX is a number from 1 to 2^63). /// </summary> /// <param name="position"> /// The position of the item in the table (0 based). An ArgumentOutOfRangeException /// exception is raised if position is less than zero or greater than the number of /// items in the list minus one. /// </param> /// <param name="item">The item being added to the table.</param> public void Insert(int position, object item) { // get a key for the new item // this must be unique and is generated by the AddNum private variable StringBuilder key = new StringBuilder("cIndexedHashTable_Key_"); key.Append(AddNum++); // add key to list of keys. Do this first in case it causes an exception. KeysByIndex.Insert(position, key.ToString()); // add value to Values list Values.Add(key, item); }
/// <summary> /// Returns the position of the passed item in the table. NOTE: this /// function is relatively inefficient and should not be called repeatedly. /// </summary> /// <param name="item">The item to search for.</param> /// <returns> /// The position of the item in the table. -1 is returned if the item is /// not found. /// </returns> public int IndexOf(object item) { foreach (DictionaryEntry DE in Values) { // is this the value we are looking for if (item == DE.Value) { // YES: return the position of it's key return(KeysByIndex.IndexOf((string)DE.Key)); } } return(-1); }
/// <summary> /// Remove the item from the table at the given index position. /// </summary> /// <param name="index"> /// The index position of the item to remove. An ArgumentOutOfRangeException /// exception is raised if index is less than zero or greater than the number /// of items in the list minus one. /// </param> public void RemoveAt(int index) { // make sure Index is in range if (index < 0 || index >= Values.Count) { throw new ArgumentOutOfRangeException("index"); } // remove the value at this position object key = KeysByIndex[index]; KeysByIndex.RemoveAt(index); Values.Remove(key); }
/// <summary> /// Insert an item into the table at a given position. /// </summary> /// <param name="position"> /// The position of the item in the table (0 based). An ArgumentOutOfRangeException /// exception is raised if position is less than zero or greater than the number of /// items in the list minus one. /// </param> /// <param name="key"> /// The key value of the item. An ArgumentNullException exception is raised if /// key is null. An ArgumentException exception is raised if the key already /// exists in the collection. /// </param> /// <param name="item">The item being added to the table.</param> public void Insert(int position, string key, object item) { // add key to list of keys. Do this first in case it causes an exception. KeysByIndex.Insert(position, key); // add value to Values list try { Values.Add(key, item); } catch (Exception) { // if an error occurs here, remove the key value from the KeysByIndex list // and rethrow the exception. KeysByIndex.RemoveAt(position); throw; } }
/// <summary> /// Remove the item from the table with the given key value. /// </summary> /// <param name="key"> /// The key value of the item to remove. An ArgumentNullException exception is /// raised if key is null. /// </param> public void Remove(string key) { Values.Remove(key); KeysByIndex.Remove(key); }
/// <summary> /// Remove all items from the table. /// </summary> public void Clear() { // clear Values hashtable and list of keys Values.Clear(); KeysByIndex.Clear(); }