//rehash element from one table to another //Arguments: h- old table, newhash - new table, oldCode - index of the element in old table ( h ) int reHash(HashTable *h, HashTable *newHash, int oldCode){ int oldPrefix = findPrefixGivenCode(h,oldCode); //store values to avoid recalculation int oldChar = findCharGivenCode(h,oldCode); bool isRehashed = findisRehashedGivenCode(h,oldCode); if (oldChar == -1){ //if empty slot return -1 return -1; } else if(isRehashed){ //if already rehashed slot, return prefix return oldPrefix; } else if (oldPrefix == emptyCode){ //if prefix is 0 ((*h)[oldCode]).isRehashed = 1; //re-insert it int t = insertInHashTable(newHash,oldPrefix,oldChar); ((*h)[oldCode]).prefix = t; return ((*h)[oldCode]).prefix; //return prefix where inserted } else{ int k = reHash(h,newHash,oldPrefix); //recursive case int m = insertInHashTable(newHash,k,oldChar); //unfold prefixes and insert when hit 0 ((*h)[oldCode]).prefix = m; ((*h)[oldCode]).isRehashed = 1; return m; } return -1; }
long memo_coin_sums(int* dens, int size, int amount, HashTable *memo) { Tuple* tuple = convert_to_tuple(dens, size, amount); if(inHashTable(tuple, memo)) { return getFromHashTable(memo, tuple); } else { if(amount<0) return 0; else if(amount==0) return 1; else if(size==1) { if(amount%dens[0]==0) return 1; else return 0; } else { long result = memo_coin_sums(dens, size-1, amount, memo) + memo_coin_sums(dens, size, amount-dens[size-1], memo); insertInHashTable(memo, tuple, result); return result; } } }
//add default ascii character to hash table h void addDefault(HashTable *h){ for (int i = 0 ; i < 256 ; i++ ){ //insert ascii codes insertInHashTable(h,0,i); } }