forked from hofmannol/AlgoDatSoSe25
Added extended testsequencies for PriorityQueue
This commit is contained in:
parent
2735abfe81
commit
f56b8c7e7a
@ -1,7 +1,7 @@
|
|||||||
from utils.memory_array import MemoryArray
|
from utils.memory_array import MemoryArray
|
||||||
from utils.memory_cell import MemoryCell
|
from utils.memory_cell import MemoryCell
|
||||||
from utils.literal import Literal
|
from utils.literal import Literal
|
||||||
from utils.constants import MAX_VALUE
|
from utils.constants import MIN_VALUE
|
||||||
from utils.memory_range import mrange
|
from utils.memory_range import mrange
|
||||||
|
|
||||||
# Impl of MemoryArray says we cant add our own Datatypes beside Literal and List
|
# Impl of MemoryArray says we cant add our own Datatypes beside Literal and List
|
||||||
@ -28,14 +28,14 @@ class HeapEntry:
|
|||||||
return True
|
return True
|
||||||
if isinstance(other, (int, float)):
|
if isinstance(other, (int, float)):
|
||||||
return self.getPriority().value > other
|
return self.getPriority().value > other
|
||||||
return self.getPriority() > other.getPriority()
|
return self.getPriority() < other.getPriority()
|
||||||
|
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
if other is None:
|
if other is None:
|
||||||
return False
|
return False
|
||||||
if isinstance(other, (int, float)):
|
if isinstance(other, (int, float)):
|
||||||
return self.getPriority().value < other
|
return self.getPriority().value < other
|
||||||
return self.getPriority() < other.getPriority()
|
return self.getPriority() > other.getPriority()
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self.getPriority() == other.getPriority()
|
return self.getPriority() == other.getPriority()
|
||||||
@ -49,7 +49,7 @@ class PriorityQueue:
|
|||||||
# Add uninitialized HeapEntry Values so the Adds/Compares do not fail on emtpy stack.
|
# Add uninitialized HeapEntry Values so the Adds/Compares do not fail on emtpy stack.
|
||||||
# Would have to switch to MIN_VALUE if we switch what is a "Higher" Prio
|
# Would have to switch to MIN_VALUE if we switch what is a "Higher" Prio
|
||||||
for i in mrange(max_size.value):
|
for i in mrange(max_size.value):
|
||||||
self.heap[i].set([HeapEntry(MAX_VALUE, MAX_VALUE)])
|
self.heap[i].set([HeapEntry(MIN_VALUE, MIN_VALUE)])
|
||||||
self.size = MemoryCell(0)
|
self.size = MemoryCell(0)
|
||||||
|
|
||||||
def parent(self, i: Literal) -> Literal:
|
def parent(self, i: Literal) -> Literal:
|
||||||
@ -124,7 +124,7 @@ class PriorityQueue:
|
|||||||
entries = []
|
entries = []
|
||||||
for i in mrange(self.size.value):
|
for i in mrange(self.size.value):
|
||||||
entry = self.heap[i].value[0]
|
entry = self.heap[i].value[0]
|
||||||
if entry.getItem() != MAX_VALUE:
|
if entry.getItem() != MIN_VALUE:
|
||||||
entries.append(str(entry))
|
entries.append(str(entry))
|
||||||
return "[" + ", ".join(entries) + "]"
|
return "[" + ", ".join(entries) + "]"
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ def testQueueRandom(number: int):
|
|||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
|
||||||
pq = PriorityQueue(Literal(101))
|
pq = PriorityQueue(Literal(number))
|
||||||
|
|
||||||
entries = []
|
entries = []
|
||||||
for _ in range(number):
|
for _ in range(number):
|
||||||
@ -144,13 +144,12 @@ def testQueueRandom(number: int):
|
|||||||
|
|
||||||
print(pq)
|
print(pq)
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
print(entry)
|
print(f"Unprioritized: {entry}")
|
||||||
|
|
||||||
while not pq.isEmpty():
|
while not pq.isEmpty():
|
||||||
print(pq.pop())
|
print(pq.pop())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Proof of Concept
|
# Proof of Concept
|
||||||
testEntry = HeapEntry("A", 2)
|
testEntry = HeapEntry("A", 2)
|
||||||
@ -174,13 +173,13 @@ if __name__ == '__main__':
|
|||||||
assert(not pq.isEmpty())
|
assert(not pq.isEmpty())
|
||||||
assert(pq.pop() == HeapEntry("A", 1))
|
assert(pq.pop() == HeapEntry("A", 1))
|
||||||
assert(pq.isEmpty())
|
assert(pq.isEmpty())
|
||||||
pq.insert(HeapEntry("A", 1))
|
|
||||||
pq.insert(HeapEntry("C", 3))
|
pq.insert(HeapEntry("C", 3))
|
||||||
pq.insert(HeapEntry("B", 2))
|
pq.insert(HeapEntry("B", 2))
|
||||||
|
pq.insert(HeapEntry("A", 1))
|
||||||
assert(pq.size == Literal(3))
|
assert(pq.size == Literal(3))
|
||||||
assert(pq.pop() == HeapEntry("A", 1))
|
|
||||||
assert(pq.pop() == HeapEntry("B", 2))
|
|
||||||
assert(pq.pop() == HeapEntry("C", 3))
|
assert(pq.pop() == HeapEntry("C", 3))
|
||||||
|
assert(pq.pop() == HeapEntry("B", 2))
|
||||||
|
assert(pq.pop() == HeapEntry("A", 1))
|
||||||
pq.insert(HeapEntry("A", 1))
|
pq.insert(HeapEntry("A", 1))
|
||||||
pq.insert(HeapEntry("C", 3))
|
pq.insert(HeapEntry("C", 3))
|
||||||
pq.insert(HeapEntry("B", 2))
|
pq.insert(HeapEntry("B", 2))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user