Ejemplo n.º 1
0
        public void SerializeTest()
        {
            byte[]           key     = new byte[] { 128 };
            byte[]           value   = new byte[] { 7, 7, 7 };
            int              index   = 9999;
            HashTableElement element = InitHashTableElement(key, value, index);

            byte[] expected = key.Append(value);

            TestHelper.AssertByteArraysAreSame(expected, element.Serialize());
        }
Ejemplo n.º 2
0
        public void CtorTest()
        {
            byte[]           key     = new byte[] { 128 };
            byte[]           value   = new byte[] { 7, 7, 7 };
            int              index   = 9999;
            HashTableElement element = InitHashTableElement(key, value, index);

            TestHelper.AssertByteArraysAreSame(key, element.Key);
            TestHelper.AssertByteArraysAreSame(value, element.Value);
            Assert.AreEqual(index, element.Index);
        }
Ejemplo n.º 3
0
        public void DeserializeTest()
        {
            byte[] key   = new byte[] { 128 };
            byte[] value = new byte[] { 7, 7, 7 };
            int    index = 9999;

            byte[]           expected = key.Append(value);
            HashTableElement element  = HashTableElement.Deserialize(expected, index, key.Length, value.Length);

            TestHelper.AssertByteArraysAreSame(key, element.Key);
            TestHelper.AssertByteArraysAreSame(value, element.Value);
            Assert.AreEqual(index, element.Index);
        }
 //private class HashTableElementComparer : IComparer<HashTableElement>
 //{
 //    public int Compare(HashTableElement x, HashTableElement y)
 //    {
 //        return x.Key.GetHash() - y.Key.GetHash();
 //    }
 //}
 private void WipeArrayAt(int index)
 {
     HashTableElement element = new HashTableElement(NullKey, new byte[GetValueSize()], index);
     PutElement(element);
 }
 private void PutElement(HashTableElement element)
 {
     _simpleCollectionNextIndex.Put(element.Index, element.Key.Append(element.Value));
 }
        private IEnumerable<HashTableElement> GetNextAvailableSpaceForKeyEnumerable(byte[] key)
        {
            int hash = key.GetHash();
            int initialIndexForKey = hash % GetTableSize();
            HashTableElement element = GetElementAt(initialIndexForKey);
            yield return element;
            while (!element.Key.EqualsBytes(NullKey) && element.Index < _simpleCollectionNextIndex.GetNextIndex()) //collision and still within the array's boundries
            {
                int nextIndex = GetNextIndexAfterCollision(element.Index);
                element = GetElementAt(nextIndex);
                if(element.Key.EqualsBytes(key)) //duplicate key
                    throw new InvalidKeyException("That key already exsists in the table");
                if (element.Index == initialIndexForKey) // we've looped all the way around
                    throw new IndexOutOfRangeException("The hash table is full");
                yield return element;
            }
            if (element.Index >= _simpleCollectionNextIndex.GetNextIndex())
                element = new HashTableElement(key, new byte[GetValueSize()], element.Index);

            yield return element;
        }
 public void Put(byte[] key, byte[] value)
 {
     AssertValidKey(key);
     AssertValidValue(value);
     int putIndex = GetNextAvailableSpaceForKey(key).Index;
     HashTableElement element = new HashTableElement(key, value, putIndex);
     PutElement(element);
 }