@@ -37,6 +37,24 @@ class Indexable(Protocol[T]):
3737Bit : TypeAlias = Literal [0 , 1 ]
3838
3939
40+ class Record (Generic [T ]):
41+ def __init__ (self , key : float , data : T ) -> None :
42+ self .key = key
43+ self .data = data
44+
45+ def __lt__ (self , other : Record [T ]) -> bool :
46+ return self .key < other .key
47+
48+ def __le__ (self , other : Record [T ]) -> bool :
49+ return self .key <= other .key
50+
51+ def __gt__ (self , other : Record [T ]) -> bool :
52+ return self .key > other .key
53+
54+ def __ge__ (self , other : Record [T ]) -> bool :
55+ return self .key >= other .key
56+
57+
4058class Array (Generic [T ]):
4159 __start : int
4260 __elements : list [T ]
@@ -139,35 +157,17 @@ class Heap(Array[CT]):
139157 heap_size : int = 0
140158
141159
142- class KeyObject (Generic [T ]):
143- def __init__ (self , key : float , data : T ) -> None :
144- self .key = key
145- self .data = data
146-
147- def __lt__ (self , other : KeyObject [T ]) -> bool :
148- return self .key < other .key
149-
150- def __le__ (self , other : KeyObject [T ]) -> bool :
151- return self .key <= other .key
152-
153- def __gt__ (self , other : KeyObject [T ]) -> bool :
154- return self .key > other .key
155-
156- def __ge__ (self , other : KeyObject [T ]) -> bool :
157- return self .key >= other .key
158-
159-
160160# TODO(#476) Consider removing this data structure, and instead implement the priority queue directly on a heap.
161161# Clarify if we need the mapping between objects and heap indices, because as mentioned in the book, a heap
162162# implementing a priority queue contains pointers to the objects, which should suffice to address the objects.
163- class PriorityQueue (Heap [KeyObject [T ]]):
163+ class PriorityQueue (Heap [Record [T ]]):
164164
165165 def __init__ (self , n : int ) -> None :
166166 # TODO(#21) implement the object mappings with a hash table instead of Python dictionary
167167 super ().__init__ (1 , n )
168168 self .heap_size : int = 0
169- self .mapping : dict [KeyObject [T ], int ] = {}
169+ self .mapping : dict [Record [T ], int ] = {}
170170
171- def __setitem__ (self , index : int , value : KeyObject [T ]) -> None :
171+ def __setitem__ (self , index : int , value : Record [T ]) -> None :
172172 super ().__setitem__ (index , value )
173173 self .mapping [value ] = index
0 commit comments