int test(int testC) { char testString[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; srand(time(NULL)); char searchedChar; int charIndex; int arrowPos; printf("Find rightmost occurence of characters in this text:\n %s\n\n", testString); int i; for (i = 0; i < testC; i++) { searchedChar = randInt('a', 'z'); printf("? %c\n", searchedChar); charIndex = searchChar(searchedChar, testString); if (charIndex != -1) { arrowPos = printfExcerpt(testString, OUTBUFFERWIDTH, charIndex); printf("\n"); for (; arrowPos > 0; arrowPos--) { printf(" "); } printf("^\n"); } else { printf("Not found\n"); } } return 0; }
void mvZeroNextToTarget(searchData *d,int placeTarget) { int placeZero = searchChar(&d->b, '0'); int xd = (placeTarget % d->b.w) - (placeZero % d->b.w); int yd = (int)(placeTarget / d->b.w) - (int)(placeZero / d->b.w); char ways[4]; int len; while(abs(xd) + abs(yd) > 0){ len = nextMove(d->b, ways, d->preMove); char next; if(len == 1) next = ways[0]; else next = chooseNext(ways, xd, yd, searchChar); move(&d->b, next); d->preMove = next; d->r.push_back(next); repairXdXy(next, &xd, &yd); #ifdef DEBUG printf("ways=%s\n", ways); printBoard(d->b); getchar(); #endif } }
/// Read Fasta file void load_fasta(string filename, vector<char> &reference_array,vector<char> &AlphapetChar) { string meta, line; long length = 0; char c; // Everything starts at zero. ///startpos.push_back(0); ifstream data(filename.c_str()); if(!data.is_open()) { cerr << "unable to open " << filename << endl; exit(1); } while(!data.eof()) { getline(data, line); // Load one line at a time. if(line.length() == 0) continue; long start = 0, end = line.length() - 1; // Meta tag line and start of a new sequence. if(line[0] == '>') { // Save previous sequence and meta data. if(length > 0) { ///descr.push_back(meta); ///reference_array.push_back('>'); //character used to separate strings ///if (!searchChar('>',AlphapetChar)) ///{ /// AlphapetChar.push_back('>'); ///} //S += '`'; // ` character used to separate strings ///startpos.push_back(reference_array.size()); //lengths.push_back(length+1); } // Reset parser state. start = 1; meta = ""; length = 0; } trim(line, start, end); // Collect meta data. if(line[0] == '>') { for(long i = start; i <= end; i++) { if(line[i] == ' ') break; meta += line[i]; } } else { // Collect sequence data. length += end - start + 1; for(long i = start; i <= end; i++) { c= std::tolower(line[i]); reference_array.push_back(c); if (!searchChar(c,AlphapetChar)) { AlphapetChar.push_back(c); } //S += std::tolower(line[i]); } } } }
int main(int argc, char **argv) { int testC; char searchedChar = EOF; //Boggus initialization char containingString[MAXSTRINGSIZE]; if (argc == 1) { scanf("%c", &searchedChar); scanf("%s", containingString); printf("%d", searchChar(searchedChar, containingString)); } else { if (argc <= 3 && !strcmp(argv[1], "--test")) { testC = 6; if (argc == 3) { testC = stringToInt(argv[2]); testC = testC < 1 ? 0 : testC; } test(testC); } else { int result; int i; BOOL charFirst = FALSE, stringFirst = FALSE; for (i = 1; i < argc; i += 2) { if ((!stringFirst && charFirst) || (strlen(argv[i]) == 1 && strlen(argv[i + 1]) > 1)) { charFirst = TRUE; result = searchChar(argv[i][0], argv[i + 1]); } else if ((stringFirst && !charFirst)|| (strlen(argv[i + 1]) == 1 && strlen(argv[i]) > 1)) { stringFirst = TRUE; result = searchChar(argv[i + 1][0], argv[i]); } if (stringFirst || charFirst) { if (result != -1) { printf("%d", result); } printf("%s%s", result == -1 ? "Not found" : "", (i < argc - 2 ? "\n" : "")); } } } } return 0; }
void mvRoot(searchData *d, int correctPlace) { int placeTarget = searchChar(&d->b, place2char(correctPlace)); if(place == -1) return; mvZeroNextToTarget(d, placeTarget); #ifdef DEBUG printBoard(d->b); getchar(); #endif mvTargetToAnswer(d, placeTarget ,correctPlace); }
Datum spg_text_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); text *inText = DatumGetTextPP(in->datum); char *inStr = VARDATA_ANY(inText); int inSize = VARSIZE_ANY_EXHDR(inText); uint8 nodeChar = '\0'; int i = 0; int commonLen = 0; /* Check for prefix match, set nodeChar to first byte after prefix */ if (in->hasPrefix) { text *prefixText = DatumGetTextPP(in->prefixDatum); char *prefixStr = VARDATA_ANY(prefixText); int prefixSize = VARSIZE_ANY_EXHDR(prefixText); commonLen = commonPrefix(inStr + in->level, prefixStr, inSize - in->level, prefixSize); if (commonLen == prefixSize) { if (inSize - in->level > commonLen) nodeChar = *(uint8 *) (inStr + in->level + commonLen); else nodeChar = '\0'; } else { /* Must split tuple because incoming value doesn't match prefix */ out->resultType = spgSplitTuple; if (commonLen == 0) { out->result.splitTuple.prefixHasPrefix = false; } else { out->result.splitTuple.prefixHasPrefix = true; out->result.splitTuple.prefixPrefixDatum = formTextDatum(prefixStr, commonLen); } out->result.splitTuple.nodeLabel = UInt8GetDatum(*(prefixStr + commonLen)); if (prefixSize - commonLen == 1) { out->result.splitTuple.postfixHasPrefix = false; } else { out->result.splitTuple.postfixHasPrefix = true; out->result.splitTuple.postfixPrefixDatum = formTextDatum(prefixStr + commonLen + 1, prefixSize - commonLen - 1); } PG_RETURN_VOID(); } } else if (inSize > in->level) { nodeChar = *(uint8 *) (inStr + in->level); } else { nodeChar = '\0'; } /* Look up nodeChar in the node label array */ if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i)) { /* * Descend to existing node. (If in->allTheSame, the core code will * ignore our nodeN specification here, but that's OK. We still * have to provide the correct levelAdd and restDatum values, and * those are the same regardless of which node gets chosen by core.) */ out->resultType = spgMatchNode; out->result.matchNode.nodeN = i; out->result.matchNode.levelAdd = commonLen + 1; if (inSize - in->level - commonLen - 1 > 0) out->result.matchNode.restDatum = formTextDatum(inStr + in->level + commonLen + 1, inSize - in->level - commonLen - 1); else out->result.matchNode.restDatum = formTextDatum(NULL, 0); } else if (in->allTheSame) { /* * Can't use AddNode action, so split the tuple. The upper tuple * has the same prefix as before and uses an empty node label for * the lower tuple. The lower tuple has no prefix and the same * node labels as the original tuple. */ out->resultType = spgSplitTuple; out->result.splitTuple.prefixHasPrefix = in->hasPrefix; out->result.splitTuple.prefixPrefixDatum = in->prefixDatum; out->result.splitTuple.nodeLabel = UInt8GetDatum('\0'); out->result.splitTuple.postfixHasPrefix = false; } else { /* Add a node for the not-previously-seen nodeChar value */ out->resultType = spgAddNode; out->result.addNode.nodeLabel = UInt8GetDatum(nodeChar); out->result.addNode.nodeN = i; } PG_RETURN_VOID(); }
Datum spg_text_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); text *inText = DatumGetTextPP(in->datum); char *inStr = VARDATA_ANY(inText); int inSize = VARSIZE_ANY_EXHDR(inText); char *prefixStr = NULL; int prefixSize = 0; int commonLen = 0; int16 nodeChar = 0; int i = 0; /* Check for prefix match, set nodeChar to first byte after prefix */ if (in->hasPrefix) { text *prefixText = DatumGetTextPP(in->prefixDatum); prefixStr = VARDATA_ANY(prefixText); prefixSize = VARSIZE_ANY_EXHDR(prefixText); commonLen = commonPrefix(inStr + in->level, prefixStr, inSize - in->level, prefixSize); if (commonLen == prefixSize) { if (inSize - in->level > commonLen) nodeChar = *(unsigned char *) (inStr + in->level + commonLen); else nodeChar = -1; } else { /* Must split tuple because incoming value doesn't match prefix */ out->resultType = spgSplitTuple; if (commonLen == 0) { out->result.splitTuple.prefixHasPrefix = false; } else { out->result.splitTuple.prefixHasPrefix = true; out->result.splitTuple.prefixPrefixDatum = formTextDatum(prefixStr, commonLen); } out->result.splitTuple.nodeLabel = Int16GetDatum(*(unsigned char *) (prefixStr + commonLen)); if (prefixSize - commonLen == 1) { out->result.splitTuple.postfixHasPrefix = false; } else { out->result.splitTuple.postfixHasPrefix = true; out->result.splitTuple.postfixPrefixDatum = formTextDatum(prefixStr + commonLen + 1, prefixSize - commonLen - 1); } PG_RETURN_VOID(); } } else if (inSize > in->level) { nodeChar = *(unsigned char *) (inStr + in->level); } else { nodeChar = -1; } /* Look up nodeChar in the node label array */ if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i)) { /* * Descend to existing node. (If in->allTheSame, the core code will * ignore our nodeN specification here, but that's OK. We still have * to provide the correct levelAdd and restDatum values, and those are * the same regardless of which node gets chosen by core.) */ int levelAdd; out->resultType = spgMatchNode; out->result.matchNode.nodeN = i; levelAdd = commonLen; if (nodeChar >= 0) levelAdd++; out->result.matchNode.levelAdd = levelAdd; if (inSize - in->level - levelAdd > 0) out->result.matchNode.restDatum = formTextDatum(inStr + in->level + levelAdd, inSize - in->level - levelAdd); else out->result.matchNode.restDatum = formTextDatum(NULL, 0); } else if (in->allTheSame) { /* * Can't use AddNode action, so split the tuple. The upper tuple has * the same prefix as before and uses a dummy node label -2 for the * lower tuple. The lower tuple has no prefix and the same node * labels as the original tuple. * * Note: it might seem tempting to shorten the upper tuple's prefix, * if it has one, then use its last byte as label for the lower tuple. * But that doesn't win since we know the incoming value matches the * whole prefix: we'd just end up splitting the lower tuple again. */ out->resultType = spgSplitTuple; out->result.splitTuple.prefixHasPrefix = in->hasPrefix; out->result.splitTuple.prefixPrefixDatum = in->prefixDatum; out->result.splitTuple.nodeLabel = Int16GetDatum(-2); out->result.splitTuple.postfixHasPrefix = false; } else { /* Add a node for the not-previously-seen nodeChar value */ out->resultType = spgAddNode; out->result.addNode.nodeLabel = Int16GetDatum(nodeChar); out->result.addNode.nodeN = i; } PG_RETURN_VOID(); }