|
| 1 | +# |
| 2 | +# This file is part of libdestruct (https://github.com/mrindeciso/libdestruct). |
| 3 | +# Copyright (c) 2024 Roberto Alessandro Bertolini. All rights reserved. |
| 4 | +# Licensed under the MIT license. See LICENSE file in the project root for details. |
| 5 | +# |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import TYPE_CHECKING |
| 10 | + |
| 11 | +from libdestruct.common.array.array import array |
| 12 | +from libdestruct.common.obj import obj |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from collections.abc import Generator, MutableSequence |
| 16 | + |
| 17 | + from libdestruct.common.obj import obj |
| 18 | + |
| 19 | + |
| 20 | +class array_impl(array): |
| 21 | + """A linear sequential array.""" |
| 22 | + |
| 23 | + size: int |
| 24 | + """The size of the array.""" |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self: array_impl, |
| 28 | + memory: MutableSequence, |
| 29 | + address: int | tuple[obj, int], |
| 30 | + backing_type: obj, |
| 31 | + count: int, |
| 32 | + ) -> None: |
| 33 | + """Initialize the array.""" |
| 34 | + self.memory = memory |
| 35 | + self.backing_type = backing_type |
| 36 | + self.count = count |
| 37 | + |
| 38 | + if isinstance(address, tuple): |
| 39 | + self._address = None |
| 40 | + self._reference = address[0] |
| 41 | + self._offset = address[1] |
| 42 | + else: |
| 43 | + self._address = address |
| 44 | + |
| 45 | + self.size = self.backing_type.size * self.count |
| 46 | + |
| 47 | + def size(self: array_impl) -> int: |
| 48 | + """Get the size of the array.""" |
| 49 | + return self.size |
| 50 | + |
| 51 | + def get(self: array_impl) -> str: |
| 52 | + """Get the array as a string.""" |
| 53 | + return f"[{', '.join(str(i) for i in self)}]" |
| 54 | + |
| 55 | + def _set(self: array_impl, _: list[obj]) -> None: |
| 56 | + """Set the array from a list.""" |
| 57 | + raise NotImplementedError("Cannot set items in an array.") |
| 58 | + |
| 59 | + def set(self: array_impl, _: list[obj]) -> None: |
| 60 | + """Set the array from a list.""" |
| 61 | + raise NotImplementedError("Cannot set items in an array.") |
| 62 | + |
| 63 | + def to_bytes(self: array_impl) -> bytes: |
| 64 | + """Return the serialized representation of the array.""" |
| 65 | + return b"".join(x for x in self) |
| 66 | + |
| 67 | + def __getitem__(self: array_impl, index: int) -> obj: |
| 68 | + """Get an item from the array.""" |
| 69 | + if self._address: |
| 70 | + return self.backing_type(self.memory, (self._address, index * self.backing_type.size)) |
| 71 | + |
| 72 | + return self.backing_type(self.memory, (self._reference, self._offset + index * self.backing_type.size)) |
| 73 | + |
| 74 | + def __setitem__(self: array_impl, index: int, value: obj) -> None: |
| 75 | + """Set an item in the array.""" |
| 76 | + raise NotImplementedError("Cannot set items in an array.") |
| 77 | + |
| 78 | + def __iter__(self: array_impl) -> Generator[obj, None, None]: |
| 79 | + """Iterate over the array.""" |
| 80 | + for i in range(self.count): |
| 81 | + yield self[i] |
0 commit comments