int HashSymbol(char *string, int scope, int section) { uint v, n; uint len; len = strlen(string); v = 41; v += HASHFUNC(scope); v += HASHFUNC(section); v += HASHFUNC(len); for (n=0; n<len; n++) v += HASHFUNC(string[n]); return v % SYMBOL_HASH_SIZE; }
KSDLL_API PSTR RecognizeImage(PSTR img) { int N; HASH img_hash = HASHFUNC(img, N); /*HASH img_hash; HASHFUNC(img, img_hash);*/ int best = 0; double best_score = 1000; for (size_t i = 0; i < hashes.size(); i++) { double score = HASHCMP(img_hash, N, hashes[i], N); //double score = HASHCMP(img_hash, hashes[i]); if (score < best_score) { best = i; best_score = score; } } return names[best]; }
KSDLL_API INT InitializeDatabase(LPSTR dir) { WIN32_FIND_DATAA fd; CHAR dirWithStar[MAX_PATH]; strcpy(dirWithStar, dir); strcat(dirWithStar, "\\*"); HANDLE hd = FindFirstFileA(dirWithStar, &fd); if (INVALID_HANDLE_VALUE == hd) { return -1; } do { // Check Extension int len = strlen(fd.cFileName); if (len < 5) continue; if (strcmp(fd.cFileName + len - 4, ".bmp") != 0) continue; if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { PSTR full_name = (PSTR)::CoTaskMemAlloc(MAX_PATH + 1); strcpy(full_name, dir); strcat(full_name, "\\"); strcat(full_name, fd.cFileName); int N; HASH hash = HASHFUNC(full_name, N); /*HASH hash; HASHFUNC(full_name, hash);*/ hashes.push_back(hash); names.push_back(full_name); } } while (FindNextFileA(hd, &fd) != 0); if (GetLastError() != ERROR_NO_MORE_FILES) { return -1; } FindClose(hd); return (INT)hashes.size(); }
SYMPTR GSTFindNameHashed( LPEXG lpexg, LPGST lpgst, SYMPTR psym, LPSSTR lpsstr, PFNCMP pfnCmp, SHFLAG fCase, HMOD *lphmod ) { LPSHT lpsht = &lpgst->shtName; if (lpsht->HashIndex == 0) { // No hash exists. Punt to the linear code. return(GSTFindNameLinear(lpexg, lpgst, psym, lpsstr, pfnCmp, fCase)); } ULONG ulId = 0; WORD iib = 0; SYMPTR psymRet = NULL; ULONG ib = 0; ULONG culp = 0; ULONG iulp = 0; BOOL fNext = psym != NULL; if ((lpsht->ccib == 0) ||(lpgst->lpalm == NULL)) return (SYMPTR)NULL; iib = HASHFUNC(lpsht->HashIndex, lpsstr, lpsht->ccib, &ulId); ib = lpsht->rgib [ iib ]; culp = lpsht->rgcib [ iib ]; // Loop through all the entries in this bucket for (iulp = 0; iulp < culp; iulp++) { LPULP lpulp = (LPULP) LpvFromAlmLfo (lpsht->lpalm, ib + (iulp * sizeof (ULP))); if (lpulp == NULL) { return NULL; } if (lpulp->ulId == ulId) { HMOD hmodT = hmodNull; // Checksums match, now check the symbols themselves SYMPTR psymT = (SYMPTR) LpvFromAlmLfo (lpgst->lpalm, lpulp->ib); if (psymT == NULL) { return NULL; } if (psymT->rectyp == S_PROCREF || psymT->rectyp == S_DATAREF) { psymT = PsymFromRef (lpexg, psymT, &hmodT); // catch case where there are no module symbols... [rm] if (psymT == NULL) { return NULL; } } if (fNext) { // We need to get back to the Current hsym before get can // get just one more. Soon as we are there we know we can // get the next one. if (psymT == psym) { fNext = FALSE; } continue; } if (GSTCmpName (psymT, lpsstr, pfnCmp, fCase)) { if (lphmod) { *lphmod = hmodT; } psymRet = psymT; break; } } } return psymRet; }
void HASH_LOOKUP1(MapType *t, Hmap *h, KEYTYPE key, GoOutput base, ...) { uintptr bucket, i; Bucket *b; KEYTYPE *k; byte *v, **valueptr; uint8 top; int8 keymaybe; valueptr = (byte**)&base; if(debug) { runtime·prints("runtime.mapaccess1_fastXXX: map="); runtime·printpointer(h); runtime·prints("; key="); t->key->alg->print(t->key->size, &key); runtime·prints("\n"); } if(h == nil || h->count == 0) { *valueptr = t->elem->zero; return; } if(raceenabled) runtime·racereadpc(h, runtime·getcallerpc(&t), HASH_LOOKUP1); if(docheck) check(t, h); if(h->B == 0) { // One-bucket table. Don't hash, just check each bucket entry. b = (Bucket*)h->buckets; if(FASTKEY(key)) { for(i = 0, k = (KEYTYPE*)b->data, v = (byte*)(k + BUCKETSIZE); i < BUCKETSIZE; i++, k++, v += h->valuesize) { if(b->tophash[i] == Empty) continue; if(QUICK_NE(key, *k)) continue; if(QUICK_EQ(key, *k) || SLOW_EQ(key, *k)) { *valueptr = v; return; } } } else { keymaybe = -1; for(i = 0, k = (KEYTYPE*)b->data, v = (byte*)(k + BUCKETSIZE); i < BUCKETSIZE; i++, k++, v += h->valuesize) { if(b->tophash[i] == Empty) continue; if(QUICK_NE(key, *k)) continue; if(QUICK_EQ(key, *k)) { *valueptr = v; return; } if(MAYBE_EQ(key, *k)) { if(keymaybe >= 0) { // Two same-length strings in this bucket. // use slow path. // TODO: keep track of more than just 1. We could // afford about 3 equals calls before it would be more // expensive than 1 hash + 1 equals. goto dohash; } keymaybe = i; } } if(keymaybe >= 0) { k = (KEYTYPE*)b->data + keymaybe; if(SLOW_EQ(key, *k)) { *valueptr = (byte*)((KEYTYPE*)b->data + BUCKETSIZE) + keymaybe * h->valuesize; return; } } } } else { dohash: bucket = h->hash0; HASHFUNC(&bucket, sizeof(KEYTYPE), &key); top = bucket >> (sizeof(uintptr)*8 - 8); if(top < MinTopHash) top += MinTopHash; bucket &= (((uintptr)1 << h->B) - 1); if(h->oldbuckets != nil) { i = bucket & (((uintptr)1 << (h->B - 1)) - 1); b = (Bucket*)(h->oldbuckets + i * h->bucketsize); if(evacuated(b)) { b = (Bucket*)(h->buckets + bucket * h->bucketsize); } } else { b = (Bucket*)(h->buckets + bucket * h->bucketsize); } do { for(i = 0, k = (KEYTYPE*)b->data, v = (byte*)(k + BUCKETSIZE); i < BUCKETSIZE; i++, k++, v += h->valuesize) { if(b->tophash[i] != top) continue; if(QUICK_NE(key, *k)) continue; if(QUICK_EQ(key, *k) || SLOW_EQ(key, *k)) { *valueptr = v; return; } } b = b->overflow; } while(b != nil); } *valueptr = t->elem->zero; }