@@ -22,10 +22,10 @@ class _Item(Generic[KEY, VAL]):
22
22
23
23
24
24
class _DeletedItem (_Item ):
25
- def __init__ (self ):
25
+ def __init__ (self ) -> None :
26
26
super ().__init__ (None , None )
27
27
28
- def __bool__ (self ):
28
+ def __bool__ (self ) -> bool :
29
29
return False
30
30
31
31
@@ -37,7 +37,9 @@ class HashMap(MutableMapping[KEY, VAL]):
37
37
Hash map with open addressing.
38
38
"""
39
39
40
- def __init__ (self , initial_block_size : int = 8 , capacity_factor : float = 0.75 ):
40
+ def __init__ (
41
+ self , initial_block_size : int = 8 , capacity_factor : float = 0.75
42
+ ) -> None :
41
43
self ._initial_block_size = initial_block_size
42
44
self ._buckets : list [_Item | None ] = [None ] * initial_block_size
43
45
assert 0.0 < capacity_factor < 1.0
@@ -75,7 +77,7 @@ def _try_set(self, ind: int, key: KEY, val: VAL) -> bool:
75
77
else :
76
78
return False
77
79
78
- def _is_full (self ):
80
+ def _is_full (self ) -> bool :
79
81
"""
80
82
Return true if we have reached safe capacity.
81
83
@@ -84,25 +86,25 @@ def _is_full(self):
84
86
limit = len (self ._buckets ) * self ._capacity_factor
85
87
return len (self ) >= int (limit )
86
88
87
- def _is_sparse (self ):
89
+ def _is_sparse (self ) -> bool :
88
90
"""Return true if we need twice fewer buckets when we have now."""
89
91
if len (self ._buckets ) <= self ._initial_block_size :
90
92
return False
91
93
limit = len (self ._buckets ) * self ._capacity_factor / 2
92
94
return len (self ) < limit
93
95
94
- def _resize (self , new_size : int ):
96
+ def _resize (self , new_size : int ) -> None :
95
97
old_buckets = self ._buckets
96
98
self ._buckets = [None ] * new_size
97
99
self ._len = 0
98
100
for item in old_buckets :
99
101
if item :
100
102
self ._add_item (item .key , item .val )
101
103
102
- def _size_up (self ):
104
+ def _size_up (self ) -> None :
103
105
self ._resize (len (self ._buckets ) * 2 )
104
106
105
- def _size_down (self ):
107
+ def _size_down (self ) -> None :
106
108
self ._resize (len (self ._buckets ) // 2 )
107
109
108
110
def _iterate_buckets (self , key : KEY ) -> Iterator [int ]:
@@ -111,7 +113,7 @@ def _iterate_buckets(self, key: KEY) -> Iterator[int]:
111
113
yield ind
112
114
ind = self ._get_next_ind (ind )
113
115
114
- def _add_item (self , key : KEY , val : VAL ):
116
+ def _add_item (self , key : KEY , val : VAL ) -> None :
115
117
for ind in self ._iterate_buckets (key ):
116
118
if self ._try_set (ind , key , val ):
117
119
break
0 commit comments