summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
Diffstat (limited to 'array.c')
-rw-r--r--array.c124
1 files changed, 61 insertions, 63 deletions
diff --git a/array.c b/array.c
index a712ad3..f97ac5c 100644
--- a/array.c
+++ b/array.c
@@ -43,8 +43,8 @@ Array *
mkrunearray(Rune *str)
{
Array *a = allocarray(AtypeRune, 1, runestrlen(str));
- a->shape[0] = a->size;
- for(int i = 0; i < a->size; i++)
+ a->shape[0] = GetSize(a);
+ for(int i = 0; i < GetSize(a); i++)
a->runedata[i] = str[i];
return a;
}
@@ -52,10 +52,10 @@ mkrunearray(Rune *str)
Array *
duparray(Array *a)
{
- Array *b = duparrayshape(a, a->type);
- memcpy(b->rawdata, a->rawdata, datasizes[a->type]*a->size);
- if(b->type == AtypeArray)
- for(int i = 0; i < b->size; i++)
+ Array *b = duparrayshape(a, GetType(a));
+ memcpy(b->rawdata, a->rawdata, datasizes[GetType(a)]*GetSize(a));
+ if(GetType(b) == AtypeArray)
+ for(int i = 0; i < GetSize(b); i++)
incarrayref(b->arraydata[i]);
return b;
}
@@ -63,22 +63,22 @@ duparray(Array *a)
Array *
duparrayshape(Array *a, int type)
{
- Array *b = allocarray(type, a->rank, a->size);
- memcpy(b->shape, a->shape, sizeof(int) * a->rank);
- b->stranded = a->stranded;
+ Array *b = allocarray(type, GetRank(a), GetSize(a));
+ memcpy(b->shape, a->shape, sizeof(*a->shape) * GetRank(a));
+ SetStrand(b, GetStrand(a));
return b;
}
int
simplearray(Array *a)
{
- return a->type != AtypeArray;
+ return GetType(a) != AtypeArray;
}
int
simplescalar(Array *a)
{
- return a->rank == 0 && a->type != AtypeArray;
+ return GetRank(a) == 0 && GetType(a) != AtypeArray;
}
Array *
@@ -101,35 +101,35 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb)
aa and bb are unchanged.
*/
- if(a->rank == 0 && b->rank != 0){
+ if(GetRank(a) == 0 && GetRank(b) != 0){
*aa = extend(a, b);
*bb = fnSame(b);
- }else if(a->rank != 0 && b->rank == 0){
+ }else if(GetRank(a) != 0 && GetRank(b) == 0){
*aa = fnSame(a);
*bb = extend(b, a);
- }else if(a->size == b->size && a->rank == b->rank){
+ }else if(GetSize(a) == GetSize(b) && GetRank(a) == GetRank(b)){
/* Check that each dimension matches */
- for(int i = 0; i < a->rank; i++){
+ for(int i = 0; i < GetRank(a); i++){
if(a->shape[i] != b->shape[i])
return 0;
}
*aa = fnSame(a);
*bb = fnSame(b);
- }else if(a->size == 1 && b->size == 1){
+ }else if(GetSize(a) == 1 && GetSize(b) == 1){
Array *shape;
- if(a->rank > b->rank)
+ if(GetRank(a) > GetRank(b))
shape = fnShape(a);
else
shape = fnShape(b);
*aa = fnReshape(shape, a);
*bb = fnReshape(shape, b);
freearray(shape);
- }else if(a->size == 1 && b->size != 1){
+ }else if(GetSize(a) == 1 && GetSize(b) != 1){
Array *shape = fnShape(b);
*aa = fnReshape(shape, a);
*bb = fnSame(b);
freearray(shape);
- }else if(a->size != 1 && b->size == 1){
+ }else if(GetSize(a) != 1 && GetSize(b) == 1){
Array *shape = fnShape(a);
*aa = fnSame(a);
*bb = fnReshape(shape, b);
@@ -142,10 +142,8 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb)
Array *
inttofloatarray(Array *a)
{
- Array *b = allocarray(AtypeFloat, a->rank, a->size);
- for(int i = 0; i < a->rank; i++)
- b->shape[i] = a->shape[i];
- for(int i = 0; i < a->size; i++)
+ Array *b = duparrayshape(a, AtypeFloat);
+ for(int i = 0; i < GetSize(a); i++)
b->floatdata[i] = a->intdata[i];
return b;
}
@@ -157,15 +155,15 @@ commontype(Array *a, Array *b, Array **aa, Array **bb, int forcefloat)
to arrays that have compatible types, with the same data.
*/
if(forcefloat){
- *aa = a->type == AtypeFloat ? fnSame(a) : inttofloatarray(a);
- *bb = b->type == AtypeFloat ? fnSame(b) : inttofloatarray(b);
- }else if(a->type == b->type){
+ *aa = GetType(a) == AtypeFloat ? fnSame(a) : inttofloatarray(a);
+ *bb = GetType(b) == AtypeFloat ? fnSame(b) : inttofloatarray(b);
+ }else if(GetType(a) == GetType(b)){
*aa = fnSame(a);
*bb = fnSame(b);
- }else if(a->type == AtypeFloat && b->type == AtypeInt){
+ }else if(GetType(a) == AtypeFloat && GetType(b) == AtypeInt){
*aa = fnSame(a);
*bb = inttofloatarray(b);
- }else if(a->type == AtypeInt && b->type == AtypeFloat){
+ }else if(GetType(a) == AtypeInt && GetType(b) == AtypeFloat){
*aa = inttofloatarray(a);
*bb = fnSame(b);
}else
@@ -177,7 +175,7 @@ Array *
arrayitem(Array *a, int index)
{
Array *res = nil;
- switch(a->type){
+ switch(GetType(a)){
case AtypeInt:
res = mkscalarint(a->intdata[index]);
break;
@@ -210,33 +208,33 @@ Array *
simplifyarray(Array *a)
{
/* simplify an array if possible. */
- if((a->type != AtypeArray && a->type != AtypeMixed) || a->size == 0)
+ if((GetType(a) != AtypeArray && GetType(a) != AtypeMixed) || GetSize(a) == 0)
return fnSame(a);
- int nested = a->type == AtypeArray;
- int type = nested ? a->arraydata[0]->type : a->mixeddata[0].type;
+ int nested = GetType(a) == AtypeArray;
+ int type = nested ? GetType(a->arraydata[0]) : a->mixeddata[0].type;
int canfloat = type == AtypeFloat || type == AtypeInt;
int sametype = 1;
int canmix = 1;
int i;
- for(i = 0; i < a->size; i++){
- int t = nested ? a->arraydata[i]->type : a->mixeddata[i].type;
+ for(i = 0; i < GetSize(a); i++){
+ int t = nested ? GetType(a->arraydata[i]) : a->mixeddata[i].type;
canfloat = canfloat && (t == AtypeFloat || t == AtypeInt);
sametype = sametype && (t == type);
canmix = canmix && (t != AtypeArray);
- if(nested && a->arraydata[i]->rank != 0)
+ if(nested && GetRank(a->arraydata[i]) != 0)
return fnSame(a); /* cannot be simplified */
}
if(sametype && type != AtypeArray){
Array *b = duparrayshape(a, type);
- for(i = 0; i < a->size; i++){
+ for(i = 0; i < GetSize(a); i++){
if(nested){
memcpy(b->rawdata + i * datasizes[type], a->arraydata[i]->rawdata, datasizes[type]);
- if(b->type == AtypeArray)
+ if(GetType(b) == AtypeArray)
incarrayref(b->arraydata[i]);
}else{
- switch(b->type){
+ switch(GetType(b)){
case AtypeInt: b->intdata[i] = a->mixeddata[i].i; break;
case AtypeFloat: b->floatdata[i] = a->mixeddata[i].f; break;
case AtypeRune: b->runedata[i] = a->mixeddata[i].r; break;
@@ -245,7 +243,7 @@ simplifyarray(Array *a)
}
}
}
- if(b->type == AtypeArray){
+ if(GetType(b) == AtypeArray){
Array *tmp = b;
b = simplifyarray(b);
freearray(tmp);
@@ -253,9 +251,9 @@ simplifyarray(Array *a)
return b;
}else if(canfloat){
Array *b = duparrayshape(a, AtypeFloat);
- for(i = 0; i < a->size; i++){
+ for(i = 0; i < GetSize(a); i++){
if(nested){
- if(a->arraydata[i]->type == AtypeFloat)
+ if(GetType(a->arraydata[i]) == AtypeFloat)
b->floatdata[i] = a->arraydata[i]->floatdata[0];
else
b->floatdata[i] = a->arraydata[i]->intdata[0];
@@ -269,8 +267,8 @@ simplifyarray(Array *a)
return b;
}else if(canmix && nested){
Array *b = duparrayshape(a, AtypeMixed);
- for(i = 0; i < a->size; i++){
- b->mixeddata[i].type = a->arraydata[i]->type;
+ for(i = 0; i < GetSize(a); i++){
+ b->mixeddata[i].type = GetType(a->arraydata[i]);
switch(b->mixeddata[i].type){
case AtypeInt: b->mixeddata[i].i = a->arraydata[i]->intdata[0]; break;
case AtypeFloat: b->mixeddata[i].f = a->arraydata[i]->floatdata[0]; break;
@@ -290,17 +288,17 @@ comparearray(Array *a, Array *b, int checkshapes)
/* returns -1 if a < b, 0 if a == b and 1 if a > b. */
int i;
- if(a->type < b->type)
+ if(GetType(a) < GetType(b))
return -1;
- else if(a->type > b->type)
+ else if(GetType(a) > GetType(b))
return 1;
if(checkshapes){
- if(a->rank < b->rank)
+ if(GetRank(a) < GetRank(b))
return -1;
- else if(a->rank > b->rank)
+ else if(GetRank(a) > GetRank(b))
return 1;
- for(i = 0; i < a->rank; i++){
+ for(i = 0; i < GetRank(a); i++){
if(a->shape[i] < b->shape[i])
return -1;
else if(a->shape[i] > b->shape[i])
@@ -308,9 +306,9 @@ comparearray(Array *a, Array *b, int checkshapes)
}
}
- for(i = 0; i < a->size && i < b->size; i++){
+ for(i = 0; i < GetSize(a) && i < GetSize(b); i++){
int sub = 0;
- switch(a->type){
+ switch(GetType(a)){
case AtypeInt:
sub = a->intdata[i] > b->intdata[i] ? 1 : a->intdata[i] == b->intdata[i] ? 0 : -1;
break;
@@ -324,16 +322,16 @@ comparearray(Array *a, Array *b, int checkshapes)
sub = comparearray(a->arraydata[i], b->arraydata[i], checkshapes);
break;
default:
- print("Missing comparison code for type %d\n", a->type);
+ print("Missing comparison code for type %d\n", GetType(a));
threadexitsall(nil);
}
if(sub != 0)
return sub;
}
- if(i < a->size)
+ if(i < GetSize(a))
return 1;
- else if(i < b->size)
+ else if(i < GetSize(b))
return -1;
else
return 0;
@@ -342,7 +340,7 @@ comparearray(Array *a, Array *b, int checkshapes)
Array *
fillelement(Array *a)
{
- switch(a->type){
+ switch(GetType(a)){
case AtypeInt: return mkscalarint(0);
case AtypeFloat: return mkscalarfloat(0);
case AtypeRune: return mkscalarrune(' ');
@@ -353,8 +351,8 @@ fillelement(Array *a)
return fill;
}
case AtypeArray:{
- Array *b = duparrayshape(a, a->type);
- for(int i = 0; i < b->size; i++){
+ Array *b = duparrayshape(a, GetType(a));
+ for(int i = 0; i < GetSize(b); i++){
Array *fill = fillelement(a->arraydata[i]);
Array *shape = fnShape(a->arraydata[i]);
b->arraydata[i] = fnReshape(shape, fill);
@@ -364,7 +362,7 @@ fillelement(Array *a)
return b;
}
default:
- print("Can't make fill element of array type %d\n", a->type);
+ print("Can't make fill element of array type %d\n", GetType(a));
threadexitsall(nil);
return 0;
}
@@ -375,10 +373,10 @@ arrayspaceused(Array *a)
{
uvlong size = 0;
size += sizeof(*a);
- size += sizeof(int) * a->rank;
- size += datasizes[a->type] * a->size;
+ size += sizeof(*a->shape) * GetRank(a);
+ size += datasizes[GetType(a)] * GetSize(a);
- for(int i = 0; i < a->size && a->type == AtypeArray; i++)
+ for(int i = 0; i < GetSize(a) && GetType(a) == AtypeArray; i++)
size += arrayspaceused(a->arraydata[i]);
return size;
}
@@ -386,11 +384,11 @@ arrayspaceused(Array *a)
int
arraydepth(Array *a, int *uniform)
{
- if(a->type == AtypeArray){
+ if(GetType(a) == AtypeArray){
int max = -1;
int subuniform;
*uniform = 1;
- for(int i = 0; i < a->size; i++){
+ for(int i = 0; i < GetSize(a); i++){
int subdepth = arraydepth(a->arraydata[i], &subuniform);
if((subdepth != max && max != -1) || subuniform == 0)
*uniform = 0;
@@ -400,7 +398,7 @@ arraydepth(Array *a, int *uniform)
return max+1;
}else{
*uniform = 1;
- if(a->rank == 0)
+ if(GetRank(a) == 0)
return 0;
else
return 1;