pset5/speller/dictionary.c
bool load(const char *dictionary)
{
    node *tmp = NULL;

    // the hash table - is a pointer to an array of node* pointers
    //      all elements initiated to NULL (calloc)
    ht = (node**) calloc( sizeof(node*), HASH_SIZE );
    if (ht == NULL)
    {
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    
    // single word buffer
    char *w;
    w = malloc( (LENGTH + 1)* sizeof(char));    // LENGTH + '\0'
    if (w == NULL)
    {
        free(ht);
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    
    // open dictionary file 
    FILE *dfile = fopen(dictionary, "r");
    if (dfile == NULL)
    {
        free(w);
        free(ht);
        return false;
    }
    
    // current index in the hash table
    uint32_t index = 0;
    
    // hash buffer
    uint32_t h;
    
    // read the dictionary word by word and save each word's hash in the table
    while ( fscanf(dfile, "%s\n", w) > 0 )
    {
        words++;
        h = hash(w);

        // first element at the position
        if (ht[h] == NULL)
        {
            // create a node
            ht[h] = malloc(sizeof(node));
            if (ht[h] == NULL)
            {
                free(w);
                free(ht);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }

            // allocate memory for the current string
            ht[h] -> c = malloc(sizeof(char) * strlen(w) + 1);
            if (ht[h] -> c == NULL)
            {
                free(w);
                free(ht);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }
            
            ht[h] -> next = NULL;
            
            // save the current string
            strcpy(ht[h] -> c, w);
        }

        // pointer exists (collision) - insert as linked list
        else
        {
            collisions++;
            
            // get memory for a new node
            tmp = malloc(sizeof(node));
            if (tmp == NULL)
            {
                free(w);
                free(ht);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }

            // get a memory for a string
            tmp -> c = malloc(sizeof(char) * strlen(w) + 1);
            if (tmp -> c == NULL)
            {
                free(w);
                free(ht);
                free(tmp);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }
            
            tmp -> next = NULL;
            
            // save the current string
            strcpy(tmp -> c, w);

            // insert the new node
            tmp -> next = ht[h] -> next;
            ht[h] -> next = tmp;
            
        }

        // advance index
        index++;
    }
    free(w);
    fclose(dfile);
    return true;   
}
/**
 * Loads dictionary into memory. Returns true if successful else false.
 * 
 * Populates a hash table (of HASH_SIZE) as array of *node pointers 
 * passing each word from the dictionary through the "hash" function
 * Collisions get inserted as linked list
 */
Loads dictionary into memory. Returns true if successful else false.

Populates a hash table (of HASH_SIZE) as array of *node pointers
passing each word from the dictionary through the "hash" function
Collisions get inserted as linked list
Loads dictionary into memory . Returns true if successful else false . Populates a hash table ( of HASH_SIZE ) as array of * node pointers passing each word from the dictionary through the " hash " function Collisions get inserted as linked list
bool load(const char *dictionary)
{
    node *tmp = NULL;
    ht = (node**) calloc( sizeof(node*), HASH_SIZE );
    if (ht == NULL)
    {
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    char *w;
    w = malloc( (LENGTH + 1)* sizeof(char));    
    if (w == NULL)
    {
        free(ht);
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    FILE *dfile = fopen(dictionary, "r");
    if (dfile == NULL)
    {
        free(w);
        free(ht);
        return false;
    }
    uint32_t index = 0;
    uint32_t h;
    while ( fscanf(dfile, "%s\n", w) > 0 )
    {
        words++;
        h = hash(w);
        if (ht[h] == NULL)
        {
            ht[h] = malloc(sizeof(node));
            if (ht[h] == NULL)
            {
                free(w);
                free(ht);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }
            ht[h] -> c = malloc(sizeof(char) * strlen(w) + 1);
            if (ht[h] -> c == NULL)
            {
                free(w);
                free(ht);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }
            ht[h] -> next = NULL;
            strcpy(ht[h] -> c, w);
        }
        else
        {
            collisions++;
            tmp = malloc(sizeof(node));
            if (tmp == NULL)
            {
                free(w);
                free(ht);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }
            tmp -> c = malloc(sizeof(char) * strlen(w) + 1);
            if (tmp -> c == NULL)
            {
                free(w);
                free(ht);
                free(tmp);
                unload();
                fclose(dfile);
                fprintf(stderr, "Not enough memory.\n");
                return false;
            }
            tmp -> next = NULL;
            strcpy(tmp -> c, w);
            tmp -> next = ht[h] -> next;
            ht[h] -> next = tmp;
        }
        index++;
    }
    free(w);
    fclose(dfile);
    return true;   
}
bool load ( const char * dictionary ) { node * tmp = NULL ; ht = ( node * * ) calloc ( sizeof ( node * ) , HASH_SIZE ) ; if ( ht == NULL ) { fprintf ( stderr , " \n " ) ; return false ; } char * w ; w = malloc ( ( LENGTH + 1 ) * sizeof ( char ) ) ; if ( w == NULL ) { free ( ht ) ; fprintf ( stderr , " \n " ) ; return false ; } FILE * dfile = fopen ( dictionary , " " ) ; if ( dfile == NULL ) { free ( w ) ; free ( ht ) ; return false ; } uint32_t index = 0 ; uint32_t h ; while ( fscanf ( dfile , " \n " , w ) > 0 ) { words ++ ; h = hash ( w ) ; if ( ht [ h ] == NULL ) { ht [ h ] = malloc ( sizeof ( node ) ) ; if ( ht [ h ] == NULL ) { free ( w ) ; free ( ht ) ; unload ( ) ; fclose ( dfile ) ; fprintf ( stderr , " \n " ) ; return false ; } ht [ h ] -> c = malloc ( sizeof ( char ) * strlen ( w ) + 1 ) ; if ( ht [ h ] -> c == NULL ) { free ( w ) ; free ( ht ) ; unload ( ) ; fclose ( dfile ) ; fprintf ( stderr , " \n " ) ; return false ; } ht [ h ] -> next = NULL ; strcpy ( ht [ h ] -> c , w ) ; } else { collisions ++ ; tmp = malloc ( sizeof ( node ) ) ; if ( tmp == NULL ) { free ( w ) ; free ( ht ) ; unload ( ) ; fclose ( dfile ) ; fprintf ( stderr , " \n " ) ; return false ; } tmp -> c = malloc ( sizeof ( char ) * strlen ( w ) + 1 ) ; if ( tmp -> c == NULL ) { free ( w ) ; free ( ht ) ; free ( tmp ) ; unload ( ) ; fclose ( dfile ) ; fprintf ( stderr , " \n " ) ; return false ; } tmp -> next = NULL ; strcpy ( tmp -> c , w ) ; tmp -> next = ht [ h ] -> next ; ht [ h ] -> next = tmp ; } index ++ ; } free ( w ) ; fclose ( dfile ) ; return true ; }
Loads dictionary into memory.
Loads dictionary into memory .
// the hash table - is a pointer to an array of node* pointers //      all elements initiated to NULL (calloc) // single word buffer // LENGTH + '\0' // open dictionary file  // current index in the hash table // hash buffer // read the dictionary word by word and save each word's hash in the table // first element at the position // create a node // allocate memory for the current string // save the current string // pointer exists (collision) - insert as linked list // get memory for a new node // get a memory for a string // save the current string // insert the new node // advance index