sonolus.script.array_like¶
ArrayLike
¶
Bases: Sequence[T]
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 |
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 |
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 |
get_unchecked(index: Num) -> T
¶
Get the element at the given index possibly without bounds checking or conversion of negative indexes.
The compiler may still determine that the index is out of bounds and throw an error, but it may skip these checks at runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Num
|
The index to get. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The element at the given index. |
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
|
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 |
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 |
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.
set_unchecked(index: Num, value: T)
¶
Set the element at the given index possibly without bounds checking or conversion of negative indexes.
The compiler may still determine that the index is out of bounds and throw an error, but it may skip these checks at runtime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Num
|
The index to set. |
required |
value
|
T
|
The value to set. |
required |
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 |
False
|
swap(i: int, j: int)
¶
Swap the values at the given positive indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
|
int
|
The first index. |
required |
j
|
int
|
The second index. |
required |
swap_unchecked(i: Num, j: Num)
¶
Swap the values at the given indices possibly without bounds checking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
|
Num
|
The first index. |
required |
j
|
Num
|
The second index. |
required |
unchecked() -> ArrayLike[T]
¶
Return a proxy object that may skip bounds checking and may not support negative indexes.
check_positive_index(index: int, length: int, include_end: bool = False) -> int | float
¶
Check that the given index is a valid index for the array of the given length and convert it to an integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
The index to check. |
required |
length
|
int
|
The length of the array. |
required |
include_end
|
bool
|
Whether to allow the index to be equal to the length of the array (i.e., one past the end). |
False
|
Returns:
| Type | Description |
|---|---|
int | float
|
The index as an integer. |
get_positive_index(index: int | float, length: int | float, *, include_end: bool = False, check: bool = True) -> int
¶
Get the positive index for the given index in the array of the given length, and also perform bounds checking.
This is used to convert negative indices relative to the end of the array to positive indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | float
|
The index to convert. |
required |
length
|
int | float
|
The length of the array. |
required |
include_end
|
bool
|
Whether to allow the index to be equal to the length of the array (i.e., one past the end). |
False
|
check
|
bool
|
Whether to perform bounds checking. Must be a compile-time constant. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
The positive integer index. |