Skip to content

Commit

Permalink
[stdlib] Remove Intable from InlineArray.__getitem__
Browse files Browse the repository at this point in the history
As mentioned at
#2337 (comment), we've
pivoted a bit and moved away from `Intable` for offsets in `__getitem__`
implementations now that we have `Indexer`.  Fix this last use of `__getitem__`
that works on a `Intable` type to use `Int` explicitly.

Do the same for `__setitem__` while we're here.

MODULAR_ORIG_COMMIT_REV_ID: cf13a3daf2056b0eeffe879049f9e865a1595e94
  • Loading branch information
JoeLoser authored and modularbot committed Sep 13, 2024
1 parent 99b7cdb commit 1d13059
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions stdlib/src/utils/static_tuple.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,17 @@ struct StaticTuple[element_type: AnyTrivialRegType, size: Int](Sized):
return result

@always_inline("nodebug")
fn __setitem__[
intable: Intable
](inout self, index: intable, val: Self.element_type):
fn __setitem__(inout self, idx: Int, val: Self.element_type):
"""Stores a single value into the tuple at the specified dynamic index.
Parameters:
intable: The intable type.
Args:
index: The index into the tuple.
idx: The index into the tuple.
val: The value to store.
"""
var offset = int(index)
debug_assert(offset < size, "index must be within bounds")
debug_assert(idx < size, "index must be within bounds")
var tmp = self
var ptr = __mlir_op.`pop.array.gep`(
UnsafePointer.address_of(tmp.array).address, offset.value
UnsafePointer.address_of(tmp.array).address, idx.value
)
Pointer(ptr)[] = val
self = tmp
Expand Down Expand Up @@ -390,25 +384,22 @@ struct InlineArray[

@always_inline("nodebug")
fn __getitem__[
IntableType: Intable,
index: IntableType,
idx: Int,
](ref [_]self: Self) -> ref [__lifetime_of(self)] Self.ElementType:
"""Get a `Reference` to the element at the given index.
Parameters:
IntableType: The inferred type of an intable argument.
index: The index of the item.
idx: The index of the item.
Returns:
A reference to the item at the given index.
"""
alias i = int(index)
constrained[-size <= i < size, "Index must be within bounds."]()
constrained[-size <= idx < size, "Index must be within bounds."]()

var normalized_idx = i
var normalized_idx = idx

@parameter
if i < 0:
if idx < 0:
normalized_idx += size

return self._get_reference_unsafe(normalized_idx)[]
Expand Down

0 comments on commit 1d13059

Please sign in to comment.