int PNNRange(LSH* lsh, value query, int P,value* array) { int i=0; int hashSize=0; hashTable neighbors; double* distArr=NULL; for (i = 0; i < lsh->L; i++) { //gets bucket where the value is List bucketList = GetListHashTable(lsh->tables[i].tables, &query, lsh->tables[i].GConfiguration); hashSize+=SizeList(bucketList); } InitHashTable(&neighbors,hashSize,sizeof(value),NULL, data.distance,hashFunc,NULL); for (i = 0; i < lsh->L; i++) { //gets bucket where the value is List bucketList = GetListHashTable(lsh->tables[i].tables, &query, lsh->tables[i].GConfiguration); value* lValue = GetFirst(bucketList); //get all neighbours in range an place them in a list while (lValue != NULL) { InsertHashTable(neighbors, lValue,NULL); lValue = GetNext(bucketList); } } value** hashArr=malloc( GetHashSize(neighbors) * sizeof(value*)); // WARNING // HashToArr(neighbors,(void**)hashArr); distArr=malloc(GetHashSize(neighbors)*sizeof(double)); for(i=0; i<GetHashSize(neighbors); i++) { distArr[i]=data.distance(hashArr[i],&query); } QuickSortValue(distArr, hashArr, GetHashSize(neighbors)); for(i=0; (i<P && i<GetHashSize(neighbors)-1 ); i++) { array[i]=*hashArr[i+1]; } DestroyHashTable(&neighbors); free(hashArr); free(distArr); return i; }
bool ComputeHash_FromText(const int hashType, const TCHAR *const textData, TCHAR *const hashOut, const size_t hashOutSize) { const size_t hashSize = GetHashSize(hashType); if((hashSize == SIZE_MAX) || (hashSize >= (hashOutSize / 2))) { if(hashSize != SIZE_MAX) ERROR_MSG(T("Output buffer is too small to hold the hash value!")); return false; } hash_ctx ctx; if(!HashFunction_Init(hashType, &ctx)) { return false; } #ifdef UNICODE const char *const textUtf8 = utf16_to_utf8(textData); if(textUtf8 == NULL) { return false; } HashFunction_Update(&ctx, (BYTE*)textUtf8, strlen(textUtf8)); delete [] textUtf8; #else HashFunction_Update(&ctx, (BYTE*)textData, strlen(textData)); #endif BYTE hashValue[128]; HashFunction_Final(&ctx, hashValue); ConvertHashToHex(hashSize, hashValue, hashOut); return true; };
bool ComputeHash_FromFile(const int hashType, const TCHAR *const fileName, TCHAR *const hashOut, const size_t hashOutSize) { const size_t hashSize = GetHashSize(hashType); if((hashSize == SIZE_MAX) || (hashSize >= (hashOutSize / 2))) { if(hashSize != SIZE_MAX) ERROR_MSG(T("Output buffer is too small to hold the hash value!")); return false; } HANDLE h = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(!VALID_HANDLE(h)) { ERROR_MSG(T("Failed to open input file for reading!")); return false; } hash_ctx ctx; if(!HashFunction_Init(hashType, &ctx)) { CLOSE_HANDLE(h); return false; } static const DWORD BUFF_SIZE = 4096; BYTE buffer[BUFF_SIZE]; for(;;) { DWORD nBytesRead = 0; if(!ReadFile(h, buffer, BUFF_SIZE, &nBytesRead, NULL)) { ERROR_MSG(T("Failed to read data from input file!")); CLOSE_HANDLE(h); return false; } if(nBytesRead < 1) { CLOSE_HANDLE(h); break; } HashFunction_Update(&ctx, buffer, nBytesRead); } BYTE hashValue[128]; HashFunction_Final(&ctx, hashValue); ConvertHashToHex(hashSize, hashValue, hashOut); return true; }