Skip to content
Browse files

Optimized scan adverb for plus (+\) #137 #63

  • Loading branch information...
1 parent 9322284 commit 215ee691a3abb8b6b90ab447247497db0ca2a6bf @kevinlawler committed
Showing with 58 additions and 12 deletions.
  1. +34 −1 k.c
  2. +16 −10 kx.c
  3. +7 −0 tests.c
  4. +1 −1 ts.h
View
35 k.c
@@ -271,6 +271,39 @@ K show(K a)
R a;
}
+K plus_scan(K x, K y)
+{
+ //Optimized scan forces you to pre-determine output type for (x,y), in contrast to optimized over
+ I yt=y->t,yn=y->n;
+
+ P(x && xt != 1 && xt != 2, 0)
+ P(yn<2 || !yt || ABS(yt) > 2 , 0)
+
+ I t = -ABS(yt); if(x) t = -MAX(ABS(xt),ABS(t));
+ I n = y->n + (x?1:0);
+
+ K z;
+
+ if(!x && y->c==1 && (yt==t || sizeof(I)==sizeof(F))) z=ci(y); //reuse vector you know will be discarded
+ else z=newK(t,n);
+ U(z)
+
+ I j=0;
+ if(x)
+ {
+ j=1;
+ if (-2==t && 2==xt)*kF(z)=*kF(x);
+ else if(-2==t && 1==xt)*kF(z)=*kI(x);
+ else if(-1==t && 1==xt)*kI(z)=*kI(x);
+ }
+
+ if (-2==t && -2==yt) {kF(z)[j]=*kF(y)+(j?*kF(z):0); DO(yn-1, kF(z)[i+j+1] = kF(z)[i+j]+kF(y)[i+1])}
+ else if(-2==t && -1==yt) {kF(z)[j]=*kI(y)+(j?*kF(z):0); DO(yn-1, kF(z)[i+j+1] = kF(z)[i+j]+kI(y)[i+1])}
+ else if(-1==t && -1==yt) {kI(z)[j]=*kI(y)+(j?*kI(z):0); DO(yn-1, kI(z)[i+j+1] = kI(z)[i+j]+kI(y)[i+1])}
+
+ R z;
+}
+
K plus_over(K x,K y)
{
I accI=0; F accF=0;
@@ -355,7 +388,7 @@ TR DT[] = //Dispatch table is append-only. Reorder/delete/insert breaks backwar
{0, 0, 0,0,0},
{0, 0, 0,0,0},
{0, 1, flip,"+",0},
- {0, 2, plus,"+",{plus_over}},
+ {0, 2, plus,"+",{plus_over,plus_scan}},
{0, 1, negate,"-",0},
{0, 2, minus,"-",0},
{0, 1, first,"*",0},
View
26 kx.c
@@ -17,7 +17,7 @@ Z V ex_(V a,I r);
//TODO: for derived verbs like +/ you can add the sub-pieces in parallel
-Z K over2(K a, V *p, K b)
+Z K overDyad(K a, V *p, K b)
{
V *o=p-1; K(*f)(K,K);
@@ -44,8 +44,14 @@ Z K over2(K a, V *p, K b)
R z;
}
-Z K scan2(K a, V *p, K b) //k4 has 1 +\ 2 3 yield 3 6 instead of 1 3 6
+Z K scanDyad(K a, V *p, K b) //k4 has 1 +\ 2 3 yield 3 6 instead of 1 3 6
{
+ V *o=p-1; K(*f)(K,K);
+
+ K k=0;
+ if(VA(*o) && (f=DT[(I)*o].alt_funcs.verb_scan))k=f(a,b); //k==0 just means not handled. Errors are not set to come from alt_funcs
+ P(k,k)
+
K u=0; K y=a?join(u=enlist(a),b):ci(b); cd(u); //oom
I yt=y->t, yn=y->n;
if(yt > 0 || yn == 0) R y;
@@ -64,7 +70,7 @@ Z K scan2(K a, V *p, K b) //k4 has 1 +\ 2 3 yield 3 6 instead of 1 3 6
R collapse(z);
}
-Z K over2l(K a, V *p, K b)
+Z K overMonad(K a, V *p, K b)
{
K u=b,c=0;I flag=0;
@@ -102,7 +108,7 @@ Z K over2l(K a, V *p, K b)
R c;
}
-Z K scan2l(K a, V *p, K b)
+Z K scanMonad(K a, V *p, K b)
{
K u=enlist(b),v,w,c=0,d;I flag=0;//TODO: optimize/memory manage enlists,firsts,reverses here
U(u);
@@ -146,7 +152,7 @@ Z K scan2l(K a, V *p, K b)
R u;
}
-Z K each2l(K a, V *p, K b)
+Z K each2(K a, V *p, K b)
{
I bt=b->t, bn=b->n;
if(bt > 0) R dv_ex(0,p-1,b);
@@ -234,8 +240,8 @@ Z K dv_ex(K a, V *p, K b)
if(2==k)
{
- if (adverb == offsetOver) R over2(a, p, b);
- if (adverb == offsetScan) R scan2(a, p, b);
+ if (adverb == offsetOver) R overDyad(a, p, b);
+ if (adverb == offsetScan) R scanDyad(a, p, b);
if (adverb == offsetEach)
{
if(!a) adverb = offsetEachright;
@@ -260,9 +266,9 @@ Z K dv_ex(K a, V *p, K b)
}
} else if(2 > k)
{
- if (adverb == offsetOver) R over2l(a, p, b);
- if (adverb == offsetScan) R scan2l(a, p, b);
- if (adverb == offsetEach) R each2l(a, p, b);
+ if (adverb == offsetOver) R overMonad(a, p, b);
+ if (adverb == offsetScan) R scanMonad(a, p, b);
+ if (adverb == offsetEach) R each2(a, p, b);
}
if(adverb == offsetEachright) R eachright2(a, p, b);
View
7 tests.c
@@ -410,6 +410,13 @@ Z I tests02()
TC((1;"wsfull"),@[.:;"&0I";:])
TC((1;"wsfull"),@[.:;"!0I";:])
+ TC(1 3 , +\\ 1 2)
+ TC(1 3.0 , +\\ 1.0 2.0)
+ TC(1 2 4.0, 1.0 +\\ 1 2)
+ TC(1 2 4.0, 1.0 +\\ 1.0 2.0)
+ TC(1 2 4 , 1 +\\ 1 2) //These change in K4
+ TC(1 2 4.0, 1 +\\ 1.0 2.0) //We break with K3's (1;2.0;4.0). K3 bug?
+
//bv_ex subtriadic
//bv_ex 1. doesn't seem to handle projectons correcly
View
2 ts.h
@@ -17,7 +17,7 @@ enum TYPE_SEVEN_MEMBERS {CONTEXT,DEPTH,CODE,LOCALS,PARAMS,CONJ,CACHE_WD,CACHE_TR
//Executable types: t-n is 7-n for n in {0,1,2,3,4,5,6,7}: 0: list of unexecuted types, 1: [derived] verb, 2: dynamically loaded function, 3: brace function{}, 4: ":[]", 5: if[], 6: while[], 7: do[]
typedef struct node{V k,v;I b;struct node *c[2];}Node;typedef Node*N;//Knuth's AVL tree
typedef struct pda{I i,s,n;S c;}Pda;typedef Pda*PDA; //holds parse state. pos in input, state, stacklength, stack
-typedef struct af{ V verb_over; } AF; //Alternative/Adverb Functions
+typedef struct af{ V verb_over; V verb_scan; } AF; //Alternative/Adverb Functions
typedef struct tr{ I adverbClass; I arity; V func; S text; AF alt_funcs; } TR; //Table Row for Dispatch Table
#define ke(x) (((K)x)->k)
#define kK(x) ke(x)

0 comments on commit 215ee69

Please sign in to comment.
Something went wrong with that request. Please try again.