summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-02-01 20:38:31 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-02-01 20:38:31 +0000
commit178745ae557eacb2326a6da5cffa2d0fb48fd7ac (patch)
treeeba8e5d2098e96301a6bcd98808ee359b9d83ab4
parent569d4dd07a42d12b55d138ac624196e548933740 (diff)
Implement the rank operator
-rw-r--r--operators.c45
1 files changed, 37 insertions, 8 deletions
diff --git a/operators.c b/operators.c
index 202667c..f6066d9 100644
--- a/operators.c
+++ b/operators.c
@@ -244,16 +244,45 @@ opAtop(Datum *lefto, Datum *righto, Array *left, Array *right)
default:
p = q = r = 0;
}
- print("Running %S with ranks %d %d %d\n", ppdatum(*lefto), p, q, r);
- if(left)
- throwerror(nil, ENotImplemented);
- else{
-
+
+ /* "Normalize" the ranks (remove negatives, and ranks above the array's ranks */
+ if(left){
+ q = q < 0 ? left->rank + q : q;
+ q = q < 0 ? 0 : q;
+ q = q > left->rank ? left->rank : q;
}
- throwerror(nil, ENotImplemented);
- return nil;
- }else
+ p = p < 0 ? right->rank + p : p;
+ p = p < 0 ? 0 : p;
+ p = p > right->rank ? right->rank : p;
+ r = r < 0 ? right->rank + r : r;
+ r = r < 0 ? 0 : r;
+ r = r > right->rank ? right->rank : r;
+
+ Array *result;
+ if(left){
+ Array *cellrankl = mkscalarint(q);
+ Array *cellrankr = mkscalarint(r);
+ Array *lefts = rundfn(L"(⌷∘⍵)¨⍳(-⍺)↓⍴⍵", nil, nil, cellrankl, left);
+ Array *rights = rundfn(L"(⌷∘⍵)¨⍳(-⍺)↓⍴⍵", nil, nil, cellrankr, right);
+ result = rundfn(L"↑⍺⍶¨⍵", lefto, nil, lefts, rights);
+ freearray(cellrankl);
+ freearray(cellrankr);
+ freearray(lefts);
+ freearray(rights);
+ }else{
+ if(right->rank == p)
+ result = runfunc(lefto->func, nil, right);
+ else{
+ Array *cellrank = mkscalarint(p);
+ result = rundfn(L"↑⍶¨(⌷∘⍵)¨⍳(-⍺)↓⍴⍵", lefto, nil, cellrank, right);
+ freearray(cellrank);
+ }
+ }
+ return result;
+ }else{
+ throwerror(L"Unknown combination of operands to ⍤", ENotImplemented);
return nil;
+ }
}
Array *