예제 #1
0
        public static ChainNode FindNodeByKey(string key, LinkedList <ChainNode> chain)
        {
            ChainNode node = chain.First.Value;

            while (node != null)
            {
                string nodeKey = (string)node.chain[0];
                if (nodeKey == key)
                {
                    return(node);
                }
                node = node.next;
            }
            return(null);
        }
예제 #2
0
 public void remove(string key) // ✅
 {
     if (key != null && key.Length > 0)
     {
         int hashIndex = Utils.Hash(key, this.entries.Length);
         LinkedList <ChainNode> chain = this.entries[hashIndex];
         if (chain != null)
         {
             ChainNode nodeToRemove = Utils.FindNodeByKey(key, chain);
             if (nodeToRemove != null)
             {
                 chain.Remove(nodeToRemove);
             }
         }
     }
 }
예제 #3
0
        public override string ToString() // ToString method for object representation. ✅
        {
            string str            = "{\n";
            bool   hasPassedFirst = false;

            for (var i = 0; i < this.entries.Length; i++)
            {
                LinkedList <ChainNode> chain = this.entries[i];
                if (chain != null && chain.First != null)
                {
                    ChainNode node = chain.First.Value;
                    while (node != null)
                    {
                        string      chainItemKey   = (string)node.chain[0];
                        object      chainItemValue = node.chain[1];
                        System.Type valueType      = chainItemValue.GetType();
                        if (hasPassedFirst)
                        {
                            // We are not at the very last entry.
                            str = $"{str},\n";
                        }
                        if (valueType.Equals(typeof(int)))
                        {
                            str = $"{str}  \"{chainItemKey}\" : {chainItemValue}";
                        }
                        else
                        {
                            str = $"{str}  \"{chainItemKey}\" : \"{chainItemValue}\"";
                        }
                        node = node.next;
                        if (!hasPassedFirst)
                        {
                            hasPassedFirst = true;
                        }
                    }
                }
            }
            str = $"{str}\n}}";
            return(str);
        }
예제 #4
0
 public void set(string key, object value) // ✅
 {
     if (key != null)
     {
         int hashIndex = Utils.Hash(key, this.entries.Length);
         this.insertionCount += 1;
         var newNode = new ChainNode(new object[] { key, value, this.insertionCount });
         // Set the value in its chain.
         LinkedList <ChainNode> chain = this.entries[hashIndex];
         if (chain != null)
         {
             // If we find our item in the chain, replace value.
             ChainNode existingNode = Utils.FindNodeByKey(key, chain);
             if (existingNode != null)
             {
                 chain.Remove(existingNode);
             }
             else if (chain.Count > 0)
             {
                 // Our node might not exist but lets check if collision count needs to increment.
                 this.collisionCount += 1;
             }
         }
         else
         {
             chain = new LinkedList <ChainNode>();
             this.entries[hashIndex] = chain;
         }
         if (chain.Last != null && chain.Last.Value != null)
         {
             ChainNode last = chain.Last.Value;
             if (last != null)
             {
                 last.setNext(newNode);
             }
         }
         chain.AddLast(newNode);
     }
 }
예제 #5
0
 public void setNext(ChainNode next)
 {
     this.next = next;
 }