Esempio n. 1
0
void insert_string(membership_type *membership, char *value)
{
    char_type *string = (char_type *)value;
    word_type hash = hashing_function(membership)(string, hashing_func_parameters(membership));

    #ifdef TRACK_STATISTICS
        membership->number_of_inserts++;            /* increase insert count. */

        if (get_bit(bit_field(membership), hash))   /* if the bit has already being set, just return. */
            return ;

        set_bit_to_1(bit_field(membership), hash);  /* set bit to 1 */
        membership->number_of_set_bits++;           /* increase number of bits set. */
    #else
        /* if we are tracking any statistics just set the bit to 1. */
        set_bit_to_1(bit_field(membership), hash);  /* set bit to 1 */
    #endif
}
Esempio n. 2
0
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
    // copy the word ( because it is constant type and cannot be used)
    int l = strlen(word);
    char *copy = malloc(l);
    
    // copy the characters and make them lower case
    for (int i = 0; i < l; i++)
        copy[i] = tolower(word[i]);
    
    // add the end of string character
    copy[l] = '\0';

    //  hash the word
    int index = hashing_function(copy);

    // check the table for a node at that index
    node* entry = hashtable[index];
    if (entry)
    {
        // point a cursor node to the head node
        node* cursor = entry;

        // loop through the nodes while a next pointer isn't null
        while (cursor->next != NULL)
            {
                if (strcmp(copy, cursor->word) == 0)
                    return true;

                cursor = cursor->next;
            };

        if (strcmp(copy, cursor->word) == 0)
    		return true;

			cursor = cursor->next;
    }
    return false;
}
Esempio n. 3
0
/* if the string is not a member return 0 otherwise return a non-zero if it is a member,
 * the non-zero value return has no meaning!
 */
word_type is_member(membership_type *membership, char *value, word_type is_present)
{
    char_type *string = (char_type *)value;
    word_type hash = hashing_function(membership)(string, hashing_func_parameters(membership));

    /* if we are not tracking statistics just return the bit value, if present its value will be non-zero, if 0 its not present. */
    #ifndef TRACK_STATISTICS
        return get_bit(bit_field(membership), hash); /* return the actual bit which is 0 or 1,2,4,8, .... */
    #else
        if (get_bit(bit_field(membership), hash)) /* we got a hit. */
        {
            if (!is_present)  /* but we weren't expecting it. */
                membership->number_of_false_positives++; /* increase count and return 1 */
            return 1;
        }
        if (is_present)
        {
            membership->number_of_false_negatives++;
            printf("WARNING!! false negative '%s'\n", string);
        }
        return 0;
    #endif
}
Esempio n. 4
0
/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    // open the input dictionary file
    FILE* fp = fopen(dictionary, "r");

    // check for an unsuccessful open
    if (fp == NULL)
    	{
    		printf("Could not open %s.\n", dictionary);
    		return false;
    	}
    	
    // set a buffer to store an output word of length = LENGTH + 1 for null terminator
    char buffer[LENGTH+2];

    // loop through the dictionary until a null character
    while (fgets(buffer, sizeof(buffer), fp))
    {

        // overwrite the \n with \0
        buffer[strlen(buffer) - 1] = '\0';

    	// create a temporary node
    	node* temp = malloc(sizeof(node));

    	// set the node's pointers to the word and to the next node
        // copies buffer's contents into the location of temp's word pointer
    	strncpy(temp->word, buffer, LENGTH + 1);
    	temp->next = NULL;

    	// hash the word to get the index
    	int index = hashing_function(buffer);
    	// printf("%i" %(index));

    	// if the hashtable contains no value at that index then assign the temp node there
        // (the entry takes the pointers of temp)
    	if (hashtable[index] == NULL)
    	{
    		hashtable[index] = temp;
    	}

    	// otherwise append the node to the end of the linked list
    	else
    	{
    		// point a cursor node to the index, (i.e the first entry in the linked list)
    		node* cursor = hashtable[index];

    		// loop through the nodes while the next pointer isn't null
    		while (cursor->next != NULL)
    		{
    			cursor = cursor->next;
    		}

    	// the end of the linked list has been found, append the node by changing the shared pointer
    		cursor->next = temp;
    	}
        words++;
    }

    // close the file
    fclose(fp);

    return true;
}