array *do_identity(double size) { int i; int isize = size; array *array_data = array_construct(isize,isize); for (i=0; i < isize; i++) { array_data->data[i*isize+i] = 1.0; } return array_data; }
array *do_transpose(array *a) { int i,j; array *array_data = array_construct(a->cols, a->rows); for (i=0; i < a->rows; i++) { for (j=0; j < a->cols; j++) { array_data->data[j*a->rows+i] = a->data[i*a->cols+j]; } } return array_data; }
STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { if (n_args < 1 || n_args > 2) { nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unexpected # of arguments, %d given", n_args)); } // TODO check args uint l; const char *typecode = mp_obj_str_get_data(args[0], &l); if (n_args == 1) { return array_new(*typecode, 0); } return array_construct(*typecode, args[1]); }
STATIC mp_obj_t array_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, 2, false); // get typecode mp_uint_t l; const char *typecode = mp_obj_str_get_data(args[0], &l); if (n_args == 1) { // 1 arg: make an empty array return array_new(*typecode, 0); } else { // 2 args: construct the array from the given iterator return array_construct(*typecode, args[1]); } }
array *do_csv_array(char *filename) { char *delim = ","; size_t len = 0; char *line = NULL; int rows = 0; int cols = 0; int i=0; int idx=0; FILE *fp = NULL; array *array_data = NULL; fp = open_file(filename, "r"); while (getline(&line, &len, fp) != -1) { double tempCols = do_word_count(line, delim); if (tempCols > cols) { cols = tempCols; } rows++; } array_data = array_construct(rows, cols); /* Read file again storing entries in array_data->data */ rewind(fp); idx = 0; rows = 0; while (getline(&line, &len, fp) != -1) { for (i=0; i < array_data->cols; i++) { char *tmp = i==0 ? line : NULL; char *token = strtok(tmp, delim); assert(token != NULL); array_data->data[idx++] = atof(token); } rows++; } assert(rows == array_data->rows); fclose(fp); if (line) free(line); /* dump_csv(array_data); */ return array_data; }
STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 1, false); if (n_args == 0) { // no args: construct an empty bytearray return array_new(BYTEARRAY_TYPECODE, 0); } else if (MP_OBJ_IS_SMALL_INT(args[0])) { // 1 arg, an integer: construct a blank bytearray of that length uint len = MP_OBJ_SMALL_INT_VALUE(args[0]); mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len); memset(o->items, 0, len); return o; } else { // 1 arg, an iterator: construct the bytearray from that return array_construct(BYTEARRAY_TYPECODE, args[0]); } }
int main(int argc, char** argv, char** envp) { const int size=10; int i; void* a=array_construct(size); for(i=0;i<size;i++) { printf("element [%d] is [%d]\n", i, array_get_value(a, i)); } for(i=0;i<size;i++) { array_set_value(a, i, i); } for(i=0;i<size;i++) { printf("element [%d] is [%d]\n", i, array_get_value(a, i)); } array_destruct(a); a=NULL; return EXIT_SUCCESS; }
int main(int argc, char** argv, char** envp) { const int size=10; int i; void* a=array_construct(size); for(i=0;i<size;i++) { printf("element [%d] is [%d]\n", i, array_get_value(a, i)); } for(i=0;i<size;i++) { array_set_value(a, i, i); } for(i=0;i<size;i++) { printf("element [%d] is [%d]\n", i, array_get_value(a, i)); } printf("array size is [%d]\n", array_size(a)); // this next line will ext the application nicely in DEBUG mode // and will trample on memory in non DEBUG mode array_set_value(a, 13, 7); array_destruct(a); a=NULL; return EXIT_SUCCESS; }
// This is top-level factory function, not virtual method // TODO: "bytearray" really should be type, not function STATIC mp_obj_t mp_builtin_bytearray(mp_obj_t arg) { return array_construct(BYTEARRAY_TYPECODE, arg); }
array *do_make_array(double rows, double cols) { return array_construct(rows, cols); }
inline void array_construct(T* memory, std::size_t size) { boost::has_trivial_default_constructor<T> type; array_construct(memory, size, type); }