Exemple #1
0
/**
 * fileHandle(char*, hashTable&), reads in a file and passes each word of the file
 * onto a tokenize function and inserts the word into a hashtable
**/
void fileHandle ( char* fileName, hashTable &h )
{
    ifstream in;
    in.open ( fileName, ios::in );

    if ( !in )
    {
        cout << "Unable to open " << fileName << ". Exiting program.\n";
        return;
    }

    string x;
    auto c1 = clock();

    while ( in >> x )
    {
        x = tokenize ( x );

        // we would add into a hash table here..
        // should only need to add in, in the insert function for
        // our hash table it should be able to handle everything else
        if ( x != "" && !h.insert ( x ) )
        {
            cout << "Unable to insert " << x << " into hashtable. Exiting program\n";
            return;
        }
    }

    cout << "Finished generating a hash table of size " << h.getSize() << ".\n";
    cout << "Read " << h.getNumWords() << " words from the file " << fileName << ".\n";
    cout << "Inserted " << h.getNumDistinct() << " distinct words into the hash table.\n";
    cout << "Compacting and sorting the hash table ... ";
    h.sort();
    cout << "finished!\n";

    auto c2 = clock();
    auto totalTime = c2 - c1;
    cout << fixed << showpoint;
    cout << "Elapsed time = " << setprecision ( 1 ) << totalTime / ( CLOCKS_PER_SEC / 1000.0 )  <<  " msec.\n";

    h.printStats ( fileName );
}