You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
65 lines
1.6 KiB
namespace hashTableStructure.HashTable
|
|
{
|
|
class SimpleHashTable
|
|
{
|
|
private const int INITIAL_SIZE = 16;
|
|
private int _size;
|
|
private Node[] _buckets;
|
|
|
|
public SimpleHashTable()
|
|
{
|
|
_size = INITIAL_SIZE;
|
|
_buckets = new Node[_size];
|
|
}
|
|
|
|
public void Put(object key, object value)
|
|
{
|
|
int index = HashFunction(key);
|
|
if (_buckets[index] == null)
|
|
{
|
|
_buckets[index] = new Node(key, value);
|
|
}
|
|
else
|
|
{
|
|
Node newNode = new Node(key, value);
|
|
newNode.Next = _buckets[index];
|
|
_buckets[index] = newNode;
|
|
}
|
|
}
|
|
|
|
public object Get(object key)
|
|
{
|
|
int index = HashFunction(key);
|
|
if (_buckets[index] != null)
|
|
{
|
|
for (Node n = _buckets[index]; n != null; n = n.Next)
|
|
{
|
|
if (n.Key == key)
|
|
return n.Value;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public bool Contains(object key)
|
|
{
|
|
int index = HashFunction(key);
|
|
if (_buckets[index] != null)
|
|
{
|
|
for (Node n = _buckets[index]; n != null; n = n.Next)
|
|
{
|
|
if (n.Key == key)
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
protected virtual int HashFunction(object key)
|
|
{
|
|
return Math.Abs(key.GetHashCode() + 1 + ((key.GetHashCode() >> 5) + 1) % _size) % _size;
|
|
}
|
|
}
|
|
} |