/* Fast way to look up the duplicates duplicates is pre-allocated make sure that the max element in array will not exceed maxElement */ static hashtable_int* getHashtable(int* data, size_t length, int numMaxElement) { hashtable_int* hashtable = hashtableCreate(static_cast<size_t>(numMaxElement*2), 0); for (size_t i = 0; i < length; i++) { const KeyType key = (KeyType)data[i]; hashtableInsertHashed(hashtable, key+1, reinterpret_cast<void*>(i+1)); } return hashtable; }
hashtable_int *hashtableRead(FILE* f) { size_t hashMagic = 0; size_t n = 0, status; hashtable_int *hashtbl = 0; status = fread(&hashMagic, sizeof(size_t),1, f); if (status && hashMagic==T_HASH_MAGIC) { size_t i; size_t dataSize; status = fread(&n, sizeof(size_t),1, f); status = fread(&dataSize, sizeof(size_t),1, f); hashtbl=hashtableCreate(n, hash); for (i=0; i<hashtbl->size; i++) { size_t j=0; status = fread(&n, sizeof(size_t),1, f); for (j=0; j<n; j++) { int key=0; void* data=0; status = fread(&key, sizeof(KeyType), 1, f); if (dataSize>sizeof(void*)) { data=malloc(dataSize); if (!data) { hashtableDestroy(hashtbl); return NULL; } status = fread(data, dataSize, 1, f); } else status = fread(&data, dataSize, 1, f); hashtableInsert(hashtbl, key, data); //free(key); } } } else return 0; return hashtbl; }