1
1
2
- from typing import Any , List
2
+
3
+ from typing import Any , List , Tuple
3
4
4
5
5
6
class HashTable :
6
7
7
8
def __init__ (self , capacity :int = 11 ) -> None :
8
- self .capacity :int = capacity
9
- self .length : int = 0
9
+ self .capacity :int = capacity
10
10
self .keys : List [int ] = [None ] * self .capacity
11
- self .values : List [Any ] = [None ] * self .capacity
11
+ self .values : List [int ] = [None ] * self .capacity
12
+ self .length : int = 0
13
+
12
14
13
15
def put (self , key :int , value :Any ):
14
- index : int = self .put_helper (key )
15
- if index != - 1 :
16
+ index , contains = self .find (key )
17
+ if contains :
16
18
self .values [index ] = value
17
19
return
18
20
hash :int = self .hash (key )
19
- if self .keys [hash ] is None or self .keys [hash ] == - 1 :
20
- self .keys [hash ] = key
21
+ if self .keys [hash ] == float ( 'inf' ) or self .keys [hash ] is None :
22
+ self .keys [hash ] = key
21
23
self .values [hash ] = value
22
24
self .length += 1
23
- elif self .keys [hash ] == key :
24
- self .values [hash ] = value
25
25
else :
26
- new_hash :int = self .rehash (hash )
27
- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != - 1 and self . keys [ new_hash ] != key :
26
+ new_hash :int = self .rehash (hash ) % self . capacity
27
+ while self .keys [new_hash ] is not None and self .keys [new_hash ] != float ( 'inf' ) and new_hash != hash :
28
28
new_hash = self .rehash (new_hash )
29
-
30
- if self .keys [new_hash ] is None or self .keys [new_hash ] == - 1 :
31
- self .keys [new_hash ] = key
29
+ if self .keys [new_hash ] == float ('inf' ) or self .keys [new_hash ] is None :
30
+ self .keys [new_hash ] = key
32
31
self .values [new_hash ] = value
33
32
self .length += 1
34
- elif self .keys [new_hash ] == key :
35
- self .values [new_hash ] = value
36
33
37
- def put_helper (self , key ) -> int :
38
- hash :int = self .hash (key )
39
- if self .keys [hash ] is None :
40
- return - 1
41
- elif self .keys [hash ] == key :
42
- return hash
43
- else :
44
- new_hash :int = self .rehash (hash )
45
- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != key :
46
- new_hash = self .rehash (new_hash )
34
+
35
+ def contains (self , key :int ) -> bool :
36
+ _ , contains = self .find (key )
37
+ return contains
47
38
48
- if self .keys [new_hash ] == key :
49
- return new_hash
50
- else :
51
- return - 1
52
39
53
40
def get (self , key :int ) -> Any :
54
- hash :int = self .hash (key )
55
- if self .keys [hash ] is None :
56
- return None
57
- elif self .keys [hash ] == key :
58
- return self .values [hash ]
59
- else :
60
- new_hash :int = self .rehash (hash )
61
- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != key :
62
- new_hash = self .rehash (new_hash )
41
+ index , contains = self .find (key )
42
+ if contains :
43
+ return self .values [index ]
44
+ return None
63
45
64
- if self .keys [new_hash ] == key :
65
- return self .values [new_hash ]
66
- else :
67
- return None
68
46
69
- def contains (self , key :int ) -> bool :
70
- hash :int = self .hash (key )
71
- if self .keys [hash ] is None :
72
- return False
73
- elif self .keys [hash ] == key :
74
- return True
75
- else :
76
- new_hash :int = self .rehash (hash )
77
- while new_hash != hash and self .keys [new_hash ] is not None and self .keys [new_hash ] != key :
78
- new_hash = self .rehash (new_hash )
47
+ def delete (self , key :int ):
48
+ index , contains = self .find (key )
49
+ if not contains :
50
+ return
51
+ self .keys [index ] = float ('inf' )
52
+ self .values [index ] = None
53
+ self .length -= 1
79
54
80
- if self .keys [new_hash ] == key :
81
- return True
82
- else :
83
- return False
84
55
85
- def delete (self , key :int ):
56
+ def find (self , key :int ) -> Tuple [ int , bool ] :
86
57
hash :int = self .hash (key )
87
- if self .keys [hash ] is None :
88
- return
89
- elif self .keys [hash ] == key :
90
- self .keys [hash ] = - 1
91
- self .values [hash ] = None
92
- self .length -= 1
58
+ if self .keys [hash ] == key :
59
+ return (hash , True )
60
+ elif self .keys [hash ] is None :
61
+ return (None , False )
93
62
else :
94
- new_hash :int = self .rehash (hash )
95
- while new_hash != hash and self .keys [new_hash ] is not None and self . keys [ new_hash ] != key :
63
+ new_hash :int = self .rehash (hash ) % self . capacity
64
+ while self . keys [ new_hash ] != key and self .keys [new_hash ] is not None and new_hash != hash :
96
65
new_hash = self .rehash (new_hash )
97
66
98
67
if self .keys [new_hash ] == key :
99
- self .keys [new_hash ] = - 1
100
- self .values [new_hash ] = None
101
- self .length -= 1
102
- else :
103
- return None
68
+ return (new_hash , True )
69
+ return (None , False )
70
+
104
71
105
72
def size (self ) -> int :
106
73
return self .length
107
74
108
75
def hash (self , key :int ) -> int :
109
76
return key % self .capacity
110
77
78
+
111
79
def rehash (self , old_hash :int ) -> int :
112
80
return (old_hash + 1 ) % self .capacity
113
81
114
82
115
-
116
83
ht : HashTable = HashTable ()
117
84
ht .put (11 , 'string 11' )
118
85
ht .put (22 , 'string 22' )
@@ -126,7 +93,7 @@ def rehash(self, old_hash:int) -> int:
126
93
print (ht .values )
127
94
print (ht .size ())
128
95
print ('Get 11' , ht .get (11 ))
129
- print ('Get 33 ' , ht .get (33 ))
96
+ print ('Get 22 ' , ht .get (22 ))
130
97
print ('Get 147' , ht .get (147 ))
131
98
print ('----------------------------------------' )
132
99
@@ -150,3 +117,4 @@ def rehash(self, old_hash:int) -> int:
150
117
print (ht .values )
151
118
print ('Contains 77' , ht .contains (77 ))
152
119
print ('Contains 44' , ht .contains (44 ))
120
+
0 commit comments