/* if 0 is present in arr[] then returns the index of FIRST occurrence of 0 in arr[low..high], otherwise returns -1 */ int firstZero(int arr[], int low, int high) { if (high >= low) { // Check if mid element is first 0 int mid = low + (high - low)/2; if (( mid == 0 || arr[mid-1] == 1) && arr[mid] == 0) return mid; if (arr[mid] == 1) // If mid element is not 0 return firstZero(arr, (mid + 1), high); else // If mid element is 0, but not first 0 return firstZero(arr, low, (mid -1)); } return -1; }
Transformation normalize(LifeList *cells) { Transformation T; Transformation normalizeT; makeWorkSpace(cells->ncells); scratch1[0].position=INT_MAX; for (T.flipxF=1; T.flipxF>=0; T.flipxF--) { for (T.flipyF=1; T.flipyF>=0; T.flipyF--) { for (T.transposeF=1; T.transposeF>=0; T.transposeF--) { copyList(cells->cellList, cells->ncells, scratch2, 0); if (T.flipxF) flipx(scratch2, cells->ncells); if (T.flipyF) flipy(scratch2, cells->ncells); if (T.transposeF) transpose(scratch2, cells->ncells); T.translateBy=firstZero(scratch2, cells->ncells); if (compare(scratch2, scratch1, cells->ncells) <= 0) { copyList(scratch2, cells->ncells, scratch1, 0); normalizeT=T; } } } } copyList(scratch1, cells->ncells, cells->cellList, 0); return normalizeT; }
// A wrapper over recursive function firstZero() int countOnes(int arr[], int n) { // Find index of first zero in given array int first = firstZero(arr, 0, n-1); // If 0 is not present at all, return 0 if (first == -1) return 0; return (n - first); }
int simpleSS(LifeList *cells, LifeList *working) { int i, transl; /* checks if pattern in cells is a simple (period 4) spaceship */ (void) firstZero(cells->cellList,cells->ncells); copyLifeList(cells, working); for (i=0; i<4; i++) generate(working); transl = firstZero(working->cellList, working->ncells); if (cells->ncells == working->ncells && !compare(working->cellList, cells->cellList, cells->ncells)) { return transl; } else return packtrans(-255, -255); }
OscillatorDesc simpleOscillation(LifeList *cells, LifeList *working, int testUpTo) { int i, initTransl, transl; OscillatorDesc osc; /* Looks for first "simple" oscillation of a pattern up to testUpTo. This includes a translation, but no other coordinate transformation. Returns the period and translation (returns -1 if no oscillation found) */ initTransl = firstZero(cells->cellList,cells->ncells); copyLifeList(cells, working); for (i=1; i<=testUpTo; i++) { generate(working); if (cells->ncells == working->ncells) { transl = firstZero(working->cellList, working->ncells); if (!compare(working->cellList, cells->cellList, cells->ncells)) break; copyList(working->cellList, working->ncells, working->cellList, transl); } } copyList(cells->cellList, cells->ncells, cells->cellList, initTransl); osc.testedUpTo = testUpTo; if (i> testUpTo) osc.period = -1; else osc.period = i; osc.T.flipxF = osc.T.flipyF = osc.T.transposeF = 0; osc.T.translateBy = transl; return osc; }
std::vector<unsigned long> ftAlgorithm::utf8toUnicode(const char *input) { std::vector<unsigned long> res; int len = std::strlen(input); int i = 0; unsigned char ucTmp; unsigned long ulTmp; while (i < len) { ucTmp = (unsigned char)input[i]; int t = firstZero(ucTmp); switch (t) { /* case 1: FT_OUT("error\n"); break; */ case 0: ulTmp = (unsigned long)ucTmp; res.push_back(ulTmp); i += 1; break; case 2: case 3: case 4: case 5: case 6: case 7: ulTmp = (unsigned long)(ucTmp & rOne[7 - t]); for (int j = 1; j < t; j++) { ulTmp <<= 6; /* if (i + j >= len) { FT_OUT("error\n"); break; } */ ucTmp = (unsigned char)input[i + j]; ulTmp |= ucTmp & rOne[6]; } res.push_back(ulTmp); i += t; break; } } return res; }