This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
Generic type support of multi dimensional Array #232
Unanswered
ramakotareddy
asked this question in
Ideas
Replies: 3 comments 10 replies
-
Couple of thoughts:
PS: please fix code snippet formatting |
Beta Was this translation helpful? Give feedback.
7 replies
-
One more thing, in your code snippet: int[][][] arr1 = [[], [], []]; You have only provided initialization for two levels of arrays. int[][][] arr1 = [[[1,2,3],[4,5,6],[7,8,9]], [[10,11,12],[13,14,15],[16,17,18]], [[19,20,21],[22,23,24],[25,26,27]]]; #3x3x3 |
Beta Was this translation helpful? Give feedback.
2 replies
-
@manuranga Please give your suggestions on "Do we need to create separate init and load functions for other types(float, bool and string types) or is these Rust functions are enough?" |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi @manuranga, As per yesterday's discussion in call, working multi dimensional arrays with other types (float, boolean and string types) without creating separate init and load functions. i have created init and load for multi dimensional arrays in Rust files like below.
Rust Functions for multi dimensional Array:
Init Function:
#[no_mangle]
pub extern "C" fn array_init_array_array(size: i32) -> *mut Vec<*mut Vec<*mut i32>> {
let size_t = if size > 0 { size } else { 8 };
let size_t = size_t as usize;
let initVec: Box<Vec<*mut Vec<*mut i32>>> = Box::new(Vec::with_capacity(size_t));
let vec_pointer = Box::into_raw(initVec);
return vec_pointer as *mut Vec<*mut Vec<*mut i32>>;
}
Load Function:
#[no_mangle]
pub extern "C" fn array_load_array_array(arr_ptr: *mut Vec<*mut Vec<*mut i32>>, index: i32) -> *mut Vec<*mut i32> {
let mut arr = unsafe { Box::from_raw(arr_ptr) };
let index_n = index as usize;
let len = index_n + 1;
if arr.len() < len {
arr.resize(len, 0 as *mut Vec<*mut i32>);
}
let mut return_val = arr[index_n];
if return_val.is_null() {
arr[index_n] = Box::into_raw(Box::new(Vec::with_capacity(len)));
}
return_val = arr[index_n];
mem::forget(arr);
return return_val;
}
multi dimensional test cases with string and int types:
public function main() {
string[][][][][] arr1 = [[], [],[],[],[]];
arr1[0][1][1][1][1] = "Hello";
arr1[1][2][2][2][2] = " World";
string a = arr1[0][1][1][1][1];
string b = arr1[1][2][2][2][2];
print_string(a);
print_string(b);
}
int[][][] arr1 = [[], [], []];
int[][][] arr2 = [[], [], []];
public function main() {
arr1 = [[], [], []];
arr1[0][1][1] = 1;
arr1[1][1][2] = 5;
arr1[3][3][3] = 10;
int index1 = 3;
arr2 = [[], [], []];
arr2[1][2][2] = 10;
arr2[3][3][3] = 50;
arr2[4][2][1] = 100;
int index2 = 4;
int a = arr1[0][1][1] + arr1[1][1][2] + arr1[index1][index1][index1];
int b = arr2[1][2][2] + arr2[3][3][3] + arr2[index2][2][1];
print_string("RESULT=");
printu32(a + b);
}
The above test cases are working with above Rust functions. Do we need to create separate init and load functions for other types(float, bool and string types) or is these Rust functions are enough?
@manuranga @ruvi-d and @KavinduZoysa Please give your suggestions.
Beta Was this translation helpful? Give feedback.
All reactions