예제 #1
0
 /// <summary>
 /// Removes a given key and associated value from a map.
 /// </summary>
 /// <returns>true if the key was in the map and was removed otherwise false</returns>
 /// <exception cref="System.ArgumentNullException">If the key is null</exception>
 /// <exception cref="System.ArgumentException">If the key is an empty string</exception>
 public bool RemoveElement(string key)
 {
     try
     {
         Node <TValue>        data     = new Node <TValue>(DefaultValue);
         int                  hashCode = key.GetHashCode();
         HashNodeMap <TValue> current  = hashNode;
         bool                 flag     = false;
         while (current != null && current.next != null)
         {
             if (current.key == hashCode)
             {
                 if (current.next != null)
                 {
                     current = current.next;
                     Count--;
                 }
                 else
                 {
                     current = null;
                 }
             }
             if (current.next != null && flag == false)
             {
                 current = current.next;
             }
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #2
0
 /// <summary>
 /// Adds a given key and value to a map.
 /// If the given key already exists in a map, then the value associated with this key should be overriden.
 /// </summary>
 /// <returns>true if the value for the key was overriden otherwise false</returns>
 /// <exception cref="System.ArgumentNullException">If the key is null</exception>
 /// <exception cref="System.ArgumentException">If the key is an empty string</exception>
 /// <exception cref="System.ArgumentNullException">If the value is null</exception>
 public bool AddElement(string key, TValue value)
 {
     try
     {
         HashNodeMap <TValue> current = null;
         if (hashNode == null)
         {
             hashNode = CreateNode(key, value);
         }
         else
         {
             current = hashNode;
             while (current.next != null)
             {
                 current = current.next;
             }
             current.next = CreateNode(key, value);
         }
         Count++;
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #3
0
        private HashNodeMap <TValue> CreateNode(string key, TValue data)
        {
            int hashCode = key.GetHashCode();
            HashNodeMap <TValue> newNodeMap = new HashNodeMap <TValue>();

            newNodeMap.data = new Node <TValue>(data);
            newNodeMap.key  = hashCode;
            return(newNodeMap);
        }
예제 #4
0
        /// <summary>
        /// Returns the value associated with a given key.
        /// </summary>
        /// <returns>The value associated with a given key or <c>DefaultValue</c> if the key does not exist in a map</returns>
        /// <exception cref="System.ArgumentNullException">If a key is null</exception>
        /// <exception cref="System.ArgumentException">If a key is an empty string</exception>
        public TValue GetValue(string key)
        {
            Node <TValue>        data     = new Node <TValue>(DefaultValue);
            int                  hashCode = key.GetHashCode();
            HashNodeMap <TValue> current  = hashNode;

            while (current != null)
            {
                if (current.key == hashCode)
                {
                    data = current.data;
                    break;
                }
                if (current.next != null)
                {
                    current = current.next;
                }
            }
            return(data.data);
        }