forked from hofmannol/AlgoDatSoSe25
92 lines
3.2 KiB
Python
92 lines
3.2 KiB
Python
from utils.literal import Literal
|
|
from utils.memory_cell import MemoryCell
|
|
from utils.memory_manager import MemoryManager
|
|
from pathlib import Path
|
|
from random import randint
|
|
|
|
class MemoryArray:
|
|
|
|
def __init__(self, size):
|
|
"""Initialisiert ein Speicherarray mit einer bestimmten Größe."""
|
|
assert isinstance(size, Literal), "Size must be a Literal or MemoryCell"
|
|
assert isinstance(size.value, int), "Size must be an int"
|
|
assert size.value > 0, "Size must be positive"
|
|
self.size = size.value
|
|
self.cells = [MemoryCell() for _ in range(self.size)]
|
|
|
|
def __getitem__(self, index):
|
|
"""Gibt den Wert einer Speicherzelle zurück."""
|
|
assert isinstance(index, Literal), "Index must be a Literal or MemoryCell"
|
|
assert isinstance(index.value, int), "Index value must be an int"
|
|
assert 0 <= index.value < self.size, "Index out of bounds"
|
|
return self.cells[index.value]
|
|
|
|
def __setitem__(self, index, value):
|
|
"""Setzt den Wert einer Speicherzelle."""
|
|
assert isinstance(index, Literal), "Index must be a Literal or MemoryCell"
|
|
assert isinstance(index.value, int), "Index value must be an int"
|
|
assert 0 <= index.value < self.size, "Index out of bounds"
|
|
assert isinstance(value, Literal), "Value must be a Literal or MemoryCell"
|
|
self.cells[index.value].set(value.value)
|
|
|
|
def __len__(self):
|
|
"""Gibt die Größe des Speicherarrays zurück."""
|
|
return self.size
|
|
|
|
def __str__(self):
|
|
"""Gibt eine Liste der Speicherzellen zurück."""
|
|
return str([cell.value for cell in self.cells])
|
|
|
|
def __iter__(self):
|
|
"""Gibt einen Iterator über die Speicherzellen zurück."""
|
|
return iter(self.cells)
|
|
|
|
def indices(self):
|
|
"""Gibt eine Liste der Indizes der Speicherzellen zurück."""
|
|
return [Literal(i) for i in range(self.size)]
|
|
|
|
def reset_counters(self):
|
|
"""Setzt alle Zähler auf 0 zurück."""
|
|
for cell in self.cells:
|
|
cell.reset_counters()
|
|
|
|
@staticmethod
|
|
def create_random_array(count, min_value, max_value):
|
|
"""Erzeugt ein zufälliges Speicherarray."""
|
|
size = Literal(count)
|
|
a = MemoryArray(size)
|
|
for i in a.indices():
|
|
a[i] = Literal(randint(min_value, max_value))
|
|
a.reset_counters()
|
|
return a
|
|
|
|
@staticmethod
|
|
def create_array_from_file(filename, limit=None):
|
|
"""Erzeugt ein Speicherarray aus einer Datei."""
|
|
this_dir = Path(__file__).resolve().parent
|
|
project_dir = this_dir.parent
|
|
filename = project_dir / filename
|
|
with open(filename) as f:
|
|
lines = f.readlines()
|
|
if limit is not None:
|
|
lines = lines[:limit]
|
|
size = Literal(len(lines))
|
|
a = MemoryArray(size)
|
|
for i, line in enumerate(lines):
|
|
a[Literal(i)] = Literal(int(line))
|
|
a.reset_counters()
|
|
return a
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import random
|
|
size = Literal(5)
|
|
a = MemoryArray(size)
|
|
for i in a.indices():
|
|
a[i] = Literal(random.randint(1,100))
|
|
print(a)
|
|
sum = MemoryCell(0)
|
|
for cell in a.cells:
|
|
sum += cell
|
|
print(sum)
|
|
MemoryManager.print_statistics() |