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; }
GdbStatus gdbAddDataEntry(GDatabase *db, GdbHashTable *table, unsigned short key, const void *data, long size) { if (db == NULL || db->fp == NULL || key == 0 || table == NULL || data == NULL || size == 0) { return GDB_ERROR; } htAdd(table, key, data, size, GDB_HT_RAW); return GDB_SUCCESS; }
int main(int argc,char **argv) { HASHTABLE ht; char * s; int i; ht = htMake(-1); for (i = 0; i < argc; i++) htAdd(ht,argv[i],(void*)argv[i]); printf("\nContents of hash table: %d items, %d slots\n", htItems( ht ), htSize( ht ) ); htWalk(ht,1,pfunc); printf("\n===============\n"); while (1) { printf("Conflicts in table: %d\n",htConflicts(ht)); htWalk(ht,0,pfunc); printf("\n"); printf("Enter key to remove: (\"quit\" to stop): "); s = get_string(); if (!strcmp(s,"quit")) break; printf("Removing element with key \"%s\"... = %s\n",s, nullstr((char*)htRemove(ht,s))); } printf("Closing hash table\n" ); htCloseWithFunction( ht, close_func ); return 0; }