Example #1
0
 public T this[string key]
 {
     get
     {
         int hash        = HashFunction(key);
         var nodellist   = _list[hash];
         var currentNode = nodellist.Head;
         while (currentNode != null)
         {
             if (currentNode.Content.Key == key)
             {
                 return(currentNode.Content.Value);
             }
             currentNode = currentNode.Next;
         }
         throw new KeyNotFoundException("Key not found in hashtable");
     }
     set
     {
         int hash = HashFunction(key);
         if (_list[hash] != null)
         {
             //Check that the key is not present
             var currentNode = _list[hash].Head;
             while (currentNode != null)
             {
                 if (currentNode.Content.Key == key)
                 {
                     throw new ArgumentException("A value with the same key already exists");
                 }
                 currentNode = currentNode.Next;
             }
             _list[hash].Add(new Pair <T> {
                 Key = key, Value = value
             });
         }
         else
         {
             _list[hash] = new LList <Pair <T> >();
             _list[hash].Add(new Pair <T> {
                 Key = key, Value = value
             });
         }
     }
 }
Example #2
0
        /// <summary>
        ///Mthod to add a new vertex with its neighbors.
        /// </summary>
        /// <param name="vertex"></param>
        /// <param name="neighbors"></param>
        public void Add(int vertex, IEnumerable <int> neighbors)
        {
            if (vertex >= _graph.Length)
            {
                throw new ArgumentOutOfRangeException("vertex not compatible with current graph definition");
            }

            var linkedList = new LList <int>();

            foreach (var neighbor in neighbors)
            {
                if (neighbor >= _graph.Length)
                {
                    throw new ArgumentOutOfRangeException("vertex not compatible with current graph definition");
                }
                linkedList.Add(neighbor);
            }
            _graph[vertex] = linkedList;
        }