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