# kevinlawler/kona

Optimized eachpair for minus (-':) #137, macro yt yn for y->t y->n

1 parent 215ee69 commit 0fd2f19a13aa9d4cdc66d739ba06526517a6e255 committed Sep 10, 2011
Showing with 41 additions and 29 deletions.
1. +0 −1 0.c
2. +11 −6 k.c
3. +13 −8 kx.c
4. +2 −0 tests.c
5. +3 −1 ts.h
6. +0 −1 v.c
7. +12 −12 vd.c
1 0.c
 @@ -784,7 +784,6 @@ K _5d(K x,K y) //can't write symbols/chars efficiently if #bytes rounded up. for this reason switched to using MAX(.,4*sizeof(I)) instead of nearI(.) //Largely Copy/pasted from 2:monadic - I yt=y->t, yn=y->n; if(4!=xt && 3!=ABS(xt)) R 0;//TODO: type error S m=CSK(x); //looks for .K or .L extensions first
17 k.c
 @@ -271,10 +271,19 @@ K show(K a) R a; } +K minus_eachpair(K x, K y) +{ + P(yn<2 || !yt || ABS(yt) > 2, 0) + K z = newK(yt,yn-1); + if (-2==yt) DO(yn-1, kF(z)[i]=kF(y)[i+1]-kF(y)[i]) + else if(-1==yt) DO(yn-1, kI(z)[i]=kI(y)[i+1]-kI(y)[i]) + if(x){K u,v; u=enlist(x); M(u,z) v=join(u,z); cd(u);cd(z); R v; } + R z; +} + 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) @@ -307,7 +316,6 @@ K plus_scan(K x, K y) K plus_over(K x,K y) { I accI=0; F accF=0; - I yt=y->t,yn=y->n; if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0; K z,r; SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F @@ -323,7 +331,6 @@ K plus_over(K x,K y) K times_over(K x,K y) { I accI=1; F accF=1; - I yt=y->t,yn=y->n; if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0; K z,r; SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F @@ -339,7 +346,6 @@ K times_over(K x,K y) K max_or_over(K x,K y) { I accI=0; F accF=-FI; - I yt=y->t, yn=y->n; if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0; K z,r; SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F @@ -355,7 +361,6 @@ K max_or_over(K x,K y) K min_and_over(K x,K y) { I accI=1; F accF=FI; - I yt=y->t, yn=y->n; if((!yt&&yn) || ABS(yt) > 2 || (x && xt!=1 && xt!=2)) R 0; K z,r; SW(ABS(yt)) //May want to consider casting y->t==1 to F's when x && x->t is F @@ -390,7 +395,7 @@ TR DT[] = //Dispatch table is append-only. Reorder/delete/insert breaks backwar {0, 1, flip,"+",0}, {0, 2, plus,"+",{plus_over,plus_scan}}, {0, 1, negate,"-",0}, - {0, 2, minus,"-",0}, + {0, 2, minus,"-",{0,0,minus_eachpair}}, {0, 1, first,"*",0}, {0, 2, times,"*",{times_over}}, {0, 1, reciprocal,"%%",0},
21 kx.c
 @@ -28,7 +28,6 @@ Z K overDyad(K a, V *p, K b) K u=0,v=0; K y=a?v=join(u=enlist(a),b):b; //oom u (TODO: need to unroll to 'x f/y' and 'f/y' to optimize?) - I yt=y->t, yn=y->n; K z=0,g=0; if(yt > 0){z=ci(y); GC;} if(yn == 0){if(VA(*o))z=LE; GC; } //Some verbs will handle this in alt_funcs @@ -53,7 +52,6 @@ Z K scanDyad(K a, V *p, K b) //k4 has 1 +\ 2 3 yield 3 6 instead of 1 3 6 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; K z=newK(0,yn),c,d; @@ -191,6 +189,12 @@ Z K eachleft2(K a, V *p, K b) Z K eachpair2(K a, V *p, K b) //2==k necessary? { + V *o=p-1; K(*f)(K,K); + + K k=0; + if(VA(*o) && (f=DT[(I)*o].alt_funcs.verb_eachpair))k=f(a,b); //k==0 just means not handled. Errors are not set to come from alt_funcs + P(k,k) + I bt=b->t, bn=b->n; if(bt > 0) R dv_ex(b,p-1,b); if(bt <= 0) @@ -200,19 +204,20 @@ Z K eachpair2(K a, V *p, K b) //2==k necessary? else if(bn < 2) R newK(0,0);//TODO: this newK and the above.....does empty list type depend on input? } - K z = newK(0,bn-1),d=0; //oom + K z = newK(0,bn-1),d=0; U(z) K g,h; if(0 >bt)DO(bn-1, h=newK(ABS(bt),1); g=newK(ABS(bt),1); memcpy(h->k,((V)b->k)+(i)*bp(bt),bp(bt)); memcpy(g->k,((V)b->k)+(i+1)*bp(bt),bp(bt)); d=dv_ex(g,p-1,h); cd(g);cd(h);U(d) kK(z)[i]=d) //TODO: err/mmo - cd(z) - oom-g-h if(0==bt)DO(bn-1, d=dv_ex(kK(b)[i+1],p-1,kK(b)[i]); U(d) kK(z)[i]=d) //TODO: err/mmo - cd(z) - z=demote(z); //oom + z=demote(z); - if(a) //mmo + if(a) { K u,v; - u=enlist(a);//oom - v=join(u,z);//oom - cd(u); + u=enlist(a); + M(u,z) + v=join(u,z); + cd(u);cd(z); R v; }
 @@ -417,6 +417,8 @@ Z I tests02() 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? + TC(10 1, 10 -'': 2 3) + //bv_ex subtriadic //bv_ex 1. doesn't seem to handle projectons correcly
4 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; V verb_scan; } AF; //Alternative/Adverb Functions +typedef struct af{ V verb_over; V verb_scan; V verb_eachpair; } 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) @@ -37,6 +37,8 @@ typedef struct tr{ I adverbClass; I arity; V func; S text; AF alt_funcs; } TR; / #define R return #define xt x->t #define xn x->n +#define yt y->t +#define yn y->n #define DO(n,x) {I i=0,_i=(n);for(;i<_i;++i){x;}} #define DO2(n,x){I j=0,_j=(n);for(;j<_j;++j){x;}} #define DO3(n,x){I k=0,_k=(n);for(;k<_k;++k){x;}}
1 v.c
 @@ -222,7 +222,6 @@ K at_ref(K *p, K b, K c, K y) // @[`a;0 1;+;10 20] P(pt > 0 && pt != 5 && pt != 6,RE) I bt=b->t, bn=b->n; - I yt,yn; if(y){ yt=y->t; yn=y->n;} if(0==bn && (-1==bt || 0==bt || 5==pt || 6==pt)) R 0;//Identity TODO ???? P(0==bn && bt <= 0,IE)
24 vd.c
 @@ -109,10 +109,10 @@ K dot(K a, K b) //NB: b can be a cheating 0-type with NULLs .. ? K dot_ref(K *p, K *x, K *z, I s, K c, K y) { K d=*p, f=x?*x:0; - I dt=d->t, dn=countI(d), ft, fn, yt, yn; + I dt=d->t, dn=countI(d), ft, fn, yn0; if(f) {ft=f->t; fn=countI(f);} - if(y) {yt=y->t; yn=countI(y);} + if(y) {yn0=countI(y);} if(-1==s && 0==fn && -3!=ft) { @@ -137,15 +137,15 @@ K dot_ref(K *p, K *x, K *z, I s, K c, K y) if(0>=s) at_ref(p,f,c,y); //what errors will this take care of ? else if(0==ft) { - if(!atomI(f) && y && !atomI(y) && fn != yn) R LE; - I n = (atomI(f) && y)?yn:fn; + if(!atomI(f) && y && !atomI(y) && fn != yn0) R LE; + I n = (atomI(f) && y)?yn0:fn; if(y) U(y=promote(y)) - DO(n, dot_ref(p, kK(f)+(i%fn), z, s, c, kK(y)[i%yn])) + DO(n, dot_ref(p, kK(f)+(i%fn), z, s, c, kK(y)[i%yn0])) cd(y); } else if(1==ABS(ft)) { - if(!atomI(f) && y && !atomI(y) && fn != yn) R LE; + if(!atomI(f) && y && !atomI(y) && fn != yn0) R LE; if( 1==ft && dt > 0) R TE; // (5,6) if(y && yt != 0 && !atomI(f)) U(y = promote(y)) @@ -157,14 +157,14 @@ K dot_ref(K *p, K *x, K *z, I s, K c, K y) DO(fn, I e=kI(f)[i]; if( e < 0 || dn <= e ) R XE; )//check is in advance DO(fn, K py=0; - if(y) py=atomI(f)?y:kK(y)[i%yn]; + if(y) py=atomI(f)?y:kK(y)[i%yn0]; dot_ref(kK(d)+(kI(f)[i]),z,z+1,s-1,c,py); ) cd(y); } else if(4==ABS(ft)) { - if(!atomI(f) && y && !atomI(y) && fn != yn) R LE; + if(!atomI(f) && y && !atomI(y) && fn != yn0) R LE; if( 4==ft && 0 >= dt) R TE; if(-4==ft && 0 >= dt) R IE; if(y && yt != 0 && !atomI(f)) U(y = promote(y)) @@ -173,7 +173,7 @@ K dot_ref(K *p, K *x, K *z, I s, K c, K y) //Only 6/4, 5/4, 5/-4 at this point DO(fn, K py = 0; - if(y) py=atomI(f)?y:kK(y)[i%yn]; //trying promote here instead of itemAtIndex like in at_ref + if(y) py=atomI(f)?y:kK(y)[i%yn0]; //trying promote here instead of itemAtIndex like in at_ref S u = kS(f)[i]; dot_ref(lookupEVOrCreate(p,u),z,z+1,s-1,c,py); //oom, cd(y), ??? ) @@ -182,10 +182,10 @@ K dot_ref(K *p, K *x, K *z, I s, K c, K y) else if(6==ft) { if(6==dt) R NULL; //identity - if(y && !atomI(y) && yn != d->n) R LE; + if(y && !atomI(y) && yn0 != d->n) R LE; if(y) U(y=promote(y)) - if(5==dt) DO(d->n, dot_ref(EVP(DI(d,i)),z,z+1,s-1,c,y?kK(y)[i%yn]:0)) - if(0>=dt) { K k=Ki(0); M(k,y?y:k); DO(countI(d), *kI(k)=i; dot_ref(p,&k,z,s,c,y?kK(y)[i%yn]:0)) cd(k); } + if(5==dt) DO(d->n, dot_ref(EVP(DI(d,i)),z,z+1,s-1,c,y?kK(y)[i%yn0]:0)) + if(0>=dt) { K k=Ki(0); M(k,y?y:k); DO(countI(d), *kI(k)=i; dot_ref(p,&k,z,s,c,y?kK(y)[i%yn0]:0)) cd(k); } cd(y); } R 0;