std::vector<long long> q::qList2Dec(K data) throw(std::string) { if (data == K_NIL) { throw std::string("nil decimal list"); } else if (data->t <= 0) { throw std::string("not a decimal list"); } assert(data->n >= 0); std::vector<long long> result(static_cast<std::size_t>(data->n), 0L); switch (data->t) { case KB: { struct Converter { long long operator()(G x) const { return x ? 1 : 0; } }; std::transform(kG(data), kG(data) + data->n, result.begin(), Converter()); break; } case KG: std::copy(kG(data), kG(data) + data->n, result.begin()); break; case KH: std::copy(kH(data), kH(data) + data->n, result.begin()); break; case KI: std::copy(kI(data), kI(data) + data->n, result.begin()); break; case KJ: std::copy(kJ(data), kJ(data) + data->n, result.begin()); break; default: throw std::string("not a decimal list"); } return result; }
int mlput(K x,K y){ int fstype=y->t,fsint=y->i,i=0,funcerr=0; K z; char b[2]={0,0}; mlint64 j={0,0}; //printf("%s:%d,%d\n","mlput",x->t,x->n); switch(x->t){ case -KB: case -KG: case -KC:b[0]=x->g;R MLPutString(ml_lp,b); case -KH:R MLPutInteger16(ml_lp,x->h); case -KI:R MLPutInteger32(ml_lp,x->i); case -KJ:*(J*)&j=x->j;R MLPutInteger64(ml_lp,j); case -KE:R MLPutReal32(ml_lp,x->e); case -KF:R MLPutReal64(ml_lp,x->f); case -KS:R MLPutSymbol(ml_lp,x->s); case KB: case KG: case KC:R MLPutByteString(ml_lp,kG(x),x->n); case KH:R MLPutInteger16List(ml_lp,kH(x),x->n); case KI:R MLPutInteger32List(ml_lp,kI(x),x->n); case KJ:R MLPutInteger64List(ml_lp,(mlint64*)kJ(x),x->n); case KE:R MLPutReal32List(ml_lp,kE(x),x->n); case KF:R MLPutReal64List(ml_lp,kF(x),x->n); case KS:if(!MLPutFunction(ml_lp,"List",x->n)){ R 0; }else{ for(i=0;i<x->n;i++)if(!MLPutSymbol(ml_lp,kS(x)[i]))R 0; } break; case 0: if(0==x->n){ R MLPutFunction(ml_lp, "List",0); }else if((3==x->n)&&(fstype==kK(x)[0]->t)){ z=kK(x)[2]; if(!MLPutFunction(ml_lp,kK(x)[1]->s,z->n)){R 0;}else{ switch(z->t){ case 0:for(i=0;i<z->n;i++)if(!mlput(kK(z)[i],y))R 0;break; case KH:for(i=0;i<z->n;i++)if(!MLPutInteger16(ml_lp,kH(z)[i]))R 0;break; case KI:for(i=0;i<z->n;i++)if(!MLPutInteger32(ml_lp,kI(z)[i]))R 0;break; case KJ:for(i=0;i<z->n;i++){*(J*)&j=kJ(z)[i];if(!MLPutInteger64(ml_lp,j))R 0;}break; case KE:for(i=0;i<z->n;i++)if(!MLPutReal32(ml_lp,kE(z)[i]))R 0;break; case KF:for(i=0;i<z->n;i++)if(!MLPutReal64(ml_lp,kF(z)[i]))R 0;break; case KS:for(i=0;i<z->n;i++)if(!MLPutSymbol(ml_lp,kS(z)[i]))R 0;break; case KC:for(i=0;i<z->n;i++){b[0]=kC(z)[i];if(!MLPutString(ml_lp,b))R 0;}break; default:break; } } }else{ if(!MLPutFunction(ml_lp,"List",x->n)){R 0;}else{for(i=0;i<x->n;i++)if(!mlput(kK(x)[i],y)){MLPutSymbol(ml_lp,"ParaErr");funcerr=1;}if(funcerr)R 0;} } break; default: R 0; } R 1; }
//Createing simple vectors int eg5() { //Create a set of vectors of differing types each of length 5. int i=0,l=5; K vsymbol=ktn(KS,l); K vint=ktn(KI,l); K vfloat=ktn(KF,l); K vdate=ktn(KD,l); K vtime=ktn(KT,l); K vdatetime=ktn(KZ,l); K vtimestamp=ktn(KP,l); for(i=0;i<l;i++) { kS(vsymbol)[i]=ss("w"); kI(vint)[i]=i; kF(vfloat)[i]=i+0.0; kI(vdate)[i]=i; kI(vtime)[i]=i; kF(vdatetime)[i]=i+0.1*i; kJ(vtimestamp)[i]=i; } k(c,"display",vint,(K)0); k(c,"display",vsymbol,(K)0); k(c,"display",vfloat,(K)0); k(c,"display",vdate,(K)0); k(c,"display",vtime,(K)0); k(c,"display",vdatetime,(K)0); k(c,"display",vtimestamp,(K)0); return 1; }
int main(int argc,char*argv[]) { K flip,result,columnNames,columnData; int row,col,nCols,nRows; int handle=khpu("localhost",1234,"user:password"); if(handle<0) exit(1); result = k(handle,"`asc",(K)0); std::string str = "([]a:til 10;b:reverse til 10;c:10#01010101010b;d:`a)"; result = k(handle,str.c_str(),(K)0); if(!result) printf("Network Error\n"),perror("Network"),exit(1); if(result->t==-128) printf("Server Error %s\n",result->s),kclose(handle),exit(1); // kclose(handle); if(result->t!=99&&result->t!=98) { printf("type %d\n",result->t); r0(result); exit(1); } flip = ktd(result); // if keyed table, unkey it. ktd decrements ref count of arg. // table (flip) is column names!list of columns (data) columnNames = kK(flip->k)[0]; columnData = kK(flip->k)[1]; nCols = columnNames->n; nRows = kK(columnData)[0]->n; for(row=0;row<nRows;row++) { if(0==row) { for(col=0;col<nCols;col++) { if(col>0)printf(","); printf("%s",kS(columnNames)[col]); } printf("\n"); } for(col=0;col<nCols;col++) { K obj=kK(columnData)[col]; if(col>0)printf(","); switch(obj->t) { case(1):{printf("%d",kG(obj)[row]);}break; case(4):{printf("%d",kG(obj)[row]);}break; case(5):{printf("%d",kH(obj)[row]);}break; case(6):{printf("%d",kI(obj)[row]);}break; case(7):{printf("%lld",kJ(obj)[row]);}break; case(8):{printf("%f",kE(obj)[row]);}break; case(9):{printf("%f",kF(obj)[row]);}break; case(11):{printf("%s",kS(obj)[row]);}break; default:{printf("unknown type");}break; } } printf("\n"); } r0(flip); return 0; }
//create ,pass and recieve a simple table. int eg7() { K cc,d,e,v,tab; K flip,result,columnNames,columnData; int row,col,nCols,nRows; cc=ktn(KS,2);kS(cc)[0]=ss("pid");kS(cc)[1]=ss("uid"); d=ktn(KS,3);kS(d)[0]=ss("ibm");kS(d)[1]=ss("gte");kS(d)[2]=ss("kvm"); e=ktn(KI,3);kI(e)[0]=1;kI(e)[1]=2;kI(e)[2]=3; v=knk(2,d,e); tab=xT(xD(cc,v)); flip=k(c,"{[x]a:update t:.z.t,y:.z.d from x;.tst.t:a;a}",tab,(K)0); //Turn into a dictionary. flip->k [transpose?] //Display table. [Borrowed from code.kx.com: // https://code.kx.com/trac/attachment/wiki/Cookbook/InterfacingWithC/csv.c ] columnNames=kK(flip->k)[0]; columnData=kK(flip->k)[1]; nCols=columnNames->n; nRows=kK(columnData)[0]->n; for(row=0;row<nRows;row++) { if(0==row) { for(col=0;col<nCols;col++) { if(col>0)printf(","); printf("%s",kS(columnNames)[col]); } printf("\n"); } for(col=0;col<nCols;col++) { K obj=kK(columnData)[col]; if(col>0)printf(","); switch(obj->t) { case(1):{printf("%d",kG(obj)[row]);}break; case(4):{printf("%d",kG(obj)[row]);}break; case(5):{printf("%d",kH(obj)[row]);}break; case(6):{printf("%d",kI(obj)[row]);}break; case(7):{printf("%lld",kJ(obj)[row]);}break; case(8):{printf("%f",kE(obj)[row]);}break; case(9):{printf("%f",kF(obj)[row]);}break; case(11):{printf("%s",kS(obj)[row]);}break; case(19):{printf("%i",kI(obj)[row]);}break; case(14):{printf("%i",kI(obj)[row]);}break; default:{printf("unknown type");}break; } } printf("\n"); } return 1; }
double *getklist(K p) { double *r; r=g_malloc(p->n*sizeof(double)); switch(p->t){ case 1 : case 4 : DO(p->n,r[i]=kG(p)[i]);break; case 5 : DO(p->n,r[i]=kH(p)[i]);break; case 6 : case 13 : case 14 : case 17 : case 18 : DO(p->n,r[i]=kI(p)[i]);break; case 7 : case 16 : DO(p->n,r[i]=kJ(p)[i]);break; case 8 : DO(p->n,r[i]=kE(p)[i]);break; case 9 : case 15 : DO(p->n,r[i]=kF(p)[i]);break; case 10 : DO(p->n,r[i]=kC(p)[i]);break; default : g_free(r);return (double *)kerr("invalid numeric type"); } return r; }
static K run_test(void (*execute_test)(void(s1)(void),void(s2)(void)), int testCount) { int i; K result, kffc[3], kpmc[4]; for (i = 0 ; i < PMC_COUNT ; i++) pmc_fixed[i] = 0; for (i = 0 ; i < FFC_COUNT ; i++) ffc_fixed[i] = 0; ioctl(fd, IOCTL_MSR_CMDS, (long long)pmc_reset); execute_baseline(testCount, &start_baseline, &stop_baseline); pmc_fixed[0] = pmc_read[1].value / testCount; pmc_fixed[1] = pmc_read[2].value / testCount; pmc_fixed[2] = pmc_read[3].value / testCount; pmc_fixed[3] = pmc_read[4].value / testCount; ffc_fixed[0] = pmc_read[5].value / testCount; ffc_fixed[1] = pmc_read[6].value / testCount; ffc_fixed[2] = pmc_read[7].value / testCount; for (i = 0 ; i < PMC_COUNT ; i++) kpmc[i] = ktn(KJ, testCount); for (i = 0 ; i < FFC_COUNT ; i++) kffc[i] = ktn(KJ, testCount); for (i = 1 ; i < 1 + PMC_COUNT + FFC_COUNT ; i++) pmc_read[i].value = 0; for (i = 0 ; i < testCount ; i++) { ioctl(fd, IOCTL_MSR_CMDS, (long long)pmc_reset); execute_test(&start_counters, &stop_counters); kJ(kpmc[0])[i] = pmc_read[1].value - pmc_fixed[0]; kJ(kpmc[1])[i] = pmc_read[2].value - pmc_fixed[1]; kJ(kpmc[2])[i] = pmc_read[3].value - pmc_fixed[2]; kJ(kpmc[3])[i] = pmc_read[4].value - pmc_fixed[3]; kJ(kffc[0])[i] = pmc_read[5].value - ffc_fixed[0]; kJ(kffc[1])[i] = pmc_read[6].value - ffc_fixed[1]; kJ(kffc[2])[i] = pmc_read[7].value - ffc_fixed[2]; } result = knk(7, kffc[0], kffc[1], kffc[2], kpmc[0], kpmc[1], kpmc[2], kpmc[3]); return result; }
static int enc(K*k,lua_State *L) { switch (lua_type(L, -1)) { case LUA_TSTRING: { size_t len;const char *str = lua_tolstring(L,-1,&len);(*k)=kpn(str,len);R 1;} break; case LUA_TNUMBER: { F num = lua_tonumber(L,-1);(*k) = (num==floor(num))?kj((J)num):kf(num);R 1;} break; case LUA_TBOOLEAN: { (*k)=kb( lua_toboolean(L,-1) );R 1;} break; case LUA_TNIL: { (*k)=ktn(0,0);R 1;} break; case LUA_TTABLE: { double p; int max = 0; int items = 0; int t_integer = 0, t_number = 0, t_boolean = 0, t_other= 0; lua_pushnil(L); /* table, startkey */ while (lua_next(L, -2) != 0) { items++; /* table, key, value */ switch (lua_type(L, -1)) { case LUA_TNUMBER: t_number++; p = lua_tonumber(L,-1); t_integer += (floor(p) == p); break; case LUA_TBOOLEAN: t_boolean++; break; default: t_other++; break; /* or anything else */ }; if (lua_type(L, -2) == LUA_TNUMBER && (p = lua_tonumber(L, -2))) { /* Integer >= 1 ? */ if (floor(p) == p && p >= 1) { if (p > max) max = p; lua_pop(L, 1); continue; } } /* Must not be an array (non integer key) */ for (lua_pop(L,1); lua_next(L, -2) != 0; lua_pop(L,1)) ++items; max = 0; break; } lua_pushnil(L); if (max != items) { /* build K dictionary */ K keys = ktn(KS,items); K values = ktn(0,items); int n = 0; /* table, startkey */ while (lua_next(L, -2) != 0) { kS(keys)[n] = ss(lua_tostring(L, -2)); if(!enc(kK(values)+n,L))R 0; lua_pop(L,1); ++n; } *k = xD(keys,values); R 1; } /* build K list */ if(t_other || ((!!t_boolean)+(!!t_number)) > 1) { K a = ktn(0,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); if(!enc(kK(a)+LI(p),L))R 0; lua_pop(L, 1); } *k = a; R 1; } if(t_boolean) { K a = ktn(KB,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); kG(a)[LI(p)] = lua_toboolean(L,-1); lua_pop(L, 1); } *k = a; R 1; } if(t_number == t_integer) { K a = ktn(KJ,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); kJ(a)[LI(p)] = (int)floor(lua_tonumber(L,-1)); lua_pop(L, 1); } *k = a; R 1; } if(t_number) { K a = ktn(KF,items); while (lua_next(L, -2) != 0) { p = lua_tonumber(L, -2); kF(a)[LI(p)] = lua_tonumber(L,-1); lua_pop(L, 1); } *k = a; R 1; } *k = ktn(0,0); R 1; }; break; default: luaL_error(L, "Cannot serialise %s: %s", lua_typename(L, lua_type(L, -1)), "can't serialize type"); R 0; }; }