Skip to content

Commit

Permalink
write life time methods for matrix type
Browse files Browse the repository at this point in the history
  • Loading branch information
forFudan committed Sep 13, 2024
1 parent 7a25b4a commit 498d401
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion numojo/core/matrix.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ struct Matrix[dtype: DType = DType.float64]():
3. The strides (row-major or column-major).
4. The data type of the elements (compile-time known).
Attributes:
- data
- shape
- size
- stride
- order
Default constructor: dtype(parameter), shape, order.
"""

Expand All @@ -68,7 +75,7 @@ struct Matrix[dtype: DType = DType.float64]():
inout self,
shape: Tuple[Int, Int],
order: String = "C",
) raises:
):
"""
Matrix NDArray initialization.
Expand All @@ -82,3 +89,30 @@ struct Matrix[dtype: DType = DType.float64]():
self.size = shape[0] * shape[1]
self.data = DTypePointer[dtype].alloc(self.size)
self.order = order

@always_inline("nodebug")
fn __copyinit__(inout self, other: Self):
"""
Copy other into self.
"""
self.shape = (other.shape[0], other.shape[1])
self.stride = (other.stride[0], other.stride[1])
self.size = other.size
self.order = other.order
self.data = DTypePointer[dtype].alloc(other.size)
memcpy(self.data, other.data, other.size)

@always_inline("nodebug")
fn __moveinit__(inout self, owned other: Self):
"""
Move other into self.
"""
self.shape = (other.shape[0], other.shape[1])
self.stride = (other.stride[0], other.stride[1])
self.size = other.size
self.order = other.order
self.data = other.data

@always_inline("nodebug")
fn __del__(owned self):
self.data.free()

0 comments on commit 498d401

Please sign in to comment.