summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apl9.h2
-rw-r--r--array.c38
-rw-r--r--functions.c9
3 files changed, 48 insertions, 1 deletions
diff --git a/apl9.h b/apl9.h
index 4bb1296..2320bb2 100644
--- a/apl9.h
+++ b/apl9.h
@@ -169,6 +169,7 @@ int scalarextend(Array *, Array *, Array **, Array **);
int commontype(Array *, Array *, Array **, Array **, int);
Array *arrayitem(Array *, int);
Array *simplifyarray(Array *);
+int comparearray(Array *, Array *);
/* eval.c */
Datum *eval(Statement *);
@@ -214,6 +215,7 @@ Array *fnPower(Array *, Array *);
Array *fnLogarithm(Array *, Array *);
Array *fnLeft(Array *, Array *);
Array *fnRight(Array *, Array *);
+Array *fnMatch(Array *, Array *);
Array *fnCatenateFirst(Array *, Array *);
Array *fnReshape(Array *, Array *);
diff --git a/array.c b/array.c
index 5fb2a32..cf5a7f3 100644
--- a/array.c
+++ b/array.c
@@ -209,4 +209,42 @@ simplifyarray(Array *a)
return b;
}else
return fnSame(a);
+}
+
+int
+comparearray(Array *a, Array *b)
+{
+ /* returns -1 if a < b, 0 if a == b and 1 if a > b.
+ Only correctly handles test for equality right now,
+ and returns 1 for unequal data. */
+
+ if(a->type != b->type)
+ return 1;
+ if(a->rank != b->rank)
+ return 1;
+ if(a->size != b->size)
+ return 1;
+ for(int i = 0; i < a->rank; i++)
+ if(a->shape[i] != b->shape[i])
+ return 1;
+ for(int i = 0; i < a->size; i++){
+ if(a->type == AtypeArray){
+ /* do something recursive here */
+ int sub = comparearray(a->arraydata[i], b->arraydata[i]);
+ if(sub != 0)
+ return sub;
+ }else{
+ int sub = memcmp(
+ a->rawdata + i * datasizes[a->type],
+ b->rawdata + i * datasizes[a->type],
+ datasizes[a->type]);
+ if(sub < 0)
+ return -1;
+ else if(sub > 0)
+ return 1;
+ }
+ }
+
+ /* if we get here, the arrays are equal */
+ return 0;
} \ No newline at end of file
diff --git a/functions.c b/functions.c
index 353ad88..90bb3fb 100644
--- a/functions.c
+++ b/functions.c
@@ -85,7 +85,7 @@ fndyad dyadfunctiondefs[] = {
0, /* < */
0, /* > */
0, /* ≥ */
- 0, /* ≡ */
+ fnMatch, /* ≡ */
0, /* ≢ */
0, /* ∨ */
0, /* ∧ */
@@ -509,6 +509,13 @@ fnRight(Array *left, Array *right)
}
Array *
+fnMatch(Array *left, Array *right)
+{
+ int cmp = comparearray(left, right);
+ return mkscalarint(cmp == 0);
+}
+
+Array *
fnCatenateFirst(Array *left, Array *right)
{
Array *leftarr;