Skip to content

Commit

Permalink
[External] [stdlib] [NFC] Rename List UnsafePointer constructor a…
Browse files Browse the repository at this point in the history
…rgs (#50487)

[External] [stdlib] [NFC] Rename `List` `UnsafePointer` constructor args

Rename `List` `UnsafePointer` constructor args. Part of
#3704.

ORIGINAL_AUTHOR=martinvuyk
<[email protected]>
PUBLIC_PR_LINK=#3742

Co-authored-by: martinvuyk <[email protected]>
Closes #3742
MODULAR_ORIG_COMMIT_REV_ID: f4e85d1d2a78c7cc81860b9757897bd5cf230f06
  • Loading branch information
modularbot and martinvuyk committed Dec 17, 2024
1 parent ea6fa59 commit 18d584a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 42 deletions.
32 changes: 9 additions & 23 deletions stdlib/benchmarks/builtin/bench_sort.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ fn bench_tiny_list_sort[type: DType](inout m: Bench) raises:
fn bench_sort_list(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[Scalar[type]].alloc(count)
var list = List[Scalar[type]](
unsafe_pointer=ptr, size=count, capacity=count
)
var list = List[Scalar[type]](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand All @@ -113,9 +111,7 @@ fn bench_tiny_list_sort[type: DType](inout m: Bench) raises:
fn bench_small_sort(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[Scalar[type]].alloc(count)
var list = List[Scalar[type]](
unsafe_pointer=ptr, size=count, capacity=count
)
var list = List[Scalar[type]](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand All @@ -134,9 +130,7 @@ fn bench_tiny_list_sort[type: DType](inout m: Bench) raises:
fn bench_insertion_sort(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[Scalar[type]].alloc(count)
var list = List[Scalar[type]](
unsafe_pointer=ptr, size=count, capacity=count
)
var list = List[Scalar[type]](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand Down Expand Up @@ -172,9 +166,7 @@ fn bench_small_list_sort[type: DType](inout m: Bench, count: Int) raises:
fn bench_sort_list(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[Scalar[type]].alloc(count)
var list = List[Scalar[type]](
unsafe_pointer=ptr, size=count, capacity=count
)
var list = List[Scalar[type]](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand All @@ -193,9 +185,7 @@ fn bench_small_list_sort[type: DType](inout m: Bench, count: Int) raises:
fn bench_insertion_sort(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[Scalar[type]].alloc(count)
var list = List[Scalar[type]](
unsafe_pointer=ptr, size=count, capacity=count
)
var list = List[Scalar[type]](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand Down Expand Up @@ -228,9 +218,7 @@ fn bench_large_list_sort[type: DType](inout m: Bench, count: Int) raises:
fn bench_sort_list(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[Scalar[type]].alloc(count)
var list = List[Scalar[type]](
unsafe_pointer=ptr, size=count, capacity=count
)
var list = List[Scalar[type]](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand All @@ -249,9 +237,7 @@ fn bench_large_list_sort[type: DType](inout m: Bench, count: Int) raises:
fn bench_heap_sort(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[Scalar[type]].alloc(count)
var list = List[Scalar[type]](
unsafe_pointer=ptr, size=count, capacity=count
)
var list = List[Scalar[type]](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand Down Expand Up @@ -287,7 +273,7 @@ fn bench_low_cardinality_list_sort(
fn bench_sort_list(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[UInt8].alloc(count)
var list = List[UInt8](unsafe_pointer=ptr, size=count, capacity=count)
var list = List[UInt8](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand All @@ -306,7 +292,7 @@ fn bench_low_cardinality_list_sort(
fn bench_heap_sort(inout b: Bencher) raises:
seed(1)
var ptr = UnsafePointer[UInt8].alloc(count)
var list = List[UInt8](unsafe_pointer=ptr, size=count, capacity=count)
var list = List[UInt8](ptr=ptr, length=count, capacity=count)

@always_inline
@parameter
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/builtin/file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ struct FileHandle:
raise (err_msg^).consume_as_error()

var list = List[UInt8](
unsafe_pointer=buf, size=int(size_copy), capacity=int(size_copy)
ptr=buf, length=int(size_copy), capacity=int(size_copy)
)

return list
Expand Down
16 changes: 6 additions & 10 deletions stdlib/src/collections/list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,17 @@ struct List[T: CollectionElement, hint_trivial_type: Bool = False](
self.append(value[])

fn __init__(
inout self,
*,
unsafe_pointer: UnsafePointer[T],
size: Int,
capacity: Int,
inout self, *, ptr: UnsafePointer[T], length: Int, capacity: Int
):
"""Constructs a list from a pointer, its size, and its capacity.
"""Constructs a list from a pointer, its length, and its capacity.
Args:
unsafe_pointer: The pointer to the data.
size: The number of elements in the list.
ptr: The pointer to the data.
length: The number of elements in the list.
capacity: The capacity of the list.
"""
self.data = unsafe_pointer
self.size = size
self.data = ptr
self.size = length
self.capacity = capacity

fn __moveinit__(inout self, owned existing: Self):
Expand Down
6 changes: 2 additions & 4 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ struct String(
var size = impl.size
var capacity = impl.capacity
self._buffer = Self._buffer_type(
unsafe_pointer=impl.steal_data(), size=size, capacity=capacity
ptr=impl.steal_data(), length=size, capacity=capacity
)

@always_inline
Expand Down Expand Up @@ -840,9 +840,7 @@ struct String(
"""
# we don't know the capacity of ptr, but we'll assume it's the same or
# larger than len
self = Self(
Self._buffer_type(unsafe_pointer=ptr, size=length, capacity=length)
)
self = Self(Self._buffer_type(ptr=ptr, length=length, capacity=length))

# ===------------------------------------------------------------------=== #
# Factory dunders
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,9 @@ fn _to_string_list[
og_ptr = unsafe_ptr_fn(i_ptr[i])
memcpy(p, og_ptr, og_len)
p[og_len] = 0 # null terminator
buf = String._buffer_type(unsafe_pointer=p, size=f_len, capacity=f_len)
buf = String._buffer_type(ptr=p, length=f_len, capacity=f_len)
(out_ptr + i).init_pointee_move(String(buf^))
return List[String](unsafe_pointer=out_ptr, size=i_len, capacity=i_len)
return List[String](ptr=out_ptr, length=i_len, capacity=i_len)


@always_inline
Expand Down
4 changes: 2 additions & 2 deletions stdlib/test/collections/test_list.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ def test_constructor_from_pointer():
new_pointer[2] = 2
# rest is not initialized

var some_list = List[Int8](unsafe_pointer=new_pointer, size=3, capacity=5)
var some_list = List[Int8](ptr=new_pointer, length=3, capacity=5)
assert_equal(some_list[0], 0)
assert_equal(some_list[1], 1)
assert_equal(some_list[2], 2)
Expand All @@ -731,7 +731,7 @@ def test_constructor_from_other_list_through_pointer():
var size = len(initial_list)
var capacity = initial_list.capacity
var some_list = List[Int8](
unsafe_pointer=initial_list.steal_data(), size=size, capacity=capacity
ptr=initial_list.steal_data(), length=size, capacity=capacity
)
assert_equal(some_list[0], 0)
assert_equal(some_list[1], 1)
Expand Down

0 comments on commit 18d584a

Please sign in to comment.