uint32_t* unrank_inner(uint32_t n, uint32_t k, uint32_t r, uint32_t* p) { if (k > 0) { swap(&p[n - 1], &p[r % n], sizeof(uint32_t)); unrank(n - 1, k - 1, r / n, p); } return p; }
// Generate all states that can be reached in one flip from the given ranked state void genAll(uint64 state, uint64 nbrs[]) { Perm p; unrank(state, p); int i; for(i=2; i<=PERM_LEN; i++) { Perm rev; revPrefix(p, i, rev); nbrs[i-2] = rank(rev); } }
PermutationArray *PermutationArray::unrank(size_t rank, int n) { element_t *p = new element_t[n]; for (int i=0; i<n; ++i) { p[i] = (element_t)(i+1); } unrank(n, rank, p); PermutationArray *ret = new PermutationArray(); ret->elmts = p; ret->n = n; return ret; }
static void construct_labels() { long i; ///< Allocate and add labels to the array of nodes n^0*u_0+ n^1*u_1 + ///< n^2*u_2 + ... + n^{k-1}*u_{k-1}. Server (u,i,uip), is server ///< attached to switch u, on dimension i, on path to switch u', which ///< differs on dimension i, so u'_i=uip. Thus, server labels have 2 ///< extra coordinates, for i and uip. ///< Note that if ui=_i, then (u,i,uip)=(up,i,ui) (this is the ///< difference between swcube and knkstar). label=malloc((servers+switches) * sizeof(long*)); for(i=0; i<switches+servers; i++) { unrank(&(label[i]),i); } }
void brick_domain::unrank_coordinates(INT rk, INT &x1, INT &y1, INT &x2, INT &y2, INT verbose_level) { //INT f_v = (verbose_level >= 1); INT x0, y0, f_vertical; unrank(rk, f_vertical, x0, y0, verbose_level); x1 = x0; y1 = y0; if (f_vertical) { x2 = x1; y2 = F->add(y1, 1); } else { x2 = F->add(x1, 1); y2 = y1; } }
void *threadcalc(void *threadid) { int i=gi,j=gj,n=gn,m=gm,w=gw,k,left,up,look,ix=*((int *)threadid),l; ull z,mask,nz,newmask,o; u16 mod=gmod,c,r=0; if(i<m-1 && j<n-1) { /* regular cell */ for(z=pstart[ix];z<pend[ix];z++) if((c=prev[z])) { mask=unrank(w,z); left=(mask>>(j<<1))&3; up=(mask>>((j<<1)+2))&3; if(left==3 && up==2) { /* join, easy case: 32 => 00 */ nz=rank(w,mask&(~(15ULL<<(j<<1)))); ADD(nz,c,mod); } else if(left==2 && up==2) { /* join 22: find mate of right 2, change it from 3 to 2 */ for(k=j+2,l=1;;k++) { o=(mask>>(k<<1))&3; if(o==2) l++; else if(o==3) { l--; if(!l) break; } } newmask=mask&(~(15ULL<<(j<<1))); newmask=newmask^(1ULL<<(k<<1)); nz=rank(w,newmask); ADD(nz,c,mod); } else if(left==3 && up==3) { /* join 33: find mate of left 3, change it from 2 to 3 */ for(k=j-1,l=1;;k--) { o=(mask>>(k<<1))&3; if(o==3) l++; else if(o==2) { l--; if(!l) break; } } newmask=mask&(~(15ULL<<(j<<1))); newmask=newmask^(1ULL<<(k<<1)); nz=rank(w,newmask); ADD(nz,c,mod); } else if(left==1 && up) {
int main(int argc, char* argv[]) { // input uint64_t r; // desired rank uint32_t n; // length of set uint32_t k; // length of permutation uint32_t* p; // identity permutation uint32_t* unranked; // unranked permutation if (argc < 3 || argc > 4) { printf("Usage: %s r n <k>\n", argv[0]); return 1; } r = strtoull(argv[1], (char**)NULL, 10); n = strtoul(argv[2], (char**)NULL, 10); if (argc == 4) { k = strtoul(argv[3], (char**)NULL, 10); } else { k = n; } if (k > n) { printf("Error: value of k must be less than or equal to n.\n"); return 1; } p = create_identity_permutation(n); unranked = unrank(n, k, r, p); print_permutation(k, unranked); free(p); return 0; }
int main(int argc, char const *argv[]) { int *array = new int[12]; int i; // array[0] = 8; // array[1] = 103; // array[2] = 109; // array[3] = 101; // array[4] = 2; // array[5] = 111; // array[6] = 7; // array[7] = 6; // array[8] = 0; // array[9] = 4; // array[10] = 5; // array[11] = 10; array[0] = 4; array[1] = 201; array[2] = 7; array[3] = 209; array[4] = 208; array[5] = 210; array[6] = 202; array[7] = 200; array[8] = 5; array[9] = 203; array[10] = 6; array[11] = 211; print_array(array, 12); i = rank(12, array, 6, 2); std::cout << "\n" << i << "\n"; array = unrank(12, i, 6, 2); print_array(array, 12); std::cout << "\n"; delete[] array; return 0; }
/** * Calculates connections * * swcube convention for server ports is that ui<uip iff u is on port 0 * and up is on port 1. */ tuple_t connection_swcube(long node, long port) { tuple_t res; long * thing; long i,j,ujp,uj; if(node<switches) { //build server label from port number and switch label, then rank //it. note that if we are connecting to ujp in jth dimension, //then port== get_port_to_server(u,j,ujp) j=get_j_from_port(port); uj=label[node][j]; ujp=get_ujp_from_port(port,uj); thing=malloc((param_k+2)*sizeof(long)); for(i=0; i<param_k; i++) thing[i]=label[node][i]; //externalise this if we use it a lot later thing[i++]=j; thing[i]=ujp; if(uj<ujp) res.port=0; else { //change to canonical label res.port=1; thing[i]=uj; thing[j]=ujp; } res.node=c2i(thing,1); } else { unrank(&thing,node);//always unranks to (u,j,ujp), where u is the //server on port 0, so uj<ujp ujp=thing[param_k+1]; j=thing[param_k]; uj=thing[j]; if(port==1) { thing[j]=ujp; thing[param_k+1]=uj; } res.node=c2i(thing,0); res.port=get_port_to_server(res.node,j,thing[param_k+1]); } free(thing); return res; }
void PermutationArray::unrank(int n, size_t rank, element_t *p) { if (n > 0) { std::swap(p[n-1], p[rank % n]); unrank(n-1, rank/n, p); } }
Cube::Cube(int corners, int edges, int last, int low, int quan) { this->corners = unrank(8, corners, 0, 8, 3); this->edges = unrank(12, edges, low, quan, 2); this->last = last; }