std::vector<std::tm> q::qList2tm(K data) throw(std::string) { if (data == K_NIL) { throw std::string("nil date or datetime list"); } else if (data->t <= 0) { throw std::string("not a date or datetime list"); } assert(data->n >= 0); std::vector<std::tm> result(static_cast<std::size_t>(data->n)); switch (data->t) { case KD: for (std::size_t i = 0; i < data->n; ++i) { Cookbook::gt_r((kI(data)[i] == ni) ? 0 : kI(data)[i], &result[i]); } break; case KZ: for (std::size_t i = 0; i < data->n; ++i) { Cookbook::gt_r((kF(data)[i] == nf) ? 0. : kF(data)[i], &result[i]); } break; default: throw std::string("not a date or datetime list"); } return result; }
//create ,pass and recieve a simple dictionary. [Note Dictionary will be // homogenous. This demo gives an idea on how to deal withthe heterogenous // case.] int eg6() { int i=0,l=2; K keys=ktn(KS,l); K vals=ktn(KI,l); K dict; K result,resultk,resultv; //A dictionary is a mapping of two conforming lists. kI(vals)[0]=1; kI(vals)[1]=2; kS(keys)[0]=ss("key1"); kS(keys)[1]=ss("key2"); // Turn these into a dictionary dict=xD(keys,vals); result=k(c,"{[x]x[`key3]:`int$.z.t;show x;x}",dict,(K)0); printf( " This return has [type=%i] and contains %i enclosed elements\n\n\n",result->t,result->i); //Extract the keys and value vectors from the k bject resultk=kK(result)[0]; resultv=kK(result)[1]; //extract the lenth of the vectors l=resultk->n; printf( " The returned dictionary has %i elements.\n",l); for(i=0;i<l;i++) { printf( " The %i key is %s \n",i,kS(resultk)[i]); printf( " The associated value is %i \n",kI(resultv)[i]); } return 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; }
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; }
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; }
K find(K a, K b) { I at=a->t, an=a->n, bt=b->t; P(at>0,DOE) if(-4==at && 4==bt)DO(an, if(kS(a)[i]==*kS(b))R Ki(i)) if(-3==at && 3==bt)DO(an, if(kC(a)[i]==*kC(b))R Ki(i)) if(-2==at && 2==bt)DO(an, if(!FC(kF(a)[i],*kF(b)))R Ki(i)) if(-2==at && 1==bt){F fb=I2F(*kI(b));DO(an, if(!FC(kF(a)[i],fb))R Ki(i));}
K itemAtIndex(K a, I i) { // Return i-th item from any type as K - TODO: oom wherever this is used I at=a->t; if( 0< at)R ci(a); if(-4==at)R Ks(kS(a)[i]); //could refactor all this if(-3==at)R Kc(kC(a)[i]); if(-2==at)R Kf(kF(a)[i]); if(-1==at)R Ki(kI(a)[i]); R ci(kK(a)[i]); }
//TODO: sort type-0 lists, functions, symbols, etc. //note: K appears to sort _reserved functions by alphabetical order, but this may be a side effect of // sorting by address (if the functions appear in alphabetical order in the source file) // though oddly enough it appears to hold across all underscore/reserved functions (not just math) // _in falls to the end since it is defined as some variation on a char func that works as an inline verb // If you look at the binary the symbols appear to be listed out of alphabetical order //one interesting way to test how functions are sorted would be to load the interpreter twice, //inputting the same collection of functions each time but in a different order //if the sort order changes for each instance then sorting is probably based on pointer/reference value //if that fails then it may be necessary to look at distinctions between wordfunc,charfunc, valence, proj, etc Z K grade_updown(K a, I r) { I at=a->t, an=a->n; P(0< at, RE) if(-3==at) R charGrade(a,r); if(-1==at) { I x,u=II,v=-II;//MIN,MAX DO(an, x=kI(a)[i]; if(x<u)u=x; if(x>v)v=x;) if(v-u < 87654321) R distributionGrade(a,r,u,v);//Magic Number
K kclone(K a)//Deep copy -- eliminate where possible { if(!a) R 0; I t=a->t,n=a->n; K z= 7==t?Kv():newK(t,n); if (4==ABS(t)) DO(n, kS(z)[i]=kS(a)[i]) //memcpy everywhere is better else if(3==ABS(t)) DO(n, kC(z)[i]=kC(a)[i]) else if(2==ABS(t)) DO(n, kF(z)[i]=kF(a)[i]) else if(1==ABS(t)) DO(n, kI(z)[i]=kI(a)[i]) else if(0== t ) DO(n, kK(z)[i]=kclone(kK(a)[i])) else if(5== t ) DO(n, kK(z)[i]=kclone(kK(a)[i])) else if(7== t ) { I k=0; z->t=a->t; I vt=z->n = a->n; K kv; V*v; SW(vt) { CS(1, k=((K)kV(a)[CODE])->n-1; M(z,kv=newK(-4,k+1)) v=(V*)kK(kv); //v[k]=0;//superfluous reminder DO(k, V w=kW(a)[i]; if(VA(w))v[i]=w; //TODO: is this ok for NAMES? see similar code in capture() else { K r=kclone(*(K*)w); //oom V q=newE(LS,r); //oom kap((K*) kV(z)+LOCALS,&q);//oom cd(q);//kap does ci q=EVP(q); //oom free z etc. kap needs checking v[i]=q; } ) ) CS(2, M(z,kv=newK(-4,3)) v=(V*)kK(kv); memcpy(v,kW(a),3*sizeof(V)); )
//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; }
K grade_updown(K a, I r) { I at=a->t, an=a->n; P(0< at, RE) if(-4==at)R symGrade(a,r); if(-3==at)R charGrade(a,r); if(-1==at||-2==at){ K z; if(an<2){z=newK(-1,an);M(z);DO(an,kI(z)[i]=i);R z;} else{ K x=0;uI y,u=(uI)-1,v=0,h=0,k;//MIN,MAX if(-2==at){x=newK(-1,an);M(x);} //trst(); //elapsed("x=newK"); if(-1==at)DO(an,y=kI(a)[i];h|=y;if(y<u)u=y;if(y>v)v=y) else DO(an,kU(x)[i]=(y=FtoI(kF(a)[i]));h|=y;if(y<u)u=y;if(y>v)v=y) //elapsed("fill x"); //O("u:%016llx v:%016llx\n",u,v); if((r&&-1==at)||((u&MSB)!=(v&MSB))){ u=(uI)-1;v=0;h=0; if(-1==at){ x=newK(-1,an);M(x); DO(an,kU(x)[i]=(y=ItoU(kI(a)[i]));h|=y;if(y<u)u=y;if(y>v)v=y)} else DO(an,kU(x)[i]=(y=ItoU(kI(x)[i]));h|=y;if(y<u)u=y;if(y>v)v=y)}
K ia2q(int *data,long *dims,long d,long size){ int length=dims[0],elesize=size/length,i; K L; if(1==d){ L=ktn(KI,length); for(i=0;i<length;i++)kI(L)[i]=*(data+i); R L; }else if(1<d){ L=knk(0); for(i=0;i<length;i++)jk(&L,ia2q(data+i*elesize,dims+1,d-1,elesize)); R L; } R (K)0; }
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; }
int ei_x_encode_same_list_time(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { if(opts->day_seconds_is_q_time) { EI(ei_x_encode_tuple_header(types, 2)); EI(ei_x_encode_atom(types, "list")); EI(ei_x_encode_atom(types, "time")); if(r->n > 0) { EI(ei_x_encode_list_header(values, r->n)); int i; for(i=0; i<r->n; ++i) { EI(ei_x_encode_ki_val(values, msec_to_sec(kI(r)[i]))); } } EI(ei_x_encode_empty_list(values)); } else { EI(ei_x_encode_same_list_integer(types, values, "time", r, opts)); } return 0; }
K runtest(K opv, K ecxv, K eaxv, K edxv, K testCount) { struct MsrInOut s_pmc_reset[9]; struct MsrInOut s_pmc_read[9]; unsigned long long s_ffc_fixed[FFC_COUNT]; unsigned long long s_pmc_fixed[PMC_COUNT]; struct MsrInOut *ptr; int i; long long count; void *handle; char *error; K result; void (*execute_test)(void (s1)(void), void(s2)(void)); // dynamically load the test library handle = dlopen("libtest.so", RTLD_NOW); //handle = dlopen("libtest.so", RTLD_LAZY); if (!handle) { krr(dlerror()); // signal exception to kdb+ return (K)0; } execute_test = dlsym(handle, "execute_test"); if ((error = dlerror()) != NULL) { krr("While locating symbof 'execute_test'"); return (K)0; } // zero the fixed-cost accumulators for (i = 0 ; i < PMC_COUNT ; i++) s_pmc_fixed[i] = 0; for (i = 0 ; i < FFC_COUNT ; i++) s_ffc_fixed[i] = 0; // set the global (static) pointers ffc_fixed = s_ffc_fixed; pmc_fixed = s_pmc_fixed; pmc_reset = s_pmc_reset; pmc_read = s_pmc_read; ptr = pmc_cfg = (struct MsrInOut*)malloc((opv->n + 1) * sizeof(struct MsrInOut)); if (pmc_cfg == NULL) { orr("malloc"); return (K)0; } record_reset(); record_read(); // record the PMC instructions to memory count = opv->n; for (i = 0 ; i < count ; i++) { wr_msrio(ptr++, kI(opv)[i], kI(ecxv)[i], kI(eaxv)[i], kI(edxv)[i]); } msr_wr_stop(ptr++); loadDriver(); if (fd == -1) { return (K)0; } result = run_test(execute_test, testCount->i); // disable and zero the PMC MSRs ioctl(fd, IOCTL_MSR_CMDS, (long long)s_pmc_reset); // return the dynamically allocated memory free(pmc_cfg); // close the dyn-lib function handle dlclose(handle); // close the MSR driver closeDriver(fd); return result; }
if(an<2){z=newK(-1,an);M(z);DO(an,kI(z)[i]=i);R z;} else{ K x=0;uI y,u=(uI)-1,v=0,h=0,k;//MIN,MAX if(-2==at){x=newK(-1,an);M(x);} //trst(); //elapsed("x=newK"); if(-1==at)DO(an,y=kI(a)[i];h|=y;if(y<u)u=y;if(y>v)v=y) else DO(an,kU(x)[i]=(y=FtoI(kF(a)[i]));h|=y;if(y<u)u=y;if(y>v)v=y) //elapsed("fill x"); //O("u:%016llx v:%016llx\n",u,v); if((r&&-1==at)||((u&MSB)!=(v&MSB))){ u=(uI)-1;v=0;h=0; if(-1==at){ x=newK(-1,an);M(x); DO(an,kU(x)[i]=(y=ItoU(kI(a)[i]));h|=y;if(y<u)u=y;if(y>v)v=y)} else DO(an,kU(x)[i]=(y=ItoU(kI(x)[i]));h|=y;if(y<u)u=y;if(y>v)v=y)} k=v-u; if(!k){z=newK(-1,an);M(z);DO(an,kI(z)[i]=i)} else if(an<IGT)z=insertGradeU(x?x:a,r); else if((k<DGT)&&((9*an+(1^19))>2*k))z=distributionGrade(x?x:a,r,u,v); else z=radixGrade(x?x:a,r,h); //elapsed("sort"); cd(x); } R z; } R mergeGrade(a,r); } K grade_up(K a){R grade_updown(a,0);} K grade_down(K a){R grade_updown(a,1);} K enlist(K x) {
// libzmq Z K0(version){K mnp=ktn(KI,3); zmq_version(&kI(mnp)[0],&kI(mnp)[1],&kI(mnp)[2]); R mnp;}