-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable.py
52 lines (45 loc) · 1.5 KB
/
hashtable.py
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
# Implementation of a HashTable
# A wrapper of a Python Dictionary
class Hashtable:
def __init__(self, loadFactor = 100):
self.numBuckets = 100
self.buckets = []
for x in range(self.numBuckets):
self.buckets.append([0])
self.loadFactor = loadFactor
self.numKeys = 0
def add(self, key):
self.numKeys += 1
if self.numKeys / self.numBuckets > self.loadFactor:
print "buckets: " + str(self.numBuckets)
print "keys: " + str(self.numKeys)
self.resize(self.numBuckets * 2)
key_hash = hash(key)
self.buckets[key_hash % self.numBuckets].append(key_hash)
def resize(self, newSize):
self.numBuckets = newSize
self.numKeys = 0
oldBuckets = self.buckets
for x in range(newSize):
self.buckets.append([0])
print "resize"
def contains(self, key):
key_hash = hash(key)
for x in self.buckets[key_hash % self.numBuckets]:
if x == key:
return True
return False
def remove(self, key):
#self.numKeys -= 1
#key_hash = hash(key)
#self.buckets[key_hash % self.numBuckets].remove(key)
return 0
def get(self, index):
#full_list = []
#for bucket in self.buckets:
# if bucket is not None:
# for element in bucket:
# full_list.append(element)
#full_list.sort()
#return full_list[index]
return 0