int maxPoints(pt* points, int pointsS) { ht = NULL; if(pointsS <= 1 || !points) return pointsS; /* first check any point is duplicated, and for how many times */ int dup[pointsS]; memset(dup, 0, sizeof(int)*pointsS); for(int i = 0; i <pointsS-1; i++) for(int j = i+1; j< pointsS && dup[i]!= -1; j++) if(isSame(points+i, points+j)) { dup[i]++; dup[j] = -1; } /* the algorithm is looping through each point, check the line it's forming with each point after it, and update a counter for each line in the hast table then finally find the line with the largest count */ ln l; map* s; for(int i = 0; i<pointsS-1; i++) { for(int j = i+1; j<pointsS && dup[i] !=-1; j++) { getLine(points+i, points + j, &l); s = htFind(l); if(s) { /* an existing line, check if created by this point only */ if(i == s->idxCrt) (s->numPt)++; } else { /* insert the new line */ if(isSame(points+i, points+j)) htAdd(l, 2, i); /* special case: same point */ else htAdd(l, 2+dup[i], i); /* 2+dup points on a line */ } } } /* htPrint(); */ /* find the largest count in the hasttable */ int ret = INT_MIN; for(s=ht; s; s=(map*)(s->hh.next)) ret = (ret>s->numPt)? ret: s->numPt; htCleanup(); return ret; }
void SearchForDefined() { char *ptr, *id, *sptr; int c; SDef tdef, *p; ptr = inptr; while(1) { if (PeekCh() == 0) // Stop at end of current input break; SkipSpaces(); sptr = inptr; id = GetIdentifier(); if (id) { if (strcmp(id, "defined") == 0) { c = NextNonSpace(0); if (c != '(') { err(20); break; } id = GetIdentifier(); if (id == NULL) { err(21); break; } c = NextNonSpace(0); if (c != ')') err(22); tdef.name = id; p = (SDef *)htFind(&HashInfo, &tdef); SubMacro((char *)(p ? "1" : "0"), inptr-sptr); } } else NextCh(); } inptr = ptr; }
void SearchAndSub() { static int InComment = 0; int c, InComment2 = 0; int QuoteToggle = 0; char *id, *ptr, *optr; SDef *p, tdef; // Check if we hit the end of the current input line we do this because // NextCh would read another line optr = inptr; while (1) { if ((c = PeekCh()) == 0) break; if (c == '\n') { c = NextCh(); QuoteToggle = 0; // Quotes cannot cross newlines InComment2 = 0; continue; } if (c == '/' && *(inptr+1) == '/') { InComment2 = 1; inptr += 2; continue; } if (c == '/' && *(inptr+1) == '*') { InComment = 1; inptr += 2; continue; } if (c == '*' && *(inptr+1) == '/') { InComment = 0; inptr += 2; continue; } if (InComment || InComment2) { c = NextCh(); continue; } if (c == '"') { // Toggle quotation mode c = NextCh(); QuoteToggle = !QuoteToggle; continue; } if (QuoteToggle) { // Just keep getting characters as long as c = NextCh(); // we're inside quotes continue; } ptr = inptr; // record the position of the input pointer id = GetIdentifier(); // try and get an identifier if (id) { tdef.name = _strdup(id); if (tdef.name == NULL) err(5); // Search and see if the identifier corresponds to a macro p = (SDef *)htFind(&HashInfo, &tdef); if (p != (SDef *)NULL) { if (fdbg) fprintf(fdbg, "macro %s\r\n", p->name); // If this isn't a macro with parameters, then just copy // the body directly to the input. Overwrite the identifier // string if (p->nArgs >= 0) { if (fdbg) fprintf(fdbg, "bef:%s", inbuf); SubParmMacro(p); if (fdbg) fprintf(fdbg, "aft:%s", inbuf); } else { SubMacro(p->body, strlen(p->name)); } } free(tdef.name); continue; // the identifier wasn't a macro so just let it be } // failed to get identifier, so just continue with the next character c = NextCh(); } inptr = optr; }