Skip to content

Commit cc7bada

Browse files
committed
#478 rename class KeyObject to Record
1 parent b2cee75 commit cc7bada

13 files changed

Lines changed: 180 additions & 180 deletions

File tree

src/book/chapter6/problem1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from book.chapter6.section5 import max_heap_insert
22
from book.data_structures import Heap
3-
from book.data_structures import KeyObject
3+
from book.data_structures import Record
44
from book.data_structures import T
55
from util import range_of
66

77

8-
def build_max_heap_(A: Heap[KeyObject[T]], n: int) -> None:
8+
def build_max_heap_(A: Heap[Record[T]], n: int) -> None:
99
"""Converts an array into a max-heap by repeatedly inserting elements to the heap.
1010
1111
Implements:

src/book/chapter6/section5.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
from book.chapter6.section1 import parent
44
from book.chapter6.section2 import max_heapify
5-
from book.data_structures import KeyObject
65
from book.data_structures import PriorityQueue
6+
from book.data_structures import Record
77
from book.data_structures import T
88
from solutions.chapter2.section1.exercise4 import linear_search
99

1010

11-
def max_heap_maximum(A: PriorityQueue[T]) -> KeyObject[T]:
11+
def max_heap_maximum(A: PriorityQueue[T]) -> Record[T]:
1212
"""Returns the element of the dynamic set with the largest key.
1313
1414
Implements:
@@ -25,7 +25,7 @@ def max_heap_maximum(A: PriorityQueue[T]) -> KeyObject[T]:
2525
return A[1]
2626

2727

28-
def max_heap_extract_max(A: PriorityQueue[T]) -> KeyObject[T]:
28+
def max_heap_extract_max(A: PriorityQueue[T]) -> Record[T]:
2929
"""Removes and returns the element of the dynamic set with the largest key.
3030
3131
Implements:
@@ -44,7 +44,7 @@ def max_heap_extract_max(A: PriorityQueue[T]) -> KeyObject[T]:
4444
return max
4545

4646

47-
def max_heap_increase_key(A: PriorityQueue[T], x: KeyObject[T], k: float) -> None:
47+
def max_heap_increase_key(A: PriorityQueue[T], x: Record[T], k: float) -> None:
4848
"""Increases the value of the element's key to the new value.
4949
5050
Implements:
@@ -64,13 +64,13 @@ def max_heap_increase_key(A: PriorityQueue[T], x: KeyObject[T], k: float) -> Non
6464
i = parent(i)
6565

6666

67-
def __find_index_of_object(A: PriorityQueue[T], x: KeyObject[T]) -> int:
67+
def __find_index_of_object(A: PriorityQueue[T], x: Record[T]) -> int:
6868
index = linear_search(A, A.heap_size, x)
6969
assert index is not None
7070
return index
7171

7272

73-
def max_heap_insert(A: PriorityQueue[T], x: KeyObject[T], n: int) -> None:
73+
def max_heap_insert(A: PriorityQueue[T], x: Record[T], n: int) -> None:
7474
"""Inserts a new element into the dynamic set.
7575
7676
Implements:

src/book/data_structures.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ class Indexable(Protocol[T]):
3737
Bit: 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+
4058
class 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

src/solutions/chapter6/problem2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from book.chapter6.section5 import max_heap_maximum
22
from book.data_structures import CT
3-
from book.data_structures import KeyObject
43
from book.data_structures import PriorityQueue
4+
from book.data_structures import Record
55
from book.data_structures import T
66
from solutions.chapter2.section1.exercise4 import linear_search
77
from util import ceil_div
@@ -63,7 +63,7 @@ def multiary_max_heapify(A: PriorityQueue[CT], d: int, i: int) -> None:
6363
multiary_max_heapify(A, d, largest)
6464

6565

66-
def multiary_max_heap_extract_max(A: PriorityQueue[T], d: int) -> KeyObject[T]:
66+
def multiary_max_heap_extract_max(A: PriorityQueue[T], d: int) -> Record[T]:
6767
"""Removes and returns the element of the multiary max-heap with the largest key.
6868
6969
Implements:
@@ -83,7 +83,7 @@ def multiary_max_heap_extract_max(A: PriorityQueue[T], d: int) -> KeyObject[T]:
8383
return max
8484

8585

86-
def multiary_max_heap_increase_key(A: PriorityQueue[T], d: int, x: KeyObject[T], k: float) -> None:
86+
def multiary_max_heap_increase_key(A: PriorityQueue[T], d: int, x: Record[T], k: float) -> None:
8787
"""Increases the key of a multiary max-heap node.
8888
8989
Implements:
@@ -104,13 +104,13 @@ def multiary_max_heap_increase_key(A: PriorityQueue[T], d: int, x: KeyObject[T],
104104
i = multiary_parent(d, i)
105105

106106

107-
def __find_index_of_object(A: PriorityQueue[T], x: KeyObject[T]) -> int:
107+
def __find_index_of_object(A: PriorityQueue[T], x: Record[T]) -> int:
108108
index = linear_search(A, A.heap_size, x)
109109
assert index is not None
110110
return index
111111

112112

113-
def multiary_max_heap_insert(A: PriorityQueue[T], d: int, x: KeyObject[T], n: int) -> None:
113+
def multiary_max_heap_insert(A: PriorityQueue[T], d: int, x: Record[T], n: int) -> None:
114114
"""Inserts a new element into a multiary max-heap.
115115
116116
Implements:

src/solutions/chapter6/section5/exercise10.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from book.chapter6.section5 import max_heap_extract_max
44
from book.chapter6.section5 import max_heap_increase_key
5-
from book.data_structures import KeyObject
65
from book.data_structures import PriorityQueue
6+
from book.data_structures import Record
77
from book.data_structures import T
88

99

10-
def max_heap_delete(A: PriorityQueue[T], x: KeyObject[T]) -> None:
10+
def max_heap_delete(A: PriorityQueue[T], x: Record[T]) -> None:
1111
"""Deletes an element from a max-heap.
1212
1313
Implements:

src/solutions/chapter6/section5/exercise11.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from typing_extensions import TypeAlias
44

55
from book.data_structures import Array
6-
from book.data_structures import KeyObject
76
from book.data_structures import PriorityQueue
7+
from book.data_structures import Record
88
from solutions.chapter6.section5.exercise3 import min_heap_extract_min
99
from solutions.chapter6.section5.exercise3 import min_heap_insert
1010
from util import range_of
@@ -40,7 +40,7 @@ def merge_sorted_lists(sorted_lists: Array[SortedList], k: int) -> SortedList:
4040
x = sorted_list.head
4141
sorted_list.head = sorted_list.head.next
4242
x.next = None
43-
min_heap_insert(Q, KeyObject[NodePayloadType](key=x.key, data=(x, sorted_list)), k)
43+
min_heap_insert(Q, Record[NodePayloadType](key=x.key, data=(x, sorted_list)), k)
4444
merged_list = SortedList()
4545
tail: SortedListNode | None = None
4646
while Q.heap_size > 0:
@@ -53,6 +53,6 @@ def merge_sorted_lists(sorted_lists: Array[SortedList], k: int) -> SortedList:
5353
tail.next = element
5454
tail = tail.next
5555
if list_.head is not None:
56-
min_heap_insert(Q, KeyObject[NodePayloadType](key=list_.head.key, data=(list_.head, list_)), k)
56+
min_heap_insert(Q, Record[NodePayloadType](key=list_.head.key, data=(list_.head, list_)), k)
5757
list_.head = list_.head.next
5858
return merged_list

src/solutions/chapter6/section5/exercise3.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import math
22

33
from book.chapter6.section1 import parent
4-
from book.data_structures import KeyObject
54
from book.data_structures import PriorityQueue
5+
from book.data_structures import Record
66
from book.data_structures import T
77
from solutions.chapter2.section1.exercise4 import linear_search
88
from solutions.chapter6.section2.exercise3 import min_heapify
99

1010

11-
def min_heap_minimum(A: PriorityQueue[T]) -> KeyObject[T]:
11+
def min_heap_minimum(A: PriorityQueue[T]) -> Record[T]:
1212
"""Returns the element of the dynamic set with the smallest key.
1313
1414
Implements:
@@ -25,7 +25,7 @@ def min_heap_minimum(A: PriorityQueue[T]) -> KeyObject[T]:
2525
return A[1]
2626

2727

28-
def min_heap_extract_min(A: PriorityQueue[T]) -> KeyObject[T]:
28+
def min_heap_extract_min(A: PriorityQueue[T]) -> Record[T]:
2929
"""Removes and returns the element of the dynamic set with the smallest key.
3030
3131
Implements:
@@ -44,7 +44,7 @@ def min_heap_extract_min(A: PriorityQueue[T]) -> KeyObject[T]:
4444
return min
4545

4646

47-
def min_heap_decrease_key(A: PriorityQueue[T], x: KeyObject[T], k: float) -> None:
47+
def min_heap_decrease_key(A: PriorityQueue[T], x: Record[T], k: float) -> None:
4848
"""Decreases the value of the element's key to the new value.
4949
5050
Implements:
@@ -64,13 +64,13 @@ def min_heap_decrease_key(A: PriorityQueue[T], x: KeyObject[T], k: float) -> Non
6464
i = parent(i)
6565

6666

67-
def __find_index_of_object(A: PriorityQueue[T], x: KeyObject[T]) -> int:
67+
def __find_index_of_object(A: PriorityQueue[T], x: Record[T]) -> int:
6868
index = linear_search(A, A.heap_size, x)
6969
assert index is not None
7070
return index
7171

7272

73-
def min_heap_insert(A: PriorityQueue[T], x: KeyObject[T], n: int) -> None:
73+
def min_heap_insert(A: PriorityQueue[T], x: Record[T], n: int) -> None:
7474
"""Inserts a new element into the dynamic set.
7575
7676
Implements:

src/solutions/chapter6/section5/exercise4.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from book.chapter6.section2 import max_heapify
2-
from book.data_structures import KeyObject
32
from book.data_structures import PriorityQueue
3+
from book.data_structures import Record
44
from book.data_structures import T
55
from solutions.chapter2.section1.exercise4 import linear_search
66

77

8-
def max_heap_decrease_key(A: PriorityQueue[T], x: KeyObject[T], k: float) -> None:
8+
def max_heap_decrease_key(A: PriorityQueue[T], x: Record[T], k: float) -> None:
99
"""Increases the value of the element's key to the new value.
1010
1111
Implements:
@@ -23,7 +23,7 @@ def max_heap_decrease_key(A: PriorityQueue[T], x: KeyObject[T], k: float) -> Non
2323
max_heapify(A, i)
2424

2525

26-
def __find_index_of_object(A: PriorityQueue[T], x: KeyObject[T]) -> int:
26+
def __find_index_of_object(A: PriorityQueue[T], x: Record[T]) -> int:
2727
index = linear_search(A, A.heap_size, x)
2828
assert index is not None
2929
return index

src/solutions/chapter6/section5/exercise8.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from book.chapter6.section1 import parent
2-
from book.data_structures import KeyObject
32
from book.data_structures import PriorityQueue
3+
from book.data_structures import Record
44
from book.data_structures import T
55
from solutions.chapter2.section1.exercise4 import linear_search
66

77

8-
def max_heap_increase_key_(A: PriorityQueue[T], x: KeyObject[T], k: float) -> None:
8+
def max_heap_increase_key_(A: PriorityQueue[T], x: Record[T], k: float) -> None:
99
"""Increases the value of the element's key to the new value. This version of the procedure uses fewer object
1010
assignments than the original Max-Heap-Insert-Key.
1111
@@ -27,7 +27,7 @@ def max_heap_increase_key_(A: PriorityQueue[T], x: KeyObject[T], k: float) -> No
2727
A[i] = x
2828

2929

30-
def __find_index_of_object(A: PriorityQueue[T], x: KeyObject[T]) -> int:
30+
def __find_index_of_object(A: PriorityQueue[T], x: Record[T]) -> int:
3131
index = linear_search(A, A.heap_size, x)
3232
assert index is not None
3333
return index

src/solutions/chapter6/section5/exercise9.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from book.chapter6.section5 import max_heap_extract_max
22
from book.chapter6.section5 import max_heap_insert
3-
from book.data_structures import KeyObject
43
from book.data_structures import PriorityQueue
4+
from book.data_structures import Record
55
from book.data_structures import T
66
from solutions.chapter6.section5.exercise3 import min_heap_extract_min
77
from solutions.chapter6.section5.exercise3 import min_heap_insert
@@ -11,7 +11,7 @@ class ControlledPriorityQueue(PriorityQueue[T]):
1111
priority: int = 1
1212

1313

14-
def min_heap_enqueue(A: ControlledPriorityQueue[T], x: KeyObject[T], n: int) -> None:
14+
def min_heap_enqueue(A: ControlledPriorityQueue[T], x: Record[T], n: int) -> None:
1515
"""Inserts an element into a first-in, first-out queue implemented with a priority queue.
1616
1717
Implements:
@@ -27,7 +27,7 @@ def min_heap_enqueue(A: ControlledPriorityQueue[T], x: KeyObject[T], n: int) ->
2727
A.priority += 1
2828

2929

30-
def min_heap_dequeue(A: ControlledPriorityQueue[T]) -> KeyObject[T]:
30+
def min_heap_dequeue(A: ControlledPriorityQueue[T]) -> Record[T]:
3131
"""Deletes an element from a first-in, first-out queue implemented with a priority queue.
3232
3333
Implements:
@@ -42,7 +42,7 @@ def min_heap_dequeue(A: ControlledPriorityQueue[T]) -> KeyObject[T]:
4242
return min_heap_extract_min(A)
4343

4444

45-
def max_heap_push(A: ControlledPriorityQueue[T], x: KeyObject[T], n: int) -> None:
45+
def max_heap_push(A: ControlledPriorityQueue[T], x: Record[T], n: int) -> None:
4646
"""Inserts an element into a stack implemented with a priority queue.
4747
4848
Implements:
@@ -58,7 +58,7 @@ def max_heap_push(A: ControlledPriorityQueue[T], x: KeyObject[T], n: int) -> Non
5858
A.priority += 1
5959

6060

61-
def max_heap_pop(A: ControlledPriorityQueue[T]) -> KeyObject[T]:
61+
def max_heap_pop(A: ControlledPriorityQueue[T]) -> Record[T]:
6262
"""Deletes an element from a stack implemented with a priority queue.
6363
6464
Implements:

0 commit comments

Comments
 (0)