void Hash::initHash(size_t bytes) { if (!hash_init_done) { hashSize = (int)(bytes/sizeof(HashEntry)); if (!hashSize) { hashMask = 0; hash_init_done++; return; } int hashPower; for (hashPower = 1; hashPower < 32; hashPower++) { if (((size_t)1 << hashPower) > hashSize) { hashPower--; break; } } hashSize = (size_t)1 << hashPower; hashMask = (uint64_t)(hashSize-1); size_t hashSizePlus = hashSize + MaxRehash; ALIGNED_MALLOC(hashTable, HashEntry, sizeof(HashEntry)*hashSizePlus,128); if (hashTable == NULL) { cerr << "hash table allocation failed!" << endl; hashSize = 0; } clearHash(); hash_init_done++; } }
void cpSpaceHashQueryRehash(cpSpaceHash *hash, cpSpaceHashQueryFunc func, void *data) { clearHash(hash); queryRehashPair pair = {hash, func, data}; cpHashSetEach(hash->handleSet, &handleQueryRehashHelper, &pair); }
void cpSpaceHashRehash(cpSpaceHash *hash) { clearHash(hash); // Rehash all of the handles. cpHashSetEach(hash->handleSet, &handleRehashHelper, hash); }
void cpSpaceHashResize(cpSpaceHash *hash, cpFloat celldim, int numcells) { // Clear the hash to release the old handle locks. clearHash(hash); hash->celldim = celldim; cpSpaceHashAllocTable(hash, next_prime(numcells)); }
//----------------------------------------------------------------- // reset compressor and issue a clear code void mgLZWEncode::clearBlock() { clearHash(); // delete all the symbols m_freeCode = m_clearCode + 2; writeCode(m_clearCode); // inform decoder m_bits = m_initBits; // reset code size m_maxCode = MAXCODE(m_bits); }
void cpSpaceHashDestroy(cpSpaceHash *hash) { clearHash(hash); freeBins(hash); // Free the handles. cpHashSetEach(hash->handleSet, &handleFreeWrap, NULL); cpHashSetFree(hash->handleSet); cpfree(hash->table); }
void cpSpaceHashDestroy(cpSpaceHash *hash) { clearHash(hash); cpHashSetFree(hash->handleSet); cpArrayEach(hash->allocatedBuffers, freeWrap, NULL); cpArrayFree(hash->allocatedBuffers); cpArrayFree(hash->pooledHandles); cpfree(hash->table); }
//----------------------------------------------------------------- // constructor mgLZWEncode::mgLZWEncode() { m_hashCode = new mgLZWCode[HASH_SIZE]; m_hashValue = new mgLZWHash[HASH_SIZE]; // init all the state variables m_bits = m_initBits = 9; // data size + 1 m_maxCode = MAXCODE(m_bits); m_clearCode = ((mgLZWCode) 1 << (m_initBits - 1)); m_EOFCode = m_clearCode + 1; m_freeCode = m_clearCode + 2; m_firstByte = TRUE; // no waiting symbol yet // init output buffering vars m_curAccum = 0; m_curBits = 0; clearHash(); // clear hash table }
int main(int argc, char** argv) { if(argc != 3){ printf("Illegal number of arguments\n"); exit(1); } else{ char * outputFileName = argv[1]; char * inputDirName = argv[2]; FILE * ifp; //input file sream FILE * ofp; //output file stream //sl is used as a sort of "sorted keyset" for the hashmap since key values are returned //by time added, this makes sure keys are sorted SortedListPtr sl = SLCreate(compareStrings, destroyBasicTypeAlloc); hashPtr indexHash = NULL; char option = '\0'; if((ifp = fopen(inputDirName,"r+")) == NULL) if(errno == EISDIR){ //if a slash is added to the end of directory remove it //only for formatting purposes truncateSlash(inputDirName); traverseDir(inputDirName, NULL, sl, &indexHash); } else{ perror("Error encountered while trying to open file or directory"); exit(2); } else{ readsFile(ifp, inputDirName, sl, &indexHash); } if((ofp = fopen(outputFileName,"r")) == NULL){ //check if file exists if(errno == ENOENT){ //if it doesnt fclose(ofp); //close the file opened for reading ofp = fopen(outputFileName,"w"); //and create it for writing if(SortedWriteToFile(ofp, sl, &indexHash) == -2) printf("No words were found.\n"); } else{ perror("Error trying to open output file"); exit(2); } } else{ printf("File exists, override file (y/n)? "); //if it exists ask user to override scanf("%c", &option); if(option == 'y'){ fclose(ofp); ofp = fopen(outputFileName,"w"); if(SortedWriteToFile(ofp, sl, &indexHash) == -2) printf("No words were found.\n"); } } //clean mess if(ifp!=NULL) fclose(ifp); if(ofp!=NULL) fclose(ofp); clearHash(&indexHash); SLDestroy(sl); } return 0; }