Skip to content

sonolus.script.array_like

ArrayLike

Bases: Sequence

Mixin for array-like objects.

Inheritors must implement __len__, __getitem__, and __setitem__.

Usage
class MyArrayLike[T](Record, ArrayLike[T]):
    def __len__(self) -> int:
        ...

    def __getitem__(self, index: int) -> T:
        ...

    def __setitem__(self, index: int, value: T):
        ...

__contains__(value: Any) -> bool

Return whether any element in the array is equal to the given value.

Parameters:

Name Type Description Default
value Any

The value to check for.

required

__getitem__(index: int) -> T abstractmethod

Return the item at the given index.

Parameters:

Name Type Description Default
index int

The index of the item. Must be an integer between 0 and len(self) - 1.

required

__iter__() -> SonolusIterator[T]

Return an iterator over the array.

__len__() -> int abstractmethod

Return the length of the array.

__reversed__()

Return a reversed view of the array.

__setitem__(index: int, value: T) abstractmethod

Set the value of the item at the given index.

Parameters:

Name Type Description Default
index int

The index of the item. Must be an integer between 0 and len(self) - 1.

required
value T

The value to set.

required

count(value: T) -> int

Return the number of elements in the array equal to the given value.

Parameters:

Name Type Description Default
value T

The value to count.

required

index(value: T, start: int = 0, stop: int | None = None) -> int

Return the index of the value in the array equal to the given value.

Parameters:

Name Type Description Default
value T

The value to search for.

required
start int

The index to start searching from.

0
stop int | None

The index to stop searching at. If None, search to the end of the array.

None

index_of_max(*, key: Callable[[T], Any] | None = None) -> int

Return the index of the maximum value in the array.

Parameters:

Name Type Description Default
key Callable[[T], Any] | None

A one-argument ordering function to use for comparison like the one used in max().

None

index_of_min(*, key: Callable[[T], Any] | None = None) -> int

Return the index of the minimum value in the array.

Parameters:

Name Type Description Default
key Callable[[T], Any] | None

A one-argument ordering function to use for comparison like the one used in min().

None

last_index(value: T) -> int

Return the last index of the value in the array equal to the given value.

Parameters:

Name Type Description Default
value T

The value to search for.

required

reverse()

Reverse the values in the array in place.

shuffle()

Shuffle the values in the array in place.

sort(*, key: Callable[[T], Any] | None = None, reverse: bool = False)

Sort the values in the array in place.

Parameters:

Name Type Description Default
key Callable[[T], Any] | None

A one-argument ordering function to use for comparison.

None
reverse bool

If True, sort in descending order, otherwise sort in ascending order.

False

swap(i: int, j: int)

Swap the values at the given indices.

Parameters:

Name Type Description Default
i int

The first index.

required
j int

The second index.

required

get_positive_index(index: int, length: int) -> int

Get the positive index for the given index in the array of the given length.

This is used to convert negative indixes relative to the end of the array to positive indices.

Parameters:

Name Type Description Default
index int

The index to convert.

required
length int

The length of the array.

required

Returns:

Type Description
int

The positive index.