diff options
author | glenda <glenda@9front> | 2022-10-22 16:45:32 +0000 |
---|---|---|
committer | glenda <glenda@9front> | 2022-10-22 16:45:32 +0000 |
commit | a82186dab48c234c507693b166acd7c13433864a (patch) | |
tree | eb6db255c93b1e6278e0e833e915c4c85372c84f /array.c | |
parent | a3b635de1753ed4ce73dabdc7e2c24dfdf77891e (diff) |
Atomic reference counting
Diffstat (limited to 'array.c')
-rw-r--r-- | array.c | 53 |
1 files changed, 27 insertions, 26 deletions
@@ -1,6 +1,7 @@ #include <u.h> #include <libc.h> #include <bio.h> +#include <thread.h> #include "apl9.h" @@ -42,8 +43,8 @@ Array * mkrunearray(Rune *str) { Array *a = allocarray(AtypeRune, 1, runestrlen(str)); - a->shape[0] = GetSize(a); - for(int i = 0; i < GetSize(a); i++) + a->shape[0] = a->size; + for(int i = 0; i < a->size; i++) a->runedata[i] = str[i]; return a; } @@ -52,17 +53,17 @@ Array * duparray(Array *a) { Array *b = duparrayshape(a, GetType(a)); - memcpy(b->rawdata, a->rawdata, datasizes[GetType(a)]*GetSize(a)); + memcpy(b->rawdata, a->rawdata, datasizes[GetType(a)]*a->size); if(GetType(b) == AtypeArray) - for(int i = 0; i < GetSize(b); i++) - incarrayref(b->arraydata[i]); + for(int i = 0; i < b->size; i++) + incref(b->arraydata[i]); return b; } Array * duparrayshape(Array *a, int type) { - Array *b = allocarray(type, GetRank(a), GetSize(a)); + Array *b = allocarray(type, GetRank(a), a->size); memcpy(b->shape, a->shape, sizeof(*a->shape) * GetRank(a)); SetStrand(b, GetStrand(a)); return b; @@ -106,7 +107,7 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb) }else if(GetRank(a) != 0 && GetRank(b) == 0){ *aa = fnSame(a); *bb = extend(b, a); - }else if(GetSize(a) == GetSize(b) && GetRank(a) == GetRank(b)){ + }else if(a->size == b->size && GetRank(a) == GetRank(b)){ /* Check that each dimension matches */ for(int i = 0; i < GetRank(a); i++){ if(a->shape[i] != b->shape[i]) @@ -114,7 +115,7 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb) } *aa = fnSame(a); *bb = fnSame(b); - }else if(GetSize(a) == 1 && GetSize(b) == 1){ + }else if(a->size == 1 && b->size == 1){ Array *shape; if(GetRank(a) > GetRank(b)) shape = fnShape(a); @@ -123,12 +124,12 @@ scalarextend(Array *a, Array *b, Array **aa, Array **bb) *aa = fnReshape(shape, a); *bb = fnReshape(shape, b); freearray(shape); - }else if(GetSize(a) == 1 && GetSize(b) != 1){ + }else if(a->size == 1 && b->size != 1){ Array *shape = fnShape(b); *aa = fnReshape(shape, a); *bb = fnSame(b); freearray(shape); - }else if(GetSize(a) != 1 && GetSize(b) == 1){ + }else if(a->size != 1 && b->size == 1){ Array *shape = fnShape(a); *aa = fnSame(a); *bb = fnReshape(shape, b); @@ -142,7 +143,7 @@ Array * inttofloatarray(Array *a) { Array *b = duparrayshape(a, AtypeFloat); - for(int i = 0; i < GetSize(a); i++) + for(int i = 0; i < a->size; i++) b->floatdata[i] = a->intdata[i]; return b; } @@ -195,7 +196,7 @@ arrayitem(Array *a, int index) break; case AtypeArray: res = a->arraydata[index]; - incarrayref(res); + incref(res); break; default: throwerror(L"Unhandled case in arrayitem 2", ENotImplemented); @@ -207,7 +208,7 @@ Array * simplifyarray(Array *a) { /* simplify an array if possible. */ - if((GetType(a) != AtypeArray && GetType(a) != AtypeMixed) || GetSize(a) == 0) + if((GetType(a) != AtypeArray && GetType(a) != AtypeMixed) || a->size == 0) return fnSame(a); int nested = GetType(a) == AtypeArray; int type = nested ? GetType(a->arraydata[0]) : a->mixeddata[0].type; @@ -216,7 +217,7 @@ simplifyarray(Array *a) int canmix = 1; int i; - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; i++){ int t = nested ? GetType(a->arraydata[i]) : a->mixeddata[i].type; canfloat = canfloat && (t == AtypeFloat || t == AtypeInt); sametype = sametype && (t == type); @@ -227,11 +228,11 @@ simplifyarray(Array *a) if(sametype && type != AtypeArray){ Array *b = duparrayshape(a, type); - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; i++){ if(nested){ memcpy(b->rawdata + i * datasizes[type], a->arraydata[i]->rawdata, datasizes[type]); if(GetType(b) == AtypeArray) - incarrayref(b->arraydata[i]); + incref(b->arraydata[i]); }else{ switch(GetType(b)){ case AtypeInt: b->intdata[i] = a->mixeddata[i].i; break; @@ -250,7 +251,7 @@ simplifyarray(Array *a) return b; }else if(canfloat){ Array *b = duparrayshape(a, AtypeFloat); - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; i++){ if(nested){ if(GetType(a->arraydata[i]) == AtypeFloat) b->floatdata[i] = a->arraydata[i]->floatdata[0]; @@ -266,7 +267,7 @@ simplifyarray(Array *a) return b; }else if(canmix && nested){ Array *b = duparrayshape(a, AtypeMixed); - for(i = 0; i < GetSize(a); i++){ + for(i = 0; i < a->size; 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; @@ -305,7 +306,7 @@ comparearray(Array *a, Array *b, int checkshapes) } } - for(i = 0; i < GetSize(a) && i < GetSize(b); i++){ + for(i = 0; i < a->size && i < b->size; i++){ int sub = 0; switch(GetType(a)){ case AtypeInt: @@ -328,9 +329,9 @@ comparearray(Array *a, Array *b, int checkshapes) return sub; } - if(i < GetSize(a)) + if(i < a->size) return 1; - else if(i < GetSize(b)) + else if(i < b->size) return -1; else return 0; @@ -350,10 +351,10 @@ fillelement(Array *a) return fill; } case AtypeArray:{ - if(GetSize(a) == 0) + if(a->size == 0) return fnSame(a->prototype); Array *b = duparrayshape(a, GetType(a)); - for(int i = 0; i < GetSize(b); i++){ + for(int i = 0; i < b->size; i++){ Array *fill = fillelement(a->arraydata[i]); Array *shape = fnShape(a->arraydata[i]); b->arraydata[i] = fnReshape(shape, fill); @@ -375,9 +376,9 @@ arrayspaceused(Array *a) uvlong size = 0; size += sizeof(*a); size += sizeof(*a->shape) * GetRank(a); - size += datasizes[GetType(a)] * GetSize(a); + size += datasizes[GetType(a)] * a->size; - for(int i = 0; i < GetSize(a) && GetType(a) == AtypeArray; i++) + for(int i = 0; i < a->size && GetType(a) == AtypeArray; i++) size += arrayspaceused(a->arraydata[i]); return size; } @@ -389,7 +390,7 @@ arraydepth(Array *a, int *uniform) int max = -1; int subuniform; *uniform = 1; - for(int i = 0; i < GetSize(a); i++){ + for(int i = 0; i < a->size; i++){ int subdepth = arraydepth(a->arraydata[i], &subuniform); if((subdepth != max && max != -1) || subuniform == 0) *uniform = 0; |