Arraylist getPerm(int rank) { int i, j, k; Intstack y, z; if (!permInit) initPerm() ; if (perm[rank] != NULL) return perm[rank]; if (perm[rank-1] == NULL) getPerm(rank-1) ; perm[rank] = newArraylist(rank * perm[rank-1]->size) ; for (i = 0; i < perm[rank-1]->size; i++) { y = perm[rank-1]->it[i]; for (j = 0; j < rank; j++) { z = newIntstack(rank, NULL); for (k = 0; k < j; k++) z->it[k] = y->it[k]; z->it[j] = rank - 1; for (k = j; k < y->size; k++) z->it[k+1] = y->it[k]; z->size = rank; putItem(z, perm[rank]) ; } } return perm[rank]; }
void decrypt(FILE *in, FILE *out, unsigned char* init_vector) { unsigned char* text = calloc(1, sizeof(char)); //holder for bytes to decrypt unsigned char* bitarr = malloc(8 * sizeof(char)); //bit manipulation array unsigned char* next_vector = calloc(8, sizeof(char)); //vector to be used on next decryption //read file in binary while((fread(text, 1, 1, in)) > 0) { int i; //grab bits and shift, store in array bitarr[0] = (*text & 0x80) >> 7; bitarr[1] = (*text & 0x40) >> 6; bitarr[2] = (*text & 0x20) >> 5; bitarr[3] = (*text & 0x10) >> 4; bitarr[4] = (*text & 0x08) >> 3; bitarr[5] = (*text & 0x04) >> 2; bitarr[6] = (*text & 0x02) >> 1; bitarr[7] = (*text & 0x01); //write cipher as next vector for(i = 0; i < 8; i++) { next_vector[i] = bitarr[i]; } //function with key2 //swap output of function with key 2 //function with key1 bitarr = finalPerm(functionF(sw(functionF(initPerm(bitarr), key2)), key1)); bzero(text, 1); //Cipher block chaining for(i = 0; i < 8; i++) { bitarr[i] = bitarr[i] ^ init_vector[i]; init_vector[i] = next_vector[i]; } //convert bits to char *text = *text + (bitarr[0] << 7); *text = *text + (bitarr[1] << 6); *text = *text + (bitarr[2] << 5); *text = *text + (bitarr[3] << 4); *text = *text + (bitarr[4] << 3); *text = *text + (bitarr[5] << 2); *text = *text + (bitarr[6] << 1); *text = *text + (bitarr[7]); //write file in binary fwrite(text, 1, 1, out); bzero(text, 1); } free(bitarr); free(text); }
static void getAlgoString(char *str, int seq) { uint8_t algoList[HASH_FUNC_COUNT]; char *sptr; initPerm(algoList, HASH_FUNC_COUNT); for (int k = 0; k < seq; k++) { nextPerm(algoList, HASH_FUNC_COUNT); } sptr = str; for (int j = 0; j < HASH_FUNC_COUNT; j++) { if (algoList[j] >= 10) sprintf(sptr, "%c", 'A' + (algoList[j] - 10)); else sprintf(sptr, "%u", (uint32_t) algoList[j]); sptr++; } *sptr = '\0'; }
RoundWorld::RoundWorld() { // std::cout << noiseSample(glm::ivec4(0, 0, 0, 0)) << "\n"; // Ensure that the perm matrix is initialized initPerm(); // Loop over the possible things in the world std::cout << "Starting world generation" << std::endl; int32_t x, y, z, w; for (x=0; x<ROUNDWORLD_DIM.x; x++) { for (y=0; y<ROUNDWORLD_DIM.y; y++) { for (z=0; z<ROUNDWORLD_DIM.z; z++) { for (w=0; w<ROUNDWORLD_DIM.w; w++) { glm::dvec4 loc(x, y, z, w); double baseSample = baseNoiseSample(loc); double cloudSample = cloudNoiseSample(loc); double sample = noiseSample(loc, baseSample); glm::dvec4 fromCenter = loc - (glm::dvec4(ROUNDWORLD_DIM) / 2.0); double rad = length(fromCenter); if (sample < 0.6) { hypercubes[x][y][z][w] = HCT_STONE; } else if (sample < 0.7) { hypercubes[x][y][z][w] = HCT_GRASS; } else if (rad > 13 && rad < 15 && cloudSample > 0.8) { hypercubes[x][y][z][w] = HCT_CLOUD; } else { hypercubes[x][y][z][w] = HCT_AIR; } } } } std::cout << x << "/" << ROUNDWORLD_DIM.x << "\n"; } std::cout << "Done world generation" << std::endl; std::cout << "Growing grass" << std::endl; for (x=0; x<ROUNDWORLD_DIM.x; x++) { for (y=0; y<ROUNDWORLD_DIM.y; y++) { for (z=0; z<ROUNDWORLD_DIM.z; z++) { for (w=0; w<ROUNDWORLD_DIM.w; w++) { if (worldSample(x, y+1, z, w) == HCT_AIR && hypercubes[x][y][z][w] == HCT_STONE) { /* if (y >= ROUNDWORLD_DIM.y / 2) { hypercubes[x][y][z][w] = HCT_GRASS; } else { hypercubes[x][y][z][w] = HCT_SAND; } */ } } } } std::cout << x << "/" << ROUNDWORLD_DIM.x << "\n"; } std::cout << "Done growing grass" << std::endl; std::cout << "Filling ponds" << std::endl; srand(124); for (x=0; x<ROUNDWORLD_DIM.x; x++) { for (y=0; y<ROUNDWORLD_DIM.y; y++) { // Only half filled with water for (z=0; z<ROUNDWORLD_DIM.z; z++) { for (w=0; w<ROUNDWORLD_DIM.w; w++) { glm::dvec4 fromCenter = glm::dvec4(x, y, z, w) - (glm::dvec4(ROUNDWORLD_DIM) / 2.0); double rad = length(fromCenter); HyperCubeTypes h = hypercubes[x][y][z][w]; if (rad < 9 && h == HCT_AIR) { hypercubes[x][y][z][w] = HCT_WATER; } else if (rad < 8 && (h == HCT_STONE || h == HCT_GRASS)) { if (rand() < RAND_MAX / 2) { hypercubes[x][y][z][w] = HCT_SAND; } else { hypercubes[x][y][z][w] = HCT_STONE; } } } } } std::cout << x << "/" << ROUNDWORLD_DIM.x << "\n"; } /* for (x=0; x<ROUNDWORLD_DIM.x; x++) { for (y=0; y<ROUNDWORLD_DIM.y / 2; y++) { // Only half filled with water for (z=0; z<ROUNDWORLD_DIM.z; z++) { for (w=0; w<ROUNDWORLD_DIM.w; w++) { if (hypercubes[x][y][z][w] == HCT_AIR) { hypercubes[x][y][z][w] = HCT_WATER; } } } } std::cout << x << "/" << ROUNDWORLD_DIM.x << "\n"; } */ std::cout << "Done filling ponds" << std::endl; std::cout << "Starting mesh generation" << std::endl; for (x=0; x<ROUNDWORLD_DIM.x; x++) { for (y=0; y<ROUNDWORLD_DIM.y; y++) { for (z=0; z<ROUNDWORLD_DIM.z; z++) { for (w=0; w<ROUNDWORLD_DIM.w; w++) { HyperCubeTypes hct = hypercubes[x][y][z][w]; #define SURROUNDED (worldSample(x-1, y, z, w) >= HCT_SOLID_START && \ worldSample(x+1, y, z, w) >= HCT_SOLID_START && \ worldSample(x, y-1, z, w) >= HCT_SOLID_START && \ worldSample(x, y+1, z, w) >= HCT_SOLID_START && \ worldSample(x, y, z-1, w) >= HCT_SOLID_START && \ worldSample(x, y, z+1, w) >= HCT_SOLID_START && \ worldSample(x, y, z, w-1) >= HCT_SOLID_START && \ worldSample(x, y, z, w+1) >= HCT_SOLID_START) switch (hct) { case HCT_AIR: break; case HCT_GRASS: if (!SURROUNDED) { grassLocs.push_back(glm::vec4(x, y, z, w)); } break; case HCT_SAND: if (!SURROUNDED) { sandLocs.push_back(glm::vec4(x, y, z, w)); } break; case HCT_STONE: if (!SURROUNDED) { stoneLocs.push_back(glm::vec4(x, y, z, w)); } break; case HCT_WATER: if (!SURROUNDED) { waterLocs.push_back(glm::vec4(x, y, z, w)); } break; case HCT_CLOUD: if (!SURROUNDED) { cloudLocs.push_back(glm::vec4(x, y, z, w)); } break; default: std::cerr << "INVALID BLOCK TYPE " << hct << "\n"; exit(-1); } } } } std::cout << x << "/" << ROUNDWORLD_DIM.x << "\n"; } std::cout << "Done mesh generation" << std::endl; }
//void NNI(tree *T, double **avgDistArray, int *count) void NNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies) { edge *e, *centerEdge; edge **edgeArray; int *location; int *p,*q; int i,j; int possibleSwaps; double *weights; p = initPerm(T->size+1); q = initPerm(T->size+1); edgeArray = (edge **) malloc((T->size+1)*sizeof(double)); weights = (double *) malloc((T->size+1)*sizeof(double)); location = (int *) malloc((T->size+1)*sizeof(int)); double epsilon = 0.0; for (i=0; i<numSpecies; i++) for (j=0; j<numSpecies; j++) epsilon += D[i][j]; epsilon = (epsilon / (numSpecies * numSpecies)) * EPSILON; for (i=0;i<T->size+1;i++) { weights[i] = 0.0; location[i] = NONE; } e = findBottomLeft(T->root->leftEdge); /* *count = 0; */ while (NULL != e) { edgeArray[e->head->index+1] = e; location[e->head->index+1] = NNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1); e = depthFirstTraverse(T,e); } possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0); permInverse(p,q,T->size+1); /*we put the negative values of weights into a heap, indexed by p with the minimum value pointed to by p[1]*/ /*p[i] is index (in edgeArray) of edge with i-th position in the heap, q[j] is the position of edge j in the heap */ while (weights[p[1]] + epsilon < 0) { centerEdge = edgeArray[p[1]]; (*count)++; T->weight = T->weight + weights[p[1]]; NNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray); location[p[1]] = NONE; weights[p[1]] = 0.0; /*after the NNI, this edge is in optimal configuration*/ popHeap(p,q,weights,possibleSwaps--,1); /*but we must retest the other four edges*/ e = centerEdge->head->leftEdge; NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps); e = centerEdge->head->rightEdge; NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps); e = siblingEdge(centerEdge); NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps); e = centerEdge->tail->parentEdge; NNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps); } free(p); free(q); free(location); free(edgeArray); }
void bNNI(meTree *T, double **avgDistArray, int *count) { meEdge *e, *centerEdge; meEdge **edgeArray; int *p, *location, *q; int i; int possibleSwaps; double *weights; p = initPerm(T->size+1); q = initPerm(T->size+1); edgeArray = (meEdge **) malloc((T->size+1)*sizeof(double)); weights = (double *) malloc((T->size+1)*sizeof(double)); location = (int *) malloc((T->size+1)*sizeof(int)); for (i=0;i<T->size+1;i++) { weights[i] = 0.0; location[i] = NONE; } if (verbose) { assignBalWeights(T,avgDistArray); weighTree(T); } e = findBottomLeft(T->root->leftEdge); while (NULL != e) { edgeArray[e->head->index+1] = e; location[e->head->index+1] = bNNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1); e = depthFirstTraverse(T,e); } possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0); permInverse(p,q,T->size+1); /*we put the negative values of weights into a heap, indexed by p with the minimum value pointed to by p[1]*/ /*p[i] is index (in edgeArray) of edge with i-th position in the heap, q[j] is the position of edge j in the heap */ /*NOTE: the loop below should test that weights[p[1]] < 0, but if compiled with optimization it is possible that weights[p[1]] ends up negative and very small, so that changes to the heap become cyclic and the loop becomes infinite. To avoid this behavior, stop the loop short of 0.0. This is a workaround until the roundoff sensitivity is removed algorithmically */ while (weights[p[1]] < -1e-8) { centerEdge = edgeArray[p[1]]; (*count)++; if (verbose) { T->weight = T->weight + weights[p[1]]; printf("New tree weight is %lf.\n",T->weight); } bNNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray); location[p[1]] = NONE; weights[p[1]] = 0.0; /*after the bNNI, this edge is in optimal configuration*/ popHeap(p,q,weights,possibleSwaps--,1); /*but we must retest the other edges of T*/ /*CHANGE 2/28/2003 expanding retesting to _all_ edges of T*/ e = depthFirstTraverse(T,NULL); while (NULL != e) { bNNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps); e = depthFirstTraverse(T,e); } } free(p); free(q); free(location); free(edgeArray); assignBalWeights(T,avgDistArray); }
//void bNNI(tree *T, double **avgDistArray, int *count) void bNNI(tree *T, double **avgDistArray, int *count, double **D, int numSpecies) { edge *e;//, *centerEdge deleted by EP, 2013-09-26, see also below edge **edgeArray; int *p, *location, *q; int i,j; int possibleSwaps; double *weights; p = initPerm(T->size+1); q = initPerm(T->size+1); edgeArray = (edge **) malloc((T->size+1)*sizeof(double)); weights = (double *) malloc((T->size+1)*sizeof(double)); location = (int *) malloc((T->size+1)*sizeof(int)); double epsilon = 0.0; for (i=0; i<numSpecies; i++) for (j=0; j<numSpecies; j++) epsilon += D[i][j]; epsilon = (epsilon / (numSpecies * numSpecies)) * EPSILON; for (i=0;i<T->size+1;i++) { weights[i] = 0.0; location[i] = NONE; } /* if (verbose) { assignBMEWeights(T,avgDistArray); weighTree(T); }*/ e = findBottomLeft(T->root->leftEdge); while (NULL != e) { edgeArray[e->head->index+1] = e; location[e->head->index+1] = bNNIEdgeTest(e,T,avgDistArray,weights + e->head->index + 1); e = depthFirstTraverse(T,e); } possibleSwaps = makeThreshHeap(p,q,weights,T->size+1,0.0); permInverse(p,q,T->size+1); /*we put the negative values of weights into a heap, indexed by p with the minimum value pointed to by p[1]*/ /*p[i] is index (in edgeArray) of edge with i-th position in the heap, q[j] is the position of edge j in the heap */ while (weights[p[1]] + epsilon < 0) { /* centerEdge = edgeArray[p[1]]; apparently unused later, deleted by EP, 2013-09-26 */ (*count)++; /* if (verbose) { T->weight = T->weight + weights[p[1]]; printf("New tree weight is %lf.\n",T->weight); }*/ bNNItopSwitch(T,edgeArray[p[1]],location[p[1]],avgDistArray); location[p[1]] = NONE; weights[p[1]] = 0.0; /*after the bNNI, this edge is in optimal configuration*/ popHeap(p,q,weights,possibleSwaps--,1); /*but we must retest the other edges of T*/ /*CHANGE 2/28/2003 expanding retesting to _all_ edges of T*/ e = depthFirstTraverse(T,NULL); while (NULL != e) { bNNIRetestEdge(p,q,e,T,avgDistArray,weights,location,&possibleSwaps); e = depthFirstTraverse(T,e); } } free(p); free(q); free(location); free(edgeArray); free(weights); assignBMEWeights(T,avgDistArray); }
void bNNI (tree *T, double **avgDistArray, int *count, FILE *statfile) { edge *e; edge **edgeArray; int *p, *location, *q; int i; int possibleSwaps; double *weights; p = initPerm (T->size+1); q = initPerm (T->size+1); edgeArray = (edge **) mCalloc ((T->size+1), sizeof (edge *)); weights = (double *) mCalloc ((T->size+1), sizeof (double)); location = (int *) mCalloc ((T->size+1), sizeof (int)); for (i=0; i<T->size+1; i++) { weights[i] = 0.0; location[i] = NONE; } assignBMEWeights (T, avgDistArray); weighTree (T); if (!isBoostrap) { if (statfile) fprintf (statfile, "\tBefore NNI: tree length is %f.\n", T->weight); if (verbose > 2) Debug ( (char*)"Before NNI: tree length is %f.", T->weight); else if (verbose > 1) Message ( (char*)". Before NNI: tree length is %f.", T->weight); } e = findBottomLeft (T->root->leftEdge); while (NULL != e) { edgeArray[e->head->index+1] = e; location[e->head->index+1] = bNNIEdgeTest (e, T, avgDistArray, weights + e->head->index + 1); e = depthFirstTraverse (T,e); } possibleSwaps = makeThreshHeap (p, q, weights, T->size+1,0.0); permInverse (p, q, T->size+1); /* We put the negative values of weights into a heap, indexed by p * with the minimum value pointed to by p[1] * p[i] is index (in edgeArray) of edge with i-th position in the * heap, q[j] is the position of edge j in the heap */ while (weights[p[1]] < -DBL_EPSILON) { (*count)++; T->weight = T->weight + weights[p[1]]; if (!isBoostrap) { if (statfile) fprintf (statfile, "\tNNI %5d: new tree length is %f.\n", *count, T->weight); if (verbose > 2) Debug ( (char*)"NNI %5d: new tree length is %f.", *count, T->weight); else if (verbose > 1) Message ( (char*)". NNI %5d: new tree length is %f.", *count, T->weight); } bNNItopSwitch (edgeArray[p[1]], location[p[1]], avgDistArray); location[p[1]] = NONE; weights[p[1]] = 0.0; //after the bNNI, this edge is in optimal configuration popHeap (p, q, weights, possibleSwaps--, 1); /* but we must retest the other edges of T */ /* CHANGE 2/28/2003 expanding retesting to _all_ edges of T */ e = depthFirstTraverse (T, NULL); while (NULL != e) { bNNIRetestEdge (p, q, e, T, avgDistArray, weights, location, &possibleSwaps); e = depthFirstTraverse (T, e); } } free (p); free (q); free (location); free (edgeArray); free (weights); assignBMEWeights (T, avgDistArray); return; }
void Simplex::setSeed(int s) { m_seed = s; initPerm(); }