Esempio n. 1
0
    /// <summary>
    /// Takes in a key variable and removes the key-value pair associated from the HashMap.
    /// </summary>
    /// <param name="key"></param>
    public void remove(K key)
    {
        ulong           hashValue = hashFunc.hashFunction(key, TABLE_SIZE);
        HashNode <K, V> prev      = null;
        HashNode <K, V> entry     = table[hashValue];

        while (entry != null && !EqualityComparer <K> .Default.Equals(entry.getKey(), key))
        {
            prev  = entry;
            entry = entry.getNext();
        }

        if (entry == null)
        {
            keys.Remove(key);
            return;
        }
        else
        {
            if (prev == null)
            {
                table[hashValue] = entry.getNext();
            }
            else
            {
                prev.setNext(entry.getNext());
            }

            keys.Remove(key);
            entry = null;
        }
    }
Esempio n. 2
0
    public void Print()
    {
        HashNode current = null;

        for (int i = 0; i < size; i++)
        {
            int chainCounter = 0;
            current = table[i];
            while (current != null)
            {
                if (chainCounter > 0)
                {
                    Console.Write(current.getdata() + " ");
                }
                else
                {
                    Console.Write(i + ": " + current.getdata() + " ");
                }
                current = current.getNextNode();
                chainCounter++;
            }

            Console.WriteLine();
        }
    }
Esempio n. 3
0
    public KeyValuePair <AABB, float> GetNearestNeighbour(GameObject obj)
    {
        int      hashOfAAbb       = HashIt(obj.transform.position);
        HashNode node             = buckets[hashOfAAbb];
        AABB     nearestNeighbour = obj.GetComponent <AABB>();
        float    distance         = float.MaxValue;

        foreach (AABB aabb in node.Content)
        {
            if (aabb.Equals(obj))
            {
                continue;
            }

            float newDist = BoundsInteraction.GetDistance(aabb, obj.GetComponent <AABB>());
            if (newDist < distance)
            {
                distance         = newDist;
                nearestNeighbour = aabb;
            }
        }

        Debug.Log("Distance: " + distance + "| Object: " + nearestNeighbour.gameObject.name);

        return(new KeyValuePair <AABB, float>(nearestNeighbour, distance));
    }
Esempio n. 4
0
    /// <summary>
    /// This function allows a new key-value pair to be added to the HashMap.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void put(K key, V value)
    {
        ulong           hashValue = hashFunc.hashFunction(key, TABLE_SIZE);
        HashNode <K, V> prev      = null;
        HashNode <K, V> entry     = table[hashValue];

        while (entry != null && !EqualityComparer <K> .Default.Equals(entry.getKey(), key))
        {
            prev  = entry;
            entry = entry.getNext();
        }

        if (entry == null)
        {
            entry = new HashNode <K, V>(key, value);
            keys.Add(key);
            if (prev == null)
            {
                table[hashValue] = entry;
            }
            else
            {
                prev.setNext(entry);
            }
        }
        else
        {
            entry.setValue(value);
        }
    }
Esempio n. 5
0
        public bool DoubleHashInsert(int key, string data)
        {
            if (tabSize == maxSize)
            {
                Console.WriteLine("Таблица заполнена!");
                return(false);
            }

            HashNode node = new HashNode(key, data);

            //double probing method
            int i       = 1;
            int hashVal = Hash1(key);

            //int stepSize = Hash2(key);

            while (table[hashVal] != null && table[hashVal].Key != key)
            {
                hashVal = (hashVal + i * Hash2(key)) % maxSize;
                i++;
            }
            if (table[hashVal] != null)
            {
                node.Next           = table[hashVal].Next;
                table[hashVal].Next = node;
                table[hashVal].count++;
            }
            else
            {
                table[hashVal] = node;
                tabSize++;
            }
            return(true);
        }
Esempio n. 6
0
        public Dictionary <int, InventSlotItem> ReadHashMap(long pointer)
        {
            var result = new Dictionary <int, InventSlotItem>();

            Stack <HashNode> stack = new Stack <HashNode>();
            var startNode          = ReadObject <HashNode>(pointer);
            var item = startNode.Root;

            stack.Push(item);

            while (stack.Count != 0)
            {
                HashNode node = stack.Pop();
                if (!node.IsNull)
                {
                    result.Add(node.Key, node.Value1);
                }

                HashNode prev = node.Previous;
                if (!prev.IsNull)
                {
                    stack.Push(prev);
                }

                HashNode next = node.Next;
                if (!next.IsNull)
                {
                    stack.Push(next);
                }
            }
            return(result);
        }
Esempio n. 7
0
        public void TestHashNode()
        {
            var hn   = new HashNode(null);
            var data = hn.Encode();

            Assert.AreEqual("0200", data.ToHexString());
        }
Esempio n. 8
0
    public bool Insert(int key)
    {
        HashNode nObj = new HashNode(key, key);
        int      hash = key % size;

        while (table[hash] != null && table[hash].getkey() % size != key % size)
        {
            hash = (hash + 1) % size;
        }
        if (table[hash] != null && hash == table[hash].getkey() % size)
        {
            //search through nbodes to see if key exists

            if (Search(key))
            {
                return(false);
            }



            nObj.setNextNode(table[hash].getNextNode());
            table[hash].setNextNode(nObj);

            return(true);
        }
        else
        {
            table[hash] = nObj;
            return(true);
        }
    }
Esempio n. 9
0
        private void Insert(TKey key, TValue value, bool addOnly)
        {
            int hash        = _comparer.GetHashCode(key);
            int bucketIndex = (hash & HashMask) % _buckets.Length;

            for (var node = _buckets[bucketIndex]; node != null; node = node.Next)
            {
                if (hash == (_comparer.GetHashCode(node.Key)) && _comparer.Equals(node.Key, key))
                {
                    if (addOnly)
                    {
                        throw new ArgumentException("An item with the same key has already been added.", "key");
                    }
                    node.Key   = key;
                    node.Value = value;
                    _touch(node, IsAccessOrdered);
                    return;
                }
            }

            var newNode = new HashNode <TKey, TValue> {
                Key = key, Value = value, Next = _buckets[bucketIndex]
            };

            _buckets[bucketIndex] = newNode;
            _touch(newNode, false);

            if (++_count > _rehashThreshold)
            {
                GrowCapacity();
            }
        }
    /** value will always be non-negative. */
    public void Put(int key, int value)
    {
        int      index   = GetHashCode(key);
        HashNode newNode = new HashNode(key, value);

        if (hashMap[index] == null)  // does not exist
        {
            hashMap[index] = newNode;
        }
        else
        {
            HashNode cur  = hashMap[index];
            HashNode prev = cur;
            while (cur != null)
            {
                if (cur.key == key)  // already exist
                {
                    cur.val = value; // update the value
                    return;
                }
                prev = cur;
                cur  = cur.next;
            }
            prev.next = newNode;
        }
        count++;
        if ((double)count >= (double)curSize * resizeFactor)
        {
            Console.WriteLine("rehashing");
            Rehash();
        }
    }
Esempio n. 11
0
        public void RangeStartTest()
        {
            HashNode node = new HashNode();

            node.RangeStart = 5;
            Assert.AreEqual(5, node.RangeStart);
        }
Esempio n. 12
0
        public void RangeEndTest()
        {
            HashNode node = new HashNode();

            node.RangeEnd = 5;
            Assert.AreEqual(5, node.RangeEnd);
        }
Esempio n. 13
0
        public void HashTest()
        {
            HashNode node = new HashNode();

            node.Hash = TestVectors.h("ABCDEF");
            CustomAssert.AreEqual(TestVectors.h("ABCDEF"), node.Hash);
        }
Esempio n. 14
0
 public ValueEnumerator(LinkedHashMap <TKey, TValue> dictionary)
 {
     _dictionary       = dictionary;
     _current          = null;
     _lastValuePresent = false;
     _lastValue        = default(TValue);
 }
Esempio n. 15
0
        /// <summary>
        /// 为一致性哈希节点从DbDataReader中赋值
        /// </summary>
        /// <param name="record"></param>
        /// <param name="reader"></param>
        /// <param name="prefix"></param>
        public static void SetHashNodeSelectFields(HashNode record, DbDataReader reader, string prefix)
        {
            record.ID = (Guid)reader[string.Format("{0}id", prefix)];

            if (reader[string.Format("{0}realnodeid", prefix)] != DBNull.Value)
            {
                record.RealNodeId = (Guid)reader[string.Format("{0}realnodeid", prefix)];
            }

            if (reader[string.Format("{0}groupid", prefix)] != DBNull.Value)
            {
                record.GroupId = (Guid)reader[string.Format("{0}groupid", prefix)];
            }

            if (reader[string.Format("{0}code", prefix)] != DBNull.Value)
            {
                record.Code = (long)reader[string.Format("{0}code", prefix)];
            }

            if (reader[string.Format("{0}status", prefix)] != DBNull.Value)
            {
                record.Status = (int)reader[string.Format("{0}status", prefix)];
            }

            if (reader[string.Format("{0}createtime", prefix)] != DBNull.Value)
            {
                record.CreateTime = (DateTime)reader[string.Format("{0}createtime", prefix)];
            }

            if (reader[string.Format("{0}modifytime", prefix)] != DBNull.Value)
            {
                record.ModifyTime = (DateTime)reader[string.Format("{0}modifytime", prefix)];
            }
        }
Esempio n. 16
0
    public void Put(string key, string value)
    {
        int      bucket  = GetHash(key);
        HashNode newNode = new HashNode(key, value);

        if (buckets[bucket] == null)
        {
            buckets[bucket] = newNode;
            Size++;
        }
        else
        {
            HashNode tmp = buckets[bucket];
            while (tmp.next != null)
            {
                if (tmp.key.Equals(key))
                {
                    tmp.value = value;
                    return;
                }
                tmp = tmp.next;
            }
            tmp.next = newNode;
            Size++;
        }
        return;
    }
 private void Rehash()
 {
     curSize *= 2; // enlarge the current size to 2 times
     HashNode[] newHashMap = new HashNode[curSize];
     foreach (var node in hashMap)
     {
         HashNode oldNode = node;
         while (oldNode != null)
         {
             int      newIdx  = GetHashCode(oldNode.key);
             HashNode newNode = new HashNode(oldNode.key, oldNode.val);
             if (newHashMap[newIdx] == null)
             {
                 newHashMap[newIdx] = newNode;
             }
             else
             {
                 HashNode curNode = newHashMap[newIdx];
                 while (curNode.next != null)
                 {
                     curNode = curNode.next;
                 }
                 curNode.next = newNode;
             }
             oldNode = oldNode.next;
         }
     }
     hashMap = newHashMap;
 }
Esempio n. 18
0
    public V remove(K key)
    {
        int             index = this.getBucketIndex(key);
        HashNode <K, V> head  = this.bucket[index];
        HashNode <K, V> prev  = null;

        while (head != null)
        {
            if (head.key.Equals(key))
            {
                break;
            }
            prev = head;
            head = head.next;
        }

        if (head == null)
        {
            throw new System.Exception("Key Error: Element not found");
        }
        if (prev != null)
        {
            prev.next = head.next;
        }
        else
        {
            this.bucket[index] = head.next;
        }
        this.size--;
        return(head.val);
    }
Esempio n. 19
0
    public void Remove(string key)
    {
        int bucket = GetHash(key);

        if (buckets[bucket] == null)
        {
            return;
        }
        HashNode tmp = buckets[bucket];

        if (tmp.key.Equals(key))
        {
            buckets[bucket] = buckets[bucket].next;
            Size--;
            return;
        }
        while (tmp.next != null)
        {
            if (tmp.next.key.Equals(key))
            {
                tmp.next = tmp.next.next;
                Size--;
                return;
            }
            tmp = tmp.next;
        }
    }
Esempio n. 20
0
    public string ToString()
    {
        string res   = "{";
        int    count = 0;

        for (int i = 0; i < numBuckets; i++)
        {
            if (buckets[i] != null)
            {
                HashNode tmp = buckets[i];
                while (tmp != null)
                {
                    res += tmp.key + ": " + tmp.value;
                    count++;
                    if (count < Size)
                    {
                        res += ", ";
                    }
                    tmp = tmp.next;
                }
            }
        }
        res += "}";
        return(res);
    }
Esempio n. 21
0
        public string GetValue(string key)
        {
            int index = CreateHash(key);

            if (NodeArray[index] == null)
            {
                return(null);
            }

            if (NodeArray != null)
            {
                if (NodeArray[index].Key == key)
                {
                    return(NodeArray[index].Value);
                }
                else
                {
                    HashNode current = NodeArray[index];
                    while (current.Next != null)
                    {
                        current = current.Next;
                        if (current.Key == key)
                        {
                            return(current.Value);
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 22
0
        public bool Contains(string key)
        {
            int index = CreateHash(key);

            if (NodeArray[index] == null)
            {
                return(false);
            }
            else
            {
                HashNode current = NodeArray[index];
                if (NodeArray[index].Key == key)
                {
                    return(true);
                }
                while (current.Next != null)
                {
                    current = current.Next;
                    if (current.Key == key)
                    {
                        return(true);
                    }
                }
                return(false);
            }
        }
Esempio n. 23
0
 private void UnlinkNode(HashNode <TKey, TValue> node)
 {
     if (node == null || (node.PreviousInOrder == null && node.NextInOrder == null))
     {
         return;
     }
     if (node == _head && node == _tail)
     {
         _head = _tail = null;
     }
     else if (node == _head)
     {
         _head = _head.NextInOrder;
         _head.PreviousInOrder = null;
     }
     else if (node == _tail)
     {
         _tail             = _tail.PreviousInOrder;
         _tail.NextInOrder = null;
     }
     else
     {
         if (node.NextInOrder != null)
         {
             node.NextInOrder.PreviousInOrder = node.PreviousInOrder;
         }
         if (node.PreviousInOrder != null)
         {
             node.PreviousInOrder.NextInOrder = node.NextInOrder;
         }
     }
     node.NextInOrder = node.PreviousInOrder = null;
 }
Esempio n. 24
0
        public new bool Remove(TKey key)
        {
            if (!_isKeyValueType && ReferenceEquals(key, null))
            {
                throw new ArgumentNullException("key");
            }

            int bucketIndex = (_comparer.GetHashCode(key) & HashMask) % _buckets.Length;

            int startCount = _count;
            HashNode <TKey, TValue> previous = null;
            int hash = _comparer.GetHashCode(key);
            var node = _buckets[bucketIndex];

            while (node != null)
            {
                // Supports duplicate key deletion, even though I don't support dup key insertions
                if (hash == (_comparer.GetHashCode(node.Key)) && _comparer.Equals(node.Key, key))
                {
                    UnlinkNode(node);
                    if (previous == null)
                    {
                        _buckets[bucketIndex] = node.Next;
                    }
                    else
                    {
                        previous.Next = node.Next;
                    }
                    _count--;
                }
                previous = node;
                node     = node.Next;
            }
            return(startCount != _count);
        }
Esempio n. 25
0
 private void LinkOrderInsertReverse(HashNode <TKey, TValue> node, bool touch)
 {
     if (!touch)
     {
         InsertAtHead(node);
     }
 }
Esempio n. 26
0
 private void LinkOrderInsert(HashNode <TKey, TValue> node, bool touch)
 {
     if (!touch)
     {
         InsertAtTail(node);
     }
 }
Esempio n. 27
0
        public void Constructor1Test()
        {
            HashNode node = new HashNode();

            Assert.IsNull(node.Hash);
            Assert.AreEqual(0, node.RangeStart);
            Assert.AreEqual(0, node.RangeEnd);
        }
Esempio n. 28
0
        public void ConstructWithRootNode()
        {
            var   hashDigest = new HashDigest <SHA256>(GetRandomBytes(Size));
            INode rootNode   = new HashNode(hashDigest);
            var   merkleTrie = new MerkleTrie(new MemoryKeyValueStore(), rootNode);

            Assert.Equal(hashDigest, merkleTrie.Hash);
        }
Esempio n. 29
0
        public void Constructor3Test()
        {
            HashNode node = new HashNode(TestVectors.h("ABCDEF"), 1, 2);

            CustomAssert.AreEqual(TestVectors.h("ABCDEF"), node.Hash);
            Assert.AreEqual(1, node.RangeStart);
            Assert.AreEqual(2, node.RangeEnd);
        }
Esempio n. 30
0
 public KeyEnumerator(LinkedHashMap <TKey, TValue> dictionary)
 {
     if (dictionary == null)
     {
         throw new ArgumentNullException("dictionary");
     }
     _dictionary = dictionary;
     _current    = null;
 }
Esempio n. 31
0
 public OceanWaterData GetData(int size, float lat)
 {
     HashNode node = new HashNode(size, lat);
     OceanWaterData result;
     if (!dataTable.TryGetValue(node, out result))
     {
         result = new OceanWaterData(renderSystem, size, lat);
         dataTable.Add(node, result);
     }
     return result;
 }