static void ssort1(string x[], int n, int depth) { int a, b, c, d, r, v; if (n <= 1) return; a = rand() % n; swap(0, a); v = i2c(0); a = b = 1; c = d = n-1; for (;;) { while (b <= c && (r = i2c(b)-v) <= 0) { if (r == 0) { swap(a, b); a++; } b++; } while (b <= c && (r = i2c(c)-v) >= 0) { if (r == 0) { swap(c, d); d--; } c--; } if (b > c) break; swap(b, c); b++; c--; } r = min(a, b-a); vecswap(0, b-r, r, x); r = min(d-c, n-d-1); vecswap(b, n-r, r, x); r = b-a; ssort1(x, r, depth); if (i2c(r) != 0) ssort1(x + r, a + n-d-1, depth+1); r = d-c; ssort1(x + n-r, r, depth); }
void ssort1(char *x[], int n, int depth,int* index) { int a, b, c, d, r, v; if (n <= 1) return; /// Choosing the pivot //a = rand() % n; a=0; /// ///////////////// swap(0, a); swapIndex(0,a); v = i2c(0); a = b = 1; c = d = n-1; for (;;) { while (b <= c && (r = i2c(b)-v) <= 0) { if (r == 0) { swap(a, b); swapIndex(a,b);a++; } b++; } while (b <= c && (r = i2c(c)-v) >= 0) { if (r == 0) { swap(c, d); swapIndex(c,d);d--; } c--; } if (b > c) break; swap(b, c); swapIndex(b,c); b++; c--; } r = min(a, b-a); vecswap(0, b-r, r, x); vecswapIndex(0, b-r, r, index); r = min(d-c, n-d-1); vecswap(b, n-r, r, x); vecswapIndex(b, n-r, r, index); r = b-a; ssort1(x, r, depth,index); if (i2c(r) != 0) ssort1(x + r, a + n-d-1, depth+1,index+r); r = d-c; ssort1(x + n-r, r, depth,index+ n-r); }
void mkqs(unsigned char *pStr, int *resI, int n, int nAlph){ int i; unsigned char **data = malloc(sizeof(char*)*n); // Initialize the data-set for(i=0; i<n; i++) { data[i] = pStr + i; } ssort1(data, n, 0); for(i=0; i<n; i++) resI[i] = (data[i] - pStr); free(data); }
unsigned long * compress_site_patterns_diploid(char ** sequence, const unsigned int * map, int count, int * length, unsigned int ** wptr, int attrib) { int i,j; char * memptr; char ** column; unsigned int * weight; unsigned long * mapping; unsigned char ** jc69_invmaps = NULL; unsigned char charmap[ASCII_SIZE]; unsigned char inv_charmap[ASCII_SIZE]; /* check that at least one sequence is given */ if (!count) return NULL; /* a map must be given */ if (!map) return NULL; /* a zero can never be used as a state */ if (map[0]) return NULL; /* if map states are out of the BYTE range, remap */ if (findmax(map) >= ASCII_SIZE) { remap_range(map,charmap); /* for now only DNA with pll_map_nt */ assert(0); } else { for (i = 0; i < ASCII_SIZE; ++i) charmap[i] = (unsigned char)(map[i]); } /* create inverse charmap to decode states back to characters when compression is finished */ for (i = 0; i < ASCII_SIZE; ++i) if (map[i]) inv_charmap[charmap[i]] = (unsigned char)i; /* encode sequences using charmap */ encode(sequence,charmap,count,*length); /* allocate memory for columns */ column = (char **)xmalloc((size_t)(*length)*sizeof(char *)); /* allocate memory for the alignment */ memptr = column[0] = (char *)xmalloc((size_t)(*length) * (size_t)(count+1) * sizeof(char)); /* map memory to each column */ for (i = 1; i < *length; ++i) column[i] = column[i-1] + (count+1); /* allocate space for weight vector */ weight = (unsigned int *)xmalloc((size_t)(*length)*sizeof(unsigned int)); /* allocate space for mapping vector */ mapping = (unsigned long *)xmalloc((size_t)(*length)*sizeof(unsigned long)); /* split alignment into columns instead of rows */ for (i = 0; i < (*length); ++i) { for (j = 0; j < count; ++j) column[i][j] = sequence[j][i]; column[i][j] = 0; } /* allocate space for storing original indices (before sorting sites) */ int * oi = (int *)xmalloc(*length * sizeof(int)); for (i = 0; i < *length; ++i) oi[i] = i; /* do the jc69 now */ if (attrib == COMPRESS_JC69) jc69_invmaps = encode_jc69(column,*length,count); /* sort the columns and keep original indices */ ssort1(column, *length, 0, oi); /*first site in uncompressed alignment maps to first site in compressed */ int compressed_length = 1; size_t ref = 0; mapping[0] = 0; weight[ref] = 1; /* find all unique columns and set their mappings A2->A3 */ int * compressed_oi = (int *)xmalloc(*length * sizeof(int)); compressed_oi[0] = oi[0]; for (i = 1; i < *length; ++i) { if (strcmp(column[i],column[i-1])) { column[ref+1] = column[i]; compressed_oi[ref+1] = oi[i]; ++ref; ++compressed_length; weight[ref] = 1; } else weight[ref]++; /* map original index i in uncompressed alignment to index in compressed */ mapping[oi[i]] = ref; } /* decode the jc69 encoding */ if (attrib == COMPRESS_JC69) { for (i=0; i < compressed_length; ++i) { unsigned char * sitemap = jc69_invmaps[compressed_oi[i]]; if (sitemap) for (j=0; j < count; ++j) column[i][j] = sitemap[(unsigned int)column[i][j]]; } for (i = 0; i < *length; ++i) if (jc69_invmaps[i]) free(jc69_invmaps[i]); free(jc69_invmaps); } /* copy the unique columns over the original sequences */ for (i = 0; i < compressed_length; ++i) for (j = 0; j < count; ++j) sequence[j][i] = column[i][j]; /* add terminating zero */ for (j = 0; j < count; ++j) sequence[j][compressed_length] = 0; /* deallocate memory */ free(memptr); free(column); /* adjust weight vector size to compressed length */ unsigned int * mem = (unsigned int *)xmalloc((size_t)compressed_length * sizeof(unsigned int)); if (mem) { /* copy weights */ for (i = 0; i < compressed_length; ++i) mem[i] = weight[i]; /* free and re-point */ free(weight); weight = mem; } *wptr = weight; /* update length */ *length = compressed_length; /* decode sequences using inv_charmap */ encode(sequence,inv_charmap,count,compressed_length); free(oi); free(compressed_oi); return mapping; }
static void ssort1(char ** x, int n, int depth, int * oi) { int a,b,c,d,r,v; if (n <= 1) return; a = rand() % n; SWAP(x[0], x[a]); if (oi) SWAP(oi[0], oi[a]); v = x[0][depth]; a = b = 1; c = d = n-1; while (1) { while (b <= c && (r = x[b][depth]-v) <= 0) { if (r == 0) { SWAP(x[a], x[b]); if (oi) SWAP(oi[a], oi[b]); ++a; } ++b; } while (b <= c && (r = x[c][depth]-v) >= 0) { if (r == 0) { SWAP(x[c], x[d]); if (oi) SWAP(oi[c], oi[d]); --d; } --c; } if (b > c) break; SWAP(x[b], x[c]); if (oi) SWAP(oi[b], oi[c]); ++b; --c; } r = MIN(a,b-a); vecswap(0,b-r,r,x,oi); r = MIN(d-c,n-d-1); vecswap(b,n-r,r,x,oi); r = b-a; ssort1(x,r,depth,oi); if (x[r][depth] != 0) { if (oi) ssort1 (x + r, a + n - d - 1, depth + 1, oi + r); else ssort1 (x + r, a + n - d - 1, depth + 1, NULL); } r = d - c; if (oi) ssort1(x+n-r,r,depth,oi+n-r); else ssort1(x+n-r,r,depth,NULL); }
void ssort1main(char *x[], int n,int* index) { ssort1(x, n, 0,index); }
void multikey1(string x[], int n) { ssort1(x, n, 0); }