ref: dcb761aebd2815fdf2d04c0b05724292de9dc98b
dir: /array.c/
#include <u.h> #include <libc.h> #include <thread.h> #include "dat.h" #include "fns.h" /* This file is the only file that knows how arrays are stored. * In theory, that allows us to experiment with other representations later. */ struct Array { int rank; usize size; usize *shape; union { void *data; vlong *intdata; Rune *chardata; }; }; void initarrays(void) { dataspecs[DataArray].size = sizeof(Array); } Array * allocarray(int type, int rank, usize size) { Array *a = alloc(DataArray); a->rank = rank; a->size = size; switch(type){ case TypeNumber: size *= sizeof(vlong); break; case TypeChar: size *= sizeof(Rune); break; } a->shape = allocextra(a, (sizeof(usize) * rank) + size); a->data = (void*)(a->shape+rank); return a; } void setint(Array *a, usize offset, vlong v) { a->intdata[offset] = v; } void setshape(Array *a, int dim, usize size) { a->shape[dim] = size; } char * printarray(Array *a) { /* TODO: this is just for debugging */ char buf[2048]; char *p = buf; p += sprint(p, "type: %s shape: ", "numeric"); for(int i = 0; i < a->rank; i++) p += sprint(p, "%lld ", a->shape[i]); p += sprint(p, "data: "); for(uvlong i = 0; i < a->size; i++) p += sprint(p, "%lld ", a->intdata[i]); return buf; }