| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| """Operators specific to slicing operations.""" |
|
|
| import collections |
|
|
|
|
| |
|
|
|
|
| class GetItemOpts(collections.namedtuple('GetItemOpts', ('element_dtype',))): |
| pass |
|
|
|
|
| def get_item(target, i, opts): |
| """The slice read operator (i.e. __getitem__). |
| |
| Note: it is unspecified whether target will be mutated or not. In general, |
| if target is mutable (like Python lists), it will be mutated. |
| |
| Args: |
| target: An entity that supports getitem semantics. |
| i: Index to read from. |
| opts: A GetItemOpts object. |
| |
| Returns: |
| The read element. |
| |
| Raises: |
| ValueError: if target is not of a supported type. |
| """ |
| assert isinstance(opts, GetItemOpts) |
|
|
| |
| return _py_get_item(target, i) |
|
|
|
|
| def _py_get_item(target, i): |
| """Overload of get_item that executes a Python list modification.""" |
| return target[i] |
|
|
|
|
| def set_item(target, i, x): |
| """The slice write operator (i.e. __setitem__). |
| |
| Note: it is unspecified whether target will be mutated or not. In general, |
| if target is mutable (like Python lists), it will be mutated. |
| |
| Args: |
| target: An entity that supports setitem semantics. |
| i: Index to modify. |
| x: The new element value. |
| |
| Returns: |
| Same as target, after the update was performed. |
| |
| Raises: |
| ValueError: if target is not of a supported type. |
| """ |
| |
| return _py_set_item(target, i, x) |
|
|
|
|
| def _py_set_item(target, i, x): |
| """Overload of set_item that executes a Python list modification.""" |
| target[i] = x |
| return target |
|
|