From df6916dbe8409ad2463167abfa605c0ac6a5c44d Mon Sep 17 00:00:00 2001 From: lyken Date: Tue, 20 Aug 2024 15:00:27 +0800 Subject: [PATCH] core/ndstrides: add NDArrayObject::make_copy --- nac3core/src/codegen/object/ndarray/mod.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/nac3core/src/codegen/object/ndarray/mod.rs b/nac3core/src/codegen/object/ndarray/mod.rs index 5d38157a..d713b08d 100644 --- a/nac3core/src/codegen/object/ndarray/mod.rs +++ b/nac3core/src/codegen/object/ndarray/mod.rs @@ -346,6 +346,24 @@ impl<'ctx> NDArrayObject<'ctx> { call_nac3_ndarray_set_strides_by_shape(generator, ctx, self.instance); } + /// Clone/Copy this ndarray - Allocate a new ndarray with the same shape as this ndarray and copy the contents over. + /// + /// The new ndarray will own its data and will be C-contiguous. + #[must_use] + pub fn make_copy( + &self, + generator: &mut G, + ctx: &mut CodeGenContext<'ctx, '_>, + ) -> Self { + let clone = NDArrayObject::alloca(generator, ctx, self.dtype, self.ndims); + + let shape = self.instance.gep(ctx, |f| f.shape).load(generator, ctx); + clone.copy_shape_from_array(generator, ctx, shape); + clone.create_data(generator, ctx); + clone.copy_data_from(generator, ctx, *self); + clone + } + /// Copy data from another ndarray. /// /// This ndarray and `src` is that their `np.size()` should be the same. Their shapes