map_negative_index
Maps a negative index to its positive equivalent based on the provided length, or returns the index unchanged if it is already non-negative.
def map_negative_index(
index: integer,
length: integer
) - > integer
Converts a potentially negative sequence index into its equivalent positive absolute position. This is used to support Python-style negative indexing where -1 represents the last element in a collection of a specific length.
Parameters
| Name | Type | Description |
|---|---|---|
| index | integer | The sequence index to be normalized, which may be negative. |
| length | integer | The total number of elements in the sequence used to calculate the positive offset. |
Returns
| Type | Description |
|---|---|
integer | The normalized non-negative index if the input was negative, or the original index if it was already positive. |