44from typing import List
55from typing import Union
66
7+ from ..utils import test_iterable
8+
79
810class _HeapNode :
911 def __init__ (self , key : Any , value : Any ):
@@ -21,7 +23,10 @@ def is_marked(self) -> bool:
2123
2224
2325class Heap :
26+ MAX_MIN = - sys .maxsize
27+
2428 def __init__ (self , comparator_f2 : Callable [[Any , Any ], bool ], xs : List [Any ] = ()):
29+ test_iterable (xs )
2530 self ._size = 0
2631 self ._comparator_f2 = comparator_f2
2732 self ._next = None
@@ -39,11 +44,11 @@ def _get_by_index(array, index) -> Union[None, Any]:
3944
4045 @classmethod
4146 def _set_entry_by_index (cls , array , index , value ):
42- if cls ._get_by_index (array , index ) == - sys . maxsize :
47+ if cls ._get_by_index (array , index ) == cls . MAX_MIN :
4348 array [index ] = value
4449 return array
4550 else :
46- array .extend ([- sys . maxsize ] * (index - len (array ) + 1 ))
51+ array .extend ([cls . MAX_MIN ] * (index - len (array ) + 1 ))
4752 return cls ._set_entry_by_index (array , index , value )
4853
4954 @property
@@ -78,7 +83,7 @@ def contains_key(self, key) -> bool:
7883 return False
7984
8085 def push (self , key : Any , value : any ) -> Any :
81- if not key :
86+ if key is None :
8287 raise RuntimeError ('Could not process heap keys equal to Null.' )
8388
8489 node = _HeapNode (key , value )
@@ -174,7 +179,7 @@ def _consolidate(self):
174179 # we'll try to consolidate them
175180 degree = root .degree
176181
177- while self ._get_by_index (degrees , degree ):
182+ while not ( self ._get_by_index (degrees , degree ) in [ self . MAX_MIN , None ] ):
178183 other_root_with_degree = degrees [degree ]
179184
180185 if self ._comparator_f2 (root .key , other_root_with_degree .key ):
@@ -185,7 +190,7 @@ def _consolidate(self):
185190 smaller , larger = other_root_with_degree , root
186191
187192 self ._link_nodes (larger , smaller )
188- degrees [degree ] = - sys . maxsize
193+ degrees [degree ] = self . MAX_MIN
189194 root = smaller
190195 degree += 1
191196
0 commit comments