-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash-table.cs
95 lines (82 loc) · 1.98 KB
/
hash-table.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
namespace Base
{
class HashTable<T>
{
private int _size;
private T[] _table;
private bool _IsPrime(int n)
{
List<int> result = new List<int>() { };
int d = 2;
while (d * d <= n)
{
if (n % d == 0)
{
result.Add(d);
n = n / d;
}
else
{
d++;
}
}
if (n > 1)
{
result.Add(n);
}
if (result.Count > 1) return false;
else return true;
}
private int _NearesPrime(int n)
{
if (n % 2 == 0)
{
n += 1;
}
while (!this._IsPrime(n))
{
n += 2;
}
return n;
}
private int _CountHash(string s)
{
int n = 7;
foreach (char c in s)
{
n = n * 31 + (int)c;
}
return (int)(this._size * ((n * ((Math.Sqrt(5) - 1) / 2)) - Math.Floor(n * ((Math.Sqrt(5) - 1) / 2))));
}
public HashTable(int size)
{
this._size = this._NearesPrime(size);
this._table = new T[this._size];
for (int i = 0; i < this._size; i++)
{
this._table[i] = default(T);
}
}
public T Search(string key)
{
return this._table[this._CountHash(key)];
}
public void Insert(string key, T value)
{
this._table[this._CountHash(key)] = value;
}
public void Remove(string key)
{
this._table[this._CountHash(key)] = default(T);
}
}
class Program
{
public static void Main()
{
Console.ReadLine();
}
}
}