shithub: lpa

ref: 50104ba72987b26febfa4e398458d47db20e9bea
dir: /array.c/

View raw version
#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;
}